forked from bluesky-social/atproto
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ozone label endpoints (bluesky-social#2043)
* sketching out label sequencer * refactor sequencer * sequencer tests * tests * add query labels endpoint & tests * add pagination * fix label formatting on temp * tidy * format labels * make use listen/notify for sequencer * fix hanging server test * Update packages/ozone/src/api/label/queryLabels.ts Co-authored-by: devin ivy <[email protected]> * pr tidy --------- Co-authored-by: devin ivy <[email protected]>
- Loading branch information
Showing
20 changed files
with
834 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Server } from '../../lexicon' | ||
import AppContext from '../../context' | ||
import { InvalidRequestError } from '@atproto/xrpc-server' | ||
import { sql } from 'kysely' | ||
import { formatLabel } from '../../mod-service/util' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
server.com.atproto.label.queryLabels(async ({ params }) => { | ||
const { uriPatterns, sources, limit, cursor } = params | ||
let builder = ctx.db.db.selectFrom('label').selectAll().limit(limit) | ||
// if includes '*', then we don't need a where clause | ||
if (!uriPatterns.includes('*')) { | ||
builder = builder.where((qb) => { | ||
// starter where clause that is always false so that we can chain `orWhere`s | ||
qb = qb.where(sql`1 = 0`) | ||
for (const pattern of uriPatterns) { | ||
// if no '*', then we're looking for an exact match | ||
if (!pattern.includes('*')) { | ||
qb = qb.orWhere('uri', '=', pattern) | ||
} else { | ||
if (pattern.indexOf('*') < pattern.length - 1) { | ||
throw new InvalidRequestError(`invalid pattern: ${pattern}`) | ||
} | ||
const searchPattern = pattern | ||
.slice(0, -1) | ||
.replaceAll('%', '') // sanitize search pattern | ||
.replaceAll('_', '\\_') // escape any underscores | ||
qb = qb.orWhere('uri', 'like', `${searchPattern}%`) | ||
} | ||
} | ||
return qb | ||
}) | ||
} | ||
if (sources && sources.length > 0) { | ||
builder = builder.where('src', 'in', sources) | ||
} | ||
if (cursor) { | ||
const cursorId = parseInt(cursor, 10) | ||
if (isNaN(cursorId)) { | ||
throw new InvalidRequestError('invalid cursor') | ||
} | ||
builder = builder.where('id', '>', cursorId) | ||
} | ||
|
||
const res = await builder.execute() | ||
|
||
const labels = res.map((l) => formatLabel(l)) | ||
const resCursor = res.at(-1)?.id.toString(10) | ||
|
||
return { | ||
encoding: 'application/json', | ||
body: { | ||
cursor: resCursor, | ||
labels, | ||
}, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Server } from '../../lexicon' | ||
import AppContext from '../../context' | ||
import Outbox from '../../sequencer/outbox' | ||
import { InvalidRequestError } from '@atproto/xrpc-server' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
server.com.atproto.label.subscribeLabels(async function* ({ | ||
params, | ||
signal, | ||
}) { | ||
const { cursor } = params | ||
const outbox = new Outbox(ctx.sequencer) | ||
|
||
if (cursor !== undefined) { | ||
const curr = await ctx.sequencer.curr() | ||
if (cursor > (curr ?? 0)) { | ||
throw new InvalidRequestError('Cursor in the future.', 'FutureCursor') | ||
} | ||
} | ||
|
||
for await (const evt of outbox.events(cursor, signal)) { | ||
yield evt | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.