From 9d690a2d455eb3cc2389995f43741af1af077641 Mon Sep 17 00:00:00 2001 From: Rongrong Date: Tue, 21 Nov 2023 02:51:12 +0800 Subject: [PATCH] fix(*): `/test` cannot be used remotely Signed-off-by: Rongrong --- docs/CHANGELOG.md | 6 ++++++ docs/CHANGELOG.zh.md | 6 ++++++ src/command/administration.py | 21 +++++++++++++-------- src/command/utils.py | 6 +++--- src/parsing/post.py | 2 -- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d299167138..b02d8c3196 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Bug fixes + +- **"Remote" `/test` unavailable**: Fix a bug preventing the bot manager from using the `/test` command "remotely". + ## Significant performance improvement, native blockquote and syntax highlighting (v2.4.0) ### BREAKING CHANGES diff --git a/docs/CHANGELOG.zh.md b/docs/CHANGELOG.zh.md index 99a51f18f4..405bbafc93 100644 --- a/docs/CHANGELOG.zh.md +++ b/docs/CHANGELOG.zh.md @@ -1,5 +1,11 @@ # 更新日志 +## 尚未发布 + +### Bug 修复 + +- **“远程” `/test` 不可用**:修复阻止 bot 管理员“远程”使用 `/test` 命令的错误。 + ## 显著提高性能、原生块状引用和语法高亮(v2.4.0) ### 重大变更 diff --git a/src/command/administration.py b/src/command/administration.py index 4e7dd15b9a..4b249415a0 100644 --- a/src/command/administration.py +++ b/src/command/administration.py @@ -61,8 +61,15 @@ async def cmd_set_option(event: Union[events.NewMessage.Event, Message], *_, lan parse_mode='html') -@command_gatekeeper(only_manager=True, timeout=None if env.DEBUG else 300) -async def cmd_test(event: Union[events.NewMessage.Event, Message], *_, lang: Optional[str] = None, **__): +@command_gatekeeper(only_manager=True, only_in_private_chat=False, timeout=None if env.DEBUG else 300) +async def cmd_test( + event: Union[events.NewMessage.Event, Message], + *_, + lang: Optional[str] = None, + chat_id: Optional[int] = None, + **__): + chat_id = chat_id or event.chat_id + args = parse_command(event.raw_text) if len(args) < 2: await event.respond(i18n[lang]['cmd_test_usage_prompt_html'], parse_mode='html') @@ -82,8 +89,6 @@ async def cmd_test(event: Union[events.NewMessage.Event, Message], *_, lang: Opt start = 0 end = 1 - uid = event.chat_id - try: wf = await web.feed_get(url, web_semaphore=False) rss_d = wf.rss_d @@ -102,7 +107,7 @@ async def cmd_test(event: Union[events.NewMessage.Event, Message], *_, lang: Opt return await asyncio.gather( - *(__send(uid, entry, rss_d.feed.title, wf.url) for entry in entries_to_send) + *(__send(chat_id, entry, rss_d.feed.title, wf.url) for entry in entries_to_send) ) except Exception as e: @@ -111,10 +116,10 @@ async def cmd_test(event: Union[events.NewMessage.Event, Message], *_, lang: Opt return -async def __send(uid, entry, feed_title, link): +async def __send(chat_id, entry, feed_title, link): post = await get_post_from_entry(entry, feed_title, link) - logger.debug(f"Sending {entry.get('title', 'Untitled')} ({entry.get('link', 'No link')})...") - await post.test_format(uid) + logger.debug(f"Sending {entry.get('title', 'Untitled')} ({entry.get('link', 'No link')}) to {chat_id}...") + await post.test_format(chat_id) @command_gatekeeper(only_manager=True) diff --git a/src/command/utils.py b/src/command/utils.py index 5324b52e34..344eef8b96 100644 --- a/src/command/utils.py +++ b/src/command/utils.py @@ -197,7 +197,7 @@ async def leave_chat(chat_id: hints.EntityLike) -> bool: def command_gatekeeper(func: Optional[Callable] = None, *, only_manager: bool = False, - only_in_private_chat: bool = False, + only_in_private_chat: bool = None, allow_in_others_private_chat: bool = False, allow_in_old_fashioned_groups: bool = False, ignore_tg_lang: bool = False, @@ -212,8 +212,8 @@ def command_gatekeeper(func: Optional[Callable] = None, timeout=timeout, quiet=quiet) - # assume that managing commands are only allowed in private chat - only_in_private_chat = only_in_private_chat or only_manager + # assume that managing commands are only allowed in private chat, unless specified + only_in_private_chat = only_manager if only_in_private_chat is None else only_in_private_chat # block contradicting settings assert not (only_in_private_chat and allow_in_old_fashioned_groups) diff --git a/src/parsing/post.py b/src/parsing/post.py index 5c1811b3a9..3219840067 100644 --- a/src/parsing/post.py +++ b/src/parsing/post.py @@ -153,8 +153,6 @@ async def send_formatted_post(self, raise SystemExit(self.feed_link, self.feed_title, self.link, self.title) from e async def test_format(self, user_id: int): - if user_id != env.MANAGER: - return sub = await db.Sub.filter(feed__link=self.feed_link, user_id=user_id).get_or_none() if sub is None: user = await db.User.get_or_none(id=user_id)