Skip to content

Commit

Permalink
fix(*): /test cannot be used remotely
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed Nov 20, 2023
1 parent 318cd49 commit 9d690a2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.zh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 更新日志

## 尚未发布

### Bug 修复

- **“远程” `/test` 不可用**:修复阻止 bot 管理员“远程”使用 `/test` 命令的错误。

## 显著提高性能、原生块状引用和语法高亮(v2.4.0)

### 重大变更
Expand Down
21 changes: 13 additions & 8 deletions src/command/administration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/command/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions src/parsing/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9d690a2

Please sign in to comment.