mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-08 04:54:32 +00:00
fix performance regression in mentions endpoint
This commit is contained in:
parent
232894cd86
commit
65983d0030
1 changed files with 43 additions and 27 deletions
|
@ -6,6 +6,7 @@
|
||||||
import { Brackets } from 'typeorm';
|
import { Brackets } from 'typeorm';
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import type { NotesRepository, FollowingsRepository } from '@/models/_.js';
|
import type { NotesRepository, FollowingsRepository } from '@/models/_.js';
|
||||||
|
import { MiNote } from '@/models/_.js';
|
||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||||
import { QueryService } from '@/core/QueryService.js';
|
import { QueryService } from '@/core/QueryService.js';
|
||||||
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
||||||
|
@ -61,37 +62,52 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
private readonly activeUsersChart: ActiveUsersChart,
|
private readonly activeUsersChart: ActiveUsersChart,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps, me) => {
|
||||||
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'),
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
|
||||||
ps.sinceId, ps.untilId)
|
.innerJoin(qb => {
|
||||||
.andWhere(new Brackets(qb => qb
|
qb
|
||||||
.where(':meIdAsList <@ note.mentions')
|
.select('note.id', 'id')
|
||||||
.orWhere(':meIdAsList <@ note.visibleUserIds')))
|
.from(qbb => qbb
|
||||||
|
.select('note.id', 'id')
|
||||||
|
.from(MiNote, 'note')
|
||||||
|
.where(new Brackets(qbbb => qbbb
|
||||||
|
// DM to me
|
||||||
|
.orWhere(':meIdAsList <@ note.visibleUserIds')
|
||||||
|
// Mentions me
|
||||||
|
.orWhere(':meIdAsList <@ note.mentions'),
|
||||||
|
))
|
||||||
.setParameters({ meIdAsList: [me.id] })
|
.setParameters({ meIdAsList: [me.id] })
|
||||||
// Avoid scanning primary key index
|
, 'source')
|
||||||
.orderBy('CONCAT(note.id)', 'DESC')
|
.innerJoin(MiNote, 'note', 'note.id = source.id');
|
||||||
.innerJoinAndSelect('note.user', 'user')
|
|
||||||
.leftJoinAndSelect('note.reply', 'reply')
|
|
||||||
.leftJoinAndSelect('note.renote', 'renote')
|
|
||||||
.leftJoinAndSelect('reply.user', 'replyUser', 'replyUser.id = note.replyUserId')
|
|
||||||
.leftJoinAndSelect('renote.user', 'renoteUser', 'renoteUser.id = note.renoteUserId');
|
|
||||||
|
|
||||||
this.queryService.generateVisibilityQuery(query, me);
|
// Mentioned or visible users can always access
|
||||||
this.queryService.generateBlockedHostQueryForNote(query);
|
//this.queryService.generateVisibilityQuery(query, me);
|
||||||
this.queryService.generateMutedUserQueryForNotes(query, me);
|
this.queryService.generateBlockedHostQueryForNote(qb);
|
||||||
this.queryService.generateMutedNoteThreadQuery(query, me);
|
this.queryService.generateMutedUserQueryForNotes(qb, me);
|
||||||
this.queryService.generateBlockedUserQueryForNotes(query, me);
|
this.queryService.generateMutedNoteThreadQuery(qb, me);
|
||||||
|
this.queryService.generateBlockedUserQueryForNotes(qb, me);
|
||||||
// A renote can't mention a user, so it will never appear here anyway.
|
// A renote can't mention a user, so it will never appear here anyway.
|
||||||
//this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
|
//this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
|
||||||
|
|
||||||
if (ps.visibility) {
|
if (ps.visibility) {
|
||||||
query.andWhere('note.visibility = :visibility', { visibility: ps.visibility });
|
qb.andWhere('note.visibility = :visibility', { visibility: ps.visibility });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ps.following) {
|
if (ps.following) {
|
||||||
this.queryService.andFollowingUser(query, ':meId', 'note.userId');
|
this.queryService
|
||||||
|
.andFollowingUser(qb, ':meId', 'note.userId')
|
||||||
|
.setParameters({ meId: me.id });
|
||||||
}
|
}
|
||||||
|
|
||||||
const mentions = await query.limit(ps.limit).getMany();
|
return qb;
|
||||||
|
}, 'source', 'source.id = note.id')
|
||||||
|
.innerJoinAndSelect('note.user', 'user')
|
||||||
|
.leftJoinAndSelect('note.reply', 'reply')
|
||||||
|
.leftJoinAndSelect('note.renote', 'renote')
|
||||||
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
||||||
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
||||||
|
.limit(ps.limit);
|
||||||
|
|
||||||
|
const mentions = await query.getMany();
|
||||||
|
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
this.activeUsersChart.read(me);
|
this.activeUsersChart.read(me);
|
||||||
|
|
Loading…
Add table
Reference in a new issue