[mastodon-client] Use execQueryLinkPagination in more places

This commit is contained in:
Laura Hausmann 2023-10-11 01:31:11 +02:00
parent fef807a165
commit 4717cc85b5
Signed by: zotan
GPG key ID: D044E84C5BE01605
3 changed files with 52 additions and 81 deletions

View file

@ -1,6 +1,5 @@
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 { generatePaginationData } 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";
@ -52,15 +51,12 @@ export class ListHelpers {
.andWhere("member.userListId = :listId", { listId: list.id }) .andWhere("member.userListId = :listId", { listId: list.id })
.innerJoinAndSelect("member.user", "user"); .innerJoinAndSelect("member.user", "user");
return query.take(limit).getMany().then(async p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(members => {
const users = p return members
.map(p => p.user) .map(p => p.user)
.filter(p => p) as User[]; .filter(p => p) as User[];
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
} }
public static async deleteList(list: UserList, ctx: MastoContext) { public static async deleteList(list: UserList, ctx: MastoContext) {

View file

@ -15,7 +15,6 @@ 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 { generatePaginationData } 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 { awaitAll } from "@/prelude/await-all.js"; import { awaitAll } from "@/prelude/await-all.js";
@ -171,15 +170,12 @@ export class NoteHelpers {
.andWhere("reaction.noteId = :noteId", { noteId: note.id }) .andWhere("reaction.noteId = :noteId", { noteId: note.id })
.innerJoinAndSelect("reaction.user", "user"); .innerJoinAndSelect("reaction.user", "user");
return query.take(limit).getMany().then(async p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(reactions => {
const users = p return reactions
.map(p => p.user) .map(p => p.user)
.filter(p => p) as User[]; .filter(p => p) as User[];
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
} }
public static async getNoteEditHistory(note: Note, ctx: MastoContext): Promise<MastodonEntity.StatusEdit[]> { public static async getNoteEditHistory(note: Note, ctx: MastoContext): Promise<MastodonEntity.StatusEdit[]> {
@ -243,15 +239,12 @@ export class NoteHelpers {
generateVisibilityQuery(query, user); generateVisibilityQuery(query, user);
return query.take(limit).getMany().then(async p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(renotes => {
const users = p return renotes
.map(p => p.user) .map(p => p.user)
.filter(p => p) as User[]; .filter(p => p) as User[];
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
} }
public static async getNoteDescendants(note: Note | string, limit: number = 10, depth: number = 2, ctx: MastoContext): Promise<Note[]> { public static async getNoteDescendants(note: Note | string, limit: number = 10, depth: number = 2, ctx: MastoContext): Promise<Note[]> {

View file

@ -40,7 +40,6 @@ 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 { generatePaginationData } from "@/server/api/mastodon/middleware/pagination.js";
import { MastoContext } from "@/server/api/mastodon/index.js"; import { MastoContext } from "@/server/api/mastodon/index.js";
export type AccountCache = { export type AccountCache = {
@ -252,24 +251,21 @@ export class UserHelpers {
query.andWhere("muting.muterId = :userId", { userId: user.id }) query.andWhere("muting.muterId = :userId", { userId: user.id })
.innerJoinAndSelect("muting.mutee", "mutee"); .innerJoinAndSelect("muting.mutee", "mutee");
return query.take(limit).getMany().then(async p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(async mutes => {
const users = p const users = mutes
.map(p => p.mutee) .map(p => p.mutee)
.filter(p => p) as User[]; .filter(p => p) as User[];
const result = await UserConverter.encodeMany(users, ctx) return await UserConverter.encodeMany(users, ctx)
.then(res => res.map(m => { .then(res => res.map(m => {
const muting = p.find(acc => acc.muteeId === m.id); const muting = mutes.find(acc => acc.muteeId === m.id);
return { return {
...m, ...m,
mute_expires_at: muting?.expiresAt?.toISOString() ?? null mute_expires_at: muting?.expiresAt?.toISOString() ?? null
} as MastodonEntity.MutedAccount } as MastodonEntity.MutedAccount
})); }));
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return result;
});
} }
public static async getUserBlocks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> { public static async getUserBlocks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
@ -286,15 +282,12 @@ export class UserHelpers {
query.andWhere("blocking.blockerId = :userId", { userId: user.id }) query.andWhere("blocking.blockerId = :userId", { userId: user.id })
.innerJoinAndSelect("blocking.blockee", "blockee"); .innerJoinAndSelect("blocking.blockee", "blockee");
return query.take(limit).getMany().then(p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(blocks => {
const users = p return blocks
.map(p => p.blockee) .map(p => p.blockee)
.filter(p => p) as User[]; .filter(p => p) as User[];
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
} }
public static async getUserFollowRequests(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> { public static async getUserFollowRequests(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
@ -311,15 +304,12 @@ export class UserHelpers {
query.andWhere("request.followeeId = :userId", { userId: user.id }) query.andWhere("request.followeeId = :userId", { userId: user.id })
.innerJoinAndSelect("request.follower", "follower"); .innerJoinAndSelect("request.follower", "follower");
return query.take(limit).getMany().then(p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(requests => {
const users = p return requests
.map(p => p.follower) .map(p => p.follower)
.filter(p => p) as User[]; .filter(p => p) as User[];
});
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
} }
public static async getUserStatuses(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, excludeReplies: boolean = false, excludeReblogs: boolean = false, pinned: boolean = false, tagged: string | undefined, ctx: MastoContext): Promise<Note[]> { public static async getUserStatuses(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, excludeReplies: boolean = false, excludeReblogs: boolean = false, pinned: boolean = false, tagged: string | undefined, ctx: MastoContext): Promise<Note[]> {
@ -399,11 +389,8 @@ export class UserHelpers {
generateVisibilityQuery(query, localUser); generateVisibilityQuery(query, localUser);
return PaginationHelpers.execQuery(query, limit, minId !== undefined) return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
.then(res => { .then(res => res.map(p => p.note as Note));
ctx.pagination = generatePaginationData(res.map(p => p.id), limit);
return res.map(p => p.note as Note);
});
} }
public static async getUserFavorites(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> { public static async getUserFavorites(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> {
@ -421,11 +408,8 @@ export class UserHelpers {
generateVisibilityQuery(query, localUser); generateVisibilityQuery(query, localUser);
return PaginationHelpers.execQuery(query, limit, minId !== undefined) return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
.then(res => { .then(res => res.map(p => p.note as Note));
ctx.pagination = generatePaginationData(res.map(p => p.id), limit);
return res.map(p => p.note as Note);
});
} }
private static async getUserRelationships(type: RelationshipType, user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> { private static async getUserRelationships(type: RelationshipType, user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
@ -463,12 +447,10 @@ export class UserHelpers {
.innerJoinAndSelect("following.followee", "followee"); .innerJoinAndSelect("following.followee", "followee");
} }
return query.take(limit).getMany().then(p => { return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
if (minId !== undefined) p = p.reverse(); .then(relations => relations
.map(p => type === "followers" ? p.follower : p.followee)
ctx.pagination = generatePaginationData(p.map(p => p.id), limit); .filter(p => p) as User[]);
return p.map(p => type === "followers" ? p.follower : p.followee).filter(p => p) as User[];
});
} }
public static async getUserFollowers(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> { public static async getUserFollowers(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {