diff --git a/handlers/middlewares/checkLinks.ts b/handlers/middlewares/checkLinks.ts index 6f9ef72..8f3fc35 100644 --- a/handlers/middlewares/checkLinks.ts +++ b/handlers/middlewares/checkLinks.ts @@ -11,6 +11,7 @@ import type { MessageEntity } from "telegraf/typings/telegram-types"; import { pMap } from "../../utils/promise"; import { telegram } from "../../bot"; import { URL } from "url"; +import XRegExp = require("xregexp"); const { excludeLinks = [], blacklistedDomains = [] } = config; @@ -20,8 +21,12 @@ if (excludeLinks === false) { return; } +const tmeDomains = ["t.me", "telega.one", "telegram.dog", "telegram.me"]; + +const tmeDomainRegex = XRegExp.union(tmeDomains); + const normalizeTme = R.replace( - /^(?:@|(?:https?:\/\/)?(?:t\.me|telegram\.(?:me|dog))\/)(\w+)(\/.+)?/i, + XRegExp.tag("i")`^(?:@|(?:https?:\/\/)?${tmeDomainRegex}\/)(\w+)(\/.+)?`, (_match, username, rest) => /^\/\d+$/.test(rest) ? `https://t.me/${username.toLowerCase()}` @@ -122,10 +127,7 @@ const dh = { }; const domainHandlers = new Map([ - ["t.me", dh.tme], - ["telega.one", dh.tme], - ["telegram.dog", dh.tme], - ["telegram.me", dh.tme], + ...tmeDomains.map((domain) => [domain, dh.tme] as const), ...blacklistedDomains.map( (domain) => [domain, dh.blacklistedDomain] as const ), @@ -175,7 +177,7 @@ const classifyAsync = R.memoize(async (url: URL) => { const classifyList = (urls: URL[]) => pMap(urls, classifyAsync).then(highestPriorityAction); -const matchTmeLinks = R.match(/\b(?:t\.me|telegram\.(?:me|dog))\/[\w-/]+/gi); +const matchTmeLinks = R.match(XRegExp.tag("gi")`\b${tmeDomainRegex}\/[\w-/]+`); const maybeProp = (prop) => (o) => (R.has(prop, o) ? [o[prop]] : []); diff --git a/package-lock.json b/package-lock.json index 7d216fc..9604283 100644 --- a/package-lock.json +++ b/package-lock.json @@ -79,6 +79,12 @@ "integrity": "sha512-xcmtfHIgF9SYjhGdsZR1nQslxG4hu0cIpFfLQ4CWdw3KzHvl7ki1AzFLQUkbDTG42ZN3ZsQfdRzXRlkAvbIy5Q==", "dev": true }, + "@types/xregexp": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/@types/xregexp/-/xregexp-4.3.0.tgz", + "integrity": "sha512-3gJTS9gt27pS7U9q5IVqo4YvKSlkf2ck8ish6etuDj6LIRxkL/2Y8RMUtK/QzvE1Yv2zwWV5yemI2BS0GGGFnA==", + "dev": true + }, "@typescript-eslint/eslint-plugin": { "version": "2.31.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.31.0.tgz", diff --git a/package.json b/package.json index 754053b..6374e9f 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@types/node": "^13.13.2", "@types/node-fetch": "^2.5.7", "@types/ramda": "^0.25.51", + "@types/xregexp": "^4.3.0", "@typescript-eslint/eslint-plugin": "^2.31.0", "@typescript-eslint/parser": "^2.31.0", "eslint": "^6.8.0",