From fe8e532a37587812cd8016a185ad3d9a5abc92e3 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 30 Oct 2022 16:19:39 +0800 Subject: [PATCH] feat: add config.service --- client/shared/index.tsx | 2 +- client/shared/model/config.ts | 4 +-- client/web/src/init.tsx | 4 +-- server/services/core/config.service.ts | 50 ++++++++++++++++++++------ 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/client/shared/index.tsx b/client/shared/index.tsx index d31e8636..680faaf6 100644 --- a/client/shared/index.tsx +++ b/client/shared/index.tsx @@ -96,7 +96,7 @@ export { // model export * as model from './model/__all__'; export { fetchAvailableServices } from './model/common'; -export { fetchGlobalConfig } from './model/config'; +export { fetchGlobalClientConfig } from './model/config'; export { createDMConverse, appendDMConverseMembers, diff --git a/client/shared/model/config.ts b/client/shared/model/config.ts index 295e11a0..56726c44 100644 --- a/client/shared/model/config.ts +++ b/client/shared/model/config.ts @@ -21,8 +21,8 @@ export function getGlobalConfig() { }; } -export async function fetchGlobalConfig(): Promise { - const { data: config } = await request.get('/api/config/global'); +export async function fetchGlobalClientConfig(): Promise { + const { data: config } = await request.get('/api/config/client'); globalConfig = { ...globalConfig, diff --git a/client/web/src/init.tsx b/client/web/src/init.tsx index beac29d3..bf743c41 100644 --- a/client/web/src/init.tsx +++ b/client/web/src/init.tsx @@ -10,7 +10,7 @@ import { setTokenGetter, showErrorToasts, t, - fetchGlobalConfig, + fetchGlobalClientConfig, request, isValidStr, isDevelopment, @@ -107,7 +107,7 @@ setErrorHook((err) => { /** * 获取前端配置 */ -fetchGlobalConfig().catch((e) => { +fetchGlobalClientConfig().catch((e) => { showErrorToasts(t('全局配置加载失败')); console.error('全局配置加载失败', e); }); diff --git a/server/services/core/config.service.ts b/server/services/core/config.service.ts index 0641236e..05afa43a 100644 --- a/server/services/core/config.service.ts +++ b/server/services/core/config.service.ts @@ -1,14 +1,11 @@ -import { - TcService, - TcDbService, - TcPureContext, - config, -} from 'tailchat-server-sdk'; +import { TcService, TcPureContext, config } from 'tailchat-server-sdk'; /** * 配置服务器 */ class ConfigService extends TcService { + config = {}; + get serviceName(): string { return 'config'; } @@ -19,19 +16,52 @@ class ConfigService extends TcService { * * 用于提供给前端使用 */ - this.registerAction('global', this.globalConfig); + this.registerAction('client', this.client); + this.registerAction('all', this.all, { + visibility: 'public', + }); + this.registerAction('get', this.get, { + visibility: 'public', + params: { + key: 'string', + }, + }); + this.registerAction('set', this.set, { + visibility: 'public', + params: { + key: 'string', + value: 'any', + }, + }); - this.registerAuthWhitelist(['/config/global']); + this.registerAuthWhitelist(['/config/client']); } /** - * 更新用户在会话中已读的最后一条消息 + * 全局配置 + * + * 用于提供给前端使用d */ - async globalConfig(ctx: TcPureContext) { + async client(ctx: TcPureContext) { return { uploadFileLimit: config.storage.limit, }; } + + async all(ctx: TcPureContext) { + return this.config; + } + + async get(ctx: TcPureContext<{ key: string }>) { + return this.config[ctx.params.key] ?? null; + } + + async set(ctx: TcPureContext<{ key: string; value: string }>) { + const { key, value } = ctx.params; + + this.config[key] = value; + await this.broker.broadcast('config.updated', this.config); + } } export default ConfigService;