Ë
    £| kt&  ã                  óî   — d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ  ej2                  e«      ZdZdd„Zddd„Zdd„Zdd„Z dd„Z!dd„Z"y)a�  /sync (owner only) and the live watch over the channels.

The live part listens to Telegram's updates: a post deleted, edited or added in a connected channel is recorded in My
posts a few seconds later, and service messages ("pinned a message", "channel photo changed" ...) are deleted at once.
The same comparison runs on a timer for everything the bot was not there to see (SYNC_INTERVAL_MINUTES).
é    )ÚannotationsN)ÚOptional)ÚeventsÚtypesé   )ÚCtxÚ	UserErrorÚcmdÚguardÚsay)Ú
SyncReport)ÚSyncer)Úescé   )Úpick_channelsuñ  Usage: <code>/sync</code>
Compares every channel with My posts right now: posts that were deleted or edited in the channel by someone else are updated here (a post deleted in the channel leaves My posts), new posts that were not made with this bot are added, and service messages (â€œpinned a messageâ€�, â€œchannel photo changedâ€� ...) that appeared meanwhile are deleted. The posts that were in a channel before the bot was added are read into My posts too (a long channel takes a few checks; <code>/sync</code> goes on where the last one stopped).
Options: <code>--channel @name</code> (only that channel), <code>--history</code> (read the channel's older posts again and add the ones that are missing; posts you made the bot forget stay forgotten).c                ó  — | xs dj                  «       }d}d}d}|t        |«      k  rT||   }|dk(  r|dz   t        |«      k  r||dz      }|dz  }n|dk(  rd	}|dz  }nt        d
|› d�«      ‚|t        |«      k  rŒT||fS )zi(channel reference or None for every channel, read the older posts again) - ValueError for anything else.Ú NFr   z	--channelr   r   z	--historyTu   I don't know â€œu   â€�.)ÚsplitÚlenÚ
ValueError)ÚrawÚtoksÚrefÚhistoryÚiÚts         ú/app/app/handlers/sync.pyÚparse_sync_argsr   $   s¨   € àŠI�2×ÑÓ€DØ€CØ€GØ	€AØ
Œc�$‹iŠ-Ø�‰GˆØ�Ò  A¡¬¨D«	Ò 1Ø�q˜1‘u‘+ˆCØ�‰F‰AØ�+ÒØˆGØ�‰F‰AäÐ/°¨s°$Ð7Ó8Ð8ð Œc�$‹i‹-ð �ˆ<Ðó    c                óœ   — t        | «      } dj                  d„ | d | D «       «      }|t        | «      |kD  rdt        | «      |z
  › d�z   S dz   S )Nz, c              3  ó2   K  — | ]  }t        |«      –— Œ y ­w©N)Ústr)Ú.0r   s     r   Ú	<genexpr>zids_text.<locals>.<genexpr>9   s   è ø€ Ð2¡k ”c˜!—f¡kùó   ‚z and z morer   )ÚsortedÚjoinr   )ÚidsÚlimitÚshowns      r   Úids_textr,   7   sV   € Ü
�‹+€CØ�I‰IÑ2 c¨&¨5¡kÓ2Ó2€EØ´s¸3³xÀ%Ò7G�eœC ›H uÑ,Ð-¨UÐ3ÑPÐPÈRÑPÐPr   c           
     óö	  — dt        | j                  j                  «      › d�g}| j                  r+dj	                  |dt        | j                  «      › �gz   «      S | j
                  r,dj	                  |dt        | j
                  «      › d�gz   «      S d}| j                  r|j                  d«       | j                  r|j                  d	| j                  › d
�«       | j                  r@d}|j                  dt        | j                  «      › dt        | j                  «      › d�«       | j                  r@d}|j                  dt        | j                  «      › dt        | j                  «      › d�«       | j                  r@d}|j                  dt        | j                  «      › dt        | j                  «      › d�«       | j                  r¶| j                  rd}| j                   rd| j                   › d�nd}| j"                  rC| j                  r"|j                  d| j                  › d|› �«       nP|j                  d|› �«       n;|j                  d| j                  › d| j$                  › d| j                  › |› d�«       | j&                  rZd}|j                  dt        | j&                  «      › dt        | j(                  xs d«      › d t        | j&                  «      › d�«       | j*                  r|j                  d!| j*                  › d"�«       | j,                  r!d}|j                  d#| j,                  › d$�«       | j.                  r;d}|j                  d| j.                  › d%t        | j0                  xs d&«      › d'�«       | j2                  r@d}|j                  dt        | j2                  «      › d(t        | j2                  «      › d)�«       | j4                  r|j                  d!| j4                  › d*�«       t        | j6                  «      t        | j8                  «      z   }|r|j                  d+|› d,�«       | j:                  r|j                  d!| j:                  › d-�«       | j<                  r)d}|j                  dt        | j<                  «      › �«       | j>                  r|j                  d.«       |r)| j>                  s| j                  s|j                  d/«       dj	                  |«      S )0Nz<b>z</b>Ú
u   âš ï¸� u+   ðŸš« Telegram has restricted this channel (zn). Nothing was changed in My posts - the saved posts stay exactly as they are, whatever the channel shows now.Tuo   ðŸ‘€ First look at this channel. From now on, posts made, edited or deleted outside this bot are recorded here.u   âœ“ z( saved post(s) compared with the channelFu   ðŸ—‘ u;    deleted in the channel â†’ removed from My posts (message Ú)u   âœ�ï¸� u8    edited in the channel â†’ updated in My posts (message u   âž• uA    new post(s) made outside the bot â†’ added to My posts (message z (z= of a kind My posts can't hold - poll, sticker ... - skipped)r   u   ðŸ“¥ uC    older post(s), sent before the bot was added â†’ added to My postsu8   ðŸ“¥ The older posts were looked through: nothing to addu   ðŸ“¥ Older posts: z" added so far, read up to message z of zB - the next check goes on, or send /sync again to go on right awayu   ðŸš« z$ post(s) are held back by Telegram (Ú
restrictedz/) - left exactly as saved in My posts (message u   â€¢ z9 saved post(s) tidied up (spaces at the ends of the text)u   ðŸ§¹ z service message(s) deletedz* service message(s) could not be deleted (Úrefusedue   ) - the bot needs the â€œDelete messagesâ€� right, and Telegram lets bots delete only recent messagesz7 saved post(s) show no buttons in the channel (message u\   ). The saved buttons were kept: /posts â†’ the channel â†’ ðŸ”§ Check buttons puts them backzQ new message(s) of a kind My posts can't hold (poll, sticker ...) were left aloneu   â�³ z2 too fresh to judge - the next check will see themzH saved post(s) were changed through the bot meanwhile - left as they areu4   â�¸ A long job started, so this check stopped early.zEverything matches.) r   ÚchannelÚtitleÚerrorr(   Úrestricted_channelÚ
first_lookÚappendÚcheckedÚdeletedr   r,   ÚeditedÚadoptedÚhistory_topÚimportedÚhistory_unsupportedÚhistory_doneÚ
history_tor0   Úrestricted_whyÚ	refreshedÚservices_deletedÚservices_failedÚservices_errorÚbuttons_missingÚunsupportedÚyoungÚ	young_newÚchanged_meanwhileÚanomalyÚbusy)ÚrepÚlinesÚnothingÚskippedÚfreshs        r   Úchannel_blockrR   =   sè  € Ø”3�s—{‘{×(Ñ(Ó)Ð*¨$Ð/Ð0€EØ
‡y‚yØ�y‰y˜ G¬C°·	±	«NÐ+;Ð"<Ð!=Ñ=Ó>Ð>Ø
×ÒØ�y‰yØà=¼cÀ#×BXÑBXÓ>YÐ=Zð [\ð \ðñó
ð 	
ð €GØ
‡~‚~Ø�‰ð  Gô  	HØ
‡{‚{Ø�‰�t˜CŸK™K˜=Ð(PÐQÔRØ
‡{‚{ØˆØ�‰�uœS §¡Ó-Ð.Ð.iÔjrÐsv×s~Ñs~Ójð  jAð  ABð  Cô  	DØ
‡z‚zØˆØ�‰�wœs 3§:¡:›Ð/Ð/gÔhpÐqt×q{Ñq{Óh|Ðg}Ð}~Ðô  	AØ
‡{‚{ØˆØ�‰�tœC §¡Ó,Ð-Ð-nÔowÐx{÷  yDñ  yDó  pEð  oFð  FGð  Hô  	IØ
‡‚Ø�<Š<ØˆGØqt÷  rIò  rI�B�s×.Ñ.Ð/Ð/lÑmð  OQˆØ×ÒØ�|Š|Ø—‘˜u S§\¡\ NÐ2uÐv}Ðu~Ðõ  Aà—‘ÐWÐX_ÐW`ÐaÕbà�L‰LØ$ S§\¡\ NÐ2TÐUX×UcÑUcÐTdÐdhÐil×ixÑixÐhyð  {Bð  zCð CRð Rôð ‡~‚~ØˆØ�‰Ø”C˜Ÿ™Ó'Ð(Ð(LÌSÐQT×QcÑQcÒQsÐgsÓMtÐLuð v:Ü:BÀ3Ç>Á>Ó:RÐ9SÐSTðVô	
ð ‡}‚}Ø�‰�t˜CŸM™M˜?Ð*cÐdÔeØ
×ÒØˆØ�‰�u˜S×1Ñ1Ð2Ð2MÐNÔOØ
×ÒØˆØ�‰Ø�c×)Ñ)Ð*Ð*TÔUXÐY\×YkÑYkÒYxÐoxÓUyÐTzð {pð pô	
ð ×ÒØˆØ�‰Ø”c˜#×-Ñ-Ó.Ð/Ð/fÔgoÐps÷  qDñ  qDó  hEð  gFð Fhð hô	
ð ‡‚Ø�‰�t˜CŸO™OÐ,Ð,}Ð~ÔÜ�—	‘	‹NœS §¡Ó/Ñ/€EÙØ�‰�t˜E˜7Ð"TÐUÔVØ
×ÒØ�‰�t˜C×1Ñ1Ð2Ð2zÐ{Ô|Ø
‡{‚{ØˆØ�‰�wœs 3§;¡;Ó/Ð0Ð1Ô2Ø
‡x‚xØ�‰ÐKÔLÙ�s—x’x¨¯ªØ�‰Ð*Ô+Ø�9‰9�UÓÐr   c                óP   — d}| s|dz   S |dz   dj                  d„ | D «       «      z   S )Nu   ðŸ”„ <b>Sync finished</b>z
Nothing to look at.ú

c              3  ó2   K  — | ]  }t        |«      –— Œ y ­wr"   )rR   )r$   Úrs     r   r%   zreport_text.<locals>.<genexpr>‘   s   è ø€ Ð&IÁ¸A¤}°Q×'7Áùr&   )r(   )ÚreportsÚheads     r   Úreport_textrY   �   s4   € Ø&€DÙØÐ-Ñ-Ð-Ø�&‰=˜6Ÿ;™;Ñ&IÁÓ&IÓIÑIÐIr   c                ól   — t        | dd «      }t        |t        j                  «      r|j                  S d S )NÚpeer_id)ÚgetattrÚ
isinstancer   ÚPeerChannelÚ
channel_id)ÚmsgÚpeers     r   Úchannel_id_ofrb   ”   s.   € Ü�3˜	 4Ó(€DÜ(¨¬u×/@Ñ/@ÔAˆ4�?‰?ÐKÀtÐKr   c                óT  ‡ ‡‡— ‰ j                   ‰ j                  c}Š‰ j                  €t        ‰ «      ‰ _        ‰ j                  Š|j	                  t        dd¬«      «      t        ‰ d¬«      ˆ ˆˆfd„«       «       }|j	                  t        j                  t        j                  «      «      ˆfd„«       }|j	                  t        j                  t        j                  «      «      ˆfd„«       }|j	                  t        j                  t        j                  «      «      ˆfd„«       }y )	NÚsyncT)Úargs)Úownerc           	   “  ób  •‡	‡
K  — | j                   j                  d«      xs dj                  «       }	 t        |«      \  }}t	        ‰j                  «       ƒ d {  –—† |«      }|st        d«      ‚‰j                  j                  «       rt        d«      ‚‰j                  4 ƒd {  –—†  |r*|D ]%  }‰j                  |j                  «      ƒ d {  –—†  Œ' t        | dt!        |«      › d�«      ƒ d {  –—† Š
d	gŠ	dˆ	ˆ
fd
„}‰j#                  |d|rdnd |¬«      ƒ d {  –—† }d d d «      ƒd {  –—†  t        | t%        «      «      ƒ d {  –—†  y 7 Œö# t        $ r/}t        |› d�t        j                  ddt        «      z   «      ‚d }~ww xY w7 Œç7 Œ¿7 Œž7 Œu7 Œg# 1 ƒd {  –—†7  sw Y   ŒwxY w7 Œa­w)Nr   r   rT   z<[^>]+>z,No channels registered yet. Use /addchannel.z`Another long job (replace, repost, shift ...) is still running - try again when it has finished.u   ðŸ”„ Looking at u    channel(s)â€¦g        c           	   “  ó  •K  — t        j                  «       }|‰d   z
  dk  ry |‰d<   	 ‰j                  dt        | j                  «      › d|j
                  › d|j                  › d�«      ƒ d {  –—†  y 7 Œ# t        $ r Y y w xY w­w)Nr   é   u   ðŸ”„ <b>z</b>: z
 (message u   )â€¦)ÚtimeÚ	monotonicÚeditr   r3   ÚphaseÚ
scanned_toÚ	Exception)ÚchrM   ÚnowÚlastÚstatuss      €€r   Úprogz&register.<locals>.h_sync.<locals>.prog´   s‚   øè ø€ Ü—n‘nÓ&�Ø˜˜a™‘= 1Ò$ØØ��Q‘ðØ Ÿ+™+¨´°R·X±X³°¸vÀcÇiÁiÀ[ÐPZÐ[^×[iÑ[iÐZjÐjnÐ&oÓp×pÒpùÜ ò Ùðüs;   ƒ&BªAA7 Á/A5Á0A7 Á4BÁ5A7 Á7	BÂ BÂBÂBT)Úexplicitr   Úprogress)ÚreturnÚNone)Úpattern_matchÚgroupÚstripr   r   Úlist_channelsr   r	   ÚreÚsubÚUSAGEÚlockÚlockedÚreset_historyÚidr   r   Úrun_allrY   )Úeventr   r   r   ÚchansÚerp   rt   rW   rr   rs   ÚctxÚdbÚsys            @@€€€r   Úh_synczregister.<locals>.h_sync    s}  úè ø€ ð ×"Ñ"×(Ñ(¨Ó+Ò1¨r×8Ñ8Ó:ˆð	HÜ*¨3Ó/‰LˆC�Ü!¨×(8Ñ(8Ó(:×":¸CÓ@ˆEñ ÜÐJÓKÐKØ�8‰8�?‰?ÔÜÐ~ÓÐØ—8—8“8ÙÛ�BØ×*Ñ*¨2¯5©5Ó1×1Ñ1ð  ä˜uÐ(8¼¸U»¸ÀNÐ&SÓT×TˆFØ�5ˆDöð ŸJ™J u°tÉWÁTÐZ^Ðim˜JÓn×nˆG÷# —8ô$ �%œ WÓ-Ó.×.Ñ.ð3 #;ùÜò 	HÜ˜q˜c ˜J¬¯©°
¸BÄÓ)FÑFÓGÐGûð	Húð øð 2øØTøð oøð# ø—8—8‘8úð$ 	/ús¿   ….F/´&E ÁEÁ	E Á$AF/Â&FÂ'F/Â*&FÃFÃ"FÃ3FÃ4*FÄFÄFÄ#F/Ä.FÄ/F/ÅF-ÅF/ÅE Å	FÅ*FÆFÆF/ÆFÆFÆFÆF/ÆF*ÆF!ÆF*Æ&F/c              “  óX  •K  — 	 | j                   }t        |«      }|€y t        |t        j                  «      r‰j                  ||«      ƒ d {  –—†  y t        |t        j                  «      s‰j                  ||«       y y 7 Œ3# t        $ r t        j                  dd¬«       Y y w xY w­w)Nz&could not handle a new channel messageT©Úexc_info)Úmessagerb   r]   r   ÚMessageServiceÚ
on_serviceÚMessageEmptyÚnote_newro   ÚlogÚwarning©ÚupdateÚmÚcidrŠ   s      €r   Úon_channel_messagez$register.<locals>.on_channel_messageÂ   s—   øè ø€ ð
	QØ—‘ˆAÜ Ó"ˆCØˆ{ØÜ˜!œU×1Ñ1Ô2Ø—m‘m C¨Ó+×+Ñ+Ü ¤5×#5Ñ#5Ô6Ø—‘˜C Õ#ð 7ð ,ùô ò 	QÜ�K‰KÐ@È4ˆKÖPð	QüsP   ƒB*…B žB*Ÿ/B ÁBÁB ÁB*Á,B Â B*ÂB Â B'Â$B*Â&B'Â'B*c              “  ó
  •K  — 	 | j                   }t        |«      }|�>t        |t        j                  t        j
                  f«      s‰j                  ||«       y y y # t        $ r t        j                  dd¬«       Y y w xY w­w)Nz*could not handle an edited channel messageTr�   )
r�   rb   r]   r   r�   r’   Ú	note_editro   r”   r•   r–   s      €r   Úon_channel_editz!register.<locals>.on_channel_editÐ   sv   øè ø€ ð	UØ—‘ˆAÜ Ó"ˆCØˆ¤z°!´e×6JÑ6JÌE×L^ÑL^Ð5_Ô'`Ø—‘˜S !Õ$ð (aˆøäò 	UÜ�K‰KÐDÈtˆKÖTð	Uüs)   ƒB…AA ÁBÁ B Á=BÁ?B Â Bc              “  óÎ   •K  — 	 ‰j                  | j                  t        | j                  «      «      ƒ d {  –—†  y 7 Œ# t        $ r t
        j                  dd¬«       Y y w xY w­w)Nz)could not handle deleted channel messagesTr�   )Únote_deletedr_   ÚlistÚmessagesro   r”   r•   )r—   rŠ   s    €r   Úon_channel_deletez#register.<locals>.on_channel_deleteÚ   sQ   øè ø€ ð	TØ—/‘/ &×"3Ñ"3´T¸&¿/¹/Ó5JÓK×KÒKùÜò 	TÜ�K‰KÐCÈdˆKÖSð	Tüs1   ƒA%…2? ·=¸? ¼A%½? ¿ A"ÁA%Á!A"Á"A%)Úclientr‰   Úsyncerr   Úonr
   r   r   ÚRawr   ÚUpdateNewChannelMessageÚUpdateEditChannelMessageÚUpdateDeleteChannelMessages)rˆ   r£   r‹   rš   r�   r¢   r‰   rŠ   s   `     @@r   Úregisterrª   ™   sò   ú€ Ø—‘˜SŸV™V€J€FˆBØ
‡z�zÐÜ˜C“[ˆŒ
Ø	�‰€Bð ‡Y�YŒs�6 Ô%Ó&Ü
ˆ3�dÔõ/ó ó 'ð/ð@ ‡Y�YŒv�z‰zœ%×7Ñ7Ó8Ó9óQó :ðQð ‡Y�YŒv�z‰zœ%×8Ñ8Ó9Ó:óUó ;ðUð ‡Y�YŒv�z‰zœ%×;Ñ;Ó<Ó=óTó >ñTr   )r   r#   rw   Útuple)é   )r*   Úintrw   r#   )rM   r   rw   r#   )rW   r    rw   r#   )rw   zOptional[int])rˆ   r   rw   rx   )#Ú__doc__Ú
__future__r   Úloggingr}   rj   Útypingr   Útelethonr   r   Úcommonr   r	   r
   r   r   Úsync_enginer   r¤   r   Útgutilr   Úreplacer   Ú	getLoggerÚ__name__r”   r   r   r,   rR   rY   rb   rª   © r   r   Ú<module>rº      sr   ðñõ #ã Û 	Û Ý ç "ç 4Õ 4Ý $Ý Ý Ý "à€g×Ñ˜Ó!€ð^ð óô&QóMó`JóLô
FTr   