Add more unfurling

- Fix regex
- Handle trunks.social and Phanpy links too
This commit is contained in:
Lim Chee Aun 2023-11-05 16:13:00 +08:00
parent 7c8d310ed9
commit 83bdc82049
2 changed files with 21 additions and 5 deletions

View file

@ -2158,10 +2158,24 @@ function _unfurlMastodonLink(instance, url) {
let remoteInstanceFetch; let remoteInstanceFetch;
let theURL = url; let theURL = url;
if (/\/\/elk\.[^\/]+\/[^.]+\.[^.]+/i.test(theURL)) {
// E.g. https://elk.zone/domain.com/@stest/123 -> https://domain.com/@stest/123 // https://elk.zone/domain.com/@stest/123 -> https://domain.com/@stest/123
if (/\/\/elk\.[^\/]+\/[^\/]+\.[^\/]+/i.test(theURL)) {
theURL = theURL.replace(/elk\.[^\/]+\//i, ''); theURL = theURL.replace(/elk\.[^\/]+\//i, '');
} }
// https://trunks.social/status/domain.com/@stest/123 -> https://domain.com/@stest/123
if (/\/\/trunks\.[^\/]+\/status\/[^\/]+\.[^\/]+/i.test(theURL)) {
theURL = theURL.replace(/trunks\.[^\/]+\/status\//i, '');
}
// https://phanpy.social/#/domain.com/s/123 -> https://domain.com/statuses/123
if (/\/#\/[^\/]+\.[^\/]+\/s\/.+/i.test(theURL)) {
const urlAfterHash = theURL.split('/#/')[1];
const finalURL = urlAfterHash.replace(/\/s\//i, '/@fakeUsername/');
theURL = `https://${finalURL}`;
}
const urlObj = new URL(theURL); const urlObj = new URL(theURL);
const domain = urlObj.hostname; const domain = urlObj.hostname;
const path = urlObj.pathname; const path = urlObj.pathname;
@ -2189,7 +2203,7 @@ function _unfurlMastodonLink(instance, url) {
const { masto } = api({ instance }); const { masto } = api({ instance });
const mastoSearchFetch = masto.v2.search const mastoSearchFetch = masto.v2.search
.fetch({ .fetch({
q: url, q: theURL,
type: 'statuses', type: 'statuses',
resolve: true, resolve: true,
limit: 1, limit: 1,

View file

@ -1,9 +1,11 @@
export default function isMastodonLinkMaybe(url) { export default function isMastodonLinkMaybe(url) {
const { pathname } = new URL(url); const { pathname, hash } = new URL(url);
return ( return (
/^\/.*\/\d+$/i.test(pathname) || /^\/.*\/\d+$/i.test(pathname) ||
/^\/@[^/]+\/(statuses|posts)\/\w+\/?$/i.test(pathname) || // GoToSocial, Takahe /^\/@[^/]+\/(statuses|posts)\/\w+\/?$/i.test(pathname) || // GoToSocial, Takahe
/^\/notes\/[a-z0-9]+$/i.test(pathname) || // Misskey, Calckey /^\/notes\/[a-z0-9]+$/i.test(pathname) || // Misskey, Calckey
/^\/(notice|objects)\/[a-z0-9-]+$/i.test(pathname) // Pleroma /^\/notes\/[a-z0-9]+$/i.test(pathname) || // Misskey, Calckey
/^\/(notice|objects)\/[a-z0-9-]+$/i.test(pathname) || // Pleroma
/#\/[^\/]+\.[^\/]+\/s\/.+/i.test(hash) // Phanpy 🫣
); );
} }