3
uQhZ2                 @   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|| _|| _|jj t | _|jj| j	  d S )N)
weakrefref_client_eventlooptime_HACK_DELAY_dueZcreate_taskdeliver_event)selfclientevent r   9/tmp/pip-build-2nz6shyl/telethon/telethon/events/album.py__init__#   s    zAlbumHack.__init__c             C   s.   | j  }|r*| jjj| |jj t | _d S )N)r   r   messagesextendr   r   r   r   )r   r   r   r   r   r   r   ,   s    zAlbumHack.extendc                s^   xX| j  }|d krd S | j|jj  }|dkrD|j| jI d H  d S ~tj|I d H  qW 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 )N)r%   r&   )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jt	|d rRd S t
j
 tttkrx$fddtj D D ]
}t|= q~W x |D ]}||k	rtt	|< qW | j 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   r.      s    )r/   r   r0   r1   r2   r3   r4   _IGNORE_DICTpopidr   len_IGNORE_MAX_SIZEitemsEvent)clsupdateZothersZself_idr+   r5   r   )r6   r-   r   buildb   s&    

zAlbum.buildc                s   t |jdkrt j|S d S )Nr   )r:   r   r'   filter)r   r   )r(   r   r   rA      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| |j || _d S )Nr   )Z	chat_peerZmsg_id	broadcast)	r'   r   Zpeer_idr9   boolpostr	   	sender_idr   )r   r   r2   )r(   r   r   r      s
    
zAlbum.Event.__init__c                s   t  j| tj| j| j|j\| _| _x| j	D ]}|j
|| jd  q0W t| j	dkr|jj| j}|d krt|| |j| j< n|j| j	 d S )Nr   )r'   _set_clientr   Z_get_entity_pairrE   Z	_entitiesZ_mb_entity_cacheZ_senderZ_input_senderr   Z_finish_initr:   Z_albumsgetr4   r   r   )r   r   msgZhack)r(   r   r   rF      s    zAlbum.Event._set_clientc             C   s   | j d jS )zM
            The shared ``grouped_id`` between all the messages.
            r   )r   r4   )r   r   r   r   r4      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 )N)text)r*   mr   r   r   	<genexpr>   s    z#Album.Event.text.<locals>.<genexpr> )nextr   )r   r   r   r   rI      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 )N)raw_text)r*   rJ   r   r   r   rK      s    z'Album.Event.raw_text.<locals>.<genexpr>rL   )rM   r   )r   r   r   r   rN      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_reply)r   r   r   r   rO      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   forward)r   r   r   r   rP      s    zAlbum.Event.forwardc                s   | j d j 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_message)r   r   r   r   rQ      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   rR      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   reply)r   rS   rT   r   r   r   rU      s    zAlbum.Event.replyc                s:   | j r6| j|d< | j 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_messages)r   rS   rT   r   r   r   
forward_to   s    
zAlbum.Event.forward_toc                s@   x&| j D ]}|jr|j||I dH S qW | 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   rN   edit)r   rS   rT   rH   r   r   r   rX     s    zAlbum.Event.editc                s2   | j r.| j 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_messagesrV   r   )r   rS   rT   r   r   r   delete   s    zAlbum.Event.deletec                s2   | j r.| j 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.
            Nr   )Zmax_id)r   Zsend_read_acknowledgerV   r   r9   )r   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   )r\   N)r   pin)r   r\   r   r   r   r]   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   )r   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   )r   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   rF   propertyr4   rI   rN   rO   rP   rQ   rR   rU   rW   rX   rY   r[   r]   r^   r`   rb   __classcell__r   r   )r(   r   r=      s&   	r=   )N)NN)r    r!   r"   r#   r   classmethodr@   rA   r   r	   r=   rd   r   r   )r(   r   r$   C   s   )r$   )r   r   r   commonr   r   r   rL   r   tlr   Ztl.custom.sendergetterr	   r;   r)   r7   r   r   r$   r   r   r   r   <module>   s   -