diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index d49eec29f7..bffb33c29d 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -406,7 +406,9 @@ export async function execPaginationQuery( } else if (kind === "notification" && userId) { params.push(userId, untilDate, untilDate); } else if (kind === "list" && ps.userIds) { - params.push(ps.userIds.pop() as string, untilDate); + const targetId = ps.userIds.pop(); + if (!targetId) break; + params.push(targetId, untilDate); } else { params.push(untilDate, untilDate); } @@ -415,7 +417,8 @@ export async function execPaginationQuery( params.push(sinceDate); } - params.push(kind === "list" ? ps.limit : queryLimit); + const fetchLimit = kind === "list" ? ps.limit : queryLimit; + params.push(fetchLimit); const result = await scyllaClient.execute(query, params, { prepare: true, @@ -437,7 +440,7 @@ export async function execPaginationQuery( } } - if (result.rowLength < queryLimit) { + if (result.rowLength < fetchLimit) { // Reached the end of partition. Queries posts created one day before. scannedPartitions++; const yesterday = new Date(untilDate.getTime() - 86400000); diff --git a/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts b/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts index ac5910a628..73734a8cf4 100644 --- a/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/user-list-timeline.ts @@ -76,6 +76,16 @@ export default define(meta, paramDef, async (ps, user) => { } if (scyllaClient) { + const userIds = await UserListJoinings.find({ + select: ["userId"], + where: { + userListId: list.id, + }, + }).then((lists) => lists.map(({ userId }) => userId)); + if (userIds.length === 0) { + return await Notes.packMany([]); + } + const followingUserIds = await LocalFollowingsCache.init(user.id).then( (cache) => cache.getAll(), ); @@ -106,11 +116,13 @@ export default define(meta, paramDef, async (ps, user) => { const foundNotes = ( (await execPaginationQuery( "list", - ps, + { ...ps, userIds }, { note: filter }, user.id, )) as ScyllaNote[] - ).slice(0, ps.limit * 1.5); // Some may filtered out by Notes.packMany, thus we take more than ps.limit. + ) + .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()) + .slice(0, ps.limit * 1.5); // Some may filtered out by Notes.packMany, thus we take more than ps.limit. foundPacked.push(...(await Notes.packMany(foundNotes, user))); if (foundNotes.length < ps.limit) break; ps.untilDate = foundNotes[foundNotes.length - 1].createdAt.getTime();