diff --git a/packages/backend/src/mfm/from-html.ts b/packages/backend/src/mfm/from-html.ts index 33bbb66f4..9b4346d2c 100644 --- a/packages/backend/src/mfm/from-html.ts +++ b/packages/backend/src/mfm/from-html.ts @@ -1,11 +1,12 @@ import { URL } from "node:url"; import * as parse5 from "parse5"; import { defaultTreeAdapter as treeAdapter } from "parse5"; +import { getSubjectHostFromUriAndUsernameCached } from "@/remote/resolve-user.js"; const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/; const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/; -export async function fromHtml(html: string, hashtagNames?: string[]): Promise { +export async function fromHtml(html: string, hashtagNames?: string[], basicMentionResolve: boolean = false): Promise { // some AP servers like Pixelfed use br tags as well as newlines html = html.replace(/\r?\n/gi, "\n"); @@ -72,7 +73,9 @@ export async function fromHtml(html: string, hashtagNames?: string[]): Promise } } +export async function getSubjectHostFromUriAndUsernameCached(uri: string, username: string): Promise { + const hostname = new URL(uri).hostname; + username = username.substring(1); // remove leading @ from username + + const user = await Users.findOneBy({ + usernameLower: username.toLowerCase(), + host: hostname + }); + + if (user) { + return user.host; + } + + return await getSubjectHostFromUri(uri) ?? hostname; +} + export async function getSubjectHostFromAcct(acct: string): Promise { try { const res = await resolveUserWebFinger(acct.toLowerCase()); diff --git a/packages/backend/test/mfm.ts b/packages/backend/test/mfm.ts index 6b7d5fb1b..5c9250048 100644 --- a/packages/backend/test/mfm.ts +++ b/packages/backend/test/mfm.ts @@ -111,6 +111,8 @@ describe("fromHtml", () => { assert.deepStrictEqual( await fromHtml( '

a @user d

', + undefined, + false ), "a @user@joiniceshrimp.org d", );