Skip to content

Commit

Permalink
Fix sanitization, improve test to catch (#2218)
Browse files Browse the repository at this point in the history
* Fix sanitization, improve test to catch

* Add changeset
  • Loading branch information
estrattonbailey authored Feb 22, 2024
1 parent 433d47d commit 4353190
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-dogs-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atproto/api': patch
---

Fix mute word upsert logic by ensuring we're comparing sanitized word values
10 changes: 5 additions & 5 deletions packages/api/src/bsky-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,24 +668,24 @@ async function updateMutedWords(

if (mutedWordsPref && AppBskyActorDefs.isMutedWordsPref(mutedWordsPref)) {
if (action === 'upsert' || action === 'update') {
for (const newItem of mutedWords) {
for (const word of mutedWords) {
let foundMatch = false

for (const existingItem of mutedWordsPref.items) {
if (existingItem.value === newItem.value) {
if (existingItem.value === sanitizeMutedWord(word).value) {
existingItem.targets =
action === 'upsert'
? Array.from(
new Set([...existingItem.targets, ...newItem.targets]),
new Set([...existingItem.targets, ...word.targets]),
)
: newItem.targets
: word.targets
foundMatch = true
break
}
}

if (action === 'upsert' && !foundMatch) {
mutedWordsPref.items.push(sanitizeMutedWord(newItem))
mutedWordsPref.items.push(sanitizeMutedWord(word))
}
}
} else if (action === 'remove') {
Expand Down
9 changes: 8 additions & 1 deletion packages/api/tests/bsky-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,10 +1199,17 @@ describe('agent', () => {
})

it('upsertMutedWords with #', async () => {
await agent.upsertMutedWords([
{ value: 'hashtag', targets: ['content'] },
])
await agent.upsertMutedWords([{ value: '#hashtag', targets: ['tag'] }])
const { mutedWords } = await agent.getPreferences()
expect(mutedWords.find((m) => m.value === '#hashtag')).toBeFalsy()
expect(mutedWords.find((m) => m.value === 'hashtag')).toBeTruthy()
expect(mutedWords.find((m) => m.value === 'hashtag')).toStrictEqual({
value: 'hashtag',
targets: ['content', 'tag'],
})
expect(mutedWords.filter((m) => m.value === 'hashtag').length).toBe(1)
})

it('updateMutedWord', async () => {
Expand Down

0 comments on commit 4353190

Please sign in to comment.