Ë
    £| k«#  ã                  óú  — d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZ dZdZ ej0                  de› de› d�ej2                  «      Z ej0                  de› de› d�ej2                  «      Z ej0                  dej2                  «      Z ej0                  d«      Z ed¬«       G d„ d«      «       Zd!d„Ze G d„ d«      «       Z e G d„ d«      «       Z!d"d„Z"d#d „Z#y)$a´  Links that point at other posts of the same channel (t.me/name/115, t.me/c/123456/115, tg://...&post=115).

When a channel is reposted its posts get new message ids, so a link to post 115 has to point at the copy of post 115.
Only the digits of the message id change; everything around them (?single, ?comment=3, the visible text) stays.
Links can sit in three places: a hyperlink behind text, a link typed into the text, and a button.
é    )ÚannotationsN)Ú	dataclassÚfield)ÚOptional)Útypes)Úadd_surrogateÚdel_surrogateé   )Úapply_span_replacements)Úis_rich_message)Ú
button_urlÚwith_urlz$(?:t\.me|telegram\.me|telegram\.dog)z[A-Za-z][A-Za-z0-9_]{2,31}z^(?:https?://)?(?:www\.)?z(/(?:s/)?(?P<seg>c/(?P<cid>\d+)|(?P<name>z))/(?P<id>\d+)(?![A-Za-z0-9_])z)(?<![\w@./=?&#-])(?:https?://)?(?:www\.)?z/(?:s/)?(?:c/\d+|z)/\d+(?![A-Za-z0-9_])z5^tg://(?P<kind>resolve|privatepost)\?(?P<query>[^#]*)z(?<=[?&])post=(\d+)T)Úfrozenc                  óR   — e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
Zded<   y
)ÚRefzjA link to a post: which channel (username in lower case, or the numeric id) and where the id's digits sit.úOptional[str]ÚnameúOptional[int]ÚcidÚintÚmsg_idÚstartÚendNÚ	seg_start)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   © ó    ú/app/app/postlinks.pyr   r   #   s*   … átà
ÓØ	ÓØƒKØƒJØ	ƒHØ#€Iˆ}Ô#r!   r   c           
     óÂ  — | xs d} t         j                  | «      }|r±t        |j                  d«      r|j                  d«      j	                  «       nd|j                  d«      rt        |j                  d«      «      ndt        |j                  d«      «      |j                  d«      |j                  d«      |j                  d«      ¬«      S t        j                  | «      }|�ryt        d„ |j                  d	«      j                  d
«      D «       «      }t        j                  | «      }|€y|j                  d«      j	                  «       dk(  r|t        j                  d|j                  dd«      «      rVt        |d   j	                  «       dt        |j                  d«      «      |j                  d«      |j                  d«      «      S |j                  d«      j	                  «       dk(  rq|j                  dd«      j!                  «       rQt        dt        |d   «      t        |j                  d«      «      |j                  d«      |j                  d«      «      S y)z[What `url` points at, if it is a link to a post of some channel; None for every other link.Ú r   Nr   ÚidÚseg)r   r   r   r   r   r   c              3  óJ   K  — | ]  }d |v sŒ|j                  d d«      –— Œ y­w)Ú=r
   N)Úsplit)Ú.0Úps     r"   Ú	<genexpr>zparse_ref.<locals>.<genexpr>>   s$   è ø€ ÐWÑ/J¨!ÈcÐUVÊh�a—g‘g˜c 1—oÑ/Jùs   ‚	#Œ#ÚqueryÚ&ÚkindÚresolvez[A-Za-z0-9_]+Údomainr
   ÚprivatepostÚchannel)Ú_WEBÚmatchr   ÚgroupÚlowerr   r   r   Ú_TGÚdictr)   Ú_POST_PARAMÚsearchÚreÚ	fullmatchÚgetÚisdigit)ÚurlÚmÚparamsÚposts       r"   Ú	parse_refrD   /   sÐ  € à
