Ë
    £| kC  ã                  óx  — 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	m
Z
mZ ddlmZmZ ddlmZmZmZ dd	lmZmZmZmZmZ  ej2                  e«      Zd
ZdddœggZ G d„ de«      Zdd„Z dd„Z!d d„Z"ddœd!d„Z#e G d„ d«      «       Z$ddœ	 	 	 	 	 d"d„Z%e G d„ d«      «       Z&dddddœ	 	 	 	 	 	 	 	 	 	 	 d#d„Z'y)$a%  Make sure the buttons shown in a channel are the ones we saved - and fix them when they are not.

Used by /repost (every new post is read back right after it is created) and by "Check buttons" in My posts,
which repairs posts whose buttons are saved in the bot but are missing in the channel.
é    )ÚannotationsN)Ú	dataclassÚfield)Ú	AwaitableÚCallableÚOptional)ÚerrorsÚtypesé   )ÚCHUNKÚfetch_messagesÚis_skippable)Úbuild_markupÚ
button_urlÚedit_rawÚflood_retryÚpeer_ofé   u   â€¦zhttps://t.me/telegram)ÚtÚuc                  ó   — e Zd ZdZy)ÚButtonsNotShownzSTelegram accepted the edit, but the post still does not show the buttons it should.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú/app/app/buttons_sync.pyr   r      s   „ Ú]r   r   c           	     óÚ   — g }t        | t        j                  «      rN| j                  D ]?  }|j                  D ].  }t        |«      }|€Œ|j                  t        |dd«      |f«       Œ0 ŒA |S )zB[(text, url)] of the link buttons of a keyboard, in reading order.ÚtextÚ )Ú
isinstancer
   ÚReplyInlineMarkupÚrowsÚbuttonsr   ÚappendÚgetattr)ÚmarkupÚoutÚrowÚbr   s        r   Úlink_buttonsr-      sb   € à€CÜ�&œ%×1Ñ1Ô2Ø—;”;ˆCØ—[”[�Ü˜q“M�Ø‘=Ø—J‘J¤¨¨6°2Ó 6¸Ð:Õ;ñ !ð ð
 €Jr   c                óJ   — t        | «      D ��cg c]  \  }}|‘Œ	 c}}S c c}}w )N)r-   )r)   Ú_r   s      r   Ú	link_urlsr0   *   s%   € Ü& vÔ.Ô/Ñ.‘$�!�QŠAÐ.Ò/Ð/ùÓ/s   �c              ƒ  óŽ   K  — t        | ||g«      ƒ d{  –—† }|r|d   nd}t        |«      ryt        |j                  «      S 7 Œ.­w)zUThe link-button URLs the channel shows on a post right now; None if the post is gone.Nr   )r   r   r0   Úreply_markup)ÚclientÚpeerÚ
