支持官方的文本表情

This commit is contained in:
John Smith 2023-08-27 00:23:52 +08:00
parent c3db0a41b3
commit 3d9095536d
6 changed files with 68 additions and 7 deletions
api
frontend
package.json
src
api/chat
views
services

View File

@ -63,6 +63,7 @@ def make_text_message_data(
translation: str = '',
content_type: int = ContentType.TEXT,
content_type_params: list = None,
text_emoticons: Iterable[Tuple[str, str]] = None
):
# 为了节省带宽用list而不是dict
return [
@ -96,6 +97,8 @@ def make_text_message_data(
content_type,
# 14: contentTypeParams
content_type_params if content_type_params is not None else [],
# 15: textEmoticons
text_emoticons if text_emoticons is not None else [],
]
@ -263,10 +266,15 @@ class ChatHandler(tornado.websocket.WebSocketHandler): # noqa
'translation': ''
}
self.send_cmd_data(Command.ADD_TEXT, text_data)
text_data[4] = 'te[dog]st'
text_data[11] = uuid.uuid4().hex
text_data[15] = [('[dog]', 'http://i0.hdslb.com/bfs/live/4428c84e694fbf4e0ef6c06e958d9352c3582740.png')]
self.send_cmd_data(Command.ADD_TEXT, text_data)
text_data[2] = '主播'
text_data[3] = 3
text_data[4] = "I can eat glass, it doesn't hurt me."
text_data[11] = uuid.uuid4().hex
text_data[15] = []
self.send_cmd_data(Command.ADD_TEXT, text_data)
self.send_cmd_data(Command.ADD_MEMBER, member_data)
self.send_cmd_data(Command.ADD_SUPER_CHAT, sc_data)

View File

@ -1,6 +1,6 @@
{
"name": "blivechat",
"version": "1.6.2",
"version": "1.7.0-beta",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",

View File

@ -322,6 +322,8 @@ export default class ChatClientDirect {
authorType = 0
}
let textEmoticons = this.parseTextEmoticons(info)
let data = {
avatarUrl: await avatar.getAvatarUrl(uid),
timestamp: info[0][4] / 1000,
@ -336,11 +338,26 @@ export default class ChatClientDirect {
medalLevel: roomId === this.roomId ? medalLevel : 0,
id: getUuid4Hex(),
translation: '',
emoticon: info[0][13].url || null
emoticon: info[0][13].url || null,
textEmoticons: textEmoticons,
}
this.onAddText(data)
}
parseTextEmoticons(info) {
try {
let modeInfo = info[0][15]
let extra = JSON.parse(modeInfo.extra)
if (!extra.emots) {
return []
}
let res = Object.values(extra.emots).map(emoticon => [emoticon.descript, emoticon.url])
return res
} catch {
return []
}
}
sendGiftCallback(command) {
if (!this.onAddGift) {
return

View File

@ -136,7 +136,8 @@ export default class ChatClientRelay {
medalLevel: data[10],
id: data[11],
translation: data[12],
emoticon: emoticon
emoticon: emoticon,
textEmoticons: data[15],
}
this.onAddText(data)
break

View File

@ -33,7 +33,8 @@ export default {
return {
config: chatConfig.deepCloneDefaultConfig(),
chatClient: null,
pronunciationConverter: null
pronunciationConverter: null,
textEmoticons: {}, //
}
},
computed: {
@ -64,6 +65,9 @@ export default {
res.set(emoticon.keyword, emoticon)
}
}
for (let emoticon of Object.values(this.textEmoticons)) {
res.set(emoticon.keyword, emoticon)
}
return res
}
},
@ -163,6 +167,15 @@ export default {
if (!this.config.showDanmaku || !this.filterTextMessage(data) || this.mergeSimilarText(data.content)) {
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 = {
id: data.id,
type: constants.MESSAGE_TYPE_TEXT,
@ -312,8 +325,8 @@ export default {
return richContent
}
//
if (this.config.emoticons.length === 0) {
//
if (this.config.emoticons.length === 0 && Object.keys(this.textEmoticons).length === 0) {
richContent.push({
type: constants.CONTENT_TYPE_TEXT,
text: data.content
@ -321,7 +334,7 @@ export default {
return richContent
}
//
//
let emoticonsTrie = this.emoticonsTrie
let startPos = 0
let pos = 0

View File

@ -2,6 +2,7 @@
import asyncio
import base64
import binascii
import json
import logging
import uuid
from typing import *
@ -231,6 +232,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
msg_type=info[0][9],
dm_type=info[0][12],
emoticon_options=info[0][13],
mode_info=info[0][15],
msg=info[1],
@ -328,6 +330,8 @@ class LiveMsgHandler(blivedm.BaseHandler):
content_type = api.chat.ContentType.TEXT
content_type_params = None
text_emoticons = self._parse_text_emoticons(message)
need_translate = self._need_translate(message.msg, room)
if need_translate:
translation = services.translate.get_translation_from_cache(message.msg)
@ -356,11 +360,29 @@ class LiveMsgHandler(blivedm.BaseHandler):
translation=translation,
content_type=content_type,
content_type_params=content_type_params,
text_emoticons=text_emoticons,
))
if need_translate:
await self._translate_and_response(message.msg, room.room_id, msg_id)
@staticmethod
def _parse_text_emoticons(message: blivedm.DanmakuMessage):
try:
extra = json.loads(message.mode_info['extra'])
# {"[dog]":{"emoticon_id":208,"emoji":"[dog]","descript":"[dog]","url":"http://i0.hdslb.com/bfs/live/4428c8
# 4e694fbf4e0ef6c06e958d9352c3582740.png","width":20,"height":20,"emoticon_unique":"emoji_208","count":1}}
emoticons = extra['emots']
if emoticons is None:
return []
res = [
(emoticon['descript'], emoticon['url'])
for emoticon in emoticons.values()
]
return res
except (json.JSONDecodeError, TypeError, KeyError):
return []
async def _on_gift(self, client: LiveClient, message: blivedm.GiftMessage):
avatar_url = services.avatar.process_avatar_url(message.face)
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)