refactor(messaging): render music torrents in interaction helper

This commit is contained in:
jxxghp
2026-09-26 16:36:47 +08:00
parent 126e954602
commit 0d80b2e579
2 changed files with 28 additions and 45 deletions

View File

@@ -14,6 +14,7 @@ from app.chain.media import MediaChain
from app.chain.music_interaction.music import (
format_music_candidate,
media_exists_retry_prompt,
post_music_aware_torrents,
post_music_candidates,
resolve_media_action,
search_music_candidates,
@@ -1104,45 +1105,6 @@ class MediaInteractionChain(ChainBase):
)
request.page = page
total = len(request.items)
if isinstance(request.current_media, MusicInfo):
if self._supports_interactive_buttons(channel):
title = f"【{request.title}】共找到{total}条相关资源,请选择下载"
buttons = self._create_torrent_buttons(
channel=channel,
request=request,
items=page_items,
total=total,
total_pages=total_pages,
)
else:
page_hint = " p: 上一页 n: 下一页" if total > self._page_size(channel) else ""
title = (
f"【{request.title}】共找到{total}条相关资源,"
f"请回复对应数字下载(0: 自动选择{page_hint})"
)
buttons = None
text = "\n".join(
f"{index}.【{context.torrent_info.site_name}】"
f"{context.torrent_info.title} "
f"{context.torrent_info.seeders}↑"
for index, context in enumerate(page_items, start=1)
)
self.post_message(
Message(
channel=channel,
source=source,
title=title,
text=text,
userid=userid,
link=self.runtime_config.resource_url,
buttons=buttons,
original_message_id=original_message_id,
original_chat_id=original_chat_id,
parse_mode="",
save_history=False,
)
)
return
if self._supports_interactive_buttons(channel):
title = f"【{request.title}】共找到{total}条相关资源,请选择下载"
buttons = self._create_torrent_buttons(
@@ -1153,13 +1115,12 @@ class MediaInteractionChain(ChainBase):
total_pages=total_pages,
)
else:
if total > self._page_size(channel):
title = f"【{request.title}】共找到{total}条相关资源,请回复对应数字下载(0: 自动选择 p: 上一页 n: 下一页)"
else:
title = f"【{request.title}】共找到{total}条相关资源,请回复对应数字下载(0: 自动选择)"
page_hint = " p: 上一页 n: 下一页" if total > self._page_size(channel) else ""
title = f"【{request.title}】共找到{total}条相关资源,请回复对应数字下载(0: 自动选择{page_hint})"
buttons = None
self.post_torrents_message(
post_music_aware_torrents(
self,
Message(
channel=channel,
source=source,

View File

@@ -7,7 +7,7 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Protocol, Tuple
from app.chain.media import MediaChain
from app.domain import title as title_rules
from app.domain.context import MediaInfo, MusicInfo
from app.domain.context import Context, MediaInfo, MusicInfo
from app.domain.meta.metamusic import MetaMusic
from app.foundation import url as url_tools
from app.schemas.message import Message
@@ -60,6 +60,10 @@ class MusicInteractionHost(Protocol):
"""将渠道通知交给宿主消息发送实现。"""
...
def post_torrents_message(self, message: Message, torrents: List[Context]) -> None:
"""由宿主按既有影视资源卡片格式发送资源列表。"""
...
def resolve_music_action(text: str) -> Tuple[Optional[str], str]:
"""解析音乐交互前缀,避免把普通聊天误判为音乐搜索。"""
@@ -215,3 +219,21 @@ def post_music_candidates(
save_history=False,
)
)
def post_music_aware_torrents(
host: MusicInteractionHost,
message: Message,
torrents: List[Context],
) -> None:
"""音乐资源直接展示原始标题,影视资源沿用既有渠道卡片。"""
if not torrents or not isinstance(torrents[0].media_info, MusicInfo):
host.post_torrents_message(message, torrents)
return
message.text = "\n".join(
f"{index}.【{context.torrent_info.site_name}】"
f"{context.torrent_info.title} {context.torrent_info.seeders}↑"
for index, context in enumerate(torrents, start=1)
)
message.parse_mode = ""
host.post_message(message)