Browse Source

Support domain.t.me

master
Eduard Kuzmenko 2 years ago committed by r4sas
parent
commit
563f4e0ea3
  1. 18
      src/lib/appManagers/appImManager.ts
  2. 13
      src/lib/appManagers/internalLink.ts
  3. 34
      src/lib/richTextProcessor/wrapUrl.ts

18
src/lib/appManagers/appImManager.ts

@ -546,6 +546,10 @@ export class AppImManager extends EventListenerBase<{
this.addAnchorListener<{pathnameParams: ['addstickers', string]}>({ this.addAnchorListener<{pathnameParams: ['addstickers', string]}>({
name: 'addstickers', name: 'addstickers',
callback: ({pathnameParams}) => { callback: ({pathnameParams}) => {
if(!pathnameParams[1]) {
return;
}
const link: InternalLink = { const link: InternalLink = {
_: INTERNAL_LINK_TYPE.STICKER_SET, _: INTERNAL_LINK_TYPE.STICKER_SET,
set: pathnameParams[1] set: pathnameParams[1]
@ -977,7 +981,8 @@ export class AppImManager extends EventListenerBase<{
private addAnchorListener<Params extends {pathnameParams?: any, uriParams?: any}>(options: { private addAnchorListener<Params extends {pathnameParams?: any, uriParams?: any}>(options: {
name: 'showMaskedAlert' | 'execBotCommand' | 'searchByHashtag' | 'addstickers' | 'im' | name: 'showMaskedAlert' | 'execBotCommand' | 'searchByHashtag' | 'addstickers' | 'im' |
'resolve' | 'privatepost' | 'addstickers' | 'voicechat' | 'joinchat' | 'join' | 'invoice', 'resolve' | 'privatepost' | 'addstickers' | 'voicechat' | 'joinchat' | 'join' | 'invoice' |
'emoji',
protocol?: 'tg', protocol?: 'tg',
callback: (params: Params, element?: HTMLAnchorElement) => boolean | any, callback: (params: Params, element?: HTMLAnchorElement) => boolean | any,
noPathnameParams?: boolean, noPathnameParams?: boolean,
@ -986,11 +991,18 @@ export class AppImManager extends EventListenerBase<{
(window as any)[(options.protocol ? options.protocol + '_' : '') + options.name] = (element?: HTMLAnchorElement/* , e: Event */) => { (window as any)[(options.protocol ? options.protocol + '_' : '') + options.name] = (element?: HTMLAnchorElement/* , e: Event */) => {
cancelEvent(null); cancelEvent(null);
const href = element.href; let href = element.href;
let pathnameParams: any[]; let pathnameParams: any[];
let uriParams: any; let uriParams: any;
if(!options.noPathnameParams) pathnameParams = new URL(element.href).pathname.split('/').slice(1); const u = new URL(href);
const match = u.host.match(/(.+?)\.t(?:elegram)?\.me/);
if(match) {
u.pathname = match[1] + (u.pathname === '/' ? '' : u.pathname);
href = u.toString();
}
if(!options.noPathnameParams) pathnameParams = new URL(href).pathname.split('/').slice(1);
if(!options.noUriParams) uriParams = this.parseUriParams(href); if(!options.noUriParams) uriParams = this.parseUriParams(href);
const res = options.callback({pathnameParams, uriParams} as Params, element); const res = options.callback({pathnameParams, uriParams} as Params, element);

13
src/lib/appManagers/internalLink.ts

@ -11,10 +11,11 @@ export enum INTERNAL_LINK_TYPE {
JOIN_CHAT, JOIN_CHAT,
VOICE_CHAT, VOICE_CHAT,
USER_PHONE_NUMBER, USER_PHONE_NUMBER,
INVOICE INVOICE,
EMOJI_SET
}; };
export type InternalLink = InternalLink.InternalLinkMessage | InternalLink.InternalLinkPrivatePost | InternalLink.InternalLinkStickerSet | InternalLink.InternalLinkJoinChat | InternalLink.InternalLinkVoiceChat | InternalLink.InternalLinkUserPhoneNumber | InternalLink.InternalLinkInvoice; export type InternalLink = InternalLink.InternalLinkMessage | InternalLink.InternalLinkPrivatePost | InternalLink.InternalLinkStickerSet | InternalLink.InternalLinkJoinChat | InternalLink.InternalLinkVoiceChat | InternalLink.InternalLinkUserPhoneNumber | InternalLink.InternalLinkInvoice | InternalLink.InternalLinkEmojiSet;
export namespace InternalLink { export namespace InternalLink {
export interface InternalLinkMessage { export interface InternalLinkMessage {
@ -62,6 +63,11 @@ export namespace InternalLink {
_: INTERNAL_LINK_TYPE.INVOICE, _: INTERNAL_LINK_TYPE.INVOICE,
slug: string slug: string
} }
export interface InternalLinkEmojiSet {
_: INTERNAL_LINK_TYPE.EMOJI_SET,
set: string
}
} }
export type InternalLinkTypeMap = { export type InternalLinkTypeMap = {
@ -71,5 +77,6 @@ export type InternalLinkTypeMap = {
[INTERNAL_LINK_TYPE.JOIN_CHAT]: InternalLink.InternalLinkJoinChat, [INTERNAL_LINK_TYPE.JOIN_CHAT]: InternalLink.InternalLinkJoinChat,
[INTERNAL_LINK_TYPE.VOICE_CHAT]: InternalLink.InternalLinkVoiceChat, [INTERNAL_LINK_TYPE.VOICE_CHAT]: InternalLink.InternalLinkVoiceChat,
[INTERNAL_LINK_TYPE.USER_PHONE_NUMBER]: InternalLink.InternalLinkUserPhoneNumber, [INTERNAL_LINK_TYPE.USER_PHONE_NUMBER]: InternalLink.InternalLinkUserPhoneNumber,
[INTERNAL_LINK_TYPE.INVOICE]: InternalLink.InternalLinkInvoice [INTERNAL_LINK_TYPE.INVOICE]: InternalLink.InternalLinkInvoice,
[INTERNAL_LINK_TYPE.EMOJI_SET]: InternalLink.InternalLinkEmojiSet
}; };

34
src/lib/richTextProcessor/wrapUrl.ts

@ -5,39 +5,42 @@
*/ */
import {PHONE_NUMBER_REG_EXP} from '.'; import {PHONE_NUMBER_REG_EXP} from '.';
import {MOUNT_CLASS_TO} from '../../config/debug';
import matchUrlProtocol from './matchUrlProtocol'; import matchUrlProtocol from './matchUrlProtocol';
export default function wrapUrl(url: string, unsafe?: number | boolean): {url: string, onclick: string} { export default function wrapUrl(url: string, unsafe?: number | boolean) {
if(!matchUrlProtocol(url)) { if(!matchUrlProtocol(url)) {
url = 'https://' + url; url = 'https://' + url;
} }
const out: {url: string, onclick?: string, onclickUrl?: string} = {url};
let tgMeMatch, telescoPeMatch, tgMatch; let tgMeMatch, telescoPeMatch, tgMatch;
let onclick: string; let onclick: string, onclickUrl: string;
/* if(unsafe === 2) { /* if(unsafe === 2) {
url = 'tg://unsafe_url?url=' + encodeURIComponent(url); url = 'tg://unsafe_url?url=' + encodeURIComponent(url);
} else */if((tgMeMatch = url.match(/^(?:https?:\/\/)?t(?:elegram)?\.me\/(.+)/))) { } else */if((tgMeMatch = url.match(/^(?:https?:\/\/)?(?:(.+?)\.)?t(?:elegram)?\.me(?:\/(.+))?/))) {
const fullPath = tgMeMatch[1]; const u = new URL(url);
if(tgMeMatch[1]) {
u.pathname = tgMeMatch[1] + (u.pathname === '/' ? '' : u.pathname);
}
const fullPath = u.pathname.slice(1);
const path = fullPath.split('/'); const path = fullPath.split('/');
if(path[0] && path[0][0] === '$' && path[0].length > 1) { if(path[0] && path[0][0] === '$' && path[0].length > 1) {
onclick = 'invoice'; onclick = 'invoice';
return {url, onclick}; } else if(/^\W/.test(fullPath) && !PHONE_NUMBER_REG_EXP.test(fullPath)) { // second regexp is for phone numbers (t.me/+38050...)
}
// second regexp is for phone numbers (t.me/+38050...)
if(/^\W/.test(fullPath) && !PHONE_NUMBER_REG_EXP.test(fullPath)) {
onclick = 'joinchat'; onclick = 'joinchat';
return {url, onclick}; } else switch(path[0]) {
}
switch(path[0]) {
case 'joinchat': case 'joinchat':
case 'addstickers': case 'addstickers':
case 'addemoji':
case 'voicechat': case 'voicechat':
case 'invoice': case 'invoice':
if(path.length !== 1) {
onclick = path[0]; onclick = path[0];
break; break;
}
default: default:
if((path[1] && path[1].match(/^\d+(?:\?(?:comment|thread)=\d+)?$/)) || path.length === 1) { if((path[1] && path[1].match(/^\d+(?:\?(?:comment|thread)=\d+)?$/)) || path.length === 1) {
@ -59,5 +62,8 @@ export default function wrapUrl(url: string, unsafe?: number | boolean): {url: s
onclick = undefined; onclick = undefined;
} }
return {url, onclick}; out.onclick = onclick;
return out;
} }
MOUNT_CLASS_TO && (MOUNT_CLASS_TO.wrapUrl = wrapUrl);

Loading…
Cancel
Save