UNPKG

@atproto/ozone

Version:

Backend service for moderating the Bluesky network.

57 lines 2.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const kysely_1 = require("kysely"); const xrpc_server_1 = require("@atproto/xrpc-server"); function default_1(server, ctx) { 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((0, kysely_1.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 xrpc_server_1.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 xrpc_server_1.InvalidRequestError('invalid cursor'); } builder = builder.where('id', '>', cursorId); } const res = await builder.execute(); const modSrvc = ctx.modService(ctx.db); const labels = await Promise.all(res.map((l) => modSrvc.views.formatLabelAndEnsureSig(l))); const resCursor = res.at(-1)?.id.toString(10); return { encoding: 'application/json', body: { cursor: resCursor, labels, }, }; }); } //# sourceMappingURL=queryLabels.js.map