diff --git a/src/pds.js b/src/pds.js index c02b94e..3d35ebc 100644 --- a/src/pds.js +++ b/src/pds.js @@ -888,6 +888,9 @@ const pdsRoutes = { '/xrpc/com.atproto.repo.describeRepo': { handler: (pds, req, url) => pds.handleDescribeRepo() }, + '/xrpc/com.atproto.repo.listRecords': { + handler: (pds, req, url) => pds.handleListRecords(url) + }, '/xrpc/com.atproto.sync.getLatestCommit': { handler: (pds, req, url) => pds.handleGetLatestCommit() }, @@ -1318,6 +1321,33 @@ export class PersonalDataServer { }) } + async handleListRecords(url) { + const collection = url.searchParams.get('collection') + if (!collection) { + return Response.json({ error: 'InvalidRequest', message: 'missing collection' }, { status: 400 }) + } + const limit = Math.min(parseInt(url.searchParams.get('limit') || '50'), 100) + const reverse = url.searchParams.get('reverse') === 'true' + const cursor = url.searchParams.get('cursor') + + const did = await this.getDid() + let query = `SELECT uri, cid, value FROM records WHERE collection = ? ORDER BY rkey ${reverse ? 'DESC' : 'ASC'} LIMIT ?` + const params = [collection, limit + 1] + + const rows = this.sql.exec(query, ...params).toArray() + const hasMore = rows.length > limit + const records = rows.slice(0, limit).map(r => ({ + uri: r.uri, + cid: r.cid, + value: cborDecode(new Uint8Array(r.value)) + })) + + return Response.json({ + records, + cursor: hasMore ? records[records.length - 1]?.uri : undefined + }) + } + handleGetLatestCommit() { const commits = this.sql.exec( `SELECT cid, rev FROM commits ORDER BY seq DESC LIMIT 1` @@ -1465,8 +1495,9 @@ async function handleRequest(request, env) { return Response.json({ repos, cursor: undefined }) } - // describeRepo uses ?repo= param instead of ?did= - if (url.pathname === '/xrpc/com.atproto.repo.describeRepo') { + // Repo endpoints use ?repo= param instead of ?did= + if (url.pathname === '/xrpc/com.atproto.repo.describeRepo' || + url.pathname === '/xrpc/com.atproto.repo.listRecords') { const repo = url.searchParams.get('repo') if (!repo) { return Response.json({ error: 'InvalidRequest', message: 'missing repo param' }, { status: 400 })