U
    h|)                     @   sp   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d	 Zd
d ZeG dd deZdS )    N   )EventBuilderEventCommonname_inner_event   )utils)types)SenderGetterc                    s   t   fdd}|S )Nc                    s   | j d krd S  | S N)actionselffunction >/tmp/pip-unpacked-wheel-c81u5j2r/telethon/events/userupdate.pywrapped   s    z!_requires_action.<locals>.wrapped	functoolswrapsr   r   r   r   r   _requires_action   s    r   c                    s   t   fdd}|S )Nc                    s   | j d krd S  | S r
   statusr   r   r   r   r      s    z!_requires_status.<locals>.wrappedr   r   r   r   r   _requires_status   s    r   c                   @   s0   e Zd ZdZedddZG dd deeZdS )
UserUpdatea  
    Occurs whenever a user goes online, starts typing, etc.

    Example
        .. code-block:: python

            from telethon import events

            @client.on(events.UserUpdate)
            async def handler(event):
                # If someone is uploading, say something
                if event.uploading:
                    await client.send_message(event.user_id, 'What are you sending?')
    Nc                 C   s   t |tjr$| jt|j|jdS t |tjrL| j|jt	|j
|jdS t |tjrt| j|jt|j|jdS t |tjr| j|j|jdS d S )Nr   )	chat_peertyping)r   )
isinstancer   ZUpdateUserStatusEventZPeerUseruser_idr   ZUpdateChannelUserTypingZfrom_idZPeerChannelZ
channel_idr   ZUpdateChatUserTypingZPeerChatZchat_idZUpdateUserTyping)clsupdateZothersself_idr   r   r   build3   s$    

zUserUpdate.buildc                       s  e Zd ZdZdddd fdd
Z fddZedd	 Zd
d Zedd Z	dd Z
edd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeed d! Zeed"d# Zeed$d% Zeed&d' Zeed(d) Zeed*d+ Zeed,d- Zeed.d/ Zd0d1 Zeed2d3 Zeed4d5 Zeed6d7 Z eed8d9 Z!  Z"S ):zUserUpdate.Eventa  
        Represents the event of a user update
        such as gone online, started typing, etc.

        Members:
            status (:tl:`UserStatus`, optional):
                The user status if the update is about going online or offline.

                You should check this attribute first before checking any
                of the seen within properties, since they will all be `None`
                if the status is not set.

            action (:tl:`SendMessageAction`, optional):
                The "typing" action if any the user is performing if any.

                You should check this attribute first before checking any
                of the typing properties, since they will all be `None`
                if the action is not set.
        N)r   r   r   c                   s2   t  |p| t| t| || _|| _d S r
   )super__init__r	   r   Zget_peer_idr   r   )r   Zpeerr   r   r   	__class__r   r   r&   X   s    zUserUpdate.Event.__init__c                    s,   t  | t| j| j|j\| _| _d S r
   )	r%   _set_clientr   Z_get_entity_pair	sender_idZ	_entitiesZ_mb_entity_cacheZ_senderZ_input_sender)r   clientr'   r   r   r)   _   s      zUserUpdate.Event._set_clientc                 C   s   | j S )zIAlias for `sender <telethon.tl.custom.sendergetter.SenderGetter.sender>`.)Zsenderr   r   r   r   userd   s    zUserUpdate.Event.userc                    s   |   I dH S )zQAlias for `get_sender <telethon.tl.custom.sendergetter.SenderGetter.get_sender>`.N)Z
get_senderr   r   r   r   get_useri   s    zUserUpdate.Event.get_userc                 C   s   | j S )zUAlias for `input_sender <telethon.tl.custom.sendergetter.SenderGetter.input_sender>`.)Zinput_senderr   r   r   r   
input_userm   s    zUserUpdate.Event.input_userc                    s   |   I dH S )z]Alias for `get_input_sender <telethon.tl.custom.sendergetter.SenderGetter.get_input_sender>`.N)Zget_input_senderr   r   r   r   get_input_userr   s    zUserUpdate.Event.get_input_userc                 C   s   | j S )zOAlias for `sender_id <telethon.tl.custom.sendergetter.SenderGetter.sender_id>`.)r*   r   r   r   r   r    v   s    zUserUpdate.Event.user_idc                 C   s   t | jtjS )zC
            `True` if the action is typing a message.
            )r   r   r   ZSendMessageTypingActionr   r   r   r   r   {   s    zUserUpdate.Event.typingc              	   C   s(   t | jtjtjtjtjtjtjtj	fS )zF
            `True` if the action is uploading something.
            )
