From 7be1e589abaa92534507a99c213a780ae5c5ab82 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sun, 19 May 2024 16:23:12 +0800 Subject: [PATCH] Support Pleroma's /notice unfurl --- src/utils/unfurl-link.jsx | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/utils/unfurl-link.jsx b/src/utils/unfurl-link.jsx index 28ed7a42..f1cd42fa 100644 --- a/src/utils/unfurl-link.jsx +++ b/src/utils/unfurl-link.jsx @@ -9,6 +9,20 @@ export const throttle = pThrottle({ interval: 1000, }); +const STATUS_ID_REGEXES = [ + /\/@[^@\/]+@?[^\/]+?\/(\d+)$/i, // Mastodon + /\/notice\/(\w+)$/i, // Pleroma +]; +function getStatusID(path) { + for (let i = 0; i < STATUS_ID_REGEXES.length; i++) { + const statusMatchID = path.match(STATUS_ID_REGEXES[i])?.[1]; + if (statusMatchID) { + return statusMatchID; + } + } + return null; +} + const denylistDomains = /(twitter|github)\.com/i; const failedUnfurls = {}; function _unfurlMastodonLink(instance, url) { @@ -53,11 +67,11 @@ function _unfurlMastodonLink(instance, url) { } const domain = urlObj.hostname; const path = urlObj.pathname; - // Regex /:username/:id, where username = @username or @username@domain, id = number - const statusRegex = /\/@([^@\/]+)@?([^\/]+)?\/(\d+)$/i; - const statusMatch = statusRegex.exec(path); - if (statusMatch) { - const id = statusMatch[3]; + // Regex /:username/:id, where username = @username or @username@domain, id = post ID + let statusMatchID = getStatusID(path); + + if (statusMatchID) { + const id = statusMatchID; const { masto } = api({ instance: domain }); remoteInstanceFetch = masto.v1.statuses .$select(id)