From b076d61d36a952232a26d9e7b7a9be278d53a549 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 10 Apr 2024 12:47:02 -1000 Subject: [PATCH] Add automatic WebFinger resolution for users that are not found in mentions --- database/entities/Status.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/database/entities/Status.ts b/database/entities/Status.ts index 443e0b23..aa492328 100644 --- a/database/entities/Status.ts +++ b/database/entities/Status.ts @@ -23,7 +23,7 @@ import { applicationToAPI } from "./Application"; import { attachmentToAPI, attachmentToLysand } from "./Attachment"; import { emojiToAPI, emojiToLysand, parseEmojis } from "./Emoji"; import type { UserWithRelations } from "./User"; -import { getUserUri, resolveUser, userToAPI } from "./User"; +import { getUserUri, resolveUser, resolveWebFinger, userToAPI } from "./User"; import { statusAndUserRelations, userRelations } from "./relations"; import { objectToInboxRequest } from "./Federation"; @@ -223,7 +223,7 @@ export const parseTextMentions = async (text: string) => { const mentionedPeople = text.match(/@[a-zA-Z0-9_]+(@[a-zA-Z0-9_.:]+)?/g) ?? []; - return await client.user.findMany({ + const found = await client.user.findMany({ where: { OR: mentionedPeople.map((person) => ({ username: person.split("@")[1], @@ -237,6 +237,29 @@ export const parseTextMentions = async (text: string) => { }, include: userRelations, }); + + const notFound = mentionedPeople.filter( + (person) => + !found.find( + (user) => + user.username === person.split("@")[1] && + user.instance?.base_url === person.split("@")[2], + ), + ); + + // Attempt to resolve mentions that were not found + for (const person of notFound) { + const user = await resolveWebFinger( + person.split("@")[1], + person.split("@")[2], + ); + + if (user) { + found.push(user); + } + } + + return found; }; export const replaceTextMentions = async (