From ef7f0dc0acc9b24f17526bed2ec75be9e033f07b Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 16 Sep 2023 11:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=A4=B4=E5=83=8F=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E5=B0=9D=E8=AF=95=E7=94=A8=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E9=9A=8F=E6=9C=BA=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/chat.py | 5 +++-- frontend/src/api/chat/ChatClientDirectWeb.js | 7 ++++--- frontend/src/api/chat/index.js | 5 +++-- services/avatar.py | 16 ++++++++++++++-- services/chat.py | 4 ++-- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/api/chat.py b/api/chat.py index 06a53c0..78f54c6 100644 --- a/api/chat.py +++ b/api/chat.py @@ -266,7 +266,7 @@ class ChatHandler(tornado.websocket.WebSocketHandler): # 测试用 async def _send_test_message(self): base_data = { - 'avatarUrl': await services.avatar.get_avatar_url(300474), + 'avatarUrl': await services.avatar.get_avatar_url(300474, 'xfgryujk'), 'timestamp': int(time.time()), 'authorName': 'xfgryujk', } @@ -372,9 +372,10 @@ class RoomInfoHandler(api.base.ApiHandler): class AvatarHandler(api.base.ApiHandler): async def get(self): uid = int(self.get_query_argument('uid')) + username = self.get_query_argument('username', '') avatar_url = await services.avatar.get_avatar_url_or_none(uid) if avatar_url is None: - avatar_url = services.avatar.DEFAULT_AVATAR_URL + avatar_url = services.avatar.get_default_avatar_url(uid, username) # 缓存3分钟 self.set_header('Cache-Control', 'private, max-age=180') else: diff --git a/frontend/src/api/chat/ChatClientDirectWeb.js b/frontend/src/api/chat/ChatClientDirectWeb.js index 3ffc0d0..1bfc20a 100644 --- a/frontend/src/api/chat/ChatClientDirectWeb.js +++ b/frontend/src/api/chat/ChatClientDirectWeb.js @@ -89,10 +89,11 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase { authorType = 0 } + let authorName = info[2][1] let data = { - avatarUrl: await chat.getAvatarUrl(uid), + avatarUrl: await chat.getAvatarUrl(uid, authorName), timestamp: info[0][4] / 1000, - authorName: info[2][1], + authorName: authorName, authorType: authorType, content: info[1], privilegeType: privilegeType, @@ -137,7 +138,7 @@ export default class ChatClientDirectWeb extends ChatClientOfficialBase { let data = command.data data = { id: getUuid4Hex(), - avatarUrl: await chat.getAvatarUrl(data.uid), + avatarUrl: await chat.getAvatarUrl(data.uid, data.username), timestamp: data.start_time, authorName: data.username, privilegeType: data.guard_level diff --git a/frontend/src/api/chat/index.js b/frontend/src/api/chat/index.js index b26d864..ae1a352 100644 --- a/frontend/src/api/chat/index.js +++ b/frontend/src/api/chat/index.js @@ -20,11 +20,12 @@ export function processAvatarUrl(avatarUrl) { return avatarUrl } -export async function getAvatarUrl(uid) { +export async function getAvatarUrl(uid, username) { let res try { res = (await axios.get('/api/avatar_url', { params: { - uid: uid + uid: uid, + username: username } })).data } catch { return DEFAULT_AVATAR_URL diff --git a/services/avatar.py b/services/avatar.py index ea10fbe..e727f14 100644 --- a/services/avatar.py +++ b/services/avatar.py @@ -2,6 +2,7 @@ import asyncio import dataclasses import datetime +import hashlib import logging import re from typing import * @@ -54,13 +55,24 @@ async def _do_init(): _avatar_fetchers = fetchers -async def get_avatar_url(user_id) -> str: +async def get_avatar_url(user_id, username) -> str: avatar_url = await get_avatar_url_or_none(user_id) if avatar_url is None: - avatar_url = DEFAULT_AVATAR_URL + avatar_url = get_default_avatar_url(user_id, username) return avatar_url +def get_default_avatar_url(user_id=0, username=''): + if user_id != 0: + str_to_hash = str(user_id) + elif username != '': + str_to_hash = username + else: + return DEFAULT_AVATAR_URL + id_hash = hashlib.md5(str_to_hash.encode('utf-8')).hexdigest() + return f'//cravatar.cn/avatar/{id_hash}?s=256&d=robohash&f=y' + + async def get_avatar_url_or_none(user_id) -> Optional[str]: if user_id == 0: return None diff --git a/services/chat.py b/services/chat.py index 0f99f03..956a686 100644 --- a/services/chat.py +++ b/services/chat.py @@ -394,7 +394,7 @@ class LiveMsgHandler(blivedm.BaseHandler): services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url) else: # 先异步调用再获取房间,因为返回时房间可能已经不存在了 - avatar_url = await services.avatar.get_avatar_url(message.uid) + avatar_url = await services.avatar.get_avatar_url(message.uid, message.uname) room = client_room_manager.get_room(client.room_key) if room is None: @@ -481,7 +481,7 @@ class LiveMsgHandler(blivedm.BaseHandler): @staticmethod async def __on_buy_guard(client: WebLiveClient, message: dm_web_models.GuardBuyMessage): # 先异步调用再获取房间,因为返回时房间可能已经不存在了 - avatar_url = await services.avatar.get_avatar_url(message.uid) + avatar_url = await services.avatar.get_avatar_url(message.uid, message.username) room = client_room_manager.get_room(client.room_key) if room is None: