前端初始化房间时从后端接口获取文本表情

This commit is contained in:
John Smith 2023-09-08 23:54:47 +08:00
parent 3fb1c41845
commit cad573312b
8 changed files with 42 additions and 37 deletions

View File

@ -378,13 +378,13 @@ class AvatarHandler(api.base.ApiHandler):
else: else:
# 缓存1天 # 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400') self.set_header('Cache-Control', 'private, max-age=86400')
self.write({ self.write({'avatarUrl': avatar_url})
'avatarUrl': avatar_url
})
class TextEmoticonMappingsHandler(api.base.ApiHandler): class TextEmoticonMappingsHandler(api.base.ApiHandler):
async def get(self): async def get(self):
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
cfg = config.get_config() cfg = config.get_config()
self.write({'textEmoticons': cfg.text_emoticons}) self.write({'textEmoticons': cfg.text_emoticons})

View File

@ -68,9 +68,7 @@ class UploadEmoticonHandler(api.base.ApiHandler):
url = await asyncio.get_running_loop().run_in_executor( url = await asyncio.get_running_loop().run_in_executor(
None, self._save_file, file.body, self.request.remote_ip None, self._save_file, file.body, self.request.remote_ip
) )
self.write({ self.write({'url': url})
'url': url
})
@staticmethod @staticmethod
def _save_file(body, client): def _save_file(body, client):

View File