message_idÚmsgsÚms        r   Ú	live_urlsr8   .   sD   è ø€ ä ¨¨z¨lÓ;×;€DÙˆˆQŠ˜T€AÜ�A„ØÜ�Q—^‘^Ó$Ð$ð	 <ús   ‚A”A•/Agffffffæ?©Úpausec             ƒ  óÈ  ‡ ‡‡‡K  — t        ‰«      }|syt        ‰ ‰‰«      ƒ d{  –—† }|€t        d‰› d�«      ‚||k(  ry	 t        ˆ ˆˆˆfd„«      ƒ d{  –—†  |rt        j                  |«      ƒ d{  –—†  t        ‰ ‰‰«      ƒ d{  –—† |k(  ryt        j                  d‰«       t        t        «      ‰fD ]=  }	 t        |fˆ ˆˆfd„	«      ƒ d{  –—†  |sŒ!t        j                  |«      ƒ d{  –—†  Œ? t        ‰ ‰‰«      ƒ d{  –—† |k(  ry	t        d‰› d
�«      ‚7 Œü7 ŒÍ# t        j
                  $ r Y Œàw xY w7 ŒÉ7 Œ¶7 Œo# t        j
                  $ r Y Œ‚w xY w7 Œj7 ŒU­w)a8  Make post `message_id` show the link buttons of `markup`, and check it by reading the post back.

    Returns "fine" (it already did), "set" (one edit was enough) or "nudged" (needed the two-step edit).
    Raises ButtonsNotShown if the buttons still are not there, LookupError if the post no longer exists.
    ÚfineNzpost z no longer existsc                 ó"   •— t        ‰ ‰‰‰¬«      S ©N)r)   ©r   )r3   r)   r5   r4   s   €€€€r   Ú<lambda>zensure_markup.<locals>.<lambda>G   s   ø€ ¤(¨6°4¸ÈFÕ"Sr   ÚsetzJpost %s: the edit did not make the buttons show - trying the two-step editc                ó"   •— t        ‰‰‰| ¬«      S r>   r?   )Ústepr3   r5   r4   s    €€€r   r@   zensure_markup.<locals>.<lambda>R   s   ø€ ´¸ÀÀzÐZ^Õ0_r   Únudgedz  still does not show its buttons)r0   r8   ÚLookupErrorr   r	   ÚMessageNotModifiedErrorÚasyncioÚsleepÚlogÚinfor   Ú
NUDGE_ROWSr   )r3   r4   r5   r)   r:   ÚwantÚliverC   s   ````    r   Úensure_markuprN   7   sd  ûè ø€ ô �VÓ€DÙØÜ˜6 4¨Ó4×4€DØ€|Ü˜E * Ð->Ð?Ó@Ð@Øˆt‚|ØðÜÖSÓT×TÐTñ Ü�m‰m˜EÓ"×"Ð"Ü�v˜t ZÓ0×0°DÒ8Øä‡H�HÐYÐ[eÔfÜœjÓ)¨6Ó2ˆð	Ü¨$Ö_Ó`×`Ð`ò Ü—-‘- Ó&×&Ñ&ð 3ô �v˜t ZÓ0×0°DÒ8ØÜ
˜E * Ð-MÐNÓ
OÐOð3 5øð 	UùÜ×)Ñ)ò Ùðúð 	#øØ0øð aùÜ×-Ñ-ò 	Ùð	úð 'øØ0ús½   †E"¥D"¦E"ÁD& ÁD$ÁD& ÁE"Á5D?Á6E"Â
EÂ2E"Â>EÃEÃEÃE"ÃE"Ã3EÃ4E"Ä
E ÄE"Ä$D& Ä&D<Ä9E"Ä;D<Ä<E"ÅE"ÅEÅEÅE"ÅEÅE"Å E"c                  ól   — e Zd ZU ded<   dZded<   dZded<    ee¬«      Zded	<    ee¬«      Z	ded
<   y)Ú
ButtonScanÚobjectÚchannelr   ÚintÚtotalr<   ©Údefault_factoryÚlistÚbadÚgoneN)
r   r   r   Ú__annotations__rT   r<   r   rW   rX   rY   r   r   r   rP   rP   ]   s7   … àƒOØ€Eˆ3ƒNØ€Dˆ#ƒMÙ dÔ+€CˆÓ+Ù tÔ,€Dˆ$Ô,r   rP   )Úprogressc             ƒ  ó  K  — t        |¬«      }t        |«      }g }|D ]C  }|j                  sŒt        t	        |j
                  «      «      }|sŒ1|j                  ||f«       ŒE t        |«      |_        t        dt        |«      t        «      D ]ð  }	||	|	t        z    }
