From b45d782f8e0069656d033d2981cbe5b031f21ed0 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 5 Mar 2023 01:15:25 +0800 Subject: [PATCH] feat: add config model and add action for get client config --- server/models/config.ts | 69 ++++++++++++++++++++++++++ server/services/core/config.service.ts | 54 +++++++++++++++++--- 2 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 server/models/config.ts diff --git a/server/models/config.ts b/server/models/config.ts new file mode 100644 index 00000000..f9ddd766 --- /dev/null +++ b/server/models/config.ts @@ -0,0 +1,69 @@ +import { + getModelForClass, + prop, + DocumentType, + modelOptions, + Severity, + ReturnModelType, +} from '@typegoose/typegoose'; +import type { Base } from '@typegoose/typegoose/lib/defaultClasses'; +import type { Types } from 'mongoose'; + +@modelOptions({ + options: { + allowMixed: Severity.ALLOW, + }, +}) +export class Config implements Base { + _id: Types.ObjectId; + id: string; + + static globalClientConfigName = '__client_config__'; + + @prop() + name: string; + + /** + * config data + */ + @prop() + data: object; + + static async getAllClientPersistConfig( + this: ReturnModelType + ): Promise { + const config = await this.findOne({ + name: Config.globalClientConfigName, + }); + + return config?.data ?? {}; + } + + /** + * set global client persist config from mongodb + */ + static async setClientPersistConfig( + this: ReturnModelType, + key: string, + value: any + ): Promise { + await this.updateOne( + { + name: Config.globalClientConfigName, + }, + { + $set: { + [key]: value, + }, + } + ); + } +} + +export type ConfigDocument = DocumentType; + +const model = getModelForClass(Config); + +export type ConfigModel = typeof model; + +export default model; diff --git a/server/services/core/config.service.ts b/server/services/core/config.service.ts index 587d06c5..2dc952a7 100644 --- a/server/services/core/config.service.ts +++ b/server/services/core/config.service.ts @@ -1,9 +1,19 @@ import _ from 'lodash'; -import { TcService, TcPureContext, config } from 'tailchat-server-sdk'; +import { + TcService, + TcPureContext, + config, + TcDbService, + TcContext, +} from 'tailchat-server-sdk'; +import type { ConfigDocument, ConfigModel } from '../../models/config'; /** * 配置服务器 */ +interface ConfigService + extends TcService, + TcDbService {} class ConfigService extends TcService { config = {}; // 自管理的配置项,globalConfig是同步过来的 @@ -12,7 +22,20 @@ class ConfigService extends TcService { } onInit(): void { - this.registerAction('client', this.client); + this.registerLocalDb(require('../../models/config').default); + this.registerAction('client', this.client, { + cache: { + keys: [], + ttl: 24 * 60 * 60, // 1 day + }, + }); + this.registerAction('setClientConfig', this.setClientConfig, { + params: { + key: 'string', + value: 'any', + }, + visibility: 'public', + }); this.registerAction('all', this.all, { visibility: 'public', }); @@ -48,21 +71,40 @@ class ConfigService extends TcService { * NOTICE: 返回内容比较简单,因此不建议增加缓存 */ async client(ctx: TcPureContext) { + const persistConfig = this.adapter.model.getAllClientPersistConfig(); + return { uploadFileLimit: config.storage.limit, emailVerification: config.emailVerification, + ...persistConfig, }; } - async all(ctx: TcPureContext) { + /** + * set client config in tailchat network + * + * usually call from admin + */ + async setClientConfig( + ctx: TcContext<{ + key: string; + value: any; + }> + ) { + const { key, value } = ctx.params; + await this.adapter.model.setClientPersistConfig(key, value); + await this.cleanActionCache('client', []); + } + + async all(ctx: TcContext) { return this.config; } - async get(ctx: TcPureContext<{ key: string }>) { + async get(ctx: TcContext<{ key: string }>) { return this.config[ctx.params.key] ?? null; } - async set(ctx: TcPureContext<{ key: string; value: any }>) { + async set(ctx: TcContext<{ key: string; value: any }>) { const { key, value } = ctx.params; _.set(this.config, key, value); @@ -72,7 +114,7 @@ class ConfigService extends TcService { /** * 添加到设置但不重复 */ - async addToSet(ctx: TcPureContext<{ key: string; value: any }>) { + async addToSet(ctx: TcContext<{ key: string; value: any }>) { const { key, value } = ctx.params; const originConfig = _.get(this.config, key) ?? [];