fix: properly hide redundant mentions

zio/stable
patak 2023-01-18 19:23:14 +01:00
parent 3132f4fdea
commit 5785047856
1 changed files with 12 additions and 9 deletions

View File

@ -458,7 +458,7 @@ function isSpacing(node: Node) {
// Extract the username from a known mention node // Extract the username from a known mention node
function getMentionHandle(node: Node): string | undefined { function getMentionHandle(node: Node): string | undefined {
return hrefToHandle(node.children?.[0].attributes.href) // node.children?.[0]?.children?.[0]?.attributes?.['data-id'] return hrefToHandle(node.children?.[0].attributes.href) ?? node.children?.[0]?.children?.[0]?.attributes?.['data-id']
} }
function transformCollapseMentions(status?: mastodon.v1.Status, inReplyToStatus?: mastodon.v1.Status): Transform { function transformCollapseMentions(status?: mastodon.v1.Status, inReplyToStatus?: mastodon.v1.Status): Transform {
@ -492,6 +492,8 @@ function transformCollapseMentions(status?: mastodon.v1.Status, inReplyToStatus?
if (mentions.length === 0) if (mentions.length === 0)
return node return node
let mentionsCount = 0
let contextualMentionsCount = 0
let removeNextSpacing = false let removeNextSpacing = false
const contextualMentions = mentions.filter((mention) => { const contextualMentions = mentions.filter((mention) => {
if (!mention) if (!mention)
@ -502,22 +504,23 @@ function transformCollapseMentions(status?: mastodon.v1.Status, inReplyToStatus?
return false return false
} }
if (isMention(mention) && inReplyToStatus) { if (isMention(mention)) {
const mentionHandle = getMentionHandle(mention) mentionsCount++
if (inReplyToStatus.account.acct === mentionHandle || inReplyToStatus.mentions.some(m => m.acct === mentionHandle)) if (inReplyToStatus) {
return false const mentionHandle = getMentionHandle(mention)
if (inReplyToStatus.account.acct === mentionHandle || inReplyToStatus.mentions.some(m => m.acct === mentionHandle))
return false
}
contextualMentionsCount++
} }
return true return true
}) })
const mentionsCount = contextualMentions.filter(m => m && isMention(m)).length
// We have a special case for single mentions that are part of a reply. // We have a special case for single mentions that are part of a reply.
// We already have the replying to badge in this case or the status is connected to the previous one. // We already have the replying to badge in this case or the status is connected to the previous one.
// This is needed because the status doesn't included the in Reply to handle, only the account id. // This is needed because the status doesn't included the in Reply to handle, only the account id.
// But this covers the majority of cases. // But this covers the majority of cases.
const showMentions = !(mentionsCount === 0 || (mentionsCount === 1 && status?.inReplyToAccountId)) const showMentions = !(contextualMentionsCount === 0 || (mentionsCount === 1 && status?.inReplyToAccountId))
const contextualChildren = children.slice(mentions.length) const contextualChildren = children.slice(mentions.length)
return { return {