From 3a36a211af55b7832a56321e08ed8e6d62597e6a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 16 Nov 2022 00:51:00 +0100 Subject: [PATCH] Remove tracking from twitter URLs --- api/resolvers/item.js | 3 ++- lib/url.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 302f1441..e33a9bb3 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -1,5 +1,5 @@ import { UserInputError, AuthenticationError } from 'apollo-server-micro' -import { ensureProtocol } from '../../lib/url' +import { ensureProtocol, removeTracking } from '../../lib/url' import serialize from './serial' import { decodeCursor, LIMIT, nextCursorEncoded } from '../../lib/cursor' import { getMetadata, metadataRuleSets } from 'page-metadata-parser' @@ -518,6 +518,7 @@ export default { upsertLink: async (parent, args, { me, models }) => { const { id, ...data } = args data.url = ensureProtocol(data.url) + data.url = removeTracking(data.url) if (id) { return await updateItem(parent, { id, data }, { me, models }) diff --git a/lib/url.js b/lib/url.js index 434ac50d..f889f88f 100644 --- a/lib/url.js +++ b/lib/url.js @@ -4,3 +4,14 @@ export function ensureProtocol (value) { } return value } + +export function removeTracking (value) { + const exprs = [ + // twitter URLs + /^(?https?:\/\/twitter\.com\/(?:#!\/)?(?\w+)\/status(?:es)?\/(?\d+))/, + ] + for (const expr of exprs) { + value = expr.exec(value)?.groups.url ?? value; + } + return value +}