summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/ReactionsBufferingService.ts
diff options
context:
space:
mode:
authorかっこかり <67428053+kakkokari-gtyih@users.noreply.github.com>2024-09-24 18:29:02 +0900
committerGitHub <noreply@github.com>2024-09-24 18:29:02 +0900
commit6a1a2bef43af929f6def408428bd734ea2bff5c6 (patch)
tree65e843100f1d004c9f7986fd38319924f4cb6a65 /packages/backend/src/core/ReactionsBufferingService.ts
parent:art: (diff)
downloadsharkey-6a1a2bef43af929f6def408428bd734ea2bff5c6.tar.gz
sharkey-6a1a2bef43af929f6def408428bd734ea2bff5c6.tar.bz2
sharkey-6a1a2bef43af929f6def408428bd734ea2bff5c6.zip
fix(backend): RBTの修正 (#14621)
* fix(backend): 絵文字の変換処理が不十分なのを修正 * enhance: リアクションバッファリングが無効になったら即bakeするように * attempt to fix test * fix
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();
+ }
}