U
    hZ2                     @   s   d dl Z d dlZd dlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ dZd	Zi Zd
ZG dd dZeG dd deZdS )    N   )EventBuilderEventCommonname_inner_event   )utils)types)SenderGetterd      g      ?c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )		AlbumHacka  
    When receiving an album from a different data-center, they will come in
    separate `Updates`, so we need to temporarily remember them for a while
    and only after produce the event.

    Of course events are not designed for this kind of wizardy, so this is
    a dirty hack that gets the job done.

    When cleaning up the code base we may want to figure out a better way
    to do this, or just leave the album problem to the users; the update
    handling code is bad enough as it is.
    c                 C   s6   t || _|| _|j t | _|j| 	  d S N)
weakrefref_client_eventlooptime_HACK_DELAY_dueZcreate_taskdeliver_event)selfclientevent r   9/tmp/pip-unpacked-wheel-c81u5j2r/telethon/events/album.py__init__#   s    zAlbumHack.__init__c                 C   s.   |   }|r*| jj| |j t | _d S r   )r   r   messagesextendr   r   r   r   )r   r   r   r   r   r   r   ,   s    zAlbumHack.extendc                    sZ   |   }|d krd S | j|j  }|dkrB|| jI d H  d S ~t|I d H  q d S )Nr   )r   r   r   r   Z_dispatch_eventr   asynciosleep)r   r   Zdiffr   r   r   r   2   s    zAlbumHack.deliver_eventN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   	r   c                       sV   e Zd ZdZdddd fddZedddZ fd	d
ZG dd dee	Z
  ZS )Albuma  
    Occurs whenever you receive an album. This event only exists
    to ease dealing with an unknown amount of messages that belong
    to the same album.

    Example
        .. code-block:: python

            from telethon import events

            @client.on(events.Album)
            async def handler(event):
                # Counting how many photos or videos the album has
                print('Got an album with', len(event), 'items')

                # Forwarding the album as a whole to some chat
                event.forward_to(chat)

                # Printing the caption
                print(event.text)

                # Replying to the fifth item in the album
                await event.messages[4].reply('Cool!')
    NFblacklist_chatsfuncc                   s   t  j|||d d S )Nr&   )superr   )r   Zchatsr'   r(   	__class__r   r   r   ^   s    zAlbum.__init__c                    s   |g}t |tjtjfrt |jtjs*d S |jj  d kr>d S tt	|d rRd S t

 tttkrfddt D D ]
}t|= q||D ]}||k	rtt	|< q|  fdd|D S d S )Nc                    s    g | ]\}} | t kr|qS r   )_IGNORE_MAX_AGE).0it)nowr   r   
<listcomp>|   s      zAlbum.build.<locals>.<listcomp>c                    s>   g | ]6}t |tjtjfrt |jtjr|jj kr|jqS r   )
isinstancer   UpdateNewMessageUpdateNewChannelMessagemessageMessage
grouped_id)r-   u)groupr   r   r1      s
   )r2   r   r3   r4   r5   r6   r7   _IGNORE_DICTpopidr   len_IGNORE_MAX_SIZEitemsEvent)clsupdateZothersself_idr.   r8   r   )r9   r0   r   buildb   s*    
zAlbum.buildc                    s   t |jdkrt |S d S Nr   )r=   r   r)   filter)r   r   r*   r   r   rF      s    zAlbum.filterc                       s   e Zd ZdZ fddZ fddZedd Zedd	 Zed
d Z	edd Z
edd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zddd d!Zd"d# Zd$d% Zd&d' Z  ZS )(zAlbum.Eventz
        Represents the event of a new album.

        Members:
            messages (Sequence[`Message <telethon.tl.custom.message.Message>`]):
                The list of messages belonging to the same album.
        c                    s<   |d }t  j|j|jt|jd t| |j || _d S )Nr   )Z	chat_peerZmsg_id	broadcast)	r)   r   Zpeer_idr<   boolpostr	   	sender_idr   )r   r   r5   r*   r   r   r      s    
 zAlbum.Event.__init__c                    s   t  | t| j| j|j\| _| _| j	D ]}|
|| jd  q.t| j	dkr|j| j}|d kr|t|| |j| j< n|| j	 d S rE   )r)   _set_clientr   Z_get_entity_pairrJ   Z	_entitiesZ_mb_entity_cacheZ_senderZ_input_senderr   Z_finish_initr=   Z_albumsgetr7   r   r   )r   r   msgZhackr*   r   r   rK      s      
zAlbum.Event._set_clientc                 C   s   | j d jS )zM
            The shared ``grouped_id`` between all the messages.
            r   )r   r7   r   r   r   r   r7      s    zAlbum.Event.grouped_idc                 C   s   t dd | jD dS )z
            The message text of the first photo with a caption,
            formatted using the client's default parse mode.
            c                 s   s   | ]}|j r|j V  qd S r   )textr-   mr   r   r   	<genexpr>   s      z#Album.Event.text.<locals>.<genexpr> nextr   rN   r   r   r   rO      s    zAlbum.Event.textc                 C   s   t dd | jD dS )zv
            The raw message text of the first photo
            with a caption, ignoring any formatting.
            c                 s   s   | ]}|j r|j V  qd S r   )raw_textrP   r   r   r   rR      s      z'Album.Event.raw_text.<locals>.<genexpr>rS   rT   rN   r   r   r   rV      s    zAlbum.Event.raw_textc                 C   s   | j d jS )a  
            `True` if the album is a reply to some other message.

            Remember that you can access the ID of the message
            this one is replying to through `reply_to_msg_id`,
            and the `Message` object with `get_reply_message()`.
            r   )r   is_replyrN   r   r   r   rW      s    
