11"""添加订阅工具"""
22
3- from typing import Optional , Type , List
3+ from typing import List , Optional , Type
44
55from pydantic import BaseModel , Field
66
77from app .agent .tools .base import MoviePilotTool
88from app .chain .subscribe import SubscribeChain
9+ from app .db .user_oper import UserOper
910from app .log import logger
10- from app .schemas .types import MediaType
11+ from app .schemas .types import MediaType , MessageChannel
1112
1213
1314class AddSubscribeInput (BaseModel ):
@@ -101,6 +102,36 @@ def get_tool_message(self, **kwargs) -> Optional[str]:
101102
102103 return message
103104
105+ async def _resolve_subscribe_username (self ) -> Optional [str ]:
106+ """优先映射为系统用户名,未绑定时回退当前渠道用户名。"""
107+ resolved_username = self ._username
108+ if not self ._channel or not self ._user_id :
109+ return resolved_username
110+
111+ try :
112+ channel = MessageChannel (self ._channel )
113+ except ValueError :
114+ return resolved_username
115+
116+ binding_keys = {
117+ MessageChannel .Telegram : ("telegram_userid" ,),
118+ MessageChannel .Discord : ("discord_userid" ,),
119+ MessageChannel .Wechat : ("wechat_userid" ,),
120+ MessageChannel .Slack : ("slack_userid" ,),
121+ MessageChannel .VoceChat : ("vocechat_userid" ,),
122+ MessageChannel .SynologyChat : ("synologychat_userid" ,),
123+ MessageChannel .QQ : ("qq_userid" , "qq_openid" ),
124+ }.get (channel )
125+ if not binding_keys :
126+ return resolved_username
127+
128+ mapped_username = await self .run_blocking (
129+ "db" ,
130+ UserOper ().get_name ,
131+ ** {key : self ._user_id for key in binding_keys },
132+ )
133+ return mapped_username or resolved_username
134+
104135 async def run (
105136 self ,
106137 title : str ,
@@ -137,6 +168,7 @@ async def run(
137168 if media_type_enum == MediaType .TV
138169 else None
139170 )
171+ subscribe_username = await self ._resolve_subscribe_username ()
140172
141173 # 构建额外的订阅参数
142174 subscribe_kwargs = {}
@@ -162,7 +194,7 @@ async def run(
162194 tmdbid = tmdb_id ,
163195 doubanid = douban_id ,
164196 season = season ,
165- username = self . _user_id ,
197+ username = subscribe_username ,
166198 ** subscribe_kwargs ,
167199 )
168200 if sid :
0 commit comments