From 6a79d82568a7c975735d0c2ac9e3ff6d7038a7a7 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Fri, 4 Aug 2023 17:32:57 -0400 Subject: [PATCH] add reply filter --- packages/backend/src/db/scylla.ts | 24 +++++++++++++++++++ .../server/api/endpoints/notes/timeline.ts | 12 ++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 301d28d0e2..af11dff0d3 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -231,3 +231,27 @@ export async function filterChannel( return foundNotes; } + +export async function filterReply( + notes: ScyllaNote[], + withReplies: boolean, + user: { id: User["id"] } | null, +): Promise { + let foundNotes = notes; + + if (!user) { + foundNotes = foundNotes.filter( + (note) => !note.replyId || note.replyUserId === note.userId, + ); + } else if (!withReplies) { + foundNotes = foundNotes.filter( + (note) => + !note.replyId || + note.replyUserId === user.id || + note.userId === user.id || + note.replyUserId === note.userId, + ); + } + + return foundNotes; +} diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts index aad7b03483..a16f834b1b 100644 --- a/packages/backend/src/server/api/endpoints/notes/timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts @@ -17,6 +17,7 @@ import { prepared, scyllaClient, filterChannel, + filterReply, } from "@/db/scylla.js"; import { LocalFollowingsCache } from "@/misc/cache.js"; @@ -76,7 +77,7 @@ export default define(meta, paramDef, async (ps, user) => { if (scyllaClient) { let untilDate = new Date(); - let foundNotes: ScyllaNote[] = []; + const foundNotes: ScyllaNote[] = []; const validIds = [user.id].concat(await followingsCache.getAll()); while (foundNotes.length < ps.limit) { @@ -97,15 +98,16 @@ export default define(meta, paramDef, async (ps, user) => { } const notes = result.rows.map(parseScyllaNote); - const filtered = notes.filter((note) => validIds.includes(note.userId)); + let filtered = notes.filter((note) => validIds.includes(note.userId)); + + filtered = await filterChannel(filtered, user); + filtered = await filterReply(filtered, ps.withReplies, user); + foundNotes.push(...filtered); untilDate = notes[notes.length - 1].createdAt; } - // Filter channels - foundNotes = await filterChannel(foundNotes, user); - return Notes.packMany(foundNotes, user); }