Skip to content

Commit

Permalink
fix: filter and cw logic (#1633)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoayco authored Feb 5, 2023
1 parent a1b2da3 commit 0fbe34c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
3 changes: 2 additions & 1 deletion components/conversation/ConversationPaginator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const { paginator } = defineProps<{
}>()
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'),
))
}
Expand Down
16 changes: 10 additions & 6 deletions components/status/StatusContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const filterResult = $computed(() => status.filtered?.length ? status.filtered[0
const filter = $computed(() => filterResult?.filter)
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>

<template>
Expand All @@ -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' : ''" />
<StatusSpoiler :enabled="status.sensitive || isFiltered" :filter="isFiltered" :is-d-m="isDM">
<template v-if="filterPhrase" #spoiler>
<p>{{ `${$t('status.filter_hidden_phrase')}: ${filterPhrase}` }}</p>
</template>
<template v-else-if="status.spoilerText" #spoiler>
<StatusSpoiler :enabled="hasSensitiveSpoilerOrMedia || isFiltered" :filter="isFiltered" :is-d-m="isDM">
<template v-if="status.spoilerText" #spoiler>
<p>{{ status.spoilerText }}</p>
</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' : ''" />
<StatusTranslation :status="status" />
<StatusPoll v-if="status.poll" :status="status" />
Expand Down
2 changes: 1 addition & 1 deletion composables/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
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)

return [...items].filter(isFiltered).filter(isReblogFiltered)
Expand Down
37 changes: 29 additions & 8 deletions tests/unit/reorder-timeline.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
/** @vitest-environment happy-dom */

import type { mastodon } from 'masto'
import { describe, expect, it } from 'vitest'

function status(id: string, filtered?: mastodon.v1.FilterContext): mastodon.v1.Status {
const fakeStatus = {
id,
account: {
id: 'FAKE ID',
} as mastodon.v1.Account,
} as mastodon.v1.Status

if (filtered) {
return {
id,
filtered: [
fakeStatus.filtered
= [
{
filter: {
filterAction: 'hide',
context: [filtered],
},
} as mastodon.v1.FilterResult,
],
} as mastodon.v1.Status
]
}

return { id } as mastodon.v1.Status
return fakeStatus
}

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) {
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')
Expand Down

0 comments on commit 0fbe34c

Please sign in to comment.