From dedae6f0837a8533b22cd61644db8bd7f62102b5 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 26 Dec 2021 23:34:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E8=BD=AC=E5=8F=91=E6=94=AF=E6=8C=81=E8=A1=A8=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/chat.py | 118 +++++++++++------- frontend/src/api/chat/ChatClientRelay.js | 13 +- frontend/src/api/chat/ChatClientTest.js | 1 + .../components/ChatRenderer/TextMessage.vue | 2 +- frontend/src/layout/index.vue | 2 +- frontend/src/views/StyleGenerator/Legacy.vue | 2 +- .../src/views/StyleGenerator/LineLike.vue | 2 +- 7 files changed, 92 insertions(+), 48 deletions(-) diff --git a/api/chat.py b/api/chat.py index d923722..a478a0b 100644 --- a/api/chat.py +++ b/api/chat.py @@ -33,6 +33,11 @@ class Command(enum.IntEnum): UPDATE_TRANSLATION = 7 +class ContentType(enum.IntEnum): + TEXT = 0 + EMOTICON = 1 + + _http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) room_manager: Optional['RoomManager'] = None @@ -56,10 +61,11 @@ class Room(blivedm.BLiveClient, blivedm.BaseHandler): medal_level = 0 medal_room_id = 0 - # TODO 带表情信息 message = blivedm.DanmakuMessage( timestamp=info[0][4], msg_type=info[0][9], + dm_type=info[0][12], + emoticon_options=info[0][13], msg=info[1], @@ -159,6 +165,15 @@ class Room(blivedm.BLiveClient, blivedm.BaseHandler): else: author_type = 0 + if message.dm_type == 1: + content_type = ContentType.EMOTICON + content_type_params = make_emoticon_params( + message.emoticon_options_dict['url'], + ) + else: + content_type = ContentType.TEXT + content_type_params = None + need_translate = self._need_translate(message.msg) if need_translate: translation = models.translate.get_translation_from_cache(message.msg) @@ -173,19 +188,21 @@ class Room(blivedm.BLiveClient, blivedm.BaseHandler): id_ = uuid.uuid4().hex # 为了节省带宽用list而不是dict self.send_message(Command.ADD_TEXT, make_text_message( - await models.avatar.get_avatar_url(message.uid), - int(message.timestamp / 1000), - message.uname, - author_type, - message.msg, - message.privilege_type, - message.msg_type, - message.user_level, - message.urank < 10000, - message.mobile_verify, - 0 if message.medal_room_id != self.room_id else message.medal_level, - id_, - translation + avatar_url=await models.avatar.get_avatar_url(message.uid), + timestamp=int(message.timestamp / 1000), + author_name=message.uname, + author_type=author_type, + content=message.msg, + privilege_type=message.privilege_type, + is_gift_danmaku=message.msg_type, + author_level=message.user_level, + is_newbie=message.urank < 10000, + is_mobile_verified=message.mobile_verify, + medal_level=0 if message.medal_room_id != self.room_id else message.medal_level, + id_=id_, + translation=translation, + content_type=content_type, + content_type_params=content_type_params, )) if need_translate: @@ -278,7 +295,7 @@ class Room(blivedm.BLiveClient, blivedm.BaseHandler): def make_text_message(avatar_url, timestamp, author_name, author_type, content, privilege_type, is_gift_danmaku, author_level, is_newbie, is_mobile_verified, medal_level, - id_, translation): + id_, translation, content_type, content_type_params): return [ # 0: avatarUrl avatar_url, @@ -305,7 +322,18 @@ def make_text_message(avatar_url, timestamp, author_name, author_type, content, # 11: id id_, # 12: translation - translation + translation, + # 13: contentType + content_type, + # 14: contentTypeParams + content_type_params if content_type_params is not None else [], + ] + + +def make_emoticon_params(url): + return [ + # 0: url + url, ] @@ -314,7 +342,7 @@ def make_translation_message(msg_id, translation): # 0: id msg_id, # 1: translation - translation + translation, ] @@ -485,19 +513,21 @@ class ChatHandler(tornado.websocket.WebSocketHandler): cfg = config.get_config() if cfg.allow_translate_rooms and self.room_id not in cfg.allow_translate_rooms: self.send_message(Command.ADD_TEXT, make_text_message( - models.avatar.DEFAULT_AVATAR_URL, - int(time.time()), - 'blivechat', - 2, - 'Translation is not allowed in this room. Please download to use translation', - 0, - False, - 60, - False, - True, - 0, - uuid.uuid4().hex, - '' + avatar_url=models.avatar.DEFAULT_AVATAR_URL, + timestamp=int(time.time()), + author_name='blivechat', + author_type=2, + content='Translation is not allowed in this room. Please download to use translation', + privilege_type=0, + is_gift_danmaku=False, + author_level=60, + is_newbie=False, + is_mobile_verified=True, + medal_level=0, + id_=uuid.uuid4().hex, + translation='', + content_type=ContentType.TEXT, + content_type_params=None, )) # 测试用 @@ -508,19 +538,21 @@ class ChatHandler(tornado.websocket.WebSocketHandler): 'authorName': 'xfgryujk', } text_data = make_text_message( - base_data['avatarUrl'], - base_data['timestamp'], - base_data['authorName'], - 0, - '我能吞下玻璃而不伤身体', - 0, - False, - 20, - False, - True, - 0, - uuid.uuid4().hex, - '' + avatar_url=base_data['avatarUrl'], + timestamp=base_data['timestamp'], + author_name=base_data['authorName'], + author_type=0, + content='我能吞下玻璃而不伤身体', + privilege_type=0, + is_gift_danmaku=False, + author_level=20, + is_newbie=False, + is_mobile_verified=True, + medal_level=0, + id_=uuid.uuid4().hex, + translation='', + content_type=ContentType.TEXT, + content_type_params=None, ) member_data = { **base_data, diff --git a/frontend/src/api/chat/ChatClientRelay.js b/frontend/src/api/chat/ChatClientRelay.js index a027d4a..401a350 100644 --- a/frontend/src/api/chat/ChatClientRelay.js +++ b/frontend/src/api/chat/ChatClientRelay.js @@ -7,6 +7,9 @@ const COMMAND_ADD_SUPER_CHAT = 5 const COMMAND_DEL_SUPER_CHAT = 6 const COMMAND_UPDATE_TRANSLATION = 7 +// const CONTENT_TYPE_TEXT = 0 +const CONTENT_TYPE_EMOTICON = 1 + const HEARTBEAT_INTERVAL = 10 * 1000 const RECEIVE_TIMEOUT = HEARTBEAT_INTERVAL + 5 * 1000 @@ -122,6 +125,14 @@ export default class ChatClientRelay { if (!this.onAddText) { break } + + let emoticon = null + let contentType = data[13] + let contentTypeParams = data[14] + if (contentType === CONTENT_TYPE_EMOTICON) { + emoticon = contentTypeParams[0] // TODO 改成对象? + } + data = { avatarUrl: data[0], timestamp: data[1], @@ -136,7 +147,7 @@ export default class ChatClientRelay { medalLevel: data[10], id: data[11], translation: data[12], - emoticon: null // TODO 支持表情 + emoticon: emoticon } this.onAddText(data) break diff --git a/frontend/src/api/chat/ChatClientTest.js b/frontend/src/api/chat/ChatClientTest.js index 5853781..fa73553 100644 --- a/frontend/src/api/chat/ChatClientTest.js +++ b/frontend/src/api/chat/ChatClientTest.js @@ -19,6 +19,7 @@ const CONTENTS = [ '有一说一,这件事大家懂的都懂,不懂的,说了你也不明白,不如不说', '让我看看', '我柜子动了,我不玩了' ] + // TODO 改成对象? const EMOTICONS = [ '/static/img/emoticons/233.png', '/static/img/emoticons/miaoa.png', diff --git a/frontend/src/components/ChatRenderer/TextMessage.vue b/frontend/src/components/ChatRenderer/TextMessage.vue index 095884e..35647e7 100644 --- a/frontend/src/components/ChatRenderer/TextMessage.vue +++ b/frontend/src/components/ChatRenderer/TextMessage.vue @@ -19,7 +19,7 @@ -
- v1.5.3 + v1.6.0-beta
diff --git a/frontend/src/views/StyleGenerator/Legacy.vue b/frontend/src/views/StyleGenerator/Legacy.vue index 6acec7b..34ac45b 100644 --- a/frontend/src/views/StyleGenerator/Legacy.vue +++ b/frontend/src/views/StyleGenerator/Legacy.vue @@ -551,7 +551,7 @@ ${!this.form.messageOnNewLine ? '' : `yt-live-chat-text-message-renderer #messag overflow: visible !important; }`} -yt-live-chat-text-message-renderer #message img { +yt-live-chat-text-message-renderer #message .emoji { width: auto !important; height: ${this.form.emoticonSize}px !important; }` diff --git a/frontend/src/views/StyleGenerator/LineLike.vue b/frontend/src/views/StyleGenerator/LineLike.vue index 898350c..49cac97 100644 --- a/frontend/src/views/StyleGenerator/LineLike.vue +++ b/frontend/src/views/StyleGenerator/LineLike.vue @@ -467,7 +467,7 @@ yt-live-chat-text-message-renderer #message { border-radius: 30px; } -yt-live-chat-text-message-renderer #message img { +yt-live-chat-text-message-renderer #message .emoji { width: auto !important; height: ${this.form.emoticonSize}px !important; }