fix performance regression in home timeline

This commit is contained in:
Hazelnoot 2025-06-05 00:35:22 -04:00
parent c76a5467f6
commit 232894cd86

View file

@ -140,64 +140,27 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
} }
private async getFromDb(ps: { untilId: string | null; sinceId: string | null; limit: number; withFiles: boolean; withRenotes: boolean; withBots: boolean; }, me: MiLocalUser) { private async getFromDb(ps: { untilId: string | null; sinceId: string | null; limit: number; withFiles: boolean; withRenotes: boolean; withBots: boolean; }, me: MiLocalUser) {
const followees = await this.userFollowingService.getFollowees(me.id);
const followingChannels = await this.channelFollowingsRepository.find({
where: {
followerId: me.id,
},
});
//#region Construct query //#region Construct query
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId) const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
.innerJoinAndSelect('note.user', 'user') .innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('note.reply', 'reply') .leftJoinAndSelect('note.reply', 'reply')
.leftJoinAndSelect('note.renote', 'renote') .leftJoinAndSelect('note.renote', 'renote')
.leftJoinAndSelect('reply.user', 'replyUser', 'replyUser.id = note.replyUserId') .leftJoinAndSelect('reply.user', 'replyUser')
.leftJoinAndSelect('renote.user', 'renoteUser', 'renoteUser.id = note.renoteUserId'); .leftJoinAndSelect('renote.user', 'renoteUser')
// 1. in a channel I follow, 2. my own post, 3. by a user I follow
if (followees.length > 0 && followingChannels.length > 0) { .andWhere(new Brackets(qb => this.queryService
// ユーザー・チャンネルともにフォローあり .orFollowingChannel(qb, ':meId', 'note.channelId')
const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)]; .orWhere(':meId = note.userId')
const followingChannelIds = followingChannels.map(x => x.followeeId); .orWhere(new Brackets(qb2 => this.queryService
query.andWhere(new Brackets(qb => { .andFollowingUser(qb2, ':meId', 'note.userId')
qb .andWhere('note.channelId IS NULL'))),
.where(new Brackets(qb2 => { ))
qb2 // 1. Not a reply, 2. a self-reply
.where('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds }) .andWhere(new Brackets(qb => qb
.andWhere('note.channelId IS NULL'); .orWhere('note.replyId IS NULL') // 返信ではない
})) .orWhere('note.replyUserId = note.userId')))
.orWhere('note.channelId IN (:...followingChannelIds)', { followingChannelIds }); .setParameters({ meId: me.id })
})); .limit(ps.limit);
} else if (followees.length > 0) {
// ユーザーフォローのみ(チャンネルフォローなし)
const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
query
.andWhere('note.channelId IS NULL')
.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
} else if (followingChannels.length > 0) {
// チャンネルフォローのみ(ユーザーフォローなし)
const followingChannelIds = followingChannels.map(x => x.followeeId);
query.andWhere(new Brackets(qb => {
qb
.where('note.channelId IN (:...followingChannelIds)', { followingChannelIds })
.orWhere('note.userId = :meId', { meId: me.id });
}));
} else {
// フォローなし
query
.andWhere('note.channelId IS NULL')
.andWhere('note.userId = :meId', { meId: me.id });
}
query.andWhere(new Brackets(qb => {
qb
.where('note.replyId IS NULL') // 返信ではない
.orWhere(new Brackets(qb => {
qb // 返信だけど投稿者自身への返信
.where('note.replyId IS NOT NULL')
.andWhere('note.replyUserId = note.userId');
}));
}));
this.queryService.generateVisibilityQuery(query, me); this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateBlockedHostQueryForNote(query); this.queryService.generateBlockedHostQueryForNote(query);
@ -212,11 +175,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (!ps.withRenotes) { if (!ps.withRenotes) {
this.queryService.generateExcludedRenotesQueryForNotes(query); this.queryService.generateExcludedRenotesQueryForNotes(query);
} else if (me) { } else {
this.queryService.generateMutedUserRenotesQueryForNotes(query, me); this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
} }
//#endregion //#endregion
return await query.limit(ps.limit).getMany(); return await query.getMany();
} }
} }