t        | ||
D ��cg c]  \  }}|j                  ‘Œ c}}«      ƒ d{  –—† }t        |
|«      D ]Ž  \  \  }}}t        |«      r|j                  j                  |«       Œ0t        |j                  «      |k(  r|xj                   dz  c_        Œ^|j"                  j                  |t        |j                  «      |f«       Œ� |sŒá ||«      ƒ d{  –—†  Œò |S c c}}w 7 Œ¾7 Œ­w)zFCompare the saved link buttons of `posts` with what the channel shows.)rR   r   Nr   )rP   r   r5   r0   r   r&   r'   ÚlenrT   Úranger   r   Úzipr   rY   r2   r<   rX   )r3   ÚchÚpostsr[   Úscanr4   ÚwantedÚpÚurlsÚiÚpartr/   r6   r7   s                 r   Úscan_buttonsrh   f   sM  è ø€ ô ˜bÔ!€DÜ�2‹;€DØ€FÛˆØ�|Š|ØÜœ a§i¡iÓ0Ó1ˆÚØ�M‰M˜1˜d˜)Õ$ð ô �V“€D„JÜ�1”c˜&“k¤5Ö)ˆØ�a˜!œe™)Ð$ˆÜ# F¨DÉDÔ2QÉDÁDÀAÀq°1·<³<ÈDÒ2QÓR×RˆÜ  džO‰L‰IˆQ��qÜ˜AŒØ—	‘	× Ñ  Õ#Ü˜1Ÿ>™>Ó*¨dÒ2Ø—	’	˜Q‘–	à—‘—‘ ¤I¨a¯n©nÓ$=¸tÐ DÕEð ,ò Ù˜4“.× Ñ ð *ð €Kùó 3RÐRøð !ús>   ‚AFÁAFÂ)FÂ?FÃ
F	ÃB#FÅ/FÅ:FÅ;FÆFc                  ó~   — e Zd ZU dZded<   dZded<   dZded<   dZded<    ee	¬«      Z
ded	<   d
Zded<   dZded<   y
)Ú	FixResultr   rS   ÚfixedrD   ÚalreadyrY   rU   rW   ÚfailedNzOptional[str]ÚabortedFÚboolÚstopped)r   r   r   rk   rZ   rD   rl   rY   r   rW   rm   rn   rp   r   r   r   rj   rj   ƒ   sJ   … à€Eˆ3ƒNØ€FˆCƒOØ€GˆSÓØ€Dˆ#ƒMÙ¨Ô.€FˆDÓ.Ø!€Gˆ]Ó!Ø€GˆTÔr   rj   g333333ó?)Údelayr:   r[   Úshould_stopc          	   ƒ  ó„  K  — t        «       }t        |«      }d}	|D ]Ë  }
|
d   }|r |«       r
d|_         |S 	 t        | ||j                  t        |j                  «      |¬«      ƒ d{  –—† }d}	|dk(  r|xj(                  dz  c_        n/|xj*                  dz  c_        |dk(  r|xj,                  dz  c_        |r ||«      ƒ d{  –—†  t%        j&                  |«      ƒ d{  –—†  ŒÍ |S 7 Œƒ# t        $ r |xj                  dz  c_        Y Œòt        $ r£}t        |«      j                  }t        j                  d|j                  ||«       |j                  j                  |j                  |f«       |	dz  }	|	t         k\  r||_        Y d}~ |S t%        j&                  |«      ƒ d{  –—†7   Y d}~�Œ˜d}~ww xY w7 Œõ7 ŒÚ­w)	zbPut the saved buttons back on the posts in `bad` (the first item of each entry is the saved post).r   Tr9   Nr   z&could not fix the buttons of %s: %s %sr<   rD   )rj   r   rp   rN   r5   r   r&   rE   rY   Ú	ExceptionÚtyper   rI   Úwarningrm   r'   ÚABORT_AFTERrn   rG   rH   rl   rk   rD   )r3   r`   rX   rq   r:   r[   rr   Úresr4   ÚstreakÚentryÚpostÚhowÚeÚnames                  r   Úfix_buttonsr   Ž   sŒ  è ø€ ô ‹+€CÜ�2‹;€DØ€FÛˆØ�Q‰xˆÙ™;œ=ØˆCŒKØð4 €Jð3	Ü% f¨d°D·O±OÄ\ÐRV×R^ÑR^ÓE_ÐglÔm×mˆCð ˆØ�&Š=Ø�KŠK˜1ÑŽKà�IŠI˜‰N�IØ�hŠØ—
’
˜a‘•
ÙÙ˜3“-×ÐÜ�m‰m˜EÓ"×"Ñ"ð; ð< €Jð1 nùÜò 	Ø�HŠH˜‰M�HÙÜò 		Ü˜“7×#Ñ#ˆDÜ�K‰KÐ@À$Ç/Á/ÐSWÐYZÔ[Ø�J‰J×Ñ˜tŸ™°Ð5Ô6Ø�a‰KˆFØœÒ$Ø"�”Üð €Jô —-‘- Ó&×&Ñ&Ýûð		úð$  øØ"ús„   ‚5G ¸0C.Á(C,Á)C.Á-AG ÃF<ÃG Ã#F>Ã$G Ã,C.Ã.F9ÄG ÄF9ÄA3F4Æ	G ÆF4Æ(F+Æ)F4Æ.G Æ4F9Æ9G Æ>G )ÚreturnrW   )r5   rS   r€   zOptional[list])r5   rS   r:   Úfloatr€   Ústr)ra   rW   r[   z1Optional[Callable[[ButtonScan], Awaitable[None]]]r€   rP   )rX   rW   rq   r�   r:   r�   r[   z0Optional[Callable[[FixResult], Awaitable[None]]]rr   zOptional[Callable[[], bool]]r€   rj   )(r   Ú
__future__r   rG   ÚloggingÚdataclassesr   r   Útypingr   r   r   Útelethonr	   r
   Úreplace_enginer   r   r   Útgutilr   r   r   r   r   Ú	getLoggerr   rI   rw   rK   ÚRuntimeErrorr   r-   r0   r8   rN   rP   rh   rj   r   r   r   r   Ú<module>rŒ      s'  ðñõ
 #ã Û ß (ß 0Ñ 0ç "ç ?Ñ ?ß LÕ Là€g×Ñ˜Ó!€à€ð Ð!8Ñ9Ð:Ð;€
ô^�lô ^ó	ó0ó%ð RUõ "PðL ÷-ð -ó ð-ð _cñØðØ*[ðàóð: ÷ð ó ðð ØØAEØ04ñ,ð 
ð,ð
 ð,ð ð,ð ?ð,ð .ð,ð ô,r   