测试下改会话
This commit is contained in:
@@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, cast
|
||||
|
||||
import aiohttp
|
||||
from astrbot.api import logger
|
||||
from astrbot.api.star import StarTools
|
||||
from astrbot.api.star import Context, StarTools
|
||||
from astrbot.core.message.message_event_result import MessageChain
|
||||
|
||||
from ..config import ConfigManager
|
||||
@@ -24,10 +24,12 @@ class SubscriptionService:
|
||||
service: "BangumiService",
|
||||
config_manager: ConfigManager,
|
||||
session: aiohttp.ClientSession | None = None,
|
||||
context: Context | None = None,
|
||||
) -> None:
|
||||
self.storage = repository
|
||||
self.service = service
|
||||
self.config_manager = config_manager
|
||||
self.context = context
|
||||
self.renderer = EpisodeRenderer(session=session)
|
||||
|
||||
async def get_subscribe_candidates(
|
||||
@@ -126,7 +128,7 @@ class SubscriptionService:
|
||||
return "🔍 未找到相关番剧", None
|
||||
return await self._build_subscribable_subject(candidates[0]["subject_id"])
|
||||
|
||||
async def subscribe_by_subject_id(self, group_id: str, subject_id: str) -> str:
|
||||
async def subscribe_by_subject_id(self, session_id: str, subject_id: str) -> str:
|
||||
"""
|
||||
基于明确 subject_id 完成订阅。
|
||||
"""
|
||||
@@ -138,7 +140,7 @@ class SubscriptionService:
|
||||
return "❌ 未知错误:未能获取番剧信息"
|
||||
|
||||
success = self.storage.subscribe_subject(
|
||||
group_id=group_id,
|
||||
group_id=session_id,
|
||||
subject_id=subject_info["subject_id"],
|
||||
name=subject_info["name"],
|
||||
air_date=subject_info["air_date"],
|
||||
@@ -146,18 +148,18 @@ class SubscriptionService:
|
||||
)
|
||||
if success:
|
||||
return (
|
||||
f"✅ 成功订阅《{subject_info['name']}》!\n如有更新将推送到本群。"
|
||||
f"✅ 成功订阅《{subject_info['name']}》!\n如有更新将推送到本会话。"
|
||||
)
|
||||
return "❌ 订阅失败,数据库错误。"
|
||||
except (BangumiApiError, DatabaseError, SubscriptionError) as e:
|
||||
logger.error(f"SubscriptionService.subscribe_by_subject_id 失败: {e}")
|
||||
return f"❌ 处理失败: {e}"
|
||||
|
||||
async def subscribe(self, group_id: str, query: str) -> str:
|
||||
async def subscribe(self, session_id: str, query: str) -> str:
|
||||
"""
|
||||
处理订阅逻辑:匹配条目 -> 存入数据库 -> 建立订阅关系。
|
||||
"""
|
||||
logger.info(f"处理追番请求: {query}, group_id={group_id}")
|
||||
logger.info(f"处理追番请求: {query}, session_id={session_id}")
|
||||
try:
|
||||
# 1. 匹配条目 (调用内部迁移后的逻辑)
|
||||
error_msg, subject_info = await self._match_subscribable_subject(query)
|
||||
@@ -171,27 +173,27 @@ class SubscriptionService:
|
||||
|
||||
# 2 & 3. 原子性地写入条目信息并建立订阅关系
|
||||
success = self.storage.subscribe_subject(
|
||||
group_id=group_id,
|
||||
group_id=session_id,
|
||||
subject_id=subject_id,
|
||||
name=name,
|
||||
air_date=subject_info["air_date"],
|
||||
total_episodes=subject_info["total_episodes"],
|
||||
)
|
||||
if success:
|
||||
return f"✅ 成功订阅《{name}》!\n如有更新将推送到本群。"
|
||||
return f"✅ 成功订阅《{name}》!\n如有更新将推送到本会话。"
|
||||
else:
|
||||
return "❌ 订阅失败,数据库错误。"
|
||||
except (BangumiApiError, DatabaseError, SubscriptionError) as e:
|
||||
logger.error(f"SubscriptionService.subscribe 失败: {e}")
|
||||
return f"❌ 处理失败: {e}"
|
||||
|
||||
async def unsubscribe(self, group_id: str, query: str) -> str:
|
||||
async def unsubscribe(self, session_id: str, query: str) -> str:
|
||||
"""
|
||||
取消订阅逻辑。
|
||||
"""
|
||||
logger.info(f"处理取消追番请求: {query}, group_id={group_id}")
|
||||
logger.info(f"处理取消追番请求: {query}, session_id={session_id}")
|
||||
try:
|
||||
error_msg, subject_info = self._match_local_subscription(group_id, query)
|
||||
error_msg, subject_info = self._match_local_subscription(session_id, query)
|
||||
if error_msg:
|
||||
return error_msg
|
||||
if not subject_info:
|
||||
@@ -200,7 +202,7 @@ class SubscriptionService:
|
||||
subject_id = subject_info["subject_id"]
|
||||
name = subject_info["name"]
|
||||
|
||||
success = self.storage.remove_subscription(group_id, subject_id)
|
||||
success = self.storage.remove_subscription(session_id, subject_id)
|
||||
if success:
|
||||
return f"✅ 已成功取消订阅《{name}》。"
|
||||
else:
|
||||
@@ -210,10 +212,10 @@ class SubscriptionService:
|
||||
return f"❌ 处理失败: {e}"
|
||||
|
||||
def _match_local_subscription(
|
||||
self, group_id: str, query: str
|
||||
self, session_id: str, query: str
|
||||
) -> tuple[str | None, UnsubscribeMatch | None]:
|
||||
"""
|
||||
在当前群组的本地订阅中做模糊匹配。
|
||||
在当前会话的本地订阅中做模糊匹配。
|
||||
"""
|
||||
normalized_query = str(query).strip()
|
||||
if not normalized_query:
|
||||
@@ -221,10 +223,10 @@ class SubscriptionService:
|
||||
|
||||
# 取 6 条用于判断是否超过默认展示上限(5 条)
|
||||
candidates = self.storage.find_group_subscription_candidates(
|
||||
group_id=group_id, keyword=normalized_query, limit=6
|
||||
group_id=session_id, keyword=normalized_query, limit=6
|
||||
)
|
||||
if not candidates:
|
||||
return f"❌ 未找到与「{normalized_query}」匹配的本群订阅番剧。", None
|
||||
return f"❌ 未找到与「{normalized_query}」匹配的本会话订阅番剧。", None
|
||||
|
||||
if len(candidates) == 1:
|
||||
subject = candidates[0]
|
||||
@@ -318,13 +320,16 @@ class SubscriptionService:
|
||||
f"🔔 番剧《{subject_name}》更新啦!\n第 {episode.ep} 集:{episode.name_cn or episode.name}"
|
||||
)
|
||||
|
||||
for group_id in subscribed_groups:
|
||||
for session_id in subscribed_groups:
|
||||
try:
|
||||
await StarTools.send_message_by_id(
|
||||
type="GroupMessage", id=group_id, message_chain=chain
|
||||
)
|
||||
logger.info(f"向群组 {group_id} 发送《{subject_name}》更新通知成功。")
|
||||
if self.context:
|
||||
await self.context.send_message(session_id, chain)
|
||||
else:
|
||||
await StarTools.send_message_by_id(
|
||||
type="GroupMessage", id=session_id, message_chain=chain
|
||||
)
|
||||
logger.info(f"向会话 {session_id} 发送《{subject_name}》更新通知成功。")
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"向群组 {group_id} 发送《{subject_name}》更新通知失败: {e}"
|
||||
f"向会话 {session_id} 发送《{subject_name}》更新通知失败: {e}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user