summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/ReactionsBufferingService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/core/ReactionsBufferingService.ts')
-rw-r--r--packages/backend/src/core/ReactionsBufferingService.ts51
1 files changed, 50 insertions, 1 deletions
diff --git a/packages/backend/src/core/ReactionsBufferingService.ts b/packages/backend/src/core/ReactionsBufferingService.ts
index b1a197feeb..b4207c5106 100644
--- a/packages/backend/src/core/ReactionsBufferingService.ts
+++ b/packages/backend/src/core/ReactionsBufferingService.ts
@@ -11,22 +11,48 @@ import { bindThis } from '@/decorators.js';
import type { MiUser, NotesRepository } from '@/models/_.js';
import type { Config } from '@/config.js';
import { PER_NOTE_REACTION_USER_PAIR_CACHE_MAX } from '@/const.js';
+import type { GlobalEvents } from '@/core/GlobalEventService.js';
+import type { OnApplicationShutdown } from '@nestjs/common';
const REDIS_DELTA_PREFIX = 'reactionsBufferDeltas';
const REDIS_PAIR_PREFIX = 'reactionsBufferPairs';
@Injectable()
-export class ReactionsBufferingService {
+export class ReactionsBufferingService implements OnApplicationShutdown {
constructor(
@Inject(DI.config)
private config: Config,
+ @Inject(DI.redisForSub)
+ private redisForSub: Redis.Redis,
+
@Inject(DI.redisForReactions)
private redisForReactions: Redis.Redis, // TODO: 専用のRedisインスタンスにする
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
) {
+ this.redisForSub.on('message', this.onMessage);
+ }
+
+ @bindThis
+ private async onMessage(_: string, data: string) {
+ const obj = JSON.parse(data);
+
+ if (obj.channel === 'internal') {
+ const { type, body } = obj.message as GlobalEvents['internal']['payload'];
+ switch (type) {
+ case 'metaUpdated': {
+ // リアクションバッファリングが有効→無効になったら即bake
+ if (body.before != null && body.before.enableReactionsBuffering && !body.after.enableReactionsBuffering) {
+ this.bake();
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
}
@bindThis
@@ -159,4 +185,27 @@ export class ReactionsBufferingService {
.execute();
}
}
+
+ @bindThis
+ public mergeReactions(src: MiNote['reactions'], delta: Record<string, number>): MiNote['reactions'] {
+ const reactions = { ...src };
+ for (const [name, count] of Object.entries(delta)) {
+ if (reactions[name] != null) {
+ reactions[name] += count;
+ } else {
+ reactions[name] = count;
+ }
+ }
+ return reactions;
+ }
+
+ @bindThis
+ public dispose(): void {
+ this.redisForSub.off('message', this.onMessage);
+ }
+
+ @bindThis
+ public onApplicationShutdown(signal?: string | undefined): void {
+ this.dispose();
+ }
}