From 81fd01d5fa9e731db4771e60b2f2d5c2bc2e7018 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Wed, 25 Jun 2025 13:24:03 -0400 Subject: [PATCH] increment/decrement note counts through CollapsedQueueService --- .../backend/src/core/CollapsedQueueService.ts | 9 +++++++-- packages/backend/src/core/NoteCreateService.ts | 13 +------------ packages/backend/src/core/NoteDeleteService.ts | 15 ++------------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/packages/backend/src/core/CollapsedQueueService.ts b/packages/backend/src/core/CollapsedQueueService.ts index fd0ccb0eaf..226f641a2d 100644 --- a/packages/backend/src/core/CollapsedQueueService.ts +++ b/packages/backend/src/core/CollapsedQueueService.ts @@ -24,7 +24,8 @@ export type UpdateInstanceJob = { }; export type UpdateUserJob = { - updatedAt: Date, + updatedAt?: Date, + additionalNotes?: number, }; @Injectable() @@ -74,8 +75,12 @@ export class CollapsedQueueService implements OnApplicationShutdown { fiveMinuteInterval, (oldJob, newJob) => ({ updatedAt: maxDate(oldJob.updatedAt, newJob.updatedAt), + additionalNotes: (oldJob.additionalNotes ?? 0) + (newJob.additionalNotes ?? 0), + }), + (id, job) => this.usersRepository.update({ id }, { + updatedAt: job.updatedAt, + notesCount: job.additionalNotes ? () => `"notesCount" + ${job.additionalNotes}` : undefined, }), - (id, job) => this.usersRepository.update({ id }, { updatedAt: job.updatedAt }), { onError: this.onQueueError, concurrency: 4, diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts index 8f515de502..ee1f681a76 100644 --- a/packages/backend/src/core/NoteCreateService.ts +++ b/packages/backend/src/core/NoteCreateService.ts @@ -605,7 +605,7 @@ export class NoteCreateService implements OnApplicationShutdown { if (!this.isRenote(note) || this.isQuote(note)) { // Increment notes count (user) - await this.incNotesCountOfUser(user); + this.collapsedQueueService.updateUserQueue.enqueue(user.id, { additionalNotes: 1 }); } this.collapsedQueueService.updateUserQueue.enqueue(user.id, { updatedAt: new Date() }); @@ -890,17 +890,6 @@ export class NoteCreateService implements OnApplicationShutdown { await this.searchService.indexNote(note); } - @bindThis - private async incNotesCountOfUser(user: { id: MiUser['id']; }) { - await this.usersRepository.createQueryBuilder().update() - .set({ - updatedAt: this.timeService.date, - notesCount: () => '"notesCount" + 1', - }) - .where('id = :id', { id: user.id }) - .execute(); - } - @bindThis private async extractMentionedUsers(user: { host: MiUser['host']; }, tokens: mfm.MfmNode[]): Promise { if (tokens == null) return []; diff --git a/packages/backend/src/core/NoteDeleteService.ts b/packages/backend/src/core/NoteDeleteService.ts index 4384b29a99..5bab95862d 100644 --- a/packages/backend/src/core/NoteDeleteService.ts +++ b/packages/backend/src/core/NoteDeleteService.ts @@ -141,14 +141,14 @@ export class NoteDeleteService { if (!isPureRenote(note)) { // Decrement notes count (user) - promises.push(this.decNotesCountOfUser(user)); + this.collapsedQueueService.updateUserQueue.enqueue(user.id, { additionalNotes: -1 }); } this.collapsedQueueService.updateUserQueue.enqueue(user.id, { updatedAt: new Date() }); for (const cascade of cascadingNotes) { if (!isPureRenote(cascade)) { - promises.push(this.decNotesCountOfUser(cascade.user)); + this.collapsedQueueService.updateUserQueue.enqueue(cascade.user.id, { additionalNotes: -1 }); } // Don't mark cascaded user as updated (active) } @@ -217,17 +217,6 @@ export class NoteDeleteService { }); } - @bindThis - private async decNotesCountOfUser(user: { id: MiUser['id']; }) { - await this.usersRepository.createQueryBuilder().update() - .set({ - updatedAt: this.timeService.date, - notesCount: () => '"notesCount" - 1', - }) - .where('id = :id', { id: user.id }) - .execute(); - } - @bindThis private async findCascadingNotes(note: MiNote): Promise<(MiNote & { user: MiUser })[]> { const cascadingNotes: MiNote[] = [];