[mastodon-client] Proper pagination for /bookmarks & /favorites
This commit is contained in:
parent
d3a88ff613
commit
7da7b6e09b
3 changed files with 34 additions and 48 deletions
|
|
@ -198,7 +198,7 @@ export function apiAccountMastodon(router: Router): void {
|
||||||
const followers = await UserConverter.encodeMany(res.data, cache);
|
const followers = await UserConverter.encodeMany(res.data, cache);
|
||||||
|
|
||||||
ctx.body = followers.map((account) => convertAccount(account));
|
ctx.body = followers.map((account) => convertAccount(account));
|
||||||
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `accounts/${ctx.params.id}/followers`);
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `v1/accounts/${ctx.params.id}/followers`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
@ -223,7 +223,7 @@ export function apiAccountMastodon(router: Router): void {
|
||||||
const following = await UserConverter.encodeMany(res.data, cache);
|
const following = await UserConverter.encodeMany(res.data, cache);
|
||||||
|
|
||||||
ctx.body = following.map((account) => convertAccount(account));
|
ctx.body = following.map((account) => convertAccount(account));
|
||||||
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `accounts/${ctx.params.id}/following`);
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `v1/accounts/${ctx.params.id}/following`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
@ -410,10 +410,11 @@ export function apiAccountMastodon(router: Router): void {
|
||||||
|
|
||||||
const cache = UserHelpers.getFreshAccountCache();
|
const cache = UserHelpers.getFreshAccountCache();
|
||||||
const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any)));
|
const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any)));
|
||||||
const bookmarks = await UserHelpers.getUserBookmarks(user, args.max_id, args.since_id, args.min_id, args.limit)
|
const res = await UserHelpers.getUserBookmarks(user, args.max_id, args.since_id, args.min_id, args.limit);
|
||||||
.then(n => NoteConverter.encodeMany(n, user, cache));
|
const bookmarks = await NoteConverter.encodeMany(res.data, user, cache);
|
||||||
|
|
||||||
ctx.body = bookmarks.map(s => convertStatus(s));
|
ctx.body = bookmarks.map(s => convertStatus(s));
|
||||||
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `v1/bookmarks`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
@ -433,10 +434,11 @@ export function apiAccountMastodon(router: Router): void {
|
||||||
|
|
||||||
const cache = UserHelpers.getFreshAccountCache();
|
const cache = UserHelpers.getFreshAccountCache();
|
||||||
const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any)));
|
const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any)));
|
||||||
const favorites = await UserHelpers.getUserFavorites(user, args.max_id, args.since_id, args.min_id, args.limit)
|
const res = await UserHelpers.getUserFavorites(user, args.max_id, args.since_id, args.min_id, args.limit);
|
||||||
.then(n => NoteConverter.encodeMany(n, user, cache));
|
const favorites = await NoteConverter.encodeMany(res.data, user, cache);
|
||||||
|
|
||||||
ctx.body = favorites.map(s => convertStatus(s));
|
ctx.body = favorites.map(s => convertStatus(s));
|
||||||
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res, `v1/favourites`);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
|
||||||
|
|
@ -69,11 +69,11 @@ export class PaginationHelpers {
|
||||||
const link: string[] = [];
|
const link: string[] = [];
|
||||||
const limit = args.limit ?? 40;
|
const limit = args.limit ?? 40;
|
||||||
if (res.maxId) {
|
if (res.maxId) {
|
||||||
const l = `<${config.url}/api/v1/${route}?limit=${limit}&max_id=${convertId(res.maxId, IdType.MastodonId)}>; rel="next"`;
|
const l = `<${config.url}/api/${route}?limit=${limit}&max_id=${convertId(res.maxId, IdType.MastodonId)}>; rel="next"`;
|
||||||
link.push(l);
|
link.push(l);
|
||||||
}
|
}
|
||||||
if (res.minId) {
|
if (res.minId) {
|
||||||
const l = `<${config.url}/api/v1/${route}?limit=${limit}&min_id=${convertId(res.minId, IdType.MastodonId)}>; rel="prev"`;
|
const l = `<${config.url}/api/${route}?limit=${limit}&min_id=${convertId(res.minId, IdType.MastodonId)}>; rel="prev"`;
|
||||||
link.push(l);
|
link.push(l);
|
||||||
}
|
}
|
||||||
if (link.length > 0){
|
if (link.length > 0){
|
||||||
|
|
|
||||||
|
|
@ -78,68 +78,52 @@ export class UserHelpers {
|
||||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getUserBookmarks(localUser: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> {
|
public static async getUserBookmarks(localUser: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<LinkPaginationObject<Note[]>> {
|
||||||
if (limit > 40) limit = 40;
|
if (limit > 40) limit = 40;
|
||||||
|
|
||||||
const bookmarkQuery = NoteFavorites.createQueryBuilder("favorite")
|
|
||||||
.select("favorite.noteId")
|
|
||||||
.where("favorite.userId = :meId");
|
|
||||||
|
|
||||||
const query = PaginationHelpers.makePaginationQuery(
|
const query = PaginationHelpers.makePaginationQuery(
|
||||||
Notes.createQueryBuilder("note"),
|
NoteFavorites.createQueryBuilder("favorite"),
|
||||||
sinceId,
|
sinceId,
|
||||||
maxId,
|
maxId,
|
||||||
minId
|
minId
|
||||||
)
|
)
|
||||||
.andWhere(`note.id IN (${bookmarkQuery.getQuery()})`)
|
.andWhere("favorite.userId = :meId", { meId: localUser.id })
|
||||||
.innerJoinAndSelect("note.user", "user")
|
.leftJoinAndSelect("favorite.note", "note");
|
||||||
.leftJoinAndSelect("user.avatar", "avatar")
|
|
||||||
.leftJoinAndSelect("user.banner", "banner")
|
|
||||||
.leftJoinAndSelect("note.reply", "reply")
|
|
||||||
.leftJoinAndSelect("note.renote", "renote")
|
|
||||||
.leftJoinAndSelect("reply.user", "replyUser")
|
|
||||||
.leftJoinAndSelect("replyUser.avatar", "replyUserAvatar")
|
|
||||||
.leftJoinAndSelect("replyUser.banner", "replyUserBanner")
|
|
||||||
.leftJoinAndSelect("renote.user", "renoteUser")
|
|
||||||
.leftJoinAndSelect("renoteUser.avatar", "renoteUserAvatar")
|
|
||||||
.leftJoinAndSelect("renoteUser.banner", "renoteUserBanner");
|
|
||||||
|
|
||||||
generateVisibilityQuery(query, localUser);
|
generateVisibilityQuery(query, localUser);
|
||||||
|
|
||||||
query.setParameters({ meId: localUser.id });
|
return PaginationHelpers.execQuery(query, limit, minId !== undefined)
|
||||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
.then(res => {
|
||||||
|
return {
|
||||||
|
data: res.map(p => p.note as Note),
|
||||||
|
maxId: res.map(p => p.id).at(-1),
|
||||||
|
minId: res.map(p => p.id)[0],
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async getUserFavorites(localUser: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> {
|
public static async getUserFavorites(localUser: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<LinkPaginationObject<Note[]>> {
|
||||||
if (limit > 40) limit = 40;
|
if (limit > 40) limit = 40;
|
||||||
|
|
||||||
const favoriteQuery = NoteReactions.createQueryBuilder("reaction")
|
|
||||||
.select("reaction.noteId")
|
|
||||||
.where("reaction.userId = :meId");
|
|
||||||
|
|
||||||
const query = PaginationHelpers.makePaginationQuery(
|
const query = PaginationHelpers.makePaginationQuery(
|
||||||
Notes.createQueryBuilder("note"),
|
NoteReactions.createQueryBuilder("reaction"),
|
||||||
sinceId,
|
sinceId,
|
||||||
maxId,
|
maxId,
|
||||||
minId
|
minId
|
||||||
)
|
)
|
||||||
.andWhere(`note.id IN (${favoriteQuery.getQuery()})`)
|
.andWhere("reaction.userId = :meId", { meId: localUser.id })
|
||||||
.innerJoinAndSelect("note.user", "user")
|
.leftJoinAndSelect("reaction.note", "note");
|
||||||
.leftJoinAndSelect("user.avatar", "avatar")
|
|
||||||
.leftJoinAndSelect("user.banner", "banner")
|
|
||||||
.leftJoinAndSelect("note.reply", "reply")
|
|
||||||
.leftJoinAndSelect("note.renote", "renote")
|
|
||||||
.leftJoinAndSelect("reply.user", "replyUser")
|
|
||||||
.leftJoinAndSelect("replyUser.avatar", "replyUserAvatar")
|
|
||||||
.leftJoinAndSelect("replyUser.banner", "replyUserBanner")
|
|
||||||
.leftJoinAndSelect("renote.user", "renoteUser")
|
|
||||||
.leftJoinAndSelect("renoteUser.avatar", "renoteUserAvatar")
|
|
||||||
.leftJoinAndSelect("renoteUser.banner", "renoteUserBanner");
|
|
||||||
|
|
||||||
generateVisibilityQuery(query, localUser);
|
generateVisibilityQuery(query, localUser);
|
||||||
|
|
||||||
query.setParameters({ meId: localUser.id });
|
return PaginationHelpers.execQuery(query, limit, minId !== undefined)
|
||||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
.then(res => {
|
||||||
|
return {
|
||||||
|
data: res.map(p => p.note as Note),
|
||||||
|
maxId: res.map(p => p.id).at(-1),
|
||||||
|
minId: res.map(p => p.id)[0],
|
||||||
|
};
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async getUserRelationships(type: RelationshipType, user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
|
private static async getUserRelationships(type: RelationshipType, user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue