o
    XzjZ2  ã                   @   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)ÚSenderGetteréd   é   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Ú_dueÚcreate_taskÚdeliver_event)ÚselfÚclientÚevent© r   úP/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/events/album.pyÚ__init__#   s   zAlbumHack.__init__c                 C   s2   |   ¡ }|r| jj |¡ |j ¡ t | _d S d S r   )r   r   ÚmessagesÚextendr   r   r   r   )r   r   r   r   r   r   r   ,   s
   þzAlbumHack.extendc                 Ã   sZ   �	 |   ¡ }|d u rd S | j|j ¡  }|dkr#| | j¡I d H  d S ~t |¡I d H  q)NTr   )r   r   r   r   Ú_dispatch_eventr   ÚasyncioÚsleep)r   r   Ú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                       sX   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   Úchatsr*   r+   ©Ú	__class__r   r   r   ^   s   zAlbum.__init__c                    sÂ   |g}t |tjtjfƒr_t |jtjƒsd S |jj‰ ˆ d u rd S t t	|ƒd ¡r)d S t
 
¡ ‰ttƒtkrD‡fdd„t ¡ D ƒD ]}t|= q>|D ]}||urRˆtt	|ƒ< qF|  ‡ 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 | ]}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)r1   Úu)Úgroupr   r   r5   …   s    þýý)r6   r   r7   r8   r9   r:   r;   Ú_IGNORE_DICTÚpopÚidr   ÚlenÚ_IGNORE_MAX_SIZEÚitemsÚEvent)ÚclsÚupdateÚothersÚself_idr2   r<   r   )r=   r4   r   Úbuildb   s.   
ÿ€ÿäzAlbum.buildc                    s   t |jƒdkrtƒ  |¡S d S ©Nr   )rA   r   r,   Úfilter)r   r   r.   r   r   rK   Œ   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   )Ú	chat_peerÚmsg_idÚ	broadcast)	r,   r   Úpeer_idr@   ÚboolÚpostr	   Ú	sender_idr   )r   r   r9   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rG|j | j¡}|d u r?t|| ƒ|j| j< d S | | j	¡ d S d S rJ   )r,   Ú_set_clientr   Ú_get_entity_pairrR   Ú	_entitiesÚ_mb_entity_cacheÚ_senderÚ_input_senderr   Ú_finish_initrA   Ú_albumsÚgetr;   r   r   )r   r   ÚmsgÚhackr.   r   r   rS       s   ÿ
úzAlbum.Event._set_clientc                 C   ó   | j d jS )zM
            The shared ``grouped_id`` between all the messages.
            r   )r   r;   ©r   r   r   r   r;   °   s   zAlbum.Event.grouped_idc                 C   ó   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   ó   � | ]	}|j r|j V  qd S r   )Útext©r1   Úmr   r   r   Ú	<genexpr>½   ó   € z#Album.Event.text.<locals>.<genexpr>Ú ©Únextr   r_   r   r   r   rb   ·   ó   zAlbum.Event.textc                 C   r`   )zv
            The raw message text of the first photo
            with a caption, ignoring any formatting.
            c                 s   ra   r   )Úraw_textrc   r   r   r   re   Å   rf   z'Album.Event.raw_text.<locals>.<genexpr>rg   rh   r_   r   r   r   rk   ¿   rj   zAlbum.Event.raw_textc                 C   r^   )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_replyr_   r   r   r   rl   Ç   s   
zAlbum.Event.is_replyc                 C   r^   )zœ
            The `Forward <telethon.tl.custom.forward.Forward>`
            information for the first message in the album if it was forwarded.
            r   )r   Úforwardr_   r   r   r   rm   Ó   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_messager_   r   r   r   rn   à   s   €zAlbum.Event.get_reply_messagec                 Ï   ó   �| j d j|i |¤Ž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   rp   é   ó   €zAlbum.Event.respondc                 Ï   ro   )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   Úreplyrq   r   r   r   ru   ñ   rt   zAlbum.Event.replyc                 Ï   s@   �| j r| j|d< |  ¡ I dH |d< | j j|i |¤Ž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   NÚ	from_peer)r   r   Úget_input_chatÚforward_messagesrq   r   r   r   Ú
forward_toù   s   €
ýzAlbum.Event.forward_toc                 Ï   sJ   �| j D ]}|jr|j|i |¤ŽI dH   S q| j d j|i |¤Ž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   rk   Úedit)r   rr   rs   r\   r   r   r   rz     s   €
ÿzAlbum.Event.editc                 Ï   s:   �| j r| j j|  ¡ I dH | jg|¢R i |¤Ž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   Údelete_messagesrw   r   rq   r   r   r   Údelete   s   €ÿþþÿzAlbum.Event.deletec                 Ã   s8   �| j r| j j|  ¡ I dH | jd jd�I dH  dS 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éÿÿÿÿ)Úmax_id)r   Úsend_read_acknowledgerw   r   r@   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   ó
   t | jƒS )zx
            Return the amount of messages in the album.

            Equivalent to ``len(self.messages)``.
            )rA   r   r_   r   r   r   Ú__len__A  ó   
zAlbum.Event.__len__c                 C   r„   )zu
            Iterate over the messages in the album.

            Equivalent to ``iter(self.messages)``.
            )Úiterr   r_   r   r   r   Ú__iter__I  r†   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  r†   zAlbum.Event.__getitem__)r$   r%   r&   r'   r   rS   Úpropertyr;   rb   rk   rl   rm   rn   rp   ru   ry   rz   r|   r€   rƒ   r…   rˆ   rŠ   Ú__classcell__r   r   r.   r   rD   ‘   s2    




	rD   r   )NN)r$   r%   r&   r'   r   ÚclassmethodrI   rK   r   r	   rD   rŒ   r   r   r.   r   r(   C   s    ÿÿ)r(   )r!   r   r   Úcommonr   r   r   rg   r   Útlr   Útl.custom.sendergetterr	   rB   r0   r>   r   r   r(   r   r   r   r   Ú<module>   s    -