Š)�€CÜ�
‰
�3‹€AÙÜØ,-¯G©G°F¬O�—‘˜“×&Ñ&Ô(ÀØ'(§w¡w¨u¤~”�A—G‘G˜E“NÔ#¸4Ü�q—w‘w˜t“}Ó%Ø—'‘'˜$“-Ø—‘�d“Ø—g‘g˜e“nô
ð 	
ô 	�	‰	�#‹€AÚÜÑW¨q¯w©w°wÓ/?×/EÑ/EÀcÔ/JÓWÓWˆÜ×!Ñ! #Ó&ˆØˆ<ØØ�7‰7�6‹?× Ñ Ó" iÒ/´B·L±LÐAQÐSY×S]ÑS]Ð^fÐhjÓSkÔ4lÜ�v˜hÑ'×-Ñ-Ó/°´s¸4¿:¹:Àa»=Ó7IÈ4Ï:É:ÐVWË=ÐZ^×ZbÑZbÐcdÓZeÓfÐfØ�7‰7�6‹?× Ñ Ó" mÒ3¸¿
¹
À9ÈbÓ8Q×8YÑ8YÔ8[Ü�tœS ¨	Ñ!2Ó3´S¸¿¹ÀA»Ó5GÈÏÉÐTUËÐX\×X`ÑX`ÐabÓXcÓdÐdØr!   c                  ó¢   — e Zd ZU dZded<   ded<    ee¬«      Zded<   d	Zded
<   d	Z	ded<   e
ddd„«       Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zy	)Ú	PostLinksa,  The links that point into one channel, and where its posts went: `ids` maps old message id -> new one.

    By default the copies are in the same channel (/repost): only the message id changes. With `target_id` the
    copies are in another channel (/shift): the channel part of the link changes too.r   Úusernamer   Ú
channel_id)Údefault_factoryr9   ÚidsNÚtarget_usernamer   Ú	target_idc                ól   —  | |j                   xs d t        |j                  «      |�|¬«      S i ¬«      S )N)rG   rH   rJ   )rG   r   r%   )ÚclsÚchrJ   s      r"   Úfor_channelzPostLinks.for_channelV   s1   € á˜RŸ[™[Ò0¨D¼cÀ"Ç%Á%»jÐUXÐUdÈcÔmÐmÐjlÔmÐmr!   c                óð   — t        |«      }|€y|j                  �5| j                  r)|j                  | j                  j                  «       k(  r|S |j                  �|j                  | j
                  k(  r|S y)z>The reference in `url` if it points at a post of THIS channel.N)rD   r   rG   r7   r   rH   )Úselfr@   Úrs      r"   Úref_ofzPostLinks.ref_of[   sa   € ä�c‹NˆØˆ9ØØ�6‰6Ð $§-¢-°A·F±F¸d¿m¹m×>QÑ>QÓ>SÒ4SØˆHØ�5‰5Ð §¡¨$¯/©/Ò!9ØˆHØr!   c                óx  — | j                  |«      }|�|j                  | j                  vryt        | j                  |j                     «      }| j                  €|j
                  |j                  |fS |j                  €y| j                  xs d| j                  › �}|j                  |j                  |› d|› �fS )zi(start, end, new text) to apply to `url` so it points at the copy of its post; None if it stays as it is.Nzc/Ú/)	rT   r   rJ   ÚstrrL   r   r   r   rK   )rR   r@   rS   Únew_idr3   s        r"   Úedit_ofzPostLinks.edit_off   s¦   € à�K‰K˜ÓˆØˆ9˜Ÿ™¨¯©Ñ0ØÜ�T—X‘X˜aŸh™hÑ'Ó(ˆØ�>‰>Ð!Ø—7‘7˜AŸE™E 6Ð)Ð)Ø�;‰;ÐØØ×&Ñ&Ò?¨B¨t¯~©~Ð.>Ð*?ˆØ�{‰{˜AŸE™E g Y¨a°¨xÐ#8Ð8Ð8r!   c                óZ   — | j                  |«      }|€|S |d |d    |d   z   ||d   d  z   S )Nr   é   r
   )rY   )rR   r@   Úes      r"   Ú	remap_urlzPostLinks.remap_urls   sA   € Ø�L‰L˜ÓˆØ�iˆsÐE S¨¨1¨Q©4 [°1°Q±4Ñ%7¸#¸aÀ¹d¸f¸+Ñ%EÐEr!   c                óø  — d}t        |xs d«      }t        j                  |«      D ](  }| j                  |j	                  d«      «      sŒ$|dz  }Œ* |xs g D ]>  }t        |t        j                  «      sŒ| j                  |j                  «      sŒ:|dz  }Œ@ t        |t        j                  «      rG|j                  D ]8  }|j                  D ]'  }	t        |	«      }
