fix: dedupe accounts in reply mentions

zio/stable
Anthony Fu 2023-01-13 01:19:24 +01:00
parent 36ae8be40a
commit 9476d14d6c
1 changed files with 10 additions and 8 deletions

View File

@ -15,7 +15,6 @@ export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatu
const { const {
attachments = [], attachments = [],
initialText = '', initialText = '',
status, status,
inReplyToId, inReplyToId,
visibility, visibility,
@ -51,17 +50,20 @@ export async function getDraftFromStatus(status: mastodon.v1.Status): Promise<Dr
}) })
} }
function mentionHTML(acct: string) { function toMentionsHTML(accounts: string[]) {
return `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>` return accounts.map(acct => `<span data-type="mention" data-id="${acct}" contenteditable="false">@${acct}</span>`).join(' ')
} }
function getAccountsToMention(status: mastodon.v1.Status) { function getAccountsToMention(status: mastodon.v1.Status) {
const userId = currentUser.value?.account.id const userId = currentUser.value?.account.id
const accountsToMention: string[] = [] const accountsToMention = new Set<string>()
if (status.account.id !== userId) if (status.account.id !== userId)
accountsToMention.push(status.account.acct) accountsToMention.add(status.account.acct)
accountsToMention.push(...(status.mentions.filter(mention => mention.id !== userId).map(mention => mention.acct))) status.mentions
return accountsToMention .filter(mention => mention.id !== userId)
.map(mention => mention.acct)
.forEach(i => accountsToMention.add(i))
return Array.from(accountsToMention)
} }
export function getReplyDraft(status: mastodon.v1.Status) { export function getReplyDraft(status: mastodon.v1.Status) {
@ -70,7 +72,7 @@ export function getReplyDraft(status: mastodon.v1.Status) {
key: `reply-${status.id}`, key: `reply-${status.id}`,
draft: () => { draft: () => {
return getDefaultDraft({ return getDefaultDraft({
initialText: accountsToMention.map(acct => mentionHTML(acct)).join(' '), initialText: toMentionsHTML(accountsToMention),
inReplyToId: status!.id, inReplyToId: status!.id,
visibility: status.visibility, visibility: status.visibility,
}) })