[mastodon-client] Improve note query performance

This commit is contained in:
Laura Hausmann 2023-09-25 13:45:03 +02:00
parent 7da7b6e09b
commit 455c249f3d
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 9 additions and 51 deletions

View File

@ -16,10 +16,7 @@ export class NoteHelpers {
.andWhere( .andWhere(
"note.id IN (SELECT id FROM note_replies(:noteId, :depth, :limit))", "note.id IN (SELECT id FROM note_replies(:noteId, :depth, :limit))",
{noteId, depth, limit}, {noteId, depth, limit},
) );
.innerJoinAndSelect("note.user", "user")
.leftJoinAndSelect("user.avatar", "avatar")
.leftJoinAndSelect("user.banner", "banner");
generateVisibilityQuery(query, user); generateVisibilityQuery(query, user);
if (user) { if (user) {

View File

@ -1,8 +1,7 @@
import { Note } from "@/models/entities/note.js"; import { Note } from "@/models/entities/note.js";
import { ILocalUser } from "@/models/entities/user.js"; import { ILocalUser } from "@/models/entities/user.js";
import { Followings, Notes } from "@/models/index.js"; import { Followings, Notes } from "@/models/index.js";
import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; import { Brackets } from "typeorm";
import { Brackets, SelectQueryBuilder } from "typeorm";
import { generateChannelQuery } from "@/server/api/common/generate-channel-query.js"; import { generateChannelQuery } from "@/server/api/common/generate-channel-query.js";
import { generateRepliesQuery } from "@/server/api/common/generate-replies-query.js"; import { generateRepliesQuery } from "@/server/api/common/generate-replies-query.js";
import { generateVisibilityQuery } from "@/server/api/common/generate-visibility-query.js"; import { generateVisibilityQuery } from "@/server/api/common/generate-visibility-query.js";
@ -13,24 +12,15 @@ import { generateMutedUserRenotesQueryForNotes } from "@/server/api/common/gener
import { fetchMeta } from "@/misc/fetch-meta.js"; import { fetchMeta } from "@/misc/fetch-meta.js";
import { ApiError } from "@/server/api/error.js"; import { ApiError } from "@/server/api/error.js";
import { meta } from "@/server/api/endpoints/notes/global-timeline.js"; import { meta } from "@/server/api/endpoints/notes/global-timeline.js";
import { NoteHelpers } from "@/server/api/mastodon/helpers/note.js";
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js"; import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
export class TimelineHelpers { export class TimelineHelpers {
public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> { public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> {
if (limit > 40) limit = 40; if (limit > 40) limit = 40;
const hasFollowing = const followingIds = await Followings.findBy({
(await Followings.count({ followerId: user.id
where: { }).then(res => res.map(p => p.followeeId));
followerId: user.id,
},
take: 1,
})) !== 0;
const followingQuery = Followings.createQueryBuilder("following")
.select("following.followeeId")
.where("following.followerId = :followerId", {followerId: user.id});
const query = PaginationHelpers.makePaginationQuery( const query = PaginationHelpers.makePaginationQuery(
Notes.createQueryBuilder("note"), Notes.createQueryBuilder("note"),
@ -41,22 +31,11 @@ export class TimelineHelpers {
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
qb.where("note.userId = :meId", {meId: user.id}); qb.where("note.userId = :meId", {meId: user.id});
if (hasFollowing) qb.orWhere(`note.userId IN (:...followingIds)`, {followingIds: followingIds});
qb.orWhere(`note.userId IN (${followingQuery.getQuery()})`);
}), }),
) )
.innerJoinAndSelect("note.user", "user")
.leftJoinAndSelect("user.avatar", "avatar")
.leftJoinAndSelect("user.banner", "banner")
.leftJoinAndSelect("note.reply", "reply") .leftJoinAndSelect("note.reply", "reply")
.leftJoinAndSelect("note.renote", "renote") .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")
.setParameters(followingQuery.getParameters());
generateChannelQuery(query, user); generateChannelQuery(query, user);
generateRepliesQuery(query, true, user); generateRepliesQuery(query, true, user);
@ -98,17 +77,8 @@ export class TimelineHelpers {
if (!local) query.andWhere("note.channelId IS NULL"); if (!local) query.andWhere("note.channelId IS NULL");
query query
.innerJoinAndSelect("note.user", "user")
.leftJoinAndSelect("user.avatar", "avatar")
.leftJoinAndSelect("user.banner", "banner")
.leftJoinAndSelect("note.reply", "reply") .leftJoinAndSelect("note.reply", "reply")
.leftJoinAndSelect("note.renote", "renote") .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");
generateRepliesQuery(query, true, user); generateRepliesQuery(query, true, user);
if (user) { if (user) {

View File

@ -51,17 +51,8 @@ export class UserHelpers {
if (excludeReblogs) query.andWhere("(note.renoteId IS NOT NULL) OR (note.text IS NOT NULL)"); if (excludeReblogs) query.andWhere("(note.renoteId IS NOT NULL) OR (note.text IS NOT NULL)");
query query
.innerJoinAndSelect("note.user", "user")
.leftJoinAndSelect("user.avatar", "avatar")
.leftJoinAndSelect("user.banner", "banner")
.leftJoinAndSelect("note.reply", "reply") .leftJoinAndSelect("note.reply", "reply")
.leftJoinAndSelect("note.renote", "renote") .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");
//FIXME this doesn't exclude replies to your own reply to someone else's post //FIXME this doesn't exclude replies to your own reply to someone else's post
generateRepliesQuery(query, !excludeReplies, localUser); generateRepliesQuery(query, !excludeReplies, localUser);