add reply filter

This commit is contained in:
Namekuji 2023-08-04 17:32:57 -04:00
parent 66309cc793
commit 6a79d82568
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
2 changed files with 31 additions and 5 deletions

View file

@ -231,3 +231,27 @@ export async function filterChannel(
return foundNotes;
}
export async function filterReply(
notes: ScyllaNote[],
withReplies: boolean,
user: { id: User["id"] } | null,
): Promise<ScyllaNote[]> {
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;
}

View file

@ -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);
}