fix: pagination

This commit is contained in:
Namekuji 2023-09-18 22:08:39 -04:00
parent a5980370de
commit f30295351a
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -80,25 +80,6 @@ export default define(meta, paramDef, async (ps, user) => {
} }
if (scyllaClient) { if (scyllaClient) {
let whereOpt: FindOptionsWhere<ClipNote> = { 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 [ let [
followingUserIds, followingUserIds,
mutedUserIds, mutedUserIds,
@ -151,11 +132,35 @@ export default define(meta, paramDef, async (ps, user) => {
return filtered; return filtered;
}; };
const foundNotes = await scyllaClient const foundPacked = [];
.execute(prepared.note.select.byIds, [noteIds], { prepare: true }) while (foundPacked.length < ps.limit) {
.then((result) => result.rows.map(parseScyllaNote)); let whereOpt: FindOptionsWhere<ClipNote> = { 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) };
}
return Notes.packMany((await filter(foundNotes)).slice(0, ps.limit), user); 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);
} }
const query = makePaginationQuery( const query = makePaginationQuery(