Skip to content

Commit

Permalink
✨ Recursively load queue pages if current page is empty (#185)
Browse files Browse the repository at this point in the history
* ✨ Recursively load queue pages if current page is empty

* 🚨 Add types for function signature

* 🔒 Make sure we don't infinitely call recursive loop
  • Loading branch information
foysalit committed Sep 11, 2024
1 parent 27890f3 commit 03dbb07
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions app/reports/page-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'next/navigation'
import { useInfiniteQuery } from '@tanstack/react-query'
import {
Agent,
AtUri,
ToolsOzoneModerationDefs,
ToolsOzoneModerationEmitEvent,
Expand Down Expand Up @@ -356,29 +357,53 @@ function useModerationQueueQuery() {
}
})

const { data } =
await labelerAgent.api.tools.ozone.moderation.queryStatuses({
limit: 50,
includeMuted: true,
...queryParams,
})

const queueDivider = QUEUE_NAMES.length
const queueIndex = QUEUE_NAMES.indexOf(queueName ?? '')
const statusesInQueue = queueName
? data.subjectStatuses.filter((status) => {
const subjectDid =
status.subject.$type === 'com.atproto.admin.defs#repoRef'
? status.subject.did
: new AtUri(`${status.subject.uri}`).host
const queueDeciderCharCode =
`${subjectDid}`.split(':').pop()?.charCodeAt(0) || 0
return queueDeciderCharCode % queueDivider === queueIndex
})
: data.subjectStatuses

return { cursor: data.cursor, subjectStatuses: statusesInQueue }
return getQueueItems(labelerAgent, queryParams, queueName)
},
getNextPageParam: (lastPage) => lastPage.cursor,
})
}

const getQueueItems = async (
labelerAgent: Agent,
queryParams: ToolsOzoneModerationQueryStatuses.QueryParams,
queueName: string | null,
attempt = 0,
) => {
const pageSize = 50
const { data } = await labelerAgent.tools.ozone.moderation.queryStatuses({
limit: pageSize,
includeMuted: true,
...queryParams,
})

const queueDivider = QUEUE_NAMES.length
const queueIndex = QUEUE_NAMES.indexOf(queueName ?? '')
const statusesInQueue = queueName
? data.subjectStatuses.filter((status) => {
const subjectDid =
status.subject.$type === 'com.atproto.admin.defs#repoRef'
? status.subject.did
: new AtUri(`${status.subject.uri}`).host
const queueDeciderCharCode =
`${subjectDid}`.split(':').pop()?.charCodeAt(0) || 0
return queueDeciderCharCode % queueDivider === queueIndex
})
: data.subjectStatuses

// This is a recursive call to get items in queue if the current page
// gives us less than full page size and there are more items to fetch
// also, use a circuit breaker to make sure we never accidentally call this more than 10 times
if (statusesInQueue.length === 0 && data.cursor && attempt < 10) {
return getQueueItems(
labelerAgent,
{
...queryParams,
cursor: data.cursor,
},
queueName,
++attempt,
)
}

return { cursor: data.cursor, subjectStatuses: statusesInQueue }
}

0 comments on commit 03dbb07

Please sign in to comment.