Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add && separator for tags filter param on queryStatuses endpoint #3261

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lexicons/tools/ozone/moderation/queryStatuses.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
"tags": {
"type": "array",
"items": {
"type": "string"
"type": "string",
"maxLength": 25,
"description": "Items in this array are applied with OR filters. To apply AND filter, put all tags in the same string and separate using && characters"
}
},
"excludeTags": {
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12498,6 +12498,9 @@ export const schemaDict = {
type: 'array',
items: {
type: 'string',
maxLength: 25,
description:
'Items in this array are applied with OR filters. To apply AND filter, put all tags in the same string and separate using && characters',
},
},
excludeTags: {
Expand Down
3 changes: 3 additions & 0 deletions packages/ozone/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12498,6 +12498,9 @@ export const schemaDict = {
type: 'array',
items: {
type: 'string',
maxLength: 25,
description:
'Items in this array are applied with OR filters. To apply AND filter, put all tags in the same string and separate using && characters',
},
},
excludeTags: {
Expand Down
40 changes: 34 additions & 6 deletions packages/ozone/src/mod-service/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import net from 'node:net'
import { Insertable, sql } from 'kysely'
import { Insertable, SelectQueryBuilder, sql } from 'kysely'
import { CID } from 'multiformats/cid'
import { AtUri, INVALID_HANDLE } from '@atproto/syntax'
import { InvalidRequestError } from '@atproto/xrpc-server'
Expand Down Expand Up @@ -791,6 +791,38 @@ export class ModerationService {
return result
}

applyTagFilter = (
builder: SelectQueryBuilder<any, any, any>,
tags: string[],
) => {
const { ref } = this.db.db.dynamic
// Build an array of conditions
const conditions = tags.map((tag) => {
if (tag.includes('&&')) {
// Split by '&&' for AND logic
const subTags = tag
.split('&&')
// Make sure spaces on either sides of '&&' are trimmed
.map((subTag) => subTag.trim())
// Remove empty strings after trimming is applied
.filter(Boolean)
return sql`(${sql.join(
subTags.map(
(subTag) =>
sql`${ref('moderation_subject_status.tags')} ? ${subTag}`,
),
sql` AND `,
)})`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ignore empty queries ? Or maybe even throw an error ?

Suggested change
return sql`(${sql.join(
subTags.map(
(subTag) =>
sql`${ref('moderation_subject_status.tags')} ? ${subTag}`,
),
sql` AND `,
)})`
// False is neutral in a union ("OR")
if (!subTags.length) return sql`FALSE`
return sql`(${sql.join(
subTags.map(
(subTag) =>
sql`${ref('moderation_subject_status.tags')} ? ${subTag}`,
),
sql` AND `,
)})`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! handled these edge cases through boolean filters.

} else {
// Single tag condition
return sql`${ref('moderation_subject_status.tags')} ? ${tag}`
}
})

// Combine all conditions with OR
return builder.where(sql`(${sql.join(conditions, sql` OR `)})`)
}

async getSubjectStatuses({
includeAllUserRecords,
cursor,
Expand Down Expand Up @@ -958,11 +990,7 @@ export class ModerationService {
}

if (tags.length) {
builder = builder.where(
sql`${ref('moderation_subject_status.tags')} ?| array[${sql.join(
tags,
)}]::TEXT[]`,
)
builder = this.applyTagFilter(builder, tags)
}

if (excludeTags.length) {
Expand Down
37 changes: 37 additions & 0 deletions packages/ozone/tests/moderation-status-tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,42 @@ describe('moderation-status-tags', () => {
'follow-churn',
)
})

it('allows filtering by tags', async () => {
await modClient.emitEvent({
subject: {
$type: 'com.atproto.admin.defs#repoRef',
did: sc.dids.alice,
},
event: {
$type: 'tools.ozone.moderation.defs#modEventTag',
add: ['report:spam', 'lang:ja', 'lang:en'],
remove: [],
},
})
const [englishAndJapaneseQueue, englishOrJapaneseQueue] =
await Promise.all([
modClient.queryStatuses({
tags: ['lang:ja&&lang:en'],
}),
modClient.queryStatuses({
tags: ['report:ja', 'lang:en'],
}),
])

// Verify that the queue only contains 1 item with both en and ja tags which is alice's account
expect(englishAndJapaneseQueue.subjectStatuses.length).toEqual(1)
expect(englishAndJapaneseQueue.subjectStatuses[0].subject.did).toEqual(
sc.dids.alice,
)

// Verify that when querying for either en or ja tags, both alice and bob are returned
expect(englishOrJapaneseQueue.subjectStatuses.length).toEqual(2)
const englishOrJapaneseDids = englishOrJapaneseQueue.subjectStatuses.map(
({ subject }) => subject.did,
)
expect(englishOrJapaneseDids).toContain(sc.dids.alice)
expect(englishOrJapaneseDids).toContain(sc.dids.bob)
})
})
})
3 changes: 3 additions & 0 deletions packages/pds/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12498,6 +12498,9 @@ export const schemaDict = {
type: 'array',
items: {
type: 'string',
maxLength: 25,
description:
'Items in this array are applied with OR filters. To apply AND filter, put all tags in the same string and separate using && characters',
},
},
excludeTags: {
Expand Down
Loading