Make mentions appear in new posts

This commit is contained in:
Jesse Wierzbinski 2024-04-08 22:54:39 -10:00
parent b5f31fc4e4
commit 9476770cdb
No known key found for this signature in database

View file

@ -206,7 +206,7 @@ export const createNewStatus = async (data: {
emojis?: Emoji[]; emojis?: Emoji[];
content_type?: string; content_type?: string;
uri?: string; uri?: string;
mentions?: User[]; mentions?: UserWithRelations[];
media_attachments?: string[]; media_attachments?: string[];
reply?: { reply?: {
status: Status; status: Status;
@ -260,6 +260,34 @@ export const createNewStatus = async (data: {
.join("\n"); .join("\n");
} }
// Turn each @username or @username@instance mention into an anchor link
for (const mention of mentions) {
const matches = data.content.match(
new RegExp(
`@${mention.username}(@${mention.instance?.base_url})?`,
"g",
),
);
if (!matches) continue;
for (const match of matches) {
formattedContent = formattedContent.replace(
new RegExp(
`@${mention.username}(@${mention.instance?.base_url})?`,
"g",
),
`<a class="u-url mention" rel="nofollow noopener noreferrer" target="_blank" href="${
mention.uri ||
new URL(
`/@${mention.username}`,
config.http.base_url,
).toString()
}">${match}</a>`,
);
}
}
const status = await client.status.create({ const status = await client.status.create({
data: { data: {
authorId: data.account.id, authorId: data.account.id,