From 1a89181e1d9fa22cf7db3ff63dd93ddddeb865cd Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 12 Sep 2022 11:06:00 +0200 Subject: [PATCH] Support `t.me` subdomains (#2023) --- src/components/left/settings/SettingsEditProfile.tsx | 3 ++- src/components/right/management/ManageInvites.tsx | 4 ++-- src/config.ts | 2 +- src/global/actions/api/chats.ts | 10 +++++++++- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/left/settings/SettingsEditProfile.tsx b/src/components/left/settings/SettingsEditProfile.tsx index 190504f5..2675b288 100644 --- a/src/components/left/settings/SettingsEditProfile.tsx +++ b/src/components/left/settings/SettingsEditProfile.tsx @@ -8,6 +8,7 @@ import { getActions, withGlobal } from '../../../global'; import { ApiMediaFormat } from '../../../api/types'; import { ProfileEditProgress } from '../../../types'; +import { TME_LINK_PREFIX } from '../../../config'; import { throttle } from '../../../util/schedulers'; import { selectUser } from '../../../global/selectors'; import { getChatAvatarHash } from '../../../global/helpers'; @@ -228,7 +229,7 @@ const SettingsEditProfile: FC = ({ {username && (

{lang('lng_username_link')}
- https://t.me/{username} + {TME_LINK_PREFIX}{username}

)} diff --git a/src/components/right/management/ManageInvites.tsx b/src/components/right/management/ManageInvites.tsx index cf1e9cd7..b2dfa8c1 100644 --- a/src/components/right/management/ManageInvites.tsx +++ b/src/components/right/management/ManageInvites.tsx @@ -7,7 +7,7 @@ import { getActions, withGlobal } from '../../../global'; import type { ApiChat, ApiExportedInvite } from '../../../api/types'; import { ManagementScreens } from '../../../types'; -import { STICKER_SIZE_INVITES } from '../../../config'; +import { STICKER_SIZE_INVITES, TME_LINK_PREFIX } from '../../../config'; import { LOCAL_TGS_URLS } from '../../common/helpers/animatedAssets'; import useHistoryBack from '../../../hooks/useHistoryBack'; import useLang from '../../../hooks/useLang'; @@ -100,7 +100,7 @@ const ManageInvites: FC = ({ }, hasDetailedCountdown ? 1000 : undefined); const primaryInvite = exportedInvites?.find(({ isPermanent }) => isPermanent); - const primaryInviteLink = chat?.username ? `t.me/${chat.username}` : primaryInvite?.link; + const primaryInviteLink = chat?.username ? `${TME_LINK_PREFIX}${chat.username}` : primaryInvite?.link; const temporalInvites = useMemo(() => { const invites = chat?.username ? exportedInvites : exportedInvites?.filter(({ isPermanent }) => !isPermanent); return invites?.sort(inviteComparator); diff --git a/src/config.ts b/src/config.ts index f1375c7e..a4db3a87 100644 --- a/src/config.ts +++ b/src/config.ts @@ -194,7 +194,7 @@ export const CONTENT_NOT_SUPPORTED = 'The message is not supported on this versi export const RE_LINK_TEMPLATE = '((ftp|https?):\\/\\/)?((www\\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,63})\\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)'; export const RE_MENTION_TEMPLATE = '(@[\\w\\d_-]+)'; export const RE_TG_LINK = /^tg:(\/\/)?([?=&\d\w_-]+)?/; -export const RE_TME_LINK = /^(https?:\/\/)?t\.me\//; +export const RE_TME_LINK = /^(https?:\/\/)?([-a-zA-Z0-9@:%_+~#=]{1,32}\.)?t\.me/; export const RE_TELEGRAM_LINK = /^(https?:\/\/)?telegram\.org\//; export const TME_LINK_PREFIX = 'https://t.me/'; diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index 44e23a6d..16037852 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -586,7 +586,15 @@ addActionHandler('openTelegramLink', (global, actions, payload) => { } const uri = new URL(url.startsWith('http') ? url : `https://${url}`); - const [part1, part2, part3] = uri.pathname.split('/').filter(Boolean).map((l) => decodeURI(l)); + if (uri.hostname === 't.me' && uri.pathname === '/') { + window.open(uri.toString(), '_blank', 'noopener'); + return; + } + + const hostParts = uri.hostname.split('.'); + if (hostParts.length > 3) return; + const pathname = hostParts.length === 3 ? `${hostParts[0]}/${uri.pathname}` : uri.pathname; + const [part1, part2, part3] = pathname.split('/').filter(Boolean).map((l) => decodeURI(l)); const params = Object.fromEntries(uri.searchParams); let hash: string | undefined;