From 155a7f93056aa047f174534fac68106361e42fd5 Mon Sep 17 00:00:00 2001 From: qwqcode Date: Tue, 7 Mar 2023 09:49:06 +0800 Subject: [PATCH] fix(ui/conf): avoid some conf overrides frontend from the backend (#449) Signed-off-by: qwqcode --- ui/packages/artalk/src/api/system.ts | 13 ++----------- ui/packages/artalk/src/config.ts | 21 +++++++++++++++++++++ ui/packages/artalk/src/list/list-lite.ts | 4 +++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/ui/packages/artalk/src/api/system.ts b/ui/packages/artalk/src/api/system.ts index 4f3a825c2..aed3c3ab7 100644 --- a/ui/packages/artalk/src/api/system.ts +++ b/ui/packages/artalk/src/api/system.ts @@ -1,5 +1,6 @@ import ArtalkConfig from '~/types/artalk-config' import ApiBase from './api-base' +import { handleBackendRefConf } from '../config' /** * 系统 API @@ -10,17 +11,7 @@ export default class SystemApi extends ApiBase { const data = await this.POST(`/conf`) const conf = (data.frontend_conf || {}) as ArtalkConfig - // Patch: `emoticons` config string to json - if (conf.emoticons && typeof conf.emoticons === "string") { - conf.emoticons = conf.emoticons.trim() - if (conf.emoticons.startsWith("[") || conf.emoticons.startsWith("{")) { - conf.emoticons = JSON.parse(conf.emoticons) // pase json - } else if (conf.emoticons === "false") { - conf.emoticons = false - } - } - - return conf + return handleBackendRefConf(conf) } /** 获取配置数据 */ diff --git a/ui/packages/artalk/src/config.ts b/ui/packages/artalk/src/config.ts index 94dd83bd1..d32d211b5 100644 --- a/ui/packages/artalk/src/config.ts +++ b/ui/packages/artalk/src/config.ts @@ -42,3 +42,24 @@ export function handelBaseConf(customConf: Partial): ArtalkConfig return conf } + +export function handleBackendRefConf(conf: Partial) { + const DisabledKeys: (keyof ArtalkConfig)[] = [ + 'el', 'pageKey', 'pageTitle', 'server', 'site', 'darkMode' + ] + Object.keys(conf).forEach(k => { + if (DisabledKeys.includes(k as any)) delete conf[k] + }) + + // Patch: `emoticons` config string to json + if (conf.emoticons && typeof conf.emoticons === "string") { + conf.emoticons = conf.emoticons.trim() + if (conf.emoticons.startsWith("[") || conf.emoticons.startsWith("{")) { + conf.emoticons = JSON.parse(conf.emoticons) // pase json + } else if (conf.emoticons === "false") { + conf.emoticons = false + } + } + + return conf +} diff --git a/ui/packages/artalk/src/list/list-lite.ts b/ui/packages/artalk/src/list/list-lite.ts index 181a60a0b..fabab7aad 100644 --- a/ui/packages/artalk/src/list/list-lite.ts +++ b/ui/packages/artalk/src/list/list-lite.ts @@ -8,6 +8,7 @@ import PgHolder, { TPgMode } from './paginator' import * as ListNest from './list-nest' import * as ListUi from './list-ui' import { backendMinVersion, version as ARTALK_VERSION } from '../../package.json' +import { handleBackendRefConf } from '../config' export default class ListLite extends Component { /** 列表评论集区域元素 */ @@ -191,7 +192,8 @@ export default class ListLite extends Component { // 装载后端提供的配置 if (!this.confLoaded) { // 仅应用一次配置 - if (this.conf.useBackendConf) this.ctx.updateConf(data.conf.frontend_conf) + const backendRefConf = handleBackendRefConf(data.conf.frontend_conf) + if (this.conf.useBackendConf) this.ctx.updateConf(backendRefConf) else this.ctx.updateConf({}) // 让事件监听 `on('conf-loaded')` 有效,与前者保持相同生命周期环节 this.confLoaded = true }