兼容开放平台接口升级,UID改成Open ID

This commit is contained in:
John Smith 2024-03-17 22:11:19 +08:00
parent 1cbdd4a2bf
commit da3e27c8cb
10 changed files with 45 additions and 55 deletions

View File

@ -70,7 +70,7 @@ def make_text_message_data(
translation: str = '',
content_type: int = ContentType.TEXT,
content_type_params: list = None,
uid: int = 0,
uid: str = '',
medal_name: str = '',
):
# 为了节省带宽用list而不是dict
@ -375,16 +375,16 @@ class RoomInfoHandler(api.base.ApiHandler):
class AvatarHandler(api.base.ApiHandler):
async def get(self):
# uid基本是0了现在这个接口唯一的作用是算用户名MD5其实可以放到前端
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.get_default_avatar_url(uid, username)
# 缓存3分钟
self.set_header('Cache-Control', 'private, max-age=180')
cache_time = 86400 if uid == 0 else 180
else:
# 缓存1天
self.set_header('Cache-Control', 'private, max-age=86400')
cache_time = 86400
self.set_header('Cache-Control', f'private, max-age={cache_time}')
self.write({'avatarUrl': avatar_url})

View File

@ -199,10 +199,10 @@ class PluginWsHandler(_PluginApiHandlerBase, tornado.websocket.WebSocketHandler)
author_name = str(data['authorName'])
if author_name == '':
author_name = self.plugin.id
uid = int(data['uid'])
uid = str(data['uid'])
avatar_url = str(data['avatarUrl'])
if avatar_url == '':
avatar_url = services.avatar.get_default_avatar_url(uid, author_name)
avatar_url = services.avatar.get_default_avatar_url(username=author_name)
data_to_send = api.chat.make_text_message_data(
content=str(data['content']),

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
__version__ = '0.0.1' # TODO UID改成open ID后再改成正式版
__version__ = '0.0.1' # TODO 上舰消息加上price后再改成正式版
from .handlers import *
from .client import *

View File

@ -245,7 +245,7 @@ async def send_text(
content: str,
author_name: str = '',
*,
uid: int = 0,
uid: str = '',
avatar_url: str = '',
author_type: int = models.AuthorType.NORMAL.value,
guard_level: int = models.GuardLevel.NONE.value,
@ -260,7 +260,7 @@ async def send_text(
:param content: 内容
:param author_name: 用户名默认为当前插件ID
:param uid: 用户ID
:param uid: 用户Open ID或ID
:param avatar_url: 用户头像URL默认自动生成
:param author_type: 用户类型见AuthorType
:param guard_level: 舰队等级见GuardLevel

View File

@ -217,8 +217,8 @@ class AddTextMsg:
"""内容类型见ContentType"""
content_type_params: Union[dict, list] = dataclasses.field(default_factory=dict)
"""跟内容类型相关的参数"""
uid: int = 0
"""用户ID"""
uid: str = ''
"""用户Open ID或ID"""
medal_name: str = ''
"""勋章名"""
@ -274,8 +274,8 @@ class AddGiftMsg:
"""礼物ID"""
gift_icon_url: str = ''
"""礼物图标URL"""
uid: int = 0
"""用户ID"""
uid: str = ''
"""用户Open ID或ID"""
privilege_type: int = GuardLevel.NONE.value
"""舰队等级见GuardLevel"""
medal_level: int = 0
@ -321,8 +321,8 @@ class AddMemberMsg:
"""数量"""
unit: str = ''
"""单位(月)"""
uid: int = 0
"""用户ID"""
uid: str = ''
"""用户Open ID或ID"""
medal_level: int = 0
"""勋章等级如果没戴当前房间勋章则为0"""
medal_name: str = ''
@ -362,8 +362,8 @@ class AddSuperChatMsg:
"""内容"""
translation: str = ''
"""内容翻译"""
uid: int = 0
"""用户ID"""
uid: str = ''
"""用户Open ID或ID"""
privilege_type: int = GuardLevel.NONE.value
"""舰队等级见GuardLevel"""
medal_level: int = 0

@ -1 +1 @@
Subproject commit fc55b75dab95ca65700f26a145fc76d7ef05eef1
Subproject commit 60ce01927a3d17ecc069d8f5f8ed62ffc0075e31

View File

@ -15,7 +15,7 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase {
this.roomOwnerAuthCode = roomOwnerAuthCode
// 调用initRoom后初始化
this.roomOwnerUid = null
this.roomOwnerOpenId = null
this.hostServerUrlList = []
this.authBody = null
this.gameId = null
@ -82,7 +82,7 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase {
this.hostServerUrlList = websocketInfo.wss_link
let anchorInfo = data.anchor_info
// this.roomId = anchorInfo.room_id
this.roomOwnerUid = anchorInfo.uid
this.roomOwnerOpenId = anchorInfo.open_id
return true
}
@ -178,7 +178,7 @@ export default class ChatClientDirectOpenLive extends ChatClientOfficialBase {
let data = command.data
let authorType
if (data.uid === this.roomOwnerUid) {
if (data.open_id === this.roomOwnerOpenId) {
authorType = 3
} else if (data.guard_level !== 0) {
authorType = 1

View File

@ -74,7 +74,7 @@ class MsgHandler(blcsdk.BaseHandler):
return
room = _get_or_add_room(extra.room_key, extra.room_id)
room.add_gift(GiftRecord(
uid=str(message.uid), # TODO SDK的uid改成Open ID
uid=message.uid,
author_name=message.author_name,
gift_name=message.gift_name,
num=message.num,
@ -104,7 +104,7 @@ class MsgHandler(blcsdk.BaseHandler):
guard_name += f'{message.unit}'
room.add_gift(GiftRecord(
uid=str(message.uid), # TODO SDK的uid改成Open ID
uid=message.uid,
author_name=message.author_name,
gift_name=guard_name,
num=message.num,
@ -118,7 +118,7 @@ class MsgHandler(blcsdk.BaseHandler):
return
room = _get_or_add_room(extra.room_key, extra.room_id)
room.add_super_chat(SuperChatRecord(
uid=str(message.uid), # TODO SDK的uid改成Open ID
uid=message.uid,
author_name=message.author_name,
price=message.price,
content=message.content,

View File

@ -56,14 +56,14 @@ async def _do_init():
_avatar_fetchers = fetchers
async def get_avatar_url(user_id, username) -> str:
async def get_avatar_url(user_id: int, username) -> str:
avatar_url = await get_avatar_url_or_none(user_id)
if avatar_url is None:
avatar_url = get_default_avatar_url(user_id, username)
return avatar_url
def get_default_avatar_url(user_id=0, username=''):
def get_default_avatar_url(user_id: int = 0, username=''):
if user_id != 0:
str_to_hash = str(user_id)
elif username != '':
@ -74,7 +74,7 @@ def get_default_avatar_url(user_id=0, username=''):
return f'//cravatar.cn/avatar/{id_hash}?s=256&d=robohash&f=y'
async def get_avatar_url_or_none(user_id) -> Optional[str]:
async def get_avatar_url_or_none(user_id: int) -> Optional[str]:
if user_id == 0:
return None
@ -109,14 +109,14 @@ async def _refresh_avatar_cache_from_web(user_id):
update_avatar_cache(user_id, avatar_url)
def update_avatar_cache(user_id, avatar_url):
def update_avatar_cache(user_id: int, avatar_url):
if user_id == 0:
return
_update_avatar_cache_in_memory(user_id, avatar_url)
_update_avatar_cache_in_database(user_id, avatar_url)
def update_avatar_cache_if_expired(user_id, avatar_url):
def update_avatar_cache_if_expired(user_id: int, avatar_url):
# 内存缓存过期了才更新,减少写入数据库的频率
if _get_avatar_url_from_memory(user_id) is None:
update_avatar_cache(user_id, avatar_url)

View File

@ -548,7 +548,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
content_type=content_type,
content_type_params=content_type_params,
# 给插件用的字段
uid=message.uid,
uid=str(message.uid) if message.uid != 0 else message.uname,
medal_name='' if message.medal_room_id != client.room_id else message.medal_name,
)
room.send_cmd_data(api.chat.Command.ADD_TEXT, data)
@ -580,7 +580,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
# 给插件用的字段
'giftId': message.gift_id,
'giftIconUrl': '',
'uid': message.uid,
'uid': str(message.uid) if message.uid != 0 else message.uname,
'privilegeType': message.guard_level,
'medalLevel': 0,
'medalName': '',
@ -611,7 +611,8 @@ class LiveMsgHandler(blivedm.BaseHandler):
# 给插件用的字段
'num': message.num,
'unit': '', # 单位在USER_TOAST_MSG消息里不想改消息。现在没有别的单位web接口也很少有人用了先写死吧
'uid': message.uid,
# TODO price
'uid': str(message.uid) if message.uid != 0 else message.username,
'medalLevel': 0,
'medalName': '',
}
@ -649,7 +650,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
'content': message.message,
'translation': translation,
# 给插件用的字段
'uid': message.uid,
'uid': str(message.uid) if message.uid != 0 else message.uname,
'privilegeType': message.guard_level,
'medalLevel': 0,
'medalName': '',
@ -713,14 +714,11 @@ class LiveMsgHandler(blivedm.BaseHandler):
#
def _on_open_live_danmaku(self, client: OpenLiveClient, message: dm_open_models.DanmakuMessage):
avatar_url = message.uface
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
room = client_room_manager.get_room(client.room_key)
if room is None:
return
if message.uid == client.room_owner_uid:
if message.open_id == client.room_owner_open_id:
author_type = 3 # 主播
elif message.guard_level != 0: # 1总督2提督3舰长
author_type = 1 # 舰队
@ -748,7 +746,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
translation = ''
data = api.chat.make_text_message_data(
avatar_url=avatar_url,
avatar_url=services.avatar.process_avatar_url(message.uface),
timestamp=message.timestamp,
author_name=message.uname,
author_type=author_type,
@ -760,7 +758,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
content_type=content_type,
content_type_params=content_type_params,
# 给插件用的字段
uid=message.uid,
uid=message.open_id,
medal_name='' if not message.fans_medal_wearing_status else message.fans_medal_name,
)
room.send_cmd_data(api.chat.Command.ADD_TEXT, data)
@ -774,9 +772,6 @@ class LiveMsgHandler(blivedm.BaseHandler):
))
def _on_open_live_gift(self, client: OpenLiveClient, message: dm_open_models.GiftMessage):
avatar_url = services.avatar.process_avatar_url(message.uface)
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
room = client_room_manager.get_room(client.room_key)
if room is None:
return
@ -784,7 +779,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
total_coin = message.price * message.gift_num
data = {
'id': message.msg_id,
'avatarUrl': avatar_url,
'avatarUrl': services.avatar.process_avatar_url(message.uface),
'timestamp': message.timestamp,
'authorName': message.uname,
'totalCoin': 0 if not message.paid else total_coin,
@ -794,7 +789,7 @@ class LiveMsgHandler(blivedm.BaseHandler):
# 给插件用的字段
'giftId': message.gift_id,
'giftIconUrl': message.gift_icon,
'uid': message.uid,
'uid': message.open_id,
'privilegeType': message.guard_level,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,
@ -805,23 +800,21 @@ class LiveMsgHandler(blivedm.BaseHandler):
)
def _on_open_live_buy_guard(self, client: OpenLiveClient, message: dm_open_models.GuardBuyMessage):
avatar_url = message.user_info.uface
services.avatar.update_avatar_cache_if_expired(message.user_info.uid, avatar_url)
room = client_room_manager.get_room(client.room_key)
if room is None:
return
data = {
'id': message.msg_id,
'avatarUrl': avatar_url,
'avatarUrl': services.avatar.process_avatar_url(message.user_info.uface),
'timestamp': message.timestamp,
'authorName': message.user_info.uname,
'privilegeType': message.guard_level,
# 给插件用的字段
'num': message.guard_num,
'unit': message.guard_unit,
'uid': message.user_info.uid,
# TODO price
'uid': message.user_info.open_id,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,
}
@ -831,9 +824,6 @@ class LiveMsgHandler(blivedm.BaseHandler):
)
def _on_open_live_super_chat(self, client: OpenLiveClient, message: dm_open_models.SuperChatMessage):
avatar_url = services.avatar.process_avatar_url(message.uface)
services.avatar.update_avatar_cache_if_expired(message.uid, avatar_url)
room = client_room_manager.get_room(client.room_key)
if room is None:
return
@ -852,14 +842,14 @@ class LiveMsgHandler(blivedm.BaseHandler):
msg_id = str(message.message_id)
data = {
'id': msg_id,
'avatarUrl': avatar_url,
'avatarUrl': services.avatar.process_avatar_url(message.uface),
'timestamp': message.start_time,
'authorName': message.uname,
'price': message.rmb,
'content': message.message,
'translation': translation,
# 给插件用的字段
'uid': message.uid,
'uid': message.open_id,
'privilegeType': message.guard_level,
'medalLevel': 0 if not message.fans_medal_wearing_status else message.fans_medal_level,
'medalName': '' if not message.fans_medal_wearing_status else message.fans_medal_name,