[mastodon-client] Generate pagination data in helper function

This also (finally) respects minId sort order.
This commit is contained in:
Laura Hausmann 2023-10-07 20:02:03 +02:00
parent 2899873b26
commit 1cdeff9861
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
5 changed files with 25 additions and 54 deletions

View file

@ -1,6 +1,6 @@
import { ILocalUser, User } from "@/models/entities/user.js"; import { ILocalUser, User } from "@/models/entities/user.js";
import { Blockings, UserListJoinings, UserLists, Users } from "@/models/index.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 { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserList } from "@/models/entities/user-list.js"; import { UserList } from "@/models/entities/user-list.js";
import { pushUserToUserList } from "@/services/user-list/push.js"; import { pushUserToUserList } from "@/services/user-list/push.js";
@ -54,11 +54,7 @@ export class ListHelpers {
return { return {
data: users, data: users,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }

View file

@ -15,7 +15,7 @@ import { genId } from "@/misc/gen-id.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { UserHelpers } from "@/server/api/mastodon/helpers/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 { addPinned, removePinned } from "@/services/i/pin.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
import { convertId, IdType } from "@/misc/convert-id.js"; import { convertId, IdType } from "@/misc/convert-id.js";
@ -170,11 +170,7 @@ export class NoteHelpers {
return { return {
data: users, data: users,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }
@ -248,11 +244,7 @@ export class NoteHelpers {
return { return {
data: users, data: users,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }

View file

@ -18,7 +18,7 @@ import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
import { awaitAll } from "@/prelude/await-all.js"; import { awaitAll } from "@/prelude/await-all.js";
import { unique } from "@/prelude/array.js"; import { unique } from "@/prelude/array.js";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.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 { export class TimelineHelpers {
public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<LinkPaginationObject<Note[]>> { public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<LinkPaginationObject<Note[]>> {
@ -213,11 +213,7 @@ export class TimelineHelpers {
}); });
const res = { const res = {
data: Promise.all(conversations.map(c => awaitAll(c))), data: Promise.all(conversations.map(c => awaitAll(c))),
pagination: { pagination: generatePaginationData(p.map(p => p.threadId ?? p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.threadId ?? p.id).at(-1),
minId: p.map(p => p.threadId ?? p.id)[0],
}
}; };
return awaitAll(res); return awaitAll(res);

View file

@ -40,7 +40,7 @@ import { MediaHelpers } from "@/server/api/mastodon/helpers/media.js";
import { UserProfile } from "@/models/entities/user-profile.js"; import { UserProfile } from "@/models/entities/user-profile.js";
import { verifyLink } from "@/services/fetch-rel-me.js"; import { verifyLink } from "@/services/fetch-rel-me.js";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.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 = { export type AccountCache = {
locks: AsyncLock; locks: AsyncLock;
@ -254,11 +254,7 @@ export class UserHelpers {
return { return {
data: result, data: result,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }
@ -284,11 +280,7 @@ export class UserHelpers {
return { return {
data: users, data: users,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }
@ -314,11 +306,7 @@ export class UserHelpers {
return { return {
data: users, data: users,
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }
@ -402,11 +390,7 @@ export class UserHelpers {
.then(res => { .then(res => {
return { return {
data: res.map(p => p.note as Note), data: res.map(p => p.note as Note),
pagination: { pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: res.map(p => p.id).at(-1),
minId: res.map(p => p.id)[0],
}
}; };
}); });
} }
@ -429,11 +413,7 @@ export class UserHelpers {
.then(res => { .then(res => {
return { return {
data: res.map(p => p.note as Note), data: res.map(p => p.note as Note),
pagination: { pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: res.map(p => p.id).at(-1),
minId: res.map(p => p.id)[0],
}
}; };
}); });
} }
@ -477,11 +457,7 @@ export class UserHelpers {
return { return {
data: p.map(p => type === "followers" ? p.follower : p.followee).filter(p => p) as User[], data: p.map(p => type === "followers" ? p.follower : p.followee).filter(p => p) as User[],
pagination: { pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
limit: limit,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
}
}; };
}); });
} }

View file

@ -1,6 +1,7 @@
import { MastoContext } from "@/server/api/mastodon/index.js"; import { MastoContext } from "@/server/api/mastodon/index.js";
import config from "@/config/index.js"; import config from "@/config/index.js";
import { convertId, IdType } from "@/misc/convert-id.js"; import { convertId, IdType } from "@/misc/convert-id.js";
import { ObjectLiteral } from "typeorm";
type PaginationData = { type PaginationData = {
limit: number; limit: number;
@ -30,4 +31,14 @@ export async function PaginationMiddleware(ctx: MastoContext, next: () => Promis
if (link.length > 0) { if (link.length > 0) {
ctx.response.append('Link', link.join(', ')); 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)
}
} }