diff --git a/packages/backend/src/server/api/mastodon/helpers/list.ts b/packages/backend/src/server/api/mastodon/helpers/list.ts index 30d17f805..ba2f64f5c 100644 --- a/packages/backend/src/server/api/mastodon/helpers/list.ts +++ b/packages/backend/src/server/api/mastodon/helpers/list.ts @@ -1,6 +1,6 @@ import { ILocalUser, User } from "@/models/entities/user.js"; import { Blockings, UserListJoinings, UserLists, Users } from "@/models/index.js"; -import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; +import { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { UserList } from "@/models/entities/user-list.js"; import { pushUserToUserList } from "@/services/user-list/push.js"; @@ -54,11 +54,7 @@ export class ListHelpers { return { data: users, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } diff --git a/packages/backend/src/server/api/mastodon/helpers/note.ts b/packages/backend/src/server/api/mastodon/helpers/note.ts index 6ceac6817..640978f25 100644 --- a/packages/backend/src/server/api/mastodon/helpers/note.ts +++ b/packages/backend/src/server/api/mastodon/helpers/note.ts @@ -15,7 +15,7 @@ import { genId } from "@/misc/gen-id.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; -import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js" +import { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js" import { addPinned, removePinned } from "@/services/i/pin.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { convertId, IdType } from "@/misc/convert-id.js"; @@ -170,11 +170,7 @@ export class NoteHelpers { return { data: users, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } @@ -248,11 +244,7 @@ export class NoteHelpers { return { data: users, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } diff --git a/packages/backend/src/server/api/mastodon/helpers/timeline.ts b/packages/backend/src/server/api/mastodon/helpers/timeline.ts index b664cb9ec..69aa55c4a 100644 --- a/packages/backend/src/server/api/mastodon/helpers/timeline.ts +++ b/packages/backend/src/server/api/mastodon/helpers/timeline.ts @@ -18,7 +18,7 @@ import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { awaitAll } from "@/prelude/await-all.js"; import { unique } from "@/prelude/array.js"; import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; -import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; +import { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; export class TimelineHelpers { public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise> { @@ -213,11 +213,7 @@ export class TimelineHelpers { }); const res = { data: Promise.all(conversations.map(c => awaitAll(c))), - pagination: { - limit: limit, - maxId: p.map(p => p.threadId ?? p.id).at(-1), - minId: p.map(p => p.threadId ?? p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.threadId ?? p.id), limit, minId !== undefined) }; return awaitAll(res); diff --git a/packages/backend/src/server/api/mastodon/helpers/user.ts b/packages/backend/src/server/api/mastodon/helpers/user.ts index d79a2d37c..2e56dd8dd 100644 --- a/packages/backend/src/server/api/mastodon/helpers/user.ts +++ b/packages/backend/src/server/api/mastodon/helpers/user.ts @@ -40,7 +40,7 @@ import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js"; import { UserProfile } from "@/models/entities/user-profile.js"; import { verifyLink } from "@/services/fetch-rel-me.js"; import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; -import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; +import { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js"; export type AccountCache = { locks: AsyncLock; @@ -254,11 +254,7 @@ export class UserHelpers { return { data: result, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } @@ -284,11 +280,7 @@ export class UserHelpers { return { data: users, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } @@ -314,11 +306,7 @@ export class UserHelpers { return { data: users, - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } @@ -402,11 +390,7 @@ export class UserHelpers { .then(res => { return { data: res.map(p => p.note as Note), - pagination: { - limit: limit, - maxId: res.map(p => p.id).at(-1), - minId: res.map(p => p.id)[0], - } + pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined) }; }); } @@ -429,11 +413,7 @@ export class UserHelpers { .then(res => { return { data: res.map(p => p.note as Note), - pagination: { - limit: limit, - maxId: res.map(p => p.id).at(-1), - minId: res.map(p => p.id)[0], - } + pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined) }; }); } @@ -477,11 +457,7 @@ export class UserHelpers { return { data: p.map(p => type === "followers" ? p.follower : p.followee).filter(p => p) as User[], - pagination: { - limit: limit, - maxId: p.map(p => p.id).at(-1), - minId: p.map(p => p.id)[0], - } + pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined) }; }); } diff --git a/packages/backend/src/server/api/mastodon/middleware/pagination.ts b/packages/backend/src/server/api/mastodon/middleware/pagination.ts index 7ae8e3c05..e5140baa5 100644 --- a/packages/backend/src/server/api/mastodon/middleware/pagination.ts +++ b/packages/backend/src/server/api/mastodon/middleware/pagination.ts @@ -1,6 +1,7 @@ import { MastoContext } from "@/server/api/mastodon/index.js"; import config from "@/config/index.js"; import { convertId, IdType } from "@/misc/convert-id.js"; +import { ObjectLiteral } from "typeorm"; type PaginationData = { limit: number; @@ -30,4 +31,14 @@ export async function PaginationMiddleware(ctx: MastoContext, next: () => Promis if (link.length > 0) { ctx.response.append('Link', link.join(', ')); } +} + +export function generatePaginationData(ids: string[], limit: number, reverse: boolean): PaginationData | undefined { + if (ids.length < 1) return undefined; + + return { + limit: limit, + maxId: ids.at(reverse ? 0 : -1), + minId: ids.at(reverse ? -1 : 0) + } } \ No newline at end of file