приватные каналы

This commit is contained in:
Artem Tsyrulnikov
2026-01-21 12:16:02 +03:00
parent b5695360c2
commit 2c0dcbef97
7 changed files with 59 additions and 89 deletions

View File

@@ -41,15 +41,16 @@ MAX_CREATIVE_MEDIA_BYTES = 20 * 1024 * 1024
_INVITE_LINK_HTML = re.compile(
r'<a\s+href=["\']https?://t\.me/(?:\+|joinchat/)[a-zA-Z0-9_-]+["\']>([^<]*)</a>',
re.IGNORECASE,
r'<a\s+href=["\']https?://t\.me/(?:\+|joinchat/)[a-zA-Z0-9_-]+["\']>(.*?)</a>',
re.IGNORECASE | re.DOTALL,
)
_INVITE_LINK_PLAIN = re.compile(r'https?://t\.me/(?:\+|joinchat/)[a-zA-Z0-9_-]+', re.IGNORECASE)
_INVITE_LINK_REPLACED = re.compile(r'<a\s+class=["\']tg-link["\']>([^<]*)</a>', re.IGNORECASE)
_INVITE_LINK_REPLACED_URL = re.compile(
r'<a\s+class=["\']tg-link["\']>\s*https?://t\.me/(?:\+|joinchat/)[a-zA-Z0-9_-]+\s*</a>',
re.IGNORECASE,
)
_INVITE_LINK_REPLACED = re.compile(r'<a\s+class=["\']tg-link["\']>(.*?)</a>', re.IGNORECASE | re.DOTALL)
_HTML_TAG = re.compile(r'<[^>]+>')
def _strip_html(text: str) -> str:
return _HTML_TAG.sub('', text).strip()
def replace_invite_link_with_tag(text: str) -> str:
@@ -70,14 +71,20 @@ def replace_invite_link_with_tag(text: str) -> str:
if total > 1:
raise CreativeMultipleInviteLinks()
# Если ссылка уже заменена, нормализуем текст если внутри был URL
# Если ссылка уже заменена, нормализуем текст если внутри был URL или теги
if replaced_count == 1:
return _INVITE_LINK_REPLACED_URL.sub('<a class="tg-link"></a>', text)
def _normalize_replaced(match: re.Match[str]) -> str:
inner = _strip_html(match.group(1))
if inner == "" or _INVITE_LINK_PLAIN.search(inner):
return '<a class="tg-link"></a>'
return f'<a class="tg-link">{inner}</a>'
return _INVITE_LINK_REPLACED.sub(_normalize_replaced, text)
if html_count == 1:
def _replace_html(match: re.Match[str]) -> str:
inner = match.group(1).strip()
inner = _strip_html(match.group(1))
if _INVITE_LINK_PLAIN.fullmatch(inner):
return '<a class="tg-link"></a>'
return f'<a class="tg-link">{inner}</a>'

View File

@@ -27,6 +27,15 @@ class Post(TimestampedModel):
@property
def url(self) -> str | None:
if not self.channel.username:
if self.channel.username:
return f'https://t.me/{self.channel.username}/{self.message_id}'
telegram_id = self.channel.telegram_id
if telegram_id is None:
return None
return f'https://t.me/{self.channel.username}/{self.message_id}'
channel_id = telegram_id
if telegram_id < 0:
channel_id = -telegram_id - 1000000000000
return f'https://t.me/c/{channel_id}/{self.message_id}'