U
    h                     @   s:   d dl Z ddlmZmZ ddlmZ G dd de jZdS )    N   )errorsutils)typesc                   @   s   e Zd ZdZdddddddZedd Zdd	 Zed
d Zdd Z	edd Z
edd Zedd Zedd Zdd ZdS )
ChatGetterz
    Helper base class that introduces the `chat`, `input_chat`
    and `chat_id` properties and `get_chat` and `get_input_chat`
    methods.
    N)
input_chatchat	broadcastc                C   s"   || _ || _|| _|| _d | _d S )N)
_chat_peer_input_chat_chat
_broadcast_client)selfZ	chat_peerr   r   r	    r   A/tmp/pip-unpacked-wheel-c81u5j2r/telethon/tl/custom/chatgetter.py__init__   s
    zChatGetter.__init__c                 C   s   | j S )a|  
        Returns the :tl:`User`, :tl:`Chat` or :tl:`Channel` where this object
        belongs to. It may be `None` if Telegram didn't send the chat.

        If you only need the ID, use `chat_id` instead.

        If you need to call a method which needs
        this chat, use `input_chat` instead.

        If you're using `telethon.events`, use `get_chat()` instead.
        )r   r   r   r   r   r      s    zChatGetter.chatc                    sj   | j dkst| j ddrd|  I dH rdz| j| jI dH | _ W n" tk
rb   |  I dH  Y nX | j S )a  
        Returns `chat`, but will make an API call to find the
        chat unless it's already cached.

        If you only need the ID, use `chat_id` instead.

        If you need to call a method which needs
        this chat, use `get_input_chat()` instead.
        Nmin)r   getattrget_input_chatr   Z
get_entityr   
ValueError_refetch_chatr   r   r   r   get_chat#   s    zChatGetter.get_chatc                 C   sV   | j dkrP| jrP| jrPz$| jjtj| jdd | _ W n tk
rN   Y nX | j S )a  
        This :tl:`InputPeer` is the input version of the chat where the
        message was sent. Similarly to `input_sender
        <telethon.tl.custom.sendergetter.SenderGetter.input_sender>`, this
        doesn't have things like username or similar, but still useful in
        some cases.

        Note that this might not be available if the library doesn't
        have enough information available.
        NF)Zadd_mark)	r   r
   r   Z_mb_entity_cachegetr   get_peer_idZ_as_input_peerAttributeErrorr   r   r   r   r   7   s    zChatGetter.input_chatc                    sx   | j dkrr| jrr| jrrzD| j}| jd2 z*3 dH W }|j|kr*|j| _|j| _ qXq*6 W n t	j
k
rp   Y nX | jS )z|
        Returns `input_chat`, but will make an API call to find the
        input chat unless it's already cached.
        Nd   )r   chat_idr   Ziter_dialogsidentityr   Zinput_entityr   r   ZRPCError)r   targetdr   r   r   r   L   s    
zChatGetter.get_input_chatc                 C   s   | j rt| j S dS )a  
        Returns the marked chat integer ID. Note that this value **will
        be different** from ``peer_id`` for incoming private messages, since
        the chat *to* which the messages go is to your own person, but
        the *chat* itself is with the one who sent the message.

        TL;DR; this gets the ID that you expect.

        If there is a chat in the object, `chat_id` will *always* be set,
        which is why you should use it instead of `chat.id <chat>`.
        N)r
   r   r   r   r   r   r   r   _   s    zChatGetter.chat_idc                 C   s   | j rt| j tjS dS )z
        `True` if the message was sent as a private message.

        Returns `None` if there isn't enough information
        (e.g. on `events.MessageDeleted <telethon.events.messagedeleted.MessageDeleted>`).
        N)r
   
isinstancer   ZPeerUserr   r   r   r   
is_privaten   s    zChatGetter.is_privatec                 C   sV   | j dkr$t| jdr$t| jj| _ t| jtjrH| j dkr@dS | j  S t| jtj	S )z
        True if the message was sent on a group or megagroup.

        Returns `None` if there isn't enough information
        (e.g. on `events.MessageDeleted <telethon.events.messagedeleted.MessageDeleted>`).
        Nr	   )
r   hasattrr   boolr	   r#   r
   r   PeerChannelZPeerChatr   r   r   r   is_groupx   s    	
zChatGetter.is_groupc                 C   s   t | jtjS )z9`True` if the message was sent on a megagroup or channel.)r#   r
   r   r'   r   r   r   r   
is_channel   s    zChatGetter.is_channelc                    s   dS )zB
        Re-fetches chat information through other means.
        Nr   r   r   r   r   r      s    zChatGetter._refetch_chat)N)__name__
__module____qualname____doc__r   propertyr   r   r   r   r   r$   r(   r)   r   r   r   r   r   r      s"   



	

r   )abc r   r   tlr   ABCr   r   r   r   r   <module>   s   