r   r   r   SendMessageChooseContactActionSendMessageChooseStickerActionSendMessageUploadAudioActionSendMessageUploadDocumentActionSendMessageUploadPhotoActionSendMessageUploadRoundActionSendMessageUploadVideoActionr   r   r   r   	uploading   s    zUserUpdate.Event.uploadingc                 C   s   t | jtjtjtjfS )zF
            `True` if the action is recording something.
            )r   r   r   SendMessageRecordAudioActionSendMessageRecordRoundActionSendMessageRecordVideoActionr   r   r   r   	recording   s
    zUserUpdate.Event.recordingc                 C   s   t | jtjS )zA
            `True` if the action is playing a game.
            )r   r   r   ZSendMessageGamePlayActionr   r   r   r   playing   s    zUserUpdate.Event.playingc                 C   s   t | jtjS )zL
            `True` if the action was cancelling other actions.
            )r   r   r   ZSendMessageCancelActionr   r   r   r   cancel   s    zUserUpdate.Event.cancelc                 C   s   t | jtjS )zC
            `True` if what's being uploaded is a geo.
            )r   r   r   ZSendMessageGeoLocationActionr   r   r   r   geo   s    zUserUpdate.Event.geoc                 C   s   t | jtjtjfS )zO
            `True` if what's being recorded/uploaded is an audio.
            )r   r   r   r8   r2   r   r   r   r   audio   s    zUserUpdate.Event.audioc                 C   s   t | jtjtjfS )zT
            `True` if what's being recorded/uploaded is a round video.
            )r   r   r   r9   r5   r   r   r   r   round   s    zUserUpdate.Event.roundc                 C   s   t | jtjtjfS )zO
            `True` if what's being recorded/uploaded is an video.
            )r   r   r   r:   r6   r   r   r   r   video   s    zUserUpdate.Event.videoc                 C   s   t | jtjS )zR
            `True` if what's being uploaded (selected) is a contact.
            )r   r   r   r0   r   r   r   r   contact   s    zUserUpdate.Event.contactc                 C   s   t | jtjS )zF
            `True` if what's being uploaded is document.
            )r   r   r   r3   r   r   r   r   document   s    zUserUpdate.Event.documentc                 C   s   t | jtjS )zG
            `True` if what's being uploaded is a sticker.
            )r   r   r   r1   r   r   r   r   sticker   s    zUserUpdate.Event.stickerc                 C   s   t | jtjS )zE
            `True` if what's being uploaded is a photo.
            )r   r   r   r4   r   r   r   r   photo   s    zUserUpdate.Event.photoc                 C   s   t | jtjr| jjS dS )zY
            Exact `datetime.datetime` when the user was last seen if known.
            N)r   r   r   UserStatusOffline
was_onliner   r   r   r   	last_seen   s    zUserUpdate.Event.last_seenc                 C   s   t | jtjr| jjS dS )z[
            The `datetime.datetime` until when the user should appear online.
            N)r   r   r   UserStatusOnlineexpiresr   r   r   r   until  s    zUserUpdate.Event.untilc                 C   s   t | jtjr(tjjtjjd| jj S t | jtj	rBtj
ddS t | jtjr\tj
ddS t | jtjrvtj
ddS t | jtjrtj
ddS tj
ddS d S )N)tzr   daysr         im  )r   r   r   rF   datetimenowtimezoneutcrG   rI   	timedeltaZUserStatusRecentlyZUserStatusLastWeekZUserStatusLastMonthr   r   r   r   _last_seen_delta
  s    z!UserUpdate.Event._last_seen_deltac                 C   s   |   tjddkS )zA
            `True` if the user is currently online,
            r   rM   rV   rQ   rU   r   r   r   r   online  s    zUserUpdate.Event.onlinec                 C   s   |   tjddkS )zC
            `True` if the user was seen within a day.
            r   rM   rW   r   r   r   r   recently   s    zUserUpdate.Event.recentlyc                 C   s   |   tjddkS )zD
            `True` if the user was seen within 7 days.
            rO   rM   rW   r   r   r   r   within_weeks(  s    zUserUpdate.Event.within_weeksc                 C   s   |   tjddkS )zE
            `True` if the user was seen within 30 days.
            rP   rM   rW   r   r   r   r   within_months0  s    zUserUpdate.Event.within_months)#__name__
__module____qualname____doc__r&   r)   propertyr,   r-   r.   r/   r    r   r   r7   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   r   rH   rK   rV   rX   rY   rZ   r[   __classcell__r   r   r'   r   r   D   s   



			r   )NN)	r\   r]   r^   r_   classmethodr$   r   r	   r   r   r   r   r   r   #   s   r   )rQ   r   commonr   r   r    r   tlr   Ztl.custom.sendergetterr	   r   r   r   r   r   r   r   <module>   s   