[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
No known key found for this signature in database
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 { 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 { UserList } from "@/models/entities/user-list.js";
import { pushUserToUserList } from "@/services/user-list/push.js";
@ -52,14 +51,11 @@ export class ListHelpers {
.andWhere("member.userListId = :listId", { listId: list.id })
.innerJoinAndSelect("member.user", "user");
return query.take(limit).getMany().then(async p => {
if (minId !== undefined) p = p.reverse();
const users = p
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx)
.then(members => {
return members
.map(p => p.user)
.filter(p => p) as User[];
ctx.pagination = generatePaginationData(p.map(p => p.id), limit);
return users;
});
}

View file

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

View file

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