From 3da8cc4227c45cd465c9c5da15b021c77f653b58 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 26 Feb 2022 23:27:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=A1=A8=E6=83=85=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/chatConfig.js | 38 +++++++++++++++++-- .../src/components/ChatRenderer/index.vue | 8 ++-- frontend/src/views/Home.vue | 16 ++++++-- frontend/src/views/Room.vue | 38 ++++++++++++++----- 4 files changed, 79 insertions(+), 21 deletions(-) diff --git a/frontend/src/api/chatConfig.js b/frontend/src/api/chatConfig.js index f8c44c5..bde941f 100644 --- a/frontend/src/api/chatConfig.js +++ b/frontend/src/api/chatConfig.js @@ -1,3 +1,5 @@ +import _ from 'lodash' + import { mergeConfig } from '@/utils' export const DEFAULT_CONFIG = { @@ -19,7 +21,13 @@ export const DEFAULT_CONFIG = { relayMessagesByServer: false, autoTranslate: false, - giftUsernamePronunciation: '' + giftUsernamePronunciation: '', + + emoticons: [] // [{ keyword: '', url: '' }, ...] +} + +export function deepCloneDefaultConfig() { + return _.cloneDeep(DEFAULT_CONFIG) } export function setLocalConfig(config) { @@ -29,8 +37,32 @@ export function setLocalConfig(config) { export function getLocalConfig() { try { - return mergeConfig(JSON.parse(window.localStorage.config), DEFAULT_CONFIG) + let config = JSON.parse(window.localStorage.config) + config = mergeConfig(config, deepCloneDefaultConfig()) + sanitizeConfig(config) + return config } catch { - return { ...DEFAULT_CONFIG } + return deepCloneDefaultConfig() } } + +export function sanitizeConfig(config) { + let newEmoticons = [] + if (config.emoticons instanceof Array) { + for (let emoticon of config.emoticons) { + try { + let newEmoticon = { + keyword: emoticon.keyword, + url: emoticon.url + } + if ((typeof newEmoticon.keyword !== 'string') || (typeof newEmoticon.url !== 'string')) { + continue + } + newEmoticons.push(newEmoticon) + } catch { + continue + } + } + } + config.emoticons = newEmoticons +} diff --git a/frontend/src/components/ChatRenderer/index.vue b/frontend/src/components/ChatRenderer/index.vue index 2633370..113eb84 100644 --- a/frontend/src/components/ChatRenderer/index.vue +++ b/frontend/src/components/ChatRenderer/index.vue @@ -211,12 +211,12 @@ export default { this.delMessages([id]) }, delMessages(ids) { - this.enqueueMessages(ids.map(id => { - return { + this.enqueueMessages(ids.map( + id => ({ type: constants.MESSAGE_TYPE_DEL, id - } - })) + }) + )) }, clearMessages() { this.messages = [] diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue index 5e21316..dec0237 100644 --- a/frontend/src/views/Home.vue +++ b/frontend/src/views/Home.vue @@ -154,8 +154,8 @@ export default { loaderUrl: '' }, form: { - roomId: parseInt(window.localStorage.roomId || '1'), - ...chatConfig.getLocalConfig() + ...chatConfig.getLocalConfig(), + roomId: parseInt(window.localStorage.roomId || '1') } } }, @@ -205,6 +205,7 @@ export default { let query = { ...this.form, + emoticons: JSON.stringify(this.form.emoticons), lang: this.$i18n.locale } delete query.roomId @@ -239,12 +240,19 @@ export default { this.$message.error(this.$t('home.failedToParseConfig') + e) return } - cfg = mergeConfig(cfg, chatConfig.DEFAULT_CONFIG) - this.form = { roomId: this.form.roomId, ...cfg } + this.importConfigFromObj(cfg) } reader.readAsText(input.files[0]) } input.click() + }, + importConfigFromObj(cfg) { + cfg = mergeConfig(cfg, chatConfig.deepCloneDefaultConfig()) + chatConfig.sanitizeConfig(cfg) + this.form = { + ...cfg, + roomId: this.form.roomId + } } } } diff --git a/frontend/src/views/Room.vue b/frontend/src/views/Room.vue index bba64d6..0bd747f 100644 --- a/frontend/src/views/Room.vue +++ b/frontend/src/views/Room.vue @@ -30,7 +30,7 @@ export default { }, data() { return { - config: { ...chatConfig.DEFAULT_CONFIG }, + config: chatConfig.deepCloneDefaultConfig(), chatClient: null, pronunciationConverter: null } @@ -76,7 +76,7 @@ export default { cfg[i] = this.strConfig[i] } } - cfg = mergeConfig(cfg, chatConfig.DEFAULT_CONFIG) + cfg = mergeConfig(cfg, chatConfig.deepCloneDefaultConfig()) cfg.minGiftPrice = toInt(cfg.minGiftPrice, chatConfig.DEFAULT_CONFIG.minGiftPrice) cfg.showDanmaku = toBool(cfg.showDanmaku) @@ -85,16 +85,30 @@ export default { cfg.mergeSimilarDanmaku = toBool(cfg.mergeSimilarDanmaku) cfg.mergeGift = toBool(cfg.mergeGift) cfg.maxNumber = toInt(cfg.maxNumber, chatConfig.DEFAULT_CONFIG.maxNumber) + cfg.blockGiftDanmaku = toBool(cfg.blockGiftDanmaku) cfg.blockLevel = toInt(cfg.blockLevel, chatConfig.DEFAULT_CONFIG.blockLevel) cfg.blockNewbie = toBool(cfg.blockNewbie) cfg.blockNotMobileVerified = toBool(cfg.blockNotMobileVerified) cfg.blockMedalLevel = toInt(cfg.blockMedalLevel, chatConfig.DEFAULT_CONFIG.blockMedalLevel) + cfg.relayMessagesByServer = toBool(cfg.relayMessagesByServer) cfg.autoTranslate = toBool(cfg.autoTranslate) + cfg.emoticons = this.toObjIfJson(cfg.emoticons) + chatConfig.sanitizeConfig(cfg) this.config = cfg }, + toObjIfJson(str) { + if (typeof str !== 'string') { + return str + } + try { + return JSON.parse(str) + } catch { + return {} + } + }, initChatClient() { if (this.roomId === null) { this.chatClient = new ChatClientTest() @@ -201,9 +215,7 @@ export default { this.$refs.renderer.addMessage(message) }, onDelSuperChat(data) { - for (let id of data.ids) { - this.$refs.renderer.delMessage(id) - } + this.$refs.renderer.delMessages(data.ids) }, onUpdateTranslation(data) { if (!this.config.autoTranslate) { @@ -224,19 +236,25 @@ export default { } else if (this.config.blockMedalLevel > 0 && data.medalLevel < this.config.blockMedalLevel) { return false } - return this.filterSuperChatMessage(data) + return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName) }, filterSuperChatMessage(data) { + return this.filterByContent(data.content) && this.filterByAuthorName(data.authorName) + }, + filterNewMemberMessage(data) { + return this.filterByAuthorName(data.authorName) + }, + filterByContent(content) { for (let keyword of this.blockKeywords) { - if (data.content.indexOf(keyword) !== -1) { + if (content.indexOf(keyword) !== -1) { return false } } - return this.filterNewMemberMessage(data) + return true }, - filterNewMemberMessage(data) { + filterByAuthorName(authorName) { for (let user of this.blockUsers) { - if (data.authorName === user) { + if (authorName === user) { return false } }