feat: add POST /notes/feed endpoint for multi-DID filtering
This commit is contained in:
12
server.ts
12
server.ts
@@ -4,6 +4,7 @@ import {
|
||||
deleteWebhooksByDid,
|
||||
getNotes,
|
||||
getNotesByDid,
|
||||
getNotesByDids,
|
||||
} from "./src/data/db.ts";
|
||||
import { log } from "./src/log.ts";
|
||||
|
||||
@@ -28,6 +29,17 @@ router.get("/:did/notes", (ctx) => {
|
||||
ctx.response.body = getNotesByDid(did, cursor, limit);
|
||||
});
|
||||
|
||||
router.post("/notes/feed", async (ctx) => {
|
||||
const body = await ctx.request.body.json();
|
||||
const { dids, cursor, limit } = body ?? {};
|
||||
if (!Array.isArray(dids) || dids.length === 0) {
|
||||
ctx.response.status = 400;
|
||||
ctx.response.body = { error: "dids must be a non-empty array" };
|
||||
return;
|
||||
}
|
||||
ctx.response.body = getNotesByDids(dids, cursor, Number(limit) || PAGINATION);
|
||||
});
|
||||
|
||||
router.post("/:did/webhooks", async (ctx) => {
|
||||
const { did } = ctx.params;
|
||||
const body = await ctx.request.body.json();
|
||||
|
||||
@@ -49,6 +49,23 @@ export const getNotesByDid = (did: string, cursor?: string, limit = 20) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const getNotesByDids = (dids: string[], cursor?: string, limit = 20) => {
|
||||
if (dids.length === 0) return { notes: [] };
|
||||
const placeholders = dids.map(() => "?").join(", ");
|
||||
const notes = cursor
|
||||
? db.prepare(
|
||||
`SELECT did, rkey, title, publishedAt, createdAt, language FROM note WHERE discoverable = 1 AND did IN (${placeholders}) AND rkey < ? ORDER BY rkey DESC LIMIT ?`,
|
||||
).all<NoteRow>(...dids, cursor, limit)
|
||||
: db.prepare(
|
||||
`SELECT did, rkey, title, publishedAt, createdAt, language FROM note WHERE discoverable = 1 AND did IN (${placeholders}) ORDER BY rkey DESC LIMIT ?`,
|
||||
).all<NoteRow>(...dids, limit);
|
||||
|
||||
return {
|
||||
notes,
|
||||
cursor: notes.length === limit ? notes[notes.length - 1].rkey : undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export const deleteNote = ({ did, rkey }: { did: string; rkey: string }) => {
|
||||
db.exec("DELETE FROM note WHERE did = ? AND rkey = ?", did, rkey);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user