Ë
    £| k`"  ã                  óB  — d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZmZ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mZmZmZmZ dd
lmZ ddl m!Z!  ejD                  e#«      Z$dd„Z%	 d	 	 	 dd„Z&dddddœ	 	 	 	 	 	 	 	 	 	 	 dd„Z'ddd„Z(ddddœ	 	 	 	 	 	 	 dd„Z)y)ai  /shift: copy posts of one channel into another channel - text, formatting, media, albums, buttons and replies
- and register the copies in My posts. The source is only read, never changed or deleted.

It is the /repost copy loop pointed at two channels. Links from a shifted post to another post of the source channel
follow the content: when that post was shifted too, the link points at its copy in the destination.

Three optional extras are remembered with the shift (job marks), so that continuing a stopped shift keeps them:
  -c          afterwards every link to a post of the source, in ANY connected channel, is pointed at the copy
              (crosslinks.relink_after_shift; the source itself is only read);
  -all        afterwards the destination gets the source's name, description and profile photo
              (profile_copy.copy_profile);
  --no-media  the copies get no files: every post is copied as its text, with its formatting and buttons (mark "nomedia";
              repost_engine.without_files). A post that is only a file has nothing to copy and is left out.

When Telegram hides the first new copies (read back right after posting) the shift stops by itself - more copies would
only be more posts nobody can see - and goes on only when the owner presses Continue (mark "hiddenok").

A source that Telegram restricts (a copyright strike: "This channel is unavailable due to copyright infringement", its
posts marked or shown as a notice) is copied from what My posts saved of its posts - every post that has a saved copy,
however intact Telegram's version looks (repost_engine.copy_source). A post without a saved copy is copied as Telegram
hands it out if that is the post itself, and skipped if it is only the notice. A source that can't be read at all (a
banned or private channel) is copied from My posts alone: mark "fromdb", set when the plan was made that way.
é    )ÚannotationsN)ÚSimpleNamespace)ÚAnyÚ	AwaitableÚCallableÚOptionalé   )ÚCrossResultÚrelink_after_shiftÚshift_links)Úcopy_profile)ÚSTOP_IF_HIDDENÚCopyJobÚDeleteResultÚRepostOptionsÚ
RepostPlanÚRepostResultÚ	copy_loopÚdelete_pendingÚplan_from_savedÚrelink_copiesÚsaved_lookupÚsaved_units)Úprobe_channel)Úpeer_ofc                óp   — t        | j                  | j                  | j                  | j                  ¬«      S )zSThe source channel as stored with the shift (it need not be registered in the bot).)ÚidÚaccess_hashÚusernameÚtitle)r   Úsrc_channel_idÚsrc_access_hashÚsrc_usernameÚ	src_title)Úshifts    ú/app/app/shift_engine.pyÚ	source_ofr'   4   s1   € äØ×Ñ¨U×-BÑ-BÈU×M_ÑM_Ðgl×gvÑgvôð ó    c           	   ƒ  ó®   K  — t        || j                  |j                  |xs d«      ƒ d{  –—† t        «       ||||¬«      }|j                  rdS |S 7 Œ&­w)z’The plan of a shift made from My posts alone, for a source that can't be read; None when My posts has nothing
    of that channel (in that range).r	   N)Úfirst_idÚlast_idÚ
unreadableÚno_media)r   Úsent_posts_fromr   r   Úerror)ÚdbÚsrcr*   r+   r,   r-   Úplans          r&   Úplan_from_my_postsr3   ;   sV   è ø€ ô
 Ø�2×%Ñ% c§f¡f¨hªm¸!Ó<×<¼m»oÐX`ÐjqØ¨ô€Dð —:’:ˆ4Ð' 4Ð'ð =ús   ‚*A¬A
­'Ag333333ó?gffffffæ?)ÚdelayÚsettle_pauseÚprogressÚshould_stopc             ƒ  óF  ‡‡‡‡‡‡‡K  — t        ‰«      Št        ‰j                  ‰j                  d¬«      ƒ d{  –—† «      }	t	        ‰‰|	«      }
‰j                  ‰j                  «      ƒ d{  –—† }d|v }d}|s"	 t        | t        ‰«      «      ƒ d{  –—† \  }}}t        d'i dt        ‰«      “d	t        ‰«      “d
t        «       “d|
“d‰j                  “d‰j                  “d‰j                  ‰j                  «      ƒ d{  –—† “d|	“dˆˆˆˆfd„“dt!        ‰‰j                  «      “dd“d|rˆˆˆfd„nd“d|“d|“dd|v “dd|v rdnt"        “Ž}t%        | ||‰||¬«      ƒ d{  –—† Š‰j&                  �sJ‰j(                  �s=d‰_        ‰r ‰‰«      ƒ d{  –—†  	 ‰j                  ‰j                  «      ƒ d{  –—† }t-        | ‰‰t        |«      |D ��cg c]  \  }}|‘Œ	 c}}|
|||¬«	      ƒ d{  –—† ‰_        ‰j                  ‰j                  «      ƒ d{  –—† }d!|v rd"|vrt9        | ‰‰‰‰‰‰«      ƒ d{  –—†  d#|v r{‰j.                  �‰j.                  j&                  sYd‰_        t;        «       ‰_        d(ˆˆfd$„}‰r ‰‰«      ƒ d{  –—†  	 t?        | ‰‰‰‰||||‰j<                  ¬%«
      ƒ d{  –—†  ‰S ‰S 7 �ŒŒ7 �Œ[7 �Œ8# t        $ r t        j                  dd¬«       Y �ŒUw xY w7 �Œó7 �Œ‘7 �Œa7 �Œ@c c}}w 7 �Œ# t        $ r:}t        j1                  d «       t3        |«      j4                  ‰_        Y d}~�ŒHd}~ww xY w7 �Œ27 �Œ7 ŒÁ7 Œ›# t        $ r:}t        j1                  d&«       t3        |«      j4                  ‰_         Y d}~‰S d}~ww xY w­w))z¶Copy the posts shift.first_id..shift.last_id of the source into `dst` (a registered channel), in order.
    Safe to run again to continue: posts that already have a copy are skipped.F)Ú
alive_onlyNÚfromdbz?could not ask Telegram whether the source channel is restrictedT)Úexc_infoÚsrc_peerÚdst_peerÚoptsÚlinksr*   r+   ÚdoneÚidmapÚrecordc                óT   •— ‰j                  ‰j                  ‰j                  | ‰«      S ©N)Úrecord_shiftr   )Úrowsr0   Údstr%   Úuser_ids    €€€€r&   Ú<lambda>zrun_shift.<locals>.<lambda>i   s   ø€ ˜BŸO™O¨E¯H©H°c·f±f¸dÀGÔLr(   ÚsavedÚ	use_savedÚunitsc                 ó\   •— t        ‰ ‰j                  ‰j                  ‰j                  «      S rD   )r   r   r*   r+   )r0   r%   r1   s   €€€r&   rI   zrun_shift.<locals>.<lambda>l   s   ø€ ”{ 2 s§v¡v¨u¯~©~¸u¿}¹}ÔMr(   Úfrom_dbÚ
restrictedr-   ÚnomediaÚstop_if_hiddenÚhiddenokr   )r4   r6   r7   r5   Úfinish)r?   r4   r5   r7   z2pointing the links of a shift at the copies failedÚprofileÚprofile_doneÚrelinkc              “  ó8   •K  — ‰r ‰‰«      ƒ d {  –—†  y y 7 Œ­wrD   © )Ú_crossr6   Úress    €€r&   Úwatchzrun_shift.<locals>.watch‡   s   øè ø€ ÙÙ" 3›-×'Ñ'ð Ø'ús   ƒ‘’)r4   r5   r6   r7   ÚresultzDpointing the links of the other channels at the shifted posts failedrX   )ÚreturnÚNone)!r'   ÚdictÚshift_pairsr   r   Úmarks_ofr   r   Ú	ExceptionÚlogÚwarningr   r   r*   r+   Úshift_done_idsr   r   r   ÚstoppedÚabortedÚphaser   ÚfinalÚ	exceptionÚtypeÚ__name__Úfinal_errorÚshift_profiler
   Úcrossr   Úcross_error)Úclientr0   r%   rG   rH   r4   r5   r6   r7   rA   r?   ÚmarksrN   rO   Ú_topÚ_errÚjobÚpairsÚ_ÚnÚer[   rZ   r1   s    ````  `              @@r&   Ú	run_shiftrz   G   s   þè ø€ ô �EÓ
€CÜ�r—~‘~ e§h¡h¸5�~ÓA×AÓB€EÜ˜˜S %Ó(€EØ—+‘+˜eŸh™hÓ'×'€EØ˜%Ð€GØ€JÙð	jÜ+8¸ÄÈÃÓ+N×%NÑ"ˆD�*˜dô ò Ü˜”ðä˜”ðô Œ_ðñ ð	ð
 —’ðð —’ðð ×$Ñ$ U§X¡XÓ.×.Ð.ðñ ð÷ Mðô ˜2˜sŸv™vÔ&ðñ ðñ SZÖMÐ_cøðñ ðñ ðð ˜eÑ#ðð  '¨%Ñ/‘q´^øð!€Cô$ ˜& #¨U¸XÐS^ÐmyÔz×
z€CØ�;‹;˜sŸ{›{ØˆŒ	ÙÙ˜3“-×Ðð	/ØŸ.™.¨¯©Ó2×2ˆEÜ+Ø˜˜C¤ e£¹UÔ.C¹U±T°Q¸ªq¸UÒ.CÈ5ÐX]Ø)°{ô÷ ˆCŒIð —k‘k %§(¡(Ó+×+ˆØ˜Ñ .¸Ñ"=Ü ¨¨E°3¸ÀÀhÓO×OÐOØ�uÑ c§i¡iÐ&;ÀÇ	Á	×@QÒ@QØˆCŒIÜ#›ˆCŒIö(ñ Ù˜s“m×#Ð#ð3Ü(Ø˜B  s¨C°uÈ<ÐbgØ +°C·I±Iô÷ ð ð €Jˆ3€Jð Bùà'ùð
 &OúÜò 	jÜ�K‰KÐYÐdhˆK×ið	júð /ùð {ùð  ùà2ûã.Cðúô ò 	/Ü�M‰MÐNÔOÜ" 1›g×.Ñ.ˆC�OŠOûð	/úð ,ùàOùð $øðùô ò 3Ü—‘ÐdÔeÜ"& q£'×"2Ñ"2�—�Ø€Jûð3üsP  ‰1N!ºK»3N!Á.K	Á/N!Á<K ÂKÂK ÂA&N!ÄK6ÄA$N!Å(K9Å)2N!ÆK<ÆN!Æ!L Æ?K?Ç L ÇLÇ'L Ç7LÇ8	L ÈN!ÈMÈ  N!É MÉAN!ÊMÊN!Ê"M Ê=MÊ>M ËN!Ë	N!ËK Ë K3Ë/N!Ë2K3Ë3N!Ë9N!Ë<N!Ë?L ÌL Ì	MÌ/M	ÍN!Í	MÍN!ÍN!ÍN!ÍM Í	NÍ$/NÎN!ÎNÎN!c              ƒ  ó`  K  — d|_         |r ||«      ƒ d{  –—†  	 t        | t        |«      |«      ƒ d{  –—† |_        |j                  j                  rO|j                  |j                  |j                  |j                  j                  |j                  |«      ƒ d{  –—†  |j                  j                  r%|j                  |j                  d«      ƒ d{  –—†  yy7 ŒÉ7 Œ¬7 ŒD7 Œ# t        $ r9}t        j                  d«       t        |«      j                  |_        Y d}~yd}~ww xY w­w)zh-all: the destination takes over the source's name, description and photo (the outcome goes into `res`).rT   NrU   z"copying the channel profile failed)rh   r   r'   rT   r    Úsave_channelr   r   r   ÚokÚset_markrb   rc   rj   rk   rl   Úprofile_error)rq   r0   r%   rG   rH   rZ   r6   ry   s           r&   rn   rn   ˜   sâ   è ø€ à€C„IÙÙ�s‹m×Ðð-Ü(¨´¸5Ó1AÀ3ÓG×GˆŒØ�;‰;×ÒØ—/‘/ #§&¡&¨#¯/©/¸3¿;¹;×;LÑ;LÈcÏlÉlÐ\cÓd×dÐdØ�;‰;�>Š>Ø—+‘+˜eŸh™h¨Ó7×7Ñ7ð ð 	øàGøàdøà7ùÜò -Ü�‰Ð:Ô;Ü  ›G×,Ñ,ˆ×Ñûð-üsm   ‚D.—C!˜D.�C) ¶C#·A)C) Â C%Â!9C) ÃC'ÃC) ÃD.Ã#C) Ã%C) Ã'C) Ã)	D+Ã2/D&Ä!D.Ä&D+Ä+D.g      à?©r4   r6   Úfallbackc          	   ƒ  óZ  ‡‡‡K  — ‰j                  ‰j                  d«      ƒ d{  –—† }dˆˆˆfd„}t        | t        ‰«      |||||¬«      ƒ d{  –—† }	‰j	                  ‰j                  «      ƒ d{  –—† d   |	_        |	j
                  dkD  xr |	j                   |	_        |	S 7 Œ7 ŒV7 Œ5­w)z_Undo: remove the copies of this shift from the destination channel (the source is not touched).i@B Nc              “  óp   •K  — ‰j                  ‰j                  ‰j                  | «      ƒ d {  –—†  y 7 Œ­wrD   )Úmark_shift_deletedr   )Úidsr0   rG   r%   s    €€€r&   Úmarkz!delete_shift_copies.<locals>.markµ   s'   øè ø€ Ø×#Ñ# E§H¡H¨c¯f©f°cÓ:×:Ò:ús   ƒ+6®4¯6r€   Únew_leftr   )r…   Úlistr]   r^   )Úshift_pending_newr   r   r   Úshift_countsÚ	remainingr/   Úblocked)
rq   r0   rG   r%   r4   r6   r�   Útodor†   rZ   s
    ```      r&   Údelete_shift_copiesrŽ   ¨   s”   úè ø€ ð ×%Ñ% e§h¡h°	Ó:×:€D÷;ô ˜v¤w¨s£|°T¸4ÀuÐW_ÐjrÔs×
s€CØŸ?™?¨5¯8©8Ó4×4°jÑA€C„MØ—-‘- !Ñ#Ò5¨C¯I©I¨€C„KØ€Jð ;øð
 tøØ4ús3   … B+¥B%¦*B+ÁB'Á"B+Á3B)Á42B+Â'B+Â)B+)r]   r   )NNNF)r-   Úboolr]   zOptional[RepostPlan])rH   Úintr4   Úfloatr5   r‘   r6   z3Optional[Callable[[RepostResult], Awaitable[None]]]r7   zOptional[Callable[[], bool]]r]   r   rD   )rH   r�   rZ   r   r]   r^   )r4   r‘   r6   z3Optional[Callable[[DeleteResult], Awaitable[None]]]r�   z*Optional[Callable[[list], Awaitable[Any]]]r]   r   )*Ú__doc__Ú
__future__r   ÚloggingÚtypesr   Útypingr   r   r   r   Ú
crosslinksr
   r   r   Úprofile_copyr   Úrepost_enginer   r   r   r   r   r   r   r   r   r   r   r   Úrestrictionsr   Útgutilr   Ú	getLoggerrl   rc   r'   r3   rz   rn   rŽ   rX   r(   r&   Ú<module>r�      s	  ðñõ. #ã Ý !ß 5Ó 5ç DÑ DÝ &÷÷ ÷ ó õ (Ý à€g×Ñ˜Ó!€óð MRð	(ØEIð	(àó	(ð& ØØDHØ04ñNð
 ðNð ðNð ðNð BðNð .ðNð óNôb-ð, ØDHØ;?ñð ðð Bðð 9ðð ôr(   