|
€Œ| j                  |
«      sŒ#|dz  }Œ) Œ: |S )zcHow many links in a message point at a post of this channel (whether or not the post has a new id).r   r$   r
   )r   Ú_TYPEDÚfinditerrT   r6   Ú
isinstancer   ÚMessageEntityTextUrlr@   ÚReplyInlineMarkupÚrowsÚbuttonsr   )rR   ÚtextÚentitiesÚmarkupÚnÚsurrA   r\   ÚrowÚbÚus              r"   ÚcountzPostLinks.countx   sÕ   € àˆÜ˜DšJ BÓ'ˆÜ—‘ Ö%ˆAØ�{‰{˜1Ÿ7™7 1›:Õ&Ø�Q‘‘ð &ð ’˜R’ˆAÜ˜!œU×7Ñ7Õ8¸T¿[¹[ÈÏÉÕ=OØ�Q‘‘ð  ô �fœe×5Ñ5Ô6Ø—{”{�ØŸœ�AÜ" 1›�AØ‘}¨¯©°Q­Ø˜Q™™ñ %ð #ð
 ˆr!   c                ó˜  — t        |xs g «      }t        |xs d«      }g }t        j                  |«      D ]d  }| j	                  |j                  d«      «      }|€Œ&|j                  |j                  «       |d   z   |j                  «       |d   z   |d   f«       Œf |rt        |||«      \  }}	n$||D �cg c]  }t        j                  |«      ‘Œ }	}}d}
