From dc43fc68ef2b8e0d6f2cfd94911b367fd915f445 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 21 Sep 2022 02:35:49 +0900 Subject: [PATCH] =?UTF-8?q?enhance(backend):=20meta=E3=81=AE=E3=83=9D?= =?UTF-8?q?=E3=83=BC=E3=83=AA=E3=83=B3=E3=82=B0=E9=A0=BB=E5=BA=A6=E3=82=92?= =?UTF-8?q?=E6=B8=9B=E3=82=89=E3=81=97=E3=80=81redis=E3=81=A7=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=82=92=E5=8F=97=E3=81=91=E5=8F=96=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/core/MetaService.ts | 33 ++++++++++++++++--- .../server/api/endpoints/admin/update-meta.ts | 16 +++++++-- .../backend/src/server/api/stream/types.ts | 2 ++ 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 4099e340b..5ae9a6fc8 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; -import type { UsersRepository } from '@/models/index.js'; +import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; import { Meta } from '@/models/entities/Meta.js'; import type { OnApplicationShutdown } from '@nestjs/common'; @@ -11,19 +11,27 @@ export class MetaService implements OnApplicationShutdown { private intervalId: NodeJS.Timer; constructor( + @Inject(DI.redisSubscriber) + private redisSubscriber: Redis.Redis, + @Inject(DI.db) private db: DataSource, ) { + this.onMessage = this.onMessage.bind(this); + if (process.env.NODE_ENV !== 'test') { this.intervalId = setInterval(() => { this.fetch(true).then(meta => { + // fetch内でもセットしてるけど仕様変更の可能性もあるため一応 this.cache = meta; }); - }, 1000 * 10); + }, 1000 * 60 * 5); } + + this.redisSubscriber.on('message', this.onMessage); } - async fetch(noCache = false): Promise { + public async fetch(noCache = false): Promise { if (!noCache && this.cache) return this.cache; return await this.db.transaction(async transactionalEntityManager => { @@ -56,8 +64,25 @@ export class MetaService implements OnApplicationShutdown { } }); } - + + private async onMessage(_, data) { + const obj = JSON.parse(data); + + if (obj.channel === 'internal') { + const { type, body } = obj.message; + switch (type) { + case 'metaUpdated': { + this.cache = body; + break; + } + default: + break; + } + } + } + public onApplicationShutdown(signal?: string | undefined) { clearInterval(this.intervalId); + this.redisSubscriber.off('message', this.onMessage); } } diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index 968ed4d26..0ab0780e3 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -5,6 +5,7 @@ import { ModerationLogService } from '@/core/ModerationLogService.js'; import { DB_MAX_NOTE_TEXT_LENGTH } from '@/misc/hard-limits.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { DI } from '@/di-symbols.js'; +import { GlobalEventService } from '@/core/GlobalEventService.js'; export const meta = { tags: ['admin'], @@ -115,6 +116,7 @@ export default class extends Endpoint { @Inject(DI.db) private db: DataSource, + private globalEventService: GlobalEventService, private moderationLogService: ModerationLogService, ) { super(meta, paramDef, async (ps, me) => { @@ -436,7 +438,7 @@ export default class extends Endpoint { set.enableActiveEmailValidation = ps.enableActiveEmailValidation; } - await this.db.transaction(async transactionalEntityManager => { + const updated = await this.db.transaction(async transactionalEntityManager => { const metas = await transactionalEntityManager.find(Meta, { order: { id: 'DESC', @@ -447,11 +449,21 @@ export default class extends Endpoint { if (meta) { await transactionalEntityManager.update(Meta, meta.id, set); + + const metas = await transactionalEntityManager.find(Meta, { + order: { + id: 'DESC', + }, + }); + + return metas[0]; } else { - await transactionalEntityManager.save(Meta, set); + return await transactionalEntityManager.save(Meta, set); } }); + this.globalEventService.publishInternalEvent('metaUpdated', updated); + this.moderationLogService.insertModerationLog(me, 'updateMeta'); }); } diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index 000f9a25d..ec05be56e 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -13,6 +13,7 @@ import type { Signin } from '@/models/entities/Signin.js'; import type { Page } from '@/models/entities/Page.js'; import type { Packed } from '@/misc/schema.js'; import type { Webhook } from '@/models/entities/Webhook.js'; +import type { Meta } from '@/models/entities/Meta.js'; import type Emitter from 'strict-event-emitter-types'; import type { EventEmitter } from 'events'; @@ -29,6 +30,7 @@ export interface InternalStreamTypes { antennaCreated: Antenna; antennaDeleted: Antenna; antennaUpdated: Antenna; + metaUpdated: Meta, } export interface BroadcastTypes {