fix: filter and cw logic (#1633)

This commit is contained in:
Ayo Ayco 2023-02-05 16:05:42 +01:00 committed by GitHub
parent a1b2da3d5e
commit 0fbe34c1e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 16 deletions

View file

@ -6,7 +6,8 @@ const { paginator } = defineProps<{
}>() }>()
function preprocess(items: mastodon.v1.Conversation[]): mastodon.v1.Conversation[] { function preprocess(items: mastodon.v1.Conversation[]): mastodon.v1.Conversation[] {
return items.filter(items => !items.lastStatus?.filtered?.find( const isAuthored = (conversation: mastodon.v1.Conversation) => conversation.lastStatus ? conversation.lastStatus.account.id === currentUser.value?.account.id : false
return items.filter(item => isAuthored(item) || !item.lastStatus?.filtered?.find(
filter => filter.filter.filterAction === 'hide' && filter.filter.context.includes('thread'), filter => filter.filter.filterAction === 'hide' && filter.filter.context.includes('thread'),
)) ))
} }

View file

@ -15,7 +15,11 @@ const filterResult = $computed(() => status.filtered?.length ? status.filtered[0
const filter = $computed(() => filterResult?.filter) const filter = $computed(() => filterResult?.filter)
const filterPhrase = $computed(() => filter?.title) const filterPhrase = $computed(() => filter?.title)
const isFiltered = $computed(() => filterPhrase && (context && context !== 'details' ? filter?.context.includes(context) : false)) const isFiltered = $computed(() => status.account.id !== currentUser.value?.account.id && filterPhrase && context && context !== 'details' && !!filter?.context.includes(context))
// check spoiler text or media attachment
// needed to handle accounts that mark all their posts as sensitive
const hasSensitiveSpoilerOrMedia = $computed(() => status.sensitive && (!!status.spoilerText || !!status.mediaAttachments.length))
</script> </script>
<template> <template>
@ -27,13 +31,13 @@ const isFiltered = $computed(() => filterPhrase && (context && context !== 'deta
}" }"
> >
<StatusBody v-if="!isFiltered && status.sensitive && !status.spoilerText" :status="status" :newer="newer" :with-action="!isDetails" :class="isDetails ? 'text-xl' : ''" /> <StatusBody v-if="!isFiltered && status.sensitive && !status.spoilerText" :status="status" :newer="newer" :with-action="!isDetails" :class="isDetails ? 'text-xl' : ''" />
<StatusSpoiler :enabled="status.sensitive || isFiltered" :filter="isFiltered" :is-d-m="isDM"> <StatusSpoiler :enabled="hasSensitiveSpoilerOrMedia || isFiltered" :filter="isFiltered" :is-d-m="isDM">
<template v-if="filterPhrase" #spoiler> <template v-if="status.spoilerText" #spoiler>
<p>{{ `${$t('status.filter_hidden_phrase')}: ${filterPhrase}` }}</p>
</template>
<template v-else-if="status.spoilerText" #spoiler>
<p>{{ status.spoilerText }}</p> <p>{{ status.spoilerText }}</p>
</template> </template>
<template v-else-if="filterPhrase" #spoiler>
<p>{{ `${$t('status.filter_hidden_phrase')}: ${filterPhrase}` }}</p>
</template>
<StatusBody v-if="!status.sensitive || status.spoilerText" :status="status" :newer="newer" :with-action="!isDetails" :class="isDetails ? 'text-xl' : ''" /> <StatusBody v-if="!status.sensitive || status.spoilerText" :status="status" :newer="newer" :with-action="!isDetails" :class="isDetails ? 'text-xl' : ''" />
<StatusTranslation :status="status" /> <StatusTranslation :status="status" />
<StatusPoll v-if="status.poll" :status="status" /> <StatusPoll v-if="status.poll" :status="status" />

View file

@ -11,7 +11,7 @@ function areStatusesConsecutive(a: mastodon.v1.Status, b: mastodon.v1.Status) {
function removeFilteredItems(items: mastodon.v1.Status[], context: mastodon.v1.FilterContext): mastodon.v1.Status[] { function removeFilteredItems(items: mastodon.v1.Status[], context: mastodon.v1.FilterContext): mastodon.v1.Status[] {
const isStrict = (filter: mastodon.v1.FilterResult) => filter.filter.filterAction === 'hide' && filter.filter.context.includes(context) const isStrict = (filter: mastodon.v1.FilterResult) => filter.filter.filterAction === 'hide' && filter.filter.context.includes(context)
const isFiltered = (item: mastodon.v1.Status) => !item.filtered?.find(isStrict) const isFiltered = (item: mastodon.v1.Status) => (item.account.id === currentUser.value?.account.id) || !item.filtered?.find(isStrict)
const isReblogFiltered = (item: mastodon.v1.Status) => !item.reblog?.filtered?.find(isStrict) const isReblogFiltered = (item: mastodon.v1.Status) => !item.reblog?.filtered?.find(isStrict)
return [...items].filter(isFiltered).filter(isReblogFiltered) return [...items].filter(isFiltered).filter(isReblogFiltered)

View file

@ -1,28 +1,49 @@
/** @vitest-environment happy-dom */
import type { mastodon } from 'masto' import type { mastodon } from 'masto'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
function status(id: string, filtered?: mastodon.v1.FilterContext): mastodon.v1.Status { function status(id: string, filtered?: mastodon.v1.FilterContext): mastodon.v1.Status {
if (filtered) { const fakeStatus = {
return {
id, id,
filtered: [ account: {
id: 'FAKE ID',
} as mastodon.v1.Account,
} as mastodon.v1.Status
if (filtered) {
fakeStatus.filtered
= [
{ {
filter: { filter: {
filterAction: 'hide', filterAction: 'hide',
context: [filtered], context: [filtered],
}, },
} as mastodon.v1.FilterResult, } as mastodon.v1.FilterResult,
], ]
} as mastodon.v1.Status
} }
return { id } as mastodon.v1.Status return fakeStatus
} }
function reply(id: string, s: mastodon.v1.Status) { function reply(id: string, s: mastodon.v1.Status) {
return { id, inReplyToId: s.id } as mastodon.v1.Status return {
id,
account: {
id: 'FAKE ID',
} as mastodon.v1.Account,
inReplyToId: s.id,
} as mastodon.v1.Status
} }
function reblog(id: string, s: mastodon.v1.Status) { function reblog(id: string, s: mastodon.v1.Status) {
return { id, reblog: s } as mastodon.v1.Status return {
id,
account: {
id: 'FAKE ID',
} as mastodon.v1.Account,
reblog: s,
} as mastodon.v1.Status
} }
const p_a1 = status('p_a1') const p_a1 = status('p_a1')