zAlbum.Event.is_replyc                 C   s   | j d jS )z
            The `Forward <telethon.tl.custom.forward.Forward>`
            information for the first message in the album if it was forwarded.
            r   )r   forwardrN   r   r   r   rX      s    zAlbum.Event.forwardc                    s   | j d  I dH S )z
            The `Message <telethon.tl.custom.message.Message>`
            that this album is replying to, or `None`.

            The result will be cached after its first use.
            r   N)r   get_reply_messagerN   r   r   r   rY      s    zAlbum.Event.get_reply_messagec                    s   | j d j||I dH S )z
            Responds to the album (not as a reply). Shorthand for
            `telethon.client.messages.MessageMethods.send_message`
            with ``entity`` already set.
            r   N)r   respondr   argskwargsr   r   r   rZ      s    zAlbum.Event.respondc                    s   | j d j||I dH S )z
            Replies to the first photo in the album (as a reply). Shorthand
            for `telethon.client.messages.MessageMethods.send_message`
            with both ``entity`` and ``reply_to`` already set.
            r   N)r   replyr[   r   r   r   r^      s    zAlbum.Event.replyc                    s:   | j r6| j|d< |  I dH |d< | j j||I dH S dS )z
            Forwards the entire album. Shorthand for
            `telethon.client.messages.MessageMethods.forward_messages`
            with both ``messages`` and ``from_peer`` already set.
            r   NZ	from_peer)r   r   get_input_chatZforward_messagesr[   r   r   r   
forward_to   s    
zAlbum.Event.forward_toc                    s@   | j D ] }|jr|j||I dH   S q| j d j||I dH S )a  
            Edits the first caption or the message, or the first messages'
            caption if no caption is set, iff it's outgoing. Shorthand for
            `telethon.client.messages.MessageMethods.edit_message`
            with both ``entity`` and ``message`` already set.

            Returns `None` if the message was incoming,
            or the edited `Message` otherwise.

            .. note::

                This is different from `client.edit_message
                <telethon.client.messages.MessageMethods.edit_message>`
                and **will respect** the previous state of the message.
                For example, if the message didn't have a link preview,
                the edit won't add one by default, and you should force
                it by setting it to `True` if you want it.

                This is generally the most desired and convenient behaviour,
                and will work for link previews and message buttons.
            Nr   )r   rV   edit)r   r\   r]   rM   r   r   r   ra     s    
zAlbum.Event.editc                    s2   | j r.| j j|  I dH | jf||I dH S dS )aH  
            Deletes the entire album. You're responsible for checking whether
            you have the permission to do so, or to except the error otherwise.
            Shorthand for
            `telethon.client.messages.MessageMethods.delete_messages` with
            ``entity`` and ``message_ids`` already set.
            N)r   Zdelete_messagesr_   r   r[   r   r   r   delete   s     zAlbum.Event.deletec                    s2   | j r.| j j|  I dH | jd jdI dH  dS )z
            Marks the entire album as read. Shorthand for
            `client.send_read_acknowledge()
            <telethon.client.messages.MessageMethods.send_read_acknowledge>`
            with both ``entity`` and ``message`` already set.
            N)Zmax_id)r   Zsend_read_acknowledger_   r   r<   rN   r   r   r   	mark_read.  s
     
zAlbum.Event.mark_readFnotifyc                   s   | j d j|dI dH S )z
            Pins the first photo in the album. Shorthand for
            `telethon.client.messages.MessageMethods.pin_message`
            with both ``entity`` and ``message`` already set.
            r   re   N)r   pin)r   rf   r   r   r   rg   9  s    zAlbum.Event.pinc                 C   s
   t | jS )zx
            Return the amount of messages in the album.

            Equivalent to ``len(self.messages)``.
            )r=   r   rN   r   r   r   __len__A  s    zAlbum.Event.__len__c                 C   s
   t | jS )zu
            Iterate over the messages in the album.

            Equivalent to ``iter(self.messages)``.
            )iterr   rN   r   r   r   __iter__I  s    zAlbum.Event.__iter__c                 C   s
   | j | S )zq
            Access the n'th message in the album.

            Equivalent to ``event.messages[n]``.
            )r   )r   nr   r   r   __getitem__Q  s    zAlbum.Event.__getitem__)r!   r"   r#   r$   r   rK   propertyr7   rO   rV   rW   rX   rY   rZ   r^   r`   ra   rb   rd   rg   rh   rj   rl   __classcell__r   r   r*   r   r@      s0   




	r@   )N)NN)r!   r"   r#   r$   r   classmethodrD   rF   r   r	   r@   rn   r   r   r*   r   r%   C   s     )r%   )r   r   r   commonr   r   r   rS   r   tlr   Ztl.custom.sendergetterr	   r>   r,   r:   r   r   r%   r   r   r   r   <module>   s   -