U
    h                     @   sz   d dl Z d dlZd dlZddlmZ ddlmZmZ ddlm	Z	 dd Z
G dd	 d	e jZG d
d de	e jZdd ZdS )    N   )utils)TLObjecttypes)
ChatGetterc              
      s   |dkrdS t |s|f}t }|D ]}t|tr~|dk rH|| q|t t	|t t
|t t|h q&t|tr|jdkr|t | q&| |I dH }t|tjr| jddI dH }|t | q&|S )z>Helper util to turn the input chat or chats into a set of IDs.Nr   iVT)Z
input_peer)r   Zis_list_likeset
isinstanceintaddupdateZget_peer_idr   ZPeerUserZPeerChatZPeerChannelr   ZSUBCLASS_OF_IDZget_input_entityZInputPeerSelfZget_me)clientchatsresultZchat r   :/tmp/pip-unpacked-wheel-c81u5j2r/telethon/events/common.py_into_id_set
   s*    

r   c                   @   sN   e Zd ZdZddddddZeejdddZd	d
 Z	dd Z
dd ZdS )EventBuilderaH  
    The common event builder, with builtin support to filter per chat.

    Args:
        chats (`entity`, optional):
            May be one or more entities (username/peer/etc.), preferably IDs.
            By default, only matching chats will be handled.

        blacklist_chats (`bool`, optional):
            Whether to treat the chats as a blacklist instead of
            as a whitelist (default). This means that every chat
            will be handled *except* those specified in ``chats``
            which will be ignored if ``blacklist_chats=True``.

        func (`callable`, optional):
            A callable (async or not) function that should accept the event as input
            parameter, and return a value indicating whether the event
            should be dispatched or not (any truthy value will do, it
            does not need to be a `bool`). It works like a custom filter:

            .. code-block:: python

                @client.on(events.NewMessage(func=lambda e: e.is_private))
                async def handler(event):
                    pass  # code here
    NF)blacklist_chatsfuncc                C   s&   || _ t|| _d| _|| _d | _d S )NF)r   boolr   resolvedr   _resolve_lock)selfr   r   r   r   r   r   __init__D   s
    
zEventBuilder.__init__c                 C   s   dS )aO  
        Builds an event for the given update if possible, or returns None.

        `others` are the rest of updates that came in the same container
        as the current `update`.

        `self_id` should be the current user's ID, since it is required
        for some events which lack this information but still need it.
        Nr   )clsr   Zothersself_idr   r   r   buildK   s    zEventBuilder.buildc              
      sZ   | j r
dS | jst | _| j4 I dH " | j sF| |I dH  d| _ W 5 Q I dH R X dS )zAHelper method to allow event builders to be resolved before usageNT)r   r   asyncioLock_resolver   r   r   r   r   resolveY   s    
zEventBuilder.resolvec                    s   t || jI d H | _d S N)r   r   r    r   r   r   r   f   s    zEventBuilder._resolvec                 C   sB   | j s
dS | jdk	r.|j| jk}|| jkr.dS | js8dS | |S )z
        Returns a truthy value if the event passed the filter and should be
        used, or falsy otherwise. The return value may need to be awaited.

        The events must have been resolved before this can be called.
        NT)r   r   chat_idr   r   )r   eventZinsider   r   r   filteri   s    

zEventBuilder.filter)N)NN)__name__
__module____qualname____doc__r   classmethodabcabstractmethodr   r!   r   r%   r   r   r   r   r   )   s   r   c                       sR   e Zd ZdZdZd fdd	Zdd Zedd	 Zd
d Z	dd Z
dd Z  ZS )EventCommonaR  
    Intermediate class with common things to all events.

    Remember that this class implements `ChatGetter
    <telethon.tl.custom.chatgetter.ChatGetter>` which
    means you have access to all chat properties and methods.

    In addition, you can access the `original_update`
    field which contains the original :tl:`Update`.
    EventNc                    s,   t  j||d i | _d | _|| _d | _d S )N)	broadcast)superr   	_entities_clientZ_message_idZoriginal_update)r   Z	chat_peerZmsg_idr/   	__class__r   r   r      s
    zEventCommon.__init__c                 C   s:   || _ | jr*t| j| j|j\| _| _nd | _| _dS )zR
        Setter so subclasses can act accordingly when the client is set.
        N)	r2   Z
_chat_peerr   Z_get_entity_pairr#   r1   Z_mb_entity_cacheZ_chatZ_input_chatr    r   r   r   _set_client   s      zEventCommon._set_clientc                 C   s   | j S )zH
        The `telethon.TelegramClient` that created this event.
        )r2   r   r   r   r   r      s    zEventCommon.clientc                 C   s   t |  S r"   r   Zpretty_formatto_dictr6   r   r   r   __str__   s    zEventCommon.__str__c                 C   s   t j|  ddS )Nr   )indentr7   r6   r   r   r   	stringify   s    zEventCommon.stringifyc                 C   s"   dd | j  D }| j|d< |S )Nc                 S   s"   i | ]\}}|d  dkr||qS )r   _r   ).0kvr   r   r   
<dictcomp>   s       z'EventCommon.to_dict.<locals>.<dictcomp>r<   )__dict__items_event_name)r   dr   r   r   r8      s    
zEventCommon.to_dict)NNN)r&   r'   r(   r)   rC   r   r5   propertyr   r9   r;   r8   __classcell__r   r   r3   r   r-      s   

r-   c                 C   s0   t | drd| j| j_ntd|  | S )z4Decorator to rename cls.Event 'Event' as 'cls.Event'r.   z{}.Eventz$Class {} does not have a inner Event)hasattrformatr&   r.   rC   warningswarn)r   r   r   r   name_inner_event   s    
rK   )r+   r   rI    r   tlr   r   Ztl.custom.chatgetterr   r   ABCr   r-   rK   r   r   r   r   <module>   s   Y2