|	D ]T  }t        |t        j                  «      sŒ| j                  |j                  «      }||j                  k7  sŒI||_        |
dz  }
ŒV t        |«      |	t!        |«      |
fS c c}w )za-> (text, entities, typed_links_changed, hyperlinks_changed). Offsets of all entities stay right.r$   r   r
   r[   )Úlistr   r_   r`   rY   r6   Úappendr   r   Úcopyra   r   rb   r]   r@   r	   Úlen)rR   rf   rg   Úentsrj   ÚreplsrA   r\   Únew_surÚnew_entsÚhyperÚnew_urls               r"   Ú
remap_textzPostLinks.remap_textŠ   s+  € ä�H’N Ó#ˆÜ˜DšJ BÓ'ˆØˆÜ—‘ Ö%ˆAØ—‘˜QŸW™W Q›ZÓ(ˆAØ‰}Ø—‘˜aŸg™g›i¨!¨A©$Ñ.°·±³	¸A¸a¹DÑ0@À!ÀAÁ$ÐGÕHð &ñ Ü 7¸¸TÀ5Ó IÑˆG‘Xà #¹DÓ%A¹D°q¤d§i¡i°¥l¸D�XÐ%AˆGØˆÛˆAÜ˜!œU×7Ñ7Õ8ØŸ.™.¨¯©Ó/�Ø˜aŸe™eÓ#Ø#�A”EØ˜Q‘J‘Eð ô ˜WÓ% x´°U³¸UÐBÐBùò &Bs   Â5Ec                óÂ  — t        |t        j                  «      s|dfS g d}}|j                  D ]’  }g }|j                  D ][  }t        |«      }|�| j                  |«      nd}|�&||k7  r!|j                  t        ||«      «       |dz  }ŒK|j                  |«       Œ] |j                  t        j                  |«      «       Œ” |rt        j                  |«      |fS ||fS )zX-> (markup, buttons_changed); the keyboard object is only rebuilt when a button changes.r   Nr
   )
ra   r   rc   rd   re   r   r]   rq   r   ÚKeyboardButtonRow)	rR   rh   rd   Úchangedrk   Úbtnsrl   rm   Únews	            r"   Úremap_markupzPostLinks.remap_markup    sÒ   € ä˜&¤%×"9Ñ"9Ô:Ø˜1�9ÐØ˜AˆgˆØ—;”;ˆCØˆDØ—[”[�Ü˜q“M�Ø+,¨=�d—n‘n QÔ'¸d�Ø�? s¨a¢xØ—K‘K¤¨¨CÓ 0Ô1Ø˜q‘L‘Gà—K‘K •Nð !ð �K‰Kœ×/Ñ/°Ó5Õ6ð ñ 29”×'Ñ'¨Ó-ÀwÐNÐN¸fÀwÐNÐNr!   ©N)rJ   zOptional[dict]Úreturnz'PostLinks'©r@   rW   r‚   zOptional[Ref])r@   rW   r‚   zOptional[tuple])r@   rW   r‚   rW   )rf   rW   r‚   r   )rf   rW   r‚   Útuple)r‚   r„   )r   r   r   r   r   r   r9   rJ   rK   rL   ÚclassmethodrP   rT   rY   r]   rn   rz   r€   r    r!   r"   rF   rF   I   sm   … ñYð
 ÓØƒOÙ dÔ+€CˆÓ+Ø%)€O�]Ó)Ø#€Iˆ}Ó#àónó ðnó	ó9óFó
ó$Cô,Or!   rF   c                  ót   — e Zd ZU dZded<   ded<   ded<   ded	<   ded
<   ded<   ded<   ded<   edd„«       Zy)ÚRelinkedzOA message whose links to other posts of the channel were pointed at the copies.rW   rf   rp   rg   z!Optional[types.ReplyInlineMarkup]rh   ÚboolÚtext_changedÚmarkup_changedr   Ún_typedÚn_hyperÚ	n_buttonsc                óN   — | j                   | j                  z   | j                  z   S r�   )r‹   rŒ   r�   )rR   s    r"   ÚlinkszRelinked.linksÀ   s   € à�|‰|˜dŸl™lÑ*¨T¯^©^Ñ;Ð;r!   N)r‚   r   )r   r   r   r   r   Úpropertyr�   r    r!   r"   r‡   r‡   ³   sB   … áYà
ƒIØƒNØ-Ó-ØÓØÓØƒLØƒLØƒNàò<ó ñ<r!   r‡   c           
     ó*  — t        | «      rdg ddf\  }}}}n/|j                  | j                  xs d| j                  «      \  }}}}|j	                  | j
                  «      \  }}|s|s|syt        |||t        |xs |«      t        |«      |||¬«      S )zbThe new text / entities / buttons of `msg` once its links are remapped; None when nothing changes.r$   r   N)rf   rg   rh   r‰   rŠ   r‹   rŒ   r�   )r   rz   Úmessagerg   r€   Úreply_markupr‡   rˆ   )Úmsgr�   rf   rg   r‹   rŒ   rh   r�   s           r"   Úrelink_messager•   Å   s    € ä�sÔØ+-¨r°1°a¨<Ñ(ˆˆh˜¡à+0×+;Ñ+;¸C¿K¹KÒ<MÈ2ÈsÏ|É|Ó+\Ñ(ˆˆh˜ Ø×*Ñ*¨3×+;Ñ+;Ó<Ñ€FˆIÙ‘w¡)ØÜØØØÜ˜'Ò, WÓ-Ü˜I“ØØØô	ð 	r!   c                ód   — i }| D ](  }|d}}|| v r|dk  r| |   |dz   }}|| v r|dk  rŒ|||<   Œ* |S )zkSeveral reposts in a row: {1: 5, 5: 9} -> {1: 9, 5: 9} (a post that moved twice points at its latest copy).r   éd   r
   r    )ÚstepÚoutÚoldÚcurÚseens        r"   Úresolve_chainr�   Ú   sW   € à
€CÛˆØ˜ˆTˆØ�T‰k˜d SšjØ˜S™	 4¨!¡8�ˆCð �T‰k˜d S›jàˆˆCŠð	 ð
 €Jr!   rƒ   )r�   rF   r‚   zOptional[Relinked])r˜   r9   r‚   r9   )$r   Ú
__future__r   rr   r<   Údataclassesr   r   Útypingr   Útelethonr   Útelethon.helpersr   r	   Úlinkswapr   Úrichmsgr   Útgutilr   r   Ú_HOSTÚ_NAMEÚcompileÚIr4   r_   r8   r:   r   rD   rF   r‡   r•   r�   r    r!   r"   Ú<module>rª      s!  ðñõ #ã Û 	ß (Ý å ß 9å -Ý $ß (à/€Ø%€à€r‡z�zØ   Ð'OÐPUÈwÐVtÐuÐwy×w{Ñw{ó€ð 
ˆ�‰Ø0°°Ð7HÈÈÐOdÐeÐgi×gkÑgkó
€ð €b‡j�jÐIÈ2Ï4É4ÓP€Øˆb�j‰jÐ/Ó0€ñ �$Ô÷$ð $ó ð$óð4 ÷fOð fOó ðfOðR ÷<ð <ó ð<ó"ô*r!   