o
    XzjS!  ã                   @   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ƒ‚)Nz/The cls must be either InputDocument/InputPhoto)r   r   ÚDOCUMENTr   ÚPHOTOÚ
ValueError)Úcls© r   úS/var/www/kodo/tsheker/venv/lib/python3.10/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dd„ ƒZ
e	j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d7d/d0„Zd1d2„ Zd3d4„ Zd5d6„ Z‡  ZS )8ÚMemorySessionc                    sF   t ƒ  ¡  d| _d | _d | _d | _d | _d | _i | _t	ƒ | _
i | _d S ©Nr   )ÚsuperÚ__init__Ú_dc_idÚ_server_addressÚ_portÚ	_auth_keyÚ_tmp_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   ó   | j S ©N)r    r*   r   r   r   r.   /   ó   zMemorySession.dc_idc                 C   r2   r3   )r!   r*   r   r   r   r/   3   r4   zMemorySession.server_addressc                 C   r2   r3   )r"   r*   r   r   r   r0   7   r4   zMemorySession.portc                 C   r2   r3   ©r#   r*   r   r   r   Úauth_key;   r4   zMemorySession.auth_keyc                 C   r2   r3   ©r$   r*   r   r   r   Útmp_auth_key?   r4   zMemorySession.tmp_auth_keyc                 C   ó
   || _ d S r3   r5   ©r+   Úvaluer   r   r   r6   C   ó   
c                 C   r9   r3   r7   r:   r   r   r   r8   G   r<   c                 C   r2   r3   ©r%   r*   r   r   r   Ú
takeout_idK   r4   zMemorySession.takeout_idc                 C   r9   r3   r=   r:   r   r   r   r>   O   r<   c                 C   s   | j  |d ¡S r3   )r)   Úget)r+   Ú	entity_idr   r   r   Úget_update_stateS   ó   zMemorySession.get_update_statec                 C   s   || j |< d S r3   )r)   )r+   r@   Ústater   r   r   Úset_update_stateV   rB   zMemorySession.set_update_statec                 C   s
   | j  ¡ S r3   )r)   Úitemsr*   r   r   r   Úget_update_statesY   s   
zMemorySession.get_update_statesc                 C   ó   d S r3   r   r*   r   r   r   Úclose\   ó   zMemorySession.closec                 C   rG   r3   r   r*   r   r   r   Úsave_   rI   zMemorySession.savec                 C   rG   r3   r   r*   r   r   r   Údeleteb   rI   zMemorySession.deletec                 C   s   | ||||fS r3   r   )ÚidÚhashÚusernameÚphoneÚnamer   r   r   Ú_entity_values_to_rowe   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 w t |ttfƒr+|j}n
t |t	ƒr3d}nd S t
|dd ƒp<d }|d urE| ¡ }t
|dd ƒ}t |¡pQd }|  |||||¡S )NF)Ú
allow_selfr   rN   rO   )Ú
isinstancer   r   Úget_input_peerÚget_peer_idÚ	TypeErrorr   r   Úaccess_hashr   ÚgetattrÚlowerÚget_display_namerQ   )r+   ÚeÚpÚ	marked_idÚp_hashrN   rO   rP   r   r   r   Ú_entity_to_rowl   s*   
û

ÿzMemorySession._entity_to_rowc                 C   s¸   t |tƒst |¡r|}n:g }t|dƒr| |j¡ t|dƒr%| |j¡ t|dƒr6t |j¡r6| 	|j¡ t|dƒrGt |j
¡rG| 	|j
¡ g }|D ]}|  |¡}|rY| |¡ qK|S )NÚuserÚchatÚchatsÚusers)rS   r   r   Úis_list_likeÚhasattrÚappendr`   ra   rb   Úextendrc   r_   )r+   ÚtloÚentitiesÚrowsr[   Úrowr   r   r   Ú_entities_to_rows‰   s$   



€zMemorySession._entities_to_rowsc                 C   s   |  j t|  |¡ƒO  _ d S r3   )r(   r'   rl   )r+   rh   r   r   r   Úprocess_entitiesŸ   s   zMemorySession.process_entitiesc                    ó0   zt ‡ fdd„| jD ƒƒW S  ty   Y d S w )Nc                 3   s*   � | ]\}}}}}|ˆ kr||fV  qd S r3   r   )Ú.0rL   rM   Ú_Úfound_phone©rO   r   r   Ú	<genexpr>¤   ó   € ÿÿz9MemorySession.get_entity_rows_by_phone.<locals>.<genexpr>©Únextr(   ÚStopIteration)r+   rO   r   rr   r   Úget_entity_rows_by_phone¢   ó   
ÿÿz&MemorySession.get_entity_rows_by_phonec                    rn   )Nc                 3   s*   � | ]\}}}}}|ˆ kr||fV  qd S r3   r   )ro   rL   rM   Úfound_usernamerp   ©rN   r   r   rs   «   rt   z<MemorySession.get_entity_rows_by_username.<locals>.<genexpr>ru   )r+   rN   r   r{   r   Úget_entity_rows_by_username©   ry   z)MemorySession.get_entity_rows_by_usernamec                    rn   )Nc                 3   s*   � | ]\}}}}}|ˆ kr||fV  qd S r3   r   )ro   rL   rM   rp   Ú
found_name©rP   r   r   rs   ²   rt   z8MemorySession.get_entity_rows_by_name.<locals>.<genexpr>ru   )r+   rP   r   r~   r   Úget_entity_rows_by_name°   ry   z%MemorySession.get_entity_rows_by_nameTc                    sv   z0|rt ‡ fdd„| jD ƒƒW S t tˆ ƒ¡t tˆ ƒ¡t tˆ ƒ¡f‰t ‡fdd„| jD ƒƒW S  ty:   Y d S w )Nc                 3   s*   � | ]\}}}}}|ˆ kr||fV  qd S r3   r   ©ro   Úfound_idrM   rp   )rL   r   r   rs   º   rt   z6MemorySession.get_entity_rows_by_id.<locals>.<genexpr>c                 3   s*   � | ]\}}}}}|ˆ v r||fV  qd S r3   r   r€   )Úidsr   r   rs   Â   rt   )rv   r(   r   rU   r   r	   r
   rw   )r+   rL   Úexactr   )rL   r‚   r   Úget_entity_rows_by_id·   s   
ÿý
ÿÿz#MemorySession.get_entity_rows_by_idc              	   C   sX  z|j dv r	|W S t |¡W S  ttfy0   t|tƒr$t |¡}d}n
t|tƒ p-|dk }Y nw d }t|t	ƒrgt 
|¡}|rE|  |¡}n-t |¡\}}|rV|sV|  |¡}nt |¡d }|rf| j|dd�}nt|tƒrr|  ||¡}|s~t|t	ƒr~|  |¡}|r§|\}}	t |¡\}}
|
tkr”t||	ƒS |
tkrœt|ƒS |
tkr¥t||	ƒS d S td|ƒ‚)N)l   ¶9 l   F?ÓL iýò@Tr   r   F)rƒ   z%Could not find input entity with key )ÚSUBCLASS_OF_IDr   rT   ÚAttributeErrorrV   rS   r   rU   ÚintÚstrÚparse_phonerx   Úparse_usernamer|   Úresolve_invite_linkr„   r   Ú
resolve_idr   r   r	   r   r
   r   r   )r+   Úkeyrƒ   ÚresultrO   rN   ÚinviteÚtupr@   Úentity_hashÚkindr   r   r   Úget_input_entityÇ   sL   


€ú

€



ÿ
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)
rS   r   r   rV   Útyper   r   rL   rW   r&   )r+   Ú
md5_digestÚ	file_sizeÚinstancer�   r;   r   r   r   Ú
cache_fileø   s
   zMemorySession.cache_filec                 C   s6   ||t  |¡f}z|| j| Ž W S  ty   Y d S w r3   )r   r   r&   ÚKeyError)r+   r•   r–   r   r�   r   r   r   Úget_fileÿ   s   ÿzMemorySession.get_file)T) r   r   r   r   r1   Úpropertyr.   r/   r0   r6   r8   Úsetterr>   rA   rD   rF   rH   rJ   rK   r   rQ   r_   rl   rm   rx   r|   r   r„   r“   r˜   rš   Ú__classcell__r   r   r,   r   r      sN    










1r   N)Úenumr   Úabstractr   Ú r   Útlr   Útl.typesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s    (