a
    fi                      @   s|   d dl mZ ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZ G dd	 d	eZG d
d deZdS )    )Enum   )Session   )utils)TLObject)PeerUserPeerChatPeerChannelInputPeerUserInputPeerChatInputPeerChannel
InputPhotoInputDocumentc                   @   s    e Zd ZdZdZedd ZdS )_SentFileTyper   r   c                 C   s(   | t krtjS | tkrtjS tdd S )Nz/The cls must be either InputDocument/InputPhoto)r   r   DOCUMENTr   PHOTO
ValueError)cls r   M/home/ec2-user/.local/lib/python3.9/site-packages/telethon/sessions/memory.py	from_type   s
    z_SentFileType.from_typeN)__name__
__module____qualname__r   r   staticmethodr   r   r   r   r   r      s   r   c                       s
  e Zd Z fddZdd Zedd Zedd Zed	d
 Zedd Z	e	j
dd Z	edd Zej
dd Zdd Zdd Zdd Zdd Zdd Z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d4d,d-Zd.d/ Zd0d1 Zd2d3 Z  ZS )5MemorySessionc                    s@   t    d| _d | _d | _d | _d | _i | _t | _	i | _
d S Nr   )super__init___dc_id_server_address_port	_auth_key_takeout_id_filesset	_entities_update_statesself	__class__r   r   r      s    
zMemorySession.__init__c                 C   s   |pd| _ || _|| _d S r   )r    r!   r"   )r*   dc_idserver_addressportr   r   r   set_dc)   s    
zMemorySession.set_dcc                 C   s   | j S N)r    r)   r   r   r   r-   .   s    zMemorySession.dc_idc                 C   s   | j S r1   )r!   r)   r   r   r   r.   2   s    zMemorySession.server_addressc                 C   s   | j S r1   )r"   r)   r   r   r   r/   6   s    zMemorySession.portc                 C   s   | j S r1   r#   r)   r   r   r   auth_key:   s    zMemorySession.auth_keyc                 C   s
   || _ d S r1   r2   r*   valuer   r   r   r3   >   s    c                 C   s   | j S r1   r$   r)   r   r   r   
takeout_idB   s    zMemorySession.takeout_idc                 C   s
   || _ d S r1   r6   r4   r   r   r   r7   F   s    c                 C   s   | j |d S r1   )r(   get)r*   	entity_idr   r   r   get_update_stateJ   s    zMemorySession.get_update_statec                 C   s   || j |< d S r1   )r(   )r*   r9   stater   r   r   set_update_stateM   s    zMemorySession.set_update_statec                 C   s
   | j  S r1   )r(   itemsr)   r   r   r   get_update_statesP   s    zMemorySession.get_update_statesc                 C   s   d S r1   r   r)   r   r   r   closeS   s    zMemorySession.closec                 C   s   d S r1   r   r)   r   r   r   saveV   s    zMemorySession.savec                 C   s   d S r1   r   r)   r   r   r   deleteY   s    zMemorySession.deletec                 C   s   | ||||fS r1   r   )idhashusernamephonenamer   r   r   _entity_values_to_row\   s    z#MemorySession._entity_values_to_rowc                 C   s   t |tsd S ztj|dd}t|}W n ty>   Y d S 0 t |ttfrV|j}nt |t	rfd}nd S t
|dd pxd }|d ur| }t
|dd }t|pd }| |||||S )NF)Z
allow_selfr   rD   rE   )
isinstancer   r   get_input_peerget_peer_id	TypeErrorr   r   access_hashr   getattrlowerZget_display_namerG   )r*   epZ	marked_idZp_hashrD   rE   rF   r   r   r   _entity_to_rowc   s(    


zMemorySession._entity_to_rowc                 C   s   t |tst|r|}ntg }t|dr4||j t|drJ||j t|drlt|jrl|	|j t|drt|j
r|	|j
 g }|D ]}| |}|r|| q|S )Nuserchatchatsusers)rH   r   r   Zis_list_likehasattrappendrR   rS   rT   extendrU   rQ   )r*   tloentitiesrowsrO   rowr   r   r   _entities_to_rows   s"    


zMemorySession._entities_to_rowsc                 C   s   |  j t| |O  _ d S r1   )r'   r&   r]   )r*   rY   r   r   r   process_entities   s    zMemorySession.process_entitiesc                    s2   zt  fdd| jD W S  ty,   Y n0 d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   ).0rB   rC   _Zfound_phonerE   r   r   	<genexpr>   s   z9MemorySession.get_entity_rows_by_phone.<locals>.<genexpr>nextr'   StopIteration)r*   rE   r   ra   r   get_entity_rows_by_phone   s    
z&MemorySession.get_entity_rows_by_phonec                    s2   zt  fdd| jD W S  ty,   Y n0 d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   )r_   rB   rC   Zfound_usernamer`   rD   r   r   rb      s   z<MemorySession.get_entity_rows_by_username.<locals>.<genexpr>rc   )r*   rD   r   rg   r   get_entity_rows_by_username   s    
z)MemorySession.get_entity_rows_by_usernamec                    s2   zt  fdd| jD W S  ty,   Y n0 d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   )r_   rB   rC   r`   Z
found_namerF   r   r   rb      s   z8MemorySession.get_entity_rows_by_name.<locals>.<genexpr>rc   )r*   rF   r   ri   r   get_entity_rows_by_name   s    
z%MemorySession.get_entity_rows_by_nameTc                    s|   zd|r t  fdd| jD W S tt tt tt ft fdd| jD W S W n tyv   Y n0 d S )Nc                 3   s(   | ] \}}}}}| kr||fV  qd S r1   r   r_   Zfound_idrC   r`   )rB   r   r   rb      s   z6MemorySession.get_entity_rows_by_id.<locals>.<genexpr>c                 3   s(   | ] \}}}}}| v r||fV  qd S r1   r   rk   )idsr   r   rb      s   )rd   r'   r   rJ   r   r	   r
   re   )r*   rB   exactr   )rB   rl   r   get_entity_rows_by_id   s    
z#MemorySession.get_entity_rows_by_idc              	   C   sf  z|j dv r|W S t|W S  ttfy`   t|trHt|}d}nt|t pZ|dk }Y n0 d }t|t	rt
|}|r| |}qt|\}}|r|s| |}qt|d }|r| j|dd}nt|tr| ||}|s t|t	r | |}|rX|\}}	t|\}}
|
tkr0t||	S |
tkrBt|S |
tkrbt||	S n
td|d S )N)l   9 l   F?L i@Tr   r   F)rm   z%Could not find input entity with key )ZSUBCLASS_OF_IDr   rI   AttributeErrorrK   rH   r   rJ   intstrZparse_phonerf   Zparse_usernamerh   Zresolve_invite_linkrn   rj   Z
resolve_idr   r   r	   r   r
   r   r   )r*   keyrm   resultrE   rD   Zinvitetupr9   Zentity_hashkindr   r   r   get_input_entity   sD    










zMemorySession.get_input_entityc                 C   sL   t |ttfstdt| ||tt|f}|j|jf}|| j	|< d S )NzCannot cache %s instance)
rH   r   r   rK   typer   r   rB   rL   r%   )r*   
md5_digest	file_sizeinstancerr   r5   r   r   r   
cache_file   s
    zMemorySession.cache_filec                 C   s:   ||t |f}z|| j|  W S  ty4   Y d S 0 d S r1   )r   r   r%   KeyError)r*   rx   ry   r   rr   r   r   r   get_file   s
    zMemorySession.get_file)T)r   r   r   r   r0   propertyr-   r.   r/   r3   setterr7   r:   r<   r>   r?   r@   rA   r   rG   rQ   r]   r^   rf   rh   rj   rn   rv   r{   r}   __classcell__r   r   r+   r   r      sD   








1r   N)enumr   Zabstractr    r   tlr   Ztl.typesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   <module>   s   (