@ -2,7 +2,7 @@ import axios from 'axios'
import { BrotliDecode } from './brotli_decode' import { BrotliDecode } from './brotli_decode'
import { getUuid4Hex } from '@/utils' import { getUuid4Hex } from '@/utils'
import * as avatar from '../avatar' import * as chat from '..'
const HEADER_SIZE = 16 const HEADER_SIZE = 16
@ -326,7 +326,7 @@ export default class ChatClientDirect {
let textEmoticons = this.parseTextEmoticons(info) let textEmoticons = this.parseTextEmoticons(info)
let data = { let data = {
avatarUrl: await avatar.getAvatarUrl(uid), avatarUrl: await chat.getAvatarUrl(uid),
timestamp: info[0][4] / 1000, timestamp: info[0][4] / 1000,
authorName: info[2][1], authorName: info[2][1],
authorType: authorType, authorType: authorType,
@ -370,7 +370,7 @@ export default class ChatClientDirect {
data = { data = {
id: getUuid4Hex(), id: getUuid4Hex(),
avatarUrl: avatar.processAvatarUrl(data.face), avatarUrl: chat.processAvatarUrl(data.face),
timestamp: data.timestamp, timestamp: data.timestamp,
authorName: data.uname, authorName: data.uname,
totalCoin: data.total_coin, totalCoin: data.total_coin,
@ -388,7 +388,7 @@ export default class ChatClientDirect {
let data = command.data let data = command.data
data = { data = {
id: getUuid4Hex(), id: getUuid4Hex(),
avatarUrl: await avatar.getAvatarUrl(data.uid), avatarUrl: await chat.getAvatarUrl(data.uid),
timestamp: data.start_time, timestamp: data.start_time,
authorName: data.username, authorName: data.username,
privilegeType: data.guard_level privilegeType: data.guard_level
@ -404,7 +404,7 @@ export default class ChatClientDirect {
let data = command.data let data = command.data
data = { data = {
id: data.id.toString(), id: data.id.toString(),
avatarUrl: avatar.processAvatarUrl(data.user_info.face), avatarUrl: chat.processAvatarUrl(data.user_info.face),
timestamp: data.start_time, timestamp: data.start_time,
authorName: data.user_info.uname, authorName: data.user_info.uname,
price: data.price, price: data.price,

View File

@ -1,6 +1,6 @@
import { getUuid4Hex } from '@/utils' import { getUuid4Hex } from '@/utils'
import * as constants from '@/components/ChatRenderer/constants' import * as constants from '@/components/ChatRenderer/constants'
import * as avatar from './avatar' import * as chat from '.'
const NAMES = [ const NAMES = [
'光羊', '光羊',
@ -104,7 +104,7 @@ const MESSAGE_GENERATORS = [
type: constants.MESSAGE_TYPE_TEXT, type: constants.MESSAGE_TYPE_TEXT,
message: { message: {
...randGuardInfo(), ...randGuardInfo(),
avatarUrl: avatar.DEFAULT_AVATAR_URL, avatarUrl: chat.DEFAULT_AVATAR_URL,
timestamp: new Date().getTime() / 1000, timestamp: new Date().getTime() / 1000,
authorName: randomChoose(NAMES), authorName: randomChoose(NAMES),
content: randomChoose(CONTENTS), content: randomChoose(CONTENTS),
@ -129,7 +129,7 @@ const MESSAGE_GENERATORS = [
type: constants.MESSAGE_TYPE_TEXT, type: constants.MESSAGE_TYPE_TEXT,
message: { message: {
...randGuardInfo(), ...randGuardInfo(),
avatarUrl: avatar.DEFAULT_AVATAR_URL, avatarUrl: chat.DEFAULT_AVATAR_URL,
timestamp: new Date().getTime() / 1000, timestamp: new Date().getTime() / 1000,
authorName: randomChoose(NAMES), authorName: randomChoose(NAMES),
content: '', content: '',
@ -155,7 +155,7 @@ const MESSAGE_GENERATORS = [
message: { message: {
...randomChoose(GIFT_INFO_LIST), ...randomChoose(GIFT_INFO_LIST),
id: getUuid4Hex(), id: getUuid4Hex(),
avatarUrl: avatar.DEFAULT_AVATAR_URL, avatarUrl: chat.DEFAULT_AVATAR_URL,
timestamp: new Date().getTime() / 1000, timestamp: new Date().getTime() / 1000,
authorName: randomChoose(NAMES), authorName: randomChoose(NAMES),
num: 1 num: 1
@ -171,7 +171,7 @@ const MESSAGE_GENERATORS = [
type: constants.MESSAGE_TYPE_SUPER_CHAT, type: constants.MESSAGE_TYPE_SUPER_CHAT,
message: { message: {
id: getUuid4Hex(), id: getUuid4Hex(),
avatarUrl: avatar.DEFAULT_AVATAR_URL, avatarUrl: chat.DEFAULT_AVATAR_URL,
timestamp: new Date().getTime() / 1000, timestamp: new Date().getTime() / 1000,
authorName: randomChoose(NAMES), authorName: randomChoose(NAMES),
price: randomChoose(SC_PRICES), price: randomChoose(SC_PRICES),
@ -189,7 +189,7 @@ const MESSAGE_GENERATORS = [
type: constants.MESSAGE_TYPE_MEMBER, type: constants.MESSAGE_TYPE_MEMBER,
message: { message: {
id: getUuid4Hex(), id: getUuid4Hex(),
avatarUrl: avatar.DEFAULT_AVATAR_URL, avatarUrl: chat.DEFAULT_AVATAR_URL,
timestamp: new Date().getTime() / 1000, timestamp: new Date().getTime() / 1000,
authorName: randomChoose(NAMES), authorName: randomChoose(NAMES),
privilegeType: randInt(1, 3) privilegeType: randInt(1, 3)

View File

@ -26,3 +26,13 @@ export async function getAvatarUrl(uid) {
} }
return res.avatarUrl return res.avatarUrl
} }
export async function getTextEmoticons() {
let res
try {
res = (await axios.get('/api/text_emoticon_mappings')).data
} catch {
return []
}
return res.textEmoticons
}

View File

@ -5,7 +5,7 @@
</template> </template>
<script> <script>
import * as avatar from '@/api/chat/avatar' import * as chat from '@/api/chat'
export default { export default {
name: 'ImgShadow', name: 'ImgShadow',
@ -26,8 +26,8 @@ export default {
}, },
methods: { methods: {
onLoadError() { onLoadError() {
if (this.showImgUrl !== avatar.DEFAULT_AVATAR_URL) { if (this.showImgUrl !== chat.DEFAULT_AVATAR_URL) {
this.showImgUrl = avatar.DEFAULT_AVATAR_URL this.showImgUrl = chat.DEFAULT_AVATAR_URL
} }
} }
} }

View File

@ -8,6 +8,7 @@ import { mergeConfig, toBool, toInt } from '@/utils'
import * as trie from '@/utils/trie' import * as trie from '@/utils/trie'
import * as pronunciation from '@/utils/pronunciation' import * as pronunciation from '@/utils/pronunciation'
import * as chatConfig from '@/api/chatConfig' import * as chatConfig from '@/api/chatConfig'
import * as chat from '@/api/chat'
import ChatClientTest from '@/api/chat/ChatClientTest' import ChatClientTest from '@/api/chat/ChatClientTest'
import ChatClientDirect from '@/api/chat/ChatClientDirect' import ChatClientDirect from '@/api/chat/ChatClientDirect'
import ChatClientRelay from '@/api/chat/ChatClientRelay' import ChatClientRelay from '@/api/chat/ChatClientRelay'
@ -34,7 +35,7 @@ export default {
config: chatConfig.deepCloneDefaultConfig(), config: chatConfig.deepCloneDefaultConfig(),
chatClient: null, chatClient: null,
pronunciationConverter: null, pronunciationConverter: null,
textEmoticons: {}, // textEmoticons: [], //
} }
}, },
computed: { computed: {
@ -60,20 +61,20 @@ export default {
}, },
emoticonsTrie() { emoticonsTrie() {
let res = new trie.Trie() let res = new trie.Trie()
for (let emoticon of this.config.emoticons) { for (let emoticons of [this.config.emoticons, this.textEmoticons]) {
if (emoticon.keyword !== '' && emoticon.url !== '') { for (let emoticon of emoticons) {
res.set(emoticon.keyword, emoticon) if (emoticon.keyword !== '' && emoticon.url !== '') {
res.set(emoticon.keyword, emoticon)
}
} }
} }
for (let emoticon of Object.values(this.textEmoticons)) {
res.set(emoticon.keyword, emoticon)
}
return res return res
} }
}, },
mounted() { mounted() {
this.initConfig() this.initConfig()
this.initChatClient() this.initChatClient()
this.initTextEmoticons()
if (this.config.giftUsernamePronunciation !== '') { if (this.config.giftUsernamePronunciation !== '') {
this.pronunciationConverter = new pronunciation.PronunciationConverter() this.pronunciationConverter = new pronunciation.PronunciationConverter()
this.pronunciationConverter.loadDict(this.config.giftUsernamePronunciation) this.pronunciationConverter.loadDict(this.config.giftUsernamePronunciation)
@ -155,6 +156,9 @@ export default {
this.chatClient.onUpdateTranslation = this.onUpdateTranslation this.chatClient.onUpdateTranslation = this.onUpdateTranslation
this.chatClient.start() this.chatClient.start()
}, },
async initTextEmoticons() {
this.textEmoticons = await chat.getTextEmoticons()
},
start() { start() {
this.chatClient.start() this.chatClient.start()
@ -167,15 +171,6 @@ export default {
if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilarText(data.content)) { if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilarText(data.content)) {
return return
} }
//
for (let [keyword, url] of data.textEmoticons) {
if (!(keyword in this.textEmoticons)) {
let emoticon = { keyword, url }
this.$set(this.textEmoticons, keyword, emoticon)
}
}
let message = { let message = {
id: data.id, id: data.id,
type: constants.MESSAGE_TYPE_TEXT, type: constants.MESSAGE_TYPE_TEXT,
@ -326,7 +321,7 @@ export default {
} }
// //
if (this.config.emoticons.length === 0 && Object.keys(this.textEmoticons).length === 0) { if (this.config.emoticons.length === 0 && this.textEmoticons.length === 0) {
richContent.push({ richContent.push({
type: constants.CONTENT_TYPE_TEXT, type: constants.CONTENT_TYPE_TEXT,
text: data.content text: data.content

View File

@ -666,3 +666,5 @@ class LiveMsgHandler(blivedm.BaseHandler):
translation translation
) )
) )
# TODO 开放平台消息处理