[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 { 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)
};
});
}

View File

@ -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)
};
});
}

View File

@ -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<LinkPaginationObject<Note[]>> {
@ -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);

View File

@ -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)
};
});
}

View File

@ -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)
}
}