skip resolving preview when a link is known to be recursive

This commit is contained in:
Hazelnoot 2025-05-20 21:37:25 -04:00
parent 38d4a7fd56
commit dc1adcc491
7 changed files with 49 additions and 13 deletions

View file

@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { host } from '@@/js/config.js';
import type * as Misskey from 'misskey-js';
import type * as mfm from '@transfem-org/sfm-js';
import { extractUrlFromMfm } from '@/utility/extract-url-from-mfm.js';
/**
* Extracts all previewable URLs from a note.
*/
export function extractPreviewUrls(note: Misskey.entities.Note, contents: mfm.MfmNode[]): string[] {
const links = extractUrlFromMfm(contents);
return links.filter(url =>
// Remote note
url !== note.url &&
url !== note.uri &&
// Local note
url !== `https://${host}/notes/${note.id}` &&
// Remote renote or quote
url !== note.renote?.url &&
url !== note.renote?.uri &&
// Local renote or quote
url !== `https://${host}/notes/${note.renote?.id}` &&
// Remote renote *of* a quote
url !== note.renote?.renote?.url &&
url !== note.renote?.renote?.uri &&
// Local renote *of* a quote
url !== `https://${host}/notes/${note.renote?.renote?.id}`);
}

View file

@ -10,6 +10,7 @@ import { unique } from '@/utility/array.js';
// [ http://a/#1, http://a/#2, http://b/#3 ] => [ http://a/#1, http://b/#3 ]
const removeHash = (x: string) => x.replace(/#[^#]*$/, '');
// TODO this is O(n^2) which could introduce a frontend DoS with a large enough character limit
export function extractUrlFromMfm(nodes: mfm.MfmNode[], respectSilentFlag = true): string[] {
const urlNodes = mfm.extract(nodes, (node) => {
return (node.type === 'url') || (node.type === 'link' && (!respectSilentFlag || !node.props.silent));