From 3bc6205150c627699a9597158bc662ae8a2c0afe Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 21 Sep 2022 02:52:19 +0900 Subject: [PATCH] =?UTF-8?q?refactor(backend):=20=E3=83=AD=E3=82=B8?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=82=92=E3=82=B5=E3=83=BC=E3=83=93=E3=82=B9?= =?UTF-8?q?=E3=81=AB=E5=88=87=E3=82=8A=E5=87=BA=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/core/MetaService.ts | 57 +++++++++++++++---- .../server/api/endpoints/admin/update-meta.ts | 32 ++--------- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts index 5ae9a6fc83..1744f64217 100644 --- a/packages/backend/src/core/MetaService.ts +++ b/packages/backend/src/core/MetaService.ts @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm'; import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; import { Meta } from '@/models/entities/Meta.js'; +import { GlobalEventService } from '@/core/GlobalEventService.js'; import type { OnApplicationShutdown } from '@nestjs/common'; @Injectable() @@ -16,6 +17,8 @@ export class MetaService implements OnApplicationShutdown { @Inject(DI.db) private db: DataSource, + + private globalEventService: GlobalEventService, ) { this.onMessage = this.onMessage.bind(this); @@ -31,6 +34,22 @@ export class MetaService implements OnApplicationShutdown { this.redisSubscriber.on('message', this.onMessage); } + private async onMessage(_, data): Promise { + 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 async fetch(noCache = false): Promise { if (!noCache && this.cache) return this.cache; @@ -65,20 +84,34 @@ export class MetaService implements OnApplicationShutdown { }); } - private async onMessage(_, data) { - const obj = JSON.parse(data); + public async update(data: Partial): Promise { + const updated = await this.db.transaction(async transactionalEntityManager => { + const metas = await transactionalEntityManager.find(Meta, { + order: { + id: 'DESC', + }, + }); - if (obj.channel === 'internal') { - const { type, body } = obj.message; - switch (type) { - case 'metaUpdated': { - this.cache = body; - break; - } - default: - break; + const meta = metas[0]; + + if (meta) { + await transactionalEntityManager.update(Meta, meta.id, data); + + const metas = await transactionalEntityManager.find(Meta, { + order: { + id: 'DESC', + }, + }); + + return metas[0]; + } else { + return await transactionalEntityManager.save(Meta, data); } - } + }); + + this.globalEventService.publishInternalEvent('metaUpdated', updated); + + return updated; } public onApplicationShutdown(signal?: string | undefined) { 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 0ab0780e3d..48fae9b947 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -1,11 +1,12 @@ import { Inject, Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; -import { Meta } from '@/models/entities/Meta.js'; +import type { Meta } from '@/models/entities/Meta.js'; 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'; +import { MetaService } from '@/core/MetaService.js'; export const meta = { tags: ['admin'], @@ -116,7 +117,7 @@ export default class extends Endpoint { @Inject(DI.db) private db: DataSource, - private globalEventService: GlobalEventService, + private metaService: MetaService, private moderationLogService: ModerationLogService, ) { super(meta, paramDef, async (ps, me) => { @@ -438,32 +439,7 @@ export default class extends Endpoint { set.enableActiveEmailValidation = ps.enableActiveEmailValidation; } - const updated = await this.db.transaction(async transactionalEntityManager => { - const metas = await transactionalEntityManager.find(Meta, { - order: { - id: 'DESC', - }, - }); - - const meta = metas[0]; - - if (meta) { - await transactionalEntityManager.update(Meta, meta.id, set); - - const metas = await transactionalEntityManager.find(Meta, { - order: { - id: 'DESC', - }, - }); - - return metas[0]; - } else { - return await transactionalEntityManager.save(Meta, set); - } - }); - - this.globalEventService.publishInternalEvent('metaUpdated', updated); - + await this.metaService.update(set); this.moderationLogService.insertModerationLog(me, 'updateMeta'); }); }