mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-28 12:57:31 -07:00
[mastodon-client] Set ctx.pagination in helper funcs
This commit is contained in:
parent
cc96b0ba72
commit
011d7f36c3
20 changed files with 88 additions and 134 deletions
|
@ -56,10 +56,9 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
const query = await UserHelpers.getUserCachedOr404(userId, ctx);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(argsToBools(limitToInt(ctx.query))));
|
||||
const res = await UserHelpers.getUserStatuses(query, args.max_id, args.since_id, args.min_id, args.limit, args['only_media'], args['exclude_replies'], args['exclude_reblogs'], args.pinned, args.tagged, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = tl.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.get<{ Params: { id: string } }>(
|
||||
|
@ -76,10 +75,9 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
const query = await UserHelpers.getUserCachedOr404(userId, ctx);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserFollowers(query, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const followers = await UserConverter.encodeMany(res.data, ctx);
|
||||
const followers = await UserConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = followers.map((account) => convertAccountId(account));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.get<{ Params: { id: string } }>(
|
||||
|
@ -90,10 +88,9 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
const query = await UserHelpers.getUserCachedOr404(userId, ctx);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserFollowing(query, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const following = await UserConverter.encodeMany(res.data, ctx);
|
||||
const following = await UserConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = following.map((account) => convertAccountId(account));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.get<{ Params: { id: string } }>(
|
||||
|
@ -177,9 +174,8 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserBookmarks(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const bookmarks = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const bookmarks = await NoteConverter.encodeMany(res, ctx);
|
||||
ctx.body = bookmarks.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.get("/v1/favourites",
|
||||
|
@ -187,9 +183,8 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserFavorites(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const favorites = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const favorites = await NoteConverter.encodeMany(res, ctx);
|
||||
ctx.body = favorites.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.get("/v1/mutes",
|
||||
|
@ -197,8 +192,7 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserMutes(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
ctx.body = res.data.map(m => convertAccountId(m));
|
||||
ctx.pagination = res.pagination;
|
||||
ctx.body = res.map(m => convertAccountId(m));
|
||||
}
|
||||
);
|
||||
router.get("/v1/blocks",
|
||||
|
@ -206,9 +200,8 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserBlocks(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const blocks = await UserConverter.encodeMany(res.data, ctx);
|
||||
const blocks = await UserConverter.encodeMany(res, ctx);
|
||||
ctx.body = blocks.map(b => convertAccountId(b));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.get("/v1/follow_requests",
|
||||
|
@ -216,9 +209,8 @@ export function setupEndpointsAccount(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await UserHelpers.getUserFollowRequests(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const requests = await UserConverter.encodeMany(res.data, ctx);
|
||||
const requests = await UserConverter.encodeMany(res, ctx);
|
||||
ctx.body = requests.map(b => convertAccountId(b));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.post<{ Params: { id: string } }>(
|
||||
|
|
|
@ -71,10 +71,9 @@ export function setupEndpointsList(router: Router): void {
|
|||
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
||||
const res = await ListHelpers.getListUsers(id, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const accounts = await UserConverter.encodeMany(res.data, ctx);
|
||||
const accounts = await UserConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = accounts.map(account => convertAccountId(account));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.post<{ Params: { id: string } }>(
|
||||
|
|
|
@ -12,10 +12,9 @@ export function setupEndpointsNotifications(router: Router): void {
|
|||
async (ctx) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)), ['types[]', 'exclude_types[]']);
|
||||
const res = await NotificationHelpers.getNotifications(args.max_id, args.since_id, args.min_id, args.limit, args['types[]'], args['exclude_types[]'], args.account_id, ctx);
|
||||
const data = await NotificationConverter.encodeMany(res.data, ctx);
|
||||
const data = await NotificationConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = data.map(n => convertNotificationIds(n));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
@ -117,9 +117,8 @@ export function setupEndpointsStatus(router: Router): void {
|
|||
const note = await NoteHelpers.getNoteOr404(id, ctx);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await NoteHelpers.getNoteRebloggedBy(note, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const users = await UserConverter.encodeMany(res.data, ctx);
|
||||
const users = await UserConverter.encodeMany(res, ctx);
|
||||
ctx.body = users.map(m => convertAccountId(m));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.get<{ Params: { id: string } }>(
|
||||
|
@ -129,10 +128,9 @@ export function setupEndpointsStatus(router: Router): void {
|
|||
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
||||
const note = await NoteHelpers.getNoteOr404(id, ctx);
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query as any)));
|
||||
const res = await NoteHelpers.getNoteFavoritedBy(note, args.max_id, args.since_id, args.min_id, args.limit);
|
||||
const users = await UserConverter.encodeMany(res.data, ctx);
|
||||
const res = await NoteHelpers.getNoteFavoritedBy(note, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const users = await UserConverter.encodeMany(res, ctx);
|
||||
ctx.body = users.map(m => convertAccountId(m));
|
||||
ctx.pagination = res.pagination;
|
||||
}
|
||||
);
|
||||
router.post<{ Params: { id: string } }>(
|
||||
|
|
|
@ -68,10 +68,9 @@ export function setupEndpointsTimeline(router: Router): void {
|
|||
async (ctx, reply) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(argsToBools(limitToInt(ctx.query))));
|
||||
const res = await TimelineHelpers.getPublicTimeline(args.max_id, args.since_id, args.min_id, args.limit, args.only_media, args.local, args.remote, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = tl.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
});
|
||||
router.get<{ Params: { hashtag: string } }>(
|
||||
"/v1/timelines/tag/:hashtag",
|
||||
|
@ -79,11 +78,10 @@ export function setupEndpointsTimeline(router: Router): void {
|
|||
async (ctx, reply) => {
|
||||
const tag = (ctx.params.hashtag ?? '').trim();
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(argsToBools(limitToInt(ctx.query))), ['any[]', 'all[]', 'none[]']);
|
||||
const res = await TimelineHelpers.getTagTimeline(ctx.user, tag, args.max_id, args.since_id, args.min_id, args.limit, args['any[]'] ?? [], args['all[]'] ?? [], args['none[]'] ?? [], args.only_media, args.local, args.remote);
|
||||
const tl = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const res = await TimelineHelpers.getTagTimeline(tag, args.max_id, args.since_id, args.min_id, args.limit, args['any[]'] ?? [], args['all[]'] ?? [], args['none[]'] ?? [], args.only_media, args.local, args.remote, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = tl.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.get("/v1/timelines/home",
|
||||
|
@ -91,10 +89,9 @@ export function setupEndpointsTimeline(router: Router): void {
|
|||
async (ctx, reply) => {
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
||||
const res = await TimelineHelpers.getHomeTimeline(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = tl.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
});
|
||||
router.get<{ Params: { listId: string } }>(
|
||||
"/v1/timelines/list/:listId",
|
||||
|
@ -106,10 +103,9 @@ export function setupEndpointsTimeline(router: Router): void {
|
|||
|
||||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
||||
const res = await TimelineHelpers.getListTimeline(list, args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res.data, ctx);
|
||||
const tl = await NoteConverter.encodeMany(res, ctx);
|
||||
|
||||
ctx.body = tl.map(s => convertStatusIds(s));
|
||||
ctx.pagination = res.pagination;
|
||||
},
|
||||
);
|
||||
router.get("/v1/conversations",
|
||||
|
@ -118,8 +114,7 @@ export function setupEndpointsTimeline(router: Router): void {
|
|||
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
||||
const res = await TimelineHelpers.getConversations(args.max_id, args.since_id, args.min_id, args.limit, ctx);
|
||||
|
||||
ctx.body = res.data.map(c => convertConversationIds(c));
|
||||
ctx.pagination = res.pagination;
|
||||
ctx.body = res.map(c => convertConversationIds(c));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { ILocalUser, User } from "@/models/entities/user.js";
|
||||
import { Blockings, UserListJoinings, UserLists, Users } from "@/models/index.js";
|
||||
import { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.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";
|
||||
|
@ -38,7 +38,7 @@ export class ListHelpers {
|
|||
})
|
||||
}
|
||||
|
||||
public static async getListUsers(id: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getListUsers(id: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
const user = ctx.user as ILocalUser;
|
||||
const list = await UserLists.findOneBy({ userId: user.id, id: id });
|
||||
|
@ -58,10 +58,8 @@ export class ListHelpers {
|
|||
.map(p => p.user)
|
||||
.filter(p => p) as User[];
|
||||
|
||||
return {
|
||||
data: users,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
return users;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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 { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.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 { convertId, IdType } from "@/misc/convert-id.js";
|
||||
|
@ -161,7 +161,7 @@ export class NoteHelpers {
|
|||
return status;
|
||||
}
|
||||
|
||||
public static async getNoteFavoritedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getNoteFavoritedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
const query = PaginationHelpers.makePaginationQuery(
|
||||
NoteReactions.createQueryBuilder("reaction"),
|
||||
|
@ -178,10 +178,8 @@ export class NoteHelpers {
|
|||
.map(p => p.user)
|
||||
.filter(p => p) as User[];
|
||||
|
||||
return {
|
||||
data: users,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
return users;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -231,7 +229,7 @@ export class NoteHelpers {
|
|||
}
|
||||
}
|
||||
|
||||
public static async getNoteRebloggedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getNoteRebloggedBy(note: Note, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
const user = ctx.user as ILocalUser | null;
|
||||
const query = PaginationHelpers.makePaginationQuery(
|
||||
|
@ -252,10 +250,8 @@ export class NoteHelpers {
|
|||
.map(p => p.user)
|
||||
.filter(p => p) as User[];
|
||||
|
||||
return {
|
||||
data: users,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
return users;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,10 @@ import { Notes, Notifications } from "@/models/index.js";
|
|||
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||
import { Notification } from "@/models/entities/notification.js";
|
||||
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
|
||||
import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { MastoContext } from "@/server/api/mastodon/index.js";
|
||||
|
||||
export class NotificationHelpers {
|
||||
public static async getNotifications(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, types: string[] | undefined, excludeTypes: string[] | undefined, accountId: string | undefined, ctx: MastoContext): Promise<LinkPaginationObject<Notification[]>> {
|
||||
public static async getNotifications(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, types: string[] | undefined, excludeTypes: string[] | undefined, accountId: string | undefined, ctx: MastoContext): Promise<Notification[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
@ -34,7 +33,7 @@ export class NotificationHelpers {
|
|||
|
||||
query.leftJoinAndSelect("notification.note", "note");
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getNotification(id: string, ctx: MastoContext): Promise<Notification | null> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { ObjectLiteral, SelectQueryBuilder } from "typeorm";
|
||||
import { LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { MastoContext } from "@/server/api/mastodon/index.js";
|
||||
|
||||
export class PaginationHelpers {
|
||||
public static makePaginationQuery<T extends ObjectLiteral>(
|
||||
|
@ -45,18 +45,16 @@ export class PaginationHelpers {
|
|||
return query.take(limit).getMany().then(found => reverse ? found.reverse() : found);
|
||||
}
|
||||
|
||||
public static async execQueryLinkPagination<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, limit: number, reverse: boolean): Promise<LinkPaginationObject<T[]>> {
|
||||
public static async execQueryLinkPagination<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, limit: number, reverse: boolean, ctx: MastoContext): Promise<T[]> {
|
||||
return this.execQuery(query, limit, reverse)
|
||||
.then(p => {
|
||||
const ids = p.map(x => x.id);
|
||||
return {
|
||||
data: p,
|
||||
pagination: p.length > 0 ? {
|
||||
limit: limit,
|
||||
maxId: ids.at(reverse ? 0 : -1),
|
||||
minId: ids.at(reverse ? -1 : 0)
|
||||
} : undefined
|
||||
}
|
||||
ctx.pagination = p.length > 0 ? {
|
||||
limit: limit,
|
||||
maxId: ids.at(reverse ? 0 : -1),
|
||||
minId: ids.at(reverse ? -1 : 0)
|
||||
} : undefined;
|
||||
return p;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,11 +18,11 @@ 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 { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { generatePaginationData } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { MastoContext } from "@/server/api/mastodon/index.js";
|
||||
|
||||
export class TimelineHelpers {
|
||||
public static async getHomeTimeline(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<LinkPaginationObject<Note[]>> {
|
||||
public static async getHomeTimeline(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
||||
|
@ -54,10 +54,10 @@ export class TimelineHelpers {
|
|||
query.andWhere("note.visibility != 'hidden'");
|
||||
query.andWhere("note.visibility != 'specified'");
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getPublicTimeline(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, local: boolean = false, remote: boolean = false, ctx: MastoContext): Promise<LinkPaginationObject<Note[]>> {
|
||||
public static async getPublicTimeline(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, local: boolean = false, remote: boolean = false, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
||||
|
@ -98,10 +98,10 @@ export class TimelineHelpers {
|
|||
|
||||
if (onlyMedia) query.andWhere("note.fileIds != '{}'");
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getListTimeline(list: UserList, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<LinkPaginationObject<Note[]>> {
|
||||
public static async getListTimeline(list: UserList, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const user = ctx.user as ILocalUser;
|
||||
if (user.id != list.userId) throw new Error("List is not owned by user");
|
||||
|
@ -123,10 +123,10 @@ export class TimelineHelpers {
|
|||
|
||||
generateVisibilityQuery(query, user);
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getTagTimeline(tag: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, any: string[], all: string[], none: string[], onlyMedia: boolean = false, local: boolean = false, remote: boolean = false, ctx: MastoContext): Promise<LinkPaginationObject<Note[]>> {
|
||||
public static async getTagTimeline(tag: string, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, any: string[], all: string[], none: string[], onlyMedia: boolean = false, local: boolean = false, remote: boolean = false, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const user = ctx.user as ILocalUser | null;
|
||||
|
||||
|
@ -165,10 +165,10 @@ export class TimelineHelpers {
|
|||
|
||||
if (onlyMedia) query.andWhere("note.fileIds != '{}'");
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getConversations(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<LinkPaginationObject<MastodonEntity.Conversation[]>> {
|
||||
public static async getConversations(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<MastodonEntity.Conversation[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const user = ctx.user as ILocalUser;
|
||||
const sq = Notes.createQueryBuilder("note")
|
||||
|
@ -216,12 +216,9 @@ export class TimelineHelpers {
|
|||
unread: unread
|
||||
}
|
||||
});
|
||||
const res = {
|
||||
data: Promise.all(conversations.map(c => awaitAll(c))),
|
||||
pagination: generatePaginationData(p.map(p => p.threadId ?? p.id), limit, minId !== undefined)
|
||||
};
|
||||
|
||||
return awaitAll(res);
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.threadId ?? p.id), limit, minId !== undefined);
|
||||
return Promise.all(conversations.map(c => awaitAll(c)));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 { generatePaginationData, LinkPaginationObject } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { generatePaginationData } from "@/server/api/mastodon/middleware/pagination.js";
|
||||
import { MastoContext } from "@/server/api/mastodon/index.js";
|
||||
|
||||
export type AccountCache = {
|
||||
|
@ -238,7 +238,7 @@ export class UserHelpers {
|
|||
});
|
||||
}
|
||||
|
||||
public static async getUserMutes(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<MastodonEntity.MutedAccount[]>> {
|
||||
public static async getUserMutes(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<MastodonEntity.MutedAccount[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
@ -267,14 +267,12 @@ export class UserHelpers {
|
|||
} as MastodonEntity.MutedAccount
|
||||
}));
|
||||
|
||||
return {
|
||||
data: result,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
public static async getUserBlocks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getUserBlocks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
@ -294,14 +292,12 @@ export class UserHelpers {
|
|||
.map(p => p.blockee)
|
||||
.filter(p => p) as User[];
|
||||
|
||||
return {
|
||||
data: users,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
return users;
|
||||
});
|
||||
}
|
||||
|
||||
public static async getUserFollowRequests(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getUserFollowRequests(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
|
||||
const user = ctx.user as ILocalUser;
|
||||
|
@ -321,20 +317,18 @@ export class UserHelpers {
|
|||
.map(p => p.follower)
|
||||
.filter(p => p) as User[];
|
||||
|
||||
return {
|
||||
data: users,
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
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<LinkPaginationObject<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[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
const localUser = ctx.user as ILocalUser | null;
|
||||
|
||||
if (tagged !== undefined) {
|
||||
//FIXME respect tagged
|
||||
return { data: [] };
|
||||
return [];
|
||||
}
|
||||
|
||||
const query = PaginationHelpers.makePaginationQuery(
|
||||
|
@ -387,10 +381,10 @@ export class UserHelpers {
|
|||
|
||||
query.setParameters({ userId: user.id });
|
||||
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined);
|
||||
return PaginationHelpers.execQueryLinkPagination(query, limit, minId !== undefined, ctx);
|
||||
}
|
||||
|
||||
public static async getUserBookmarks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<LinkPaginationObject<Note[]>> {
|
||||
public static async getUserBookmarks(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
|
||||
const localUser = ctx.user as ILocalUser;
|
||||
|
@ -407,14 +401,12 @@ export class UserHelpers {
|
|||
|
||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined)
|
||||
.then(res => {
|
||||
return {
|
||||
data: res.map(p => p.note as Note),
|
||||
pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(res.map(p => p.id), limit, minId !== undefined);
|
||||
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<LinkPaginationObject<Note[]>> {
|
||||
public static async getUserFavorites(maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, ctx: MastoContext): Promise<Note[]> {
|
||||
if (limit > 40) limit = 40;
|
||||
|
||||
const localUser = ctx.user as ILocalUser;
|
||||
|
@ -431,22 +423,20 @@ export class UserHelpers {
|
|||
|
||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined)
|
||||
.then(res => {
|
||||
return {
|
||||
data: res.map(p => p.note as Note),
|
||||
pagination: generatePaginationData(res.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(res.map(p => p.id), limit, minId !== undefined);
|
||||
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<LinkPaginationObject<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[]> {
|
||||
if (limit > 80) limit = 80;
|
||||
|
||||
const localUser = ctx.user as ILocalUser | null;
|
||||
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
||||
if (profile.ffVisibility === "private") {
|
||||
if (!localUser || user.id !== localUser.id) return { data: [] };
|
||||
if (!localUser || user.id !== localUser.id) return [];
|
||||
} else if (profile.ffVisibility === "followers") {
|
||||
if (!localUser) return { data: [] };
|
||||
if (!localUser) return [];
|
||||
if (user.id !== localUser.id) {
|
||||
const isFollowed = await Followings.exist({
|
||||
where: {
|
||||
|
@ -454,7 +444,7 @@ export class UserHelpers {
|
|||
followerId: localUser.id,
|
||||
},
|
||||
});
|
||||
if (!isFollowed) return { data: [] };
|
||||
if (!isFollowed) return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -476,18 +466,16 @@ export class UserHelpers {
|
|||
return query.take(limit).getMany().then(p => {
|
||||
if (minId !== undefined) p = p.reverse();
|
||||
|
||||
return {
|
||||
data: p.map(p => type === "followers" ? p.follower : p.followee).filter(p => p) as User[],
|
||||
pagination: generatePaginationData(p.map(p => p.id), limit, minId !== undefined)
|
||||
};
|
||||
ctx.pagination = generatePaginationData(p.map(p => p.id), limit, minId !== undefined);
|
||||
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<LinkPaginationObject<User[]>> {
|
||||
public static async getUserFollowers(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
return this.getUserRelationships('followers', user, maxId, sinceId, minId, limit, ctx);
|
||||
}
|
||||
|
||||
public static async getUserFollowing(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<LinkPaginationObject<User[]>> {
|
||||
public static async getUserFollowing(user: User, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, ctx: MastoContext): Promise<User[]> {
|
||||
return this.getUserRelationships('following', user, maxId, sinceId, minId, limit, ctx);
|
||||
}
|
||||
|
||||
|
|
|
@ -8,11 +8,6 @@ type PaginationData = {
|
|||
minId?: string | undefined;
|
||||
}
|
||||
|
||||
export type LinkPaginationObject<T> = {
|
||||
data: T;
|
||||
pagination?: PaginationData;
|
||||
}
|
||||
|
||||
export async function PaginationMiddleware(ctx: MastoContext, next: () => Promise<any>) {
|
||||
await next();
|
||||
if (!ctx.pagination) return;
|
||||
|
|
Loading…
Reference in a new issue