
    sojV                     z    d Z ddlZddlZddlmZmZ  ej        e          Z ej	        d          Z
dedefdZdS )	a  
Pre-resolves user mentions before a message is sent, so a mention that can't
be resolved degrades to a plain bold name instead of risking the whole send
failing (or the mention silently rendering as unstyled text with no
indication anything went wrong).

Constructing a working "text mention" (an inline mention of a user by ID,
Telethon's ``MessageEntityMentionName`` - what ``<a href="tg://user?id=X">``
in HTML mode, or ``[name](tg://user?id=X)`` in this codebase's MarkdownV2,
both produce) requires the sending account to know that user's access_hash.
Telegram only ever gives a bot a user's access_hash once that bot has
"encountered" them somehow (a message, a shared chat, a callback query, ...)
- an ID alone is never enough. See:
https://docs.telethon.dev/en/stable/concepts/entities.html

This module checks, for every mention in an outgoing message, whether the
target user is actually resolvable - first via this compat layer's own
access_hash cache (``_types.remember_access_hash``, populated from every
user entity this bot has ever observed, anywhere in the codebase), then by
attempting a live ``get_entity()`` call as a fallback. If neither succeeds,
the mention is rewritten from a link into plain bold text so the person's
name still shows and the rest of the message is unaffected.
    N   )get_cached_access_hashremember_access_hashz&\[([^\]]*)\]\(tg://user\?id\\?=(\d+)\)textreturnc                 p  K   |d|vr|S t          t                              |                    }|s|S i }|D ]}|                    d          |                    d          }}t	          |          }|                    d          }||v rUt          |          e	 |                     |           d{V }	t          |	           # t          $ r* t          
                    d| d           d| d||<   Y w xY w|s|S |                                D ]\  }
}|                    |
|          }|S )	z
    Scans `text` (MarkdownV2) for [name](tg://user?id=X) mentions and
    rewrites any unresolvable ones to plain *bold* text. Returns the
    (possibly modified) text, ready for markdown-to-HTML conversion.
    Nztg://user?idr      r   ztg_compat: user z is not resolvable (never encountered by this bot session) - sending their name as plain bold text instead of a clickable mention*)list_MENTION_REfinditergroupintr   
get_entityr   	Exceptionloggerinfoitemsreplace)clientr   matchesreplacementsmatchnameuser_id_struser_id
full_matchentityoldnews               /app/src/tg_compat/_mentions.pyresolve_mentionsr"   -   s      |~T11;''--..G L 3 3!KKNNEKKNNkk""[[^^
%%!'**6		3!,,W55555555F (((( 	3 	3 	3KK*7 * * *  
 (34{{{L$$$	3   &&(( & &S||C%%Ks    *C1C?>C?)__doc__relogging_typesr   r   	getLogger__name__r   compiler   strr"        r!   <module>r-      s    0 
			  @ @ @ @ @ @ @ @		8	$	$ bjBCC. . . . . . . .r,   