diff --git a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts index e4a38cffb..584a6ce02 100644 --- a/packages/backend/src/server/api/endpoints/notes/global-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/global-timeline.ts @@ -93,13 +93,27 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); - process.nextTick(() => { if (user) { activeUsersChart.read(user); } }); + + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(timeline, user); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts index 3d6103da8..78846861a 100644 --- a/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/hybrid-timeline.ts @@ -151,11 +151,25 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); - process.nextTick(() => { activeUsersChart.read(user); }); - return await Notes.packMany(timeline, user); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } + + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts index 22e5965fc..fead87738 100644 --- a/packages/backend/src/server/api/endpoints/notes/local-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/local-timeline.ts @@ -123,13 +123,27 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); - process.nextTick(() => { if (user) { activeUsersChart.read(user); } }); - return await Notes.packMany(timeline, user); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } + + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/mentions.ts b/packages/backend/src/server/api/endpoints/notes/mentions.ts index 3e5c4f18b..7ffe83e5c 100644 --- a/packages/backend/src/server/api/endpoints/notes/mentions.ts +++ b/packages/backend/src/server/api/endpoints/notes/mentions.ts @@ -86,9 +86,24 @@ export default define(meta, paramDef, async (ps, user) => { query.setParameters(followingQuery.getParameters()); } - const mentions = await query.take(ps.limit).getMany(); - read(user.id, mentions); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(mentions, user); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + read(user.id, found); + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts b/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts index 6dacadec2..c73114de6 100644 --- a/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/recommended-timeline.ts @@ -126,13 +126,27 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); - process.nextTick(() => { if (user) { activeUsersChart.read(user); } }); - return await Notes.packMany(timeline, user); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } + + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/renotes.ts b/packages/backend/src/server/api/endpoints/notes/renotes.ts index b09243e7e..60d398088 100644 --- a/packages/backend/src/server/api/endpoints/notes/renotes.ts +++ b/packages/backend/src/server/api/endpoints/notes/renotes.ts @@ -74,7 +74,21 @@ export default define(meta, paramDef, async (ps, user) => { if (user) generateMutedUserQuery(query, user); if (user) generateBlockedUserQuery(query, user); - const renotes = await query.take(ps.limit).getMany(); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(renotes, user); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/replies.ts b/packages/backend/src/server/api/endpoints/notes/replies.ts index 0a8e90949..94a752a64 100644 --- a/packages/backend/src/server/api/endpoints/notes/replies.ts +++ b/packages/backend/src/server/api/endpoints/notes/replies.ts @@ -58,7 +58,21 @@ export default define(meta, paramDef, async (ps, user) => { if (user) generateMutedUserQuery(query, user); if (user) generateBlockedUserQuery(query, user); - const timeline = await query.take(ps.limit).getMany(); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(timeline, user); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts index d8d0dbbf7..f1cae78ba 100644 --- a/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts +++ b/packages/backend/src/server/api/endpoints/notes/search-by-tag.ts @@ -145,8 +145,21 @@ export default define(meta, paramDef, async (ps, me) => { } } - // Search notes - const notes = await query.take(ps.limit).getMany(); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, me)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(notes, me); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts index 083f41365..e79f6b589 100644 --- a/packages/backend/src/server/api/endpoints/notes/timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts @@ -143,11 +143,25 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); - process.nextTick(() => { activeUsersChart.read(user); }); - return await Notes.packMany(timeline, user); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } + + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; }); 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 c1e535722..1123a33ae 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 @@ -138,9 +138,27 @@ export default define(meta, paramDef, async (ps, user) => { } //#endregion - const timeline = await query.take(ps.limit).getMany(); + process.nextTick(() => { + if (user) { + activeUsersChart.read(user); + } + }); - activeUsersChart.read(user); + // We fetch more than requested because some may be filtered out, and if there's less than + // requested, the pagination stops. + const found = []; + const take = Math.floor(ps.limit * 1.5); + let skip = 0; + while (found.length < ps.limit) { + const notes = await query.take(take).skip(skip).getMany(); + found.push(...await Notes.packMany(notes, user)) + skip += take; + if (notes.length < take) break; + } - return await Notes.packMany(timeline, user); + if (found.length > ps.limit) { + found.length = ps.limit; + } + + return found; });