summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/users/notes.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-10-03 20:26:11 +0900
committerGitHub <noreply@github.com>2023-10-03 20:26:11 +0900
commit6277a5545c746fac15ee6b4fe58de2e354ed7fda (patch)
tree700b0d162795f03d644a15ba2375628d66f95d92 /packages/backend/src/server/api/endpoints/users/notes.ts
parentUpdate about-misskey.vue (diff)
downloadsharkey-6277a5545c746fac15ee6b4fe58de2e354ed7fda.tar.gz
sharkey-6277a5545c746fac15ee6b4fe58de2e354ed7fda.tar.bz2
sharkey-6277a5545c746fac15ee6b4fe58de2e354ed7fda.zip
feat: improve tl performance (#11946)
* wip * wip * wip * wip * wip * wip * Update NoteCreateService.ts * wip * wip * wip * wip * Update NoteCreateService.ts * wip * Update NoteCreateService.ts * wip * Update user-notes.ts * wip * wip * wip * Update NoteCreateService.ts * wip * Update timeline.ts * Update timeline.ts * Update timeline.ts * Update timeline.ts * Update timeline.ts * wip * Update timelines.ts * Update timelines.ts * Update timelines.ts * wip * wip * wip * Update timelines.ts * Update misskey-js.api.md * Update timelines.ts * Update timelines.ts * wip * wip * wip * Update timelines.ts * wip * Update timelines.ts * wip * test * Update activitypub.ts * refactor: UserListJoining -> UserListMembership * Update NoteCreateService.ts * wip
Diffstat (limited to 'packages/backend/src/server/api/endpoints/users/notes.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/users/notes.ts109
1 files changed, 42 insertions, 67 deletions
diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts
index e660a0bb25..abcc02eace 100644
--- a/packages/backend/src/server/api/endpoints/users/notes.ts
+++ b/packages/backend/src/server/api/endpoints/users/notes.ts
@@ -5,12 +5,14 @@
import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
-import type { NotesRepository } from '@/models/_.js';
+import * as Redis from 'ioredis';
+import type { MiNote, NotesRepository } from '@/models/_.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
-import { QueryService } from '@/core/QueryService.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { DI } from '@/di-symbols.js';
import { GetterService } from '@/server/api/GetterService.js';
+import { CacheService } from '@/core/CacheService.js';
+import { IdService } from '@/core/IdService.js';
import { ApiError } from '../../error.js';
export const meta = {
@@ -50,9 +52,6 @@ export const paramDef = {
untilDate: { type: 'integer' },
includeMyRenotes: { type: 'boolean', default: true },
withFiles: { type: 'boolean', default: false },
- fileType: { type: 'array', items: {
- type: 'string',
- } },
excludeNsfw: { type: 'boolean', default: false },
},
required: ['userId'],
@@ -61,87 +60,63 @@ export const paramDef = {
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
+ @Inject(DI.redisForTimelines)
+ private redisForTimelines: Redis.Redis,
+
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private noteEntityService: NoteEntityService,
- private queryService: QueryService,
private getterService: GetterService,
+ private cacheService: CacheService,
+ private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
- // Lookup user
- const user = await this.getterService.getUser(ps.userId).catch(err => {
- if (err.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
- throw err;
- });
+ let timeline: MiNote[] = [];
- //#region Construct query
- const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
- .andWhere('note.userId = :userId', { userId: user.id })
- .innerJoinAndSelect('note.user', 'user')
- .leftJoinAndSelect('note.reply', 'reply')
- .leftJoinAndSelect('note.renote', 'renote')
- .leftJoinAndSelect('note.channel', 'channel')
- .leftJoinAndSelect('reply.user', 'replyUser')
- .leftJoinAndSelect('renote.user', 'renoteUser');
+ const limit = ps.limit + (ps.untilId ? 1 : 0); // untilIdに指定したものも含まれるため+1
+ let noteIdsRes: [string, string[]][] = [];
- query.andWhere(new Brackets(qb => {
- qb.orWhere('note.channelId IS NULL');
- qb.orWhere('channel.isSensitive = false');
- }));
-
- this.queryService.generateVisibilityQuery(query, me);
- if (me) {
- this.queryService.generateMutedUserQuery(query, me, user);
- this.queryService.generateBlockedUserQuery(query, me);
+ if (!ps.sinceId && !ps.sinceDate) {
+ noteIdsRes = await this.redisForTimelines.xrevrange(
+ ps.withFiles ? `userTimelineWithFiles:${ps.userId}` : ps.withReplies ? `userTimelineWithReplies:${ps.userId}` : `userTimeline:${ps.userId}`,
+ ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
+ '-',
+ 'COUNT', limit);
}
- if (ps.withFiles) {
- query.andWhere('note.fileIds != \'{}\'');
+ const noteIds = noteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId);
+
+ if (noteIds.length === 0) {
+ return [];
}
- if (ps.fileType != null) {
- query.andWhere('note.fileIds != \'{}\'');
- query.andWhere(new Brackets(qb => {
- for (const type of ps.fileType!) {
- const i = ps.fileType!.indexOf(type);
- qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, { [`type${i}`]: type });
- }
- }));
+ const isFollowing = me ? Object.hasOwn(await this.cacheService.userFollowingsCache.fetch(me.id), ps.userId) : false;
- if (ps.excludeNsfw) {
- query.andWhere('note.cw IS NULL');
- query.andWhere('0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)');
- }
- }
+ const query = this.notesRepository.createQueryBuilder('note')
+ .where('note.id IN (:...noteIds)', { noteIds: noteIds })
+ .innerJoinAndSelect('note.user', 'user')
+ .leftJoinAndSelect('note.reply', 'reply')
+ .leftJoinAndSelect('note.renote', 'renote')
+ .leftJoinAndSelect('reply.user', 'replyUser')
+ .leftJoinAndSelect('renote.user', 'renoteUser')
+ .leftJoinAndSelect('note.channel', 'channel');
- if (!ps.withReplies) {
- query.andWhere('note.replyId IS NULL');
- }
+ timeline = await query.getMany();
- if (ps.withRenotes === false) {
- query.andWhere(new Brackets(qb => {
- qb.orWhere('note.renoteId IS NULL');
- qb.orWhere(new Brackets(qb => {
- qb.orWhere('note.text IS NOT NULL');
- qb.orWhere('note.fileIds != \'{}\'');
- }));
- }));
- }
+ timeline = timeline.filter(note => {
+ if (note.renoteId) {
+ if (note.text == null && note.fileIds.length === 0 && !note.hasPoll) {
+ if (ps.withRenotes === false) return false;
+ }
+ }
- if (ps.includeMyRenotes === false) {
- query.andWhere(new Brackets(qb => {
- qb.orWhere('note.userId != :userId', { userId: user.id });
- qb.orWhere('note.renoteId IS NULL');
- qb.orWhere('note.text IS NOT NULL');
- qb.orWhere('note.fileIds != \'{}\'');
- qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
- }));
- }
+ if (note.visibility === 'followers' && !isFollowing) return false;
- //#endregion
+ return true;
+ });
- const timeline = await query.limit(ps.limit).getMany();
+ timeline.sort((a, b) => a.id > b.id ? -1 : 1);
return await this.noteEntityService.packMany(timeline, me);
});