From e25d0603d62ba57f88caea9b86b0a2c0d951719e Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 18 Sep 2023 22:19:06 -0400 Subject: [PATCH] Revert "fix: pagination" This reverts commit f30295351ad6ab7437b29c39a7d39e9f3e3651f9. --- .../src/server/api/endpoints/clips/notes.ts | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/clips/notes.ts b/packages/backend/src/server/api/endpoints/clips/notes.ts index f1e153ce08..ff5330b9f7 100644 --- a/packages/backend/src/server/api/endpoints/clips/notes.ts +++ b/packages/backend/src/server/api/endpoints/clips/notes.ts @@ -80,6 +80,25 @@ export default define(meta, paramDef, async (ps, user) => { } if (scyllaClient) { + let whereOpt: FindOptionsWhere = { clipId: clip.id }; + if (ps.sinceId && ps.untilId) { + whereOpt = { ...whereOpt, noteId: Between(ps.sinceId, ps.untilId) }; + } else if (ps.sinceId) { + whereOpt = { ...whereOpt, noteId: MoreThan(ps.sinceId) }; + } else if (ps.untilId) { + whereOpt = { ...whereOpt, noteId: LessThan(ps.untilId) }; + } + + const noteIds = await ClipNotes.find({ + where: whereOpt, + order: { id: "DESC" }, + take: ps.limit * 5, + }).then((clips) => clips.map(({ noteId }) => noteId)); + + if (noteIds.length === 0) { + throw new ApiError(meta.errors.noSuchClip); + } + let [ followingUserIds, mutedUserIds, @@ -132,35 +151,11 @@ export default define(meta, paramDef, async (ps, user) => { return filtered; }; - const foundPacked = []; - while (foundPacked.length < ps.limit) { - let whereOpt: FindOptionsWhere = { clipId: clip.id }; - if (ps.sinceId && ps.untilId) { - whereOpt = { ...whereOpt, noteId: Between(ps.sinceId, ps.untilId) }; - } else if (ps.sinceId) { - whereOpt = { ...whereOpt, noteId: MoreThan(ps.sinceId) }; - } else if (ps.untilId) { - whereOpt = { ...whereOpt, noteId: LessThan(ps.untilId) }; - } + const foundNotes = await scyllaClient + .execute(prepared.note.select.byIds, [noteIds], { prepare: true }) + .then((result) => result.rows.map(parseScyllaNote)); - const noteIds = await ClipNotes.find({ - where: whereOpt, - order: { noteId: "DESC" }, - take: ps.limit * 1.5, // Some may be filtered out by Notes.packMany, thus we take more than ps.limit. - }).then((clips) => clips.map(({ noteId }) => noteId)); - - if (noteIds.length === 0) break; - - const foundNotes = (await scyllaClient - .execute(prepared.note.select.byIds, [noteIds], { prepare: true }) - .then((result) => result.rows.map(parseScyllaNote))) - - foundPacked.push(...(await Notes.packMany((await filter(foundNotes)), user))); - if (foundNotes.length < ps.limit) break; - ps.untilId = foundNotes[foundNotes.length - 1].id; - } - - return foundPacked.slice(0, ps.limit); + return Notes.packMany((await filter(foundNotes)).slice(0, ps.limit), user); } const query = makePaginationQuery(