Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add /comment command #259

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@

列出所有被屏蔽的uin。

.. option:: /comment list

.. versionadded:: 0.9.4

列出所引用说说的评论。

.. option:: /comment add <content>

.. versionadded:: 0.9.4

评论所引用的说说。

.. option:: /comment add private <content>

.. versionadded:: 0.9.4

私密评论所引用的说说。

.. option:: help

发送帮助信息。
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "Qzone3TG"
version = "0.9.3.dev4"
version = "0.9.4.dev2"
description = "Forward Qzone feeds to telegram."
authors = ["aioqzone <zzzzss990315@gmail.com>"]
readme = "README.md"
Expand Down
6 changes: 6 additions & 0 deletions src/qzone3tg/_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from tylisten import hookdef


@hookdef
def is_uin_blocked(uin: int) -> bool:
return False
3 changes: 3 additions & 0 deletions src/qzone3tg/app/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ async def lst_forever():
self.log.debug("init_timers done")

def init_hooks(self):
from qzone3tg._messages import is_uin_blocked

from ._hook import add_feed_impls, add_hb_impls, add_up_impls

self.is_uin_blocked = is_uin_blocked()
add_feed_impls(self)
add_hb_impls(self)
add_up_impls(self)
Expand Down
6 changes: 5 additions & 1 deletion src/qzone3tg/app/base/_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def get_mids(feed: BaseFeed) -> list[int]:
@self.qzone.feed_processed.add_impl
async def FeedProcEnd(bid: int, feed: FeedContent):
self.log.debug(f"bid={bid}: {feed}")
if feed.uin in self.blockset:
if any(await self.is_uin_blocked.results(feed.uin)):
self.log.info(f"Blocklist hit: {feed.uin}({feed.nickname})")
return await FeedDropped(bid, feed)

Expand All @@ -68,6 +68,10 @@ async def FeedDropped(bid: int, feed):
async def StopFeedFetch(feed: FeedData) -> bool:
return await self.store.exists(*FeedOrm.primkey(feed))

@self.is_uin_blocked.add_impl
def in_blockset(uin: int):
return uin in self.blockset

self.qzone.stop_fetch = StopFeedFetch


Expand Down
18 changes: 12 additions & 6 deletions src/qzone3tg/app/interact/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from ..base import BaseApp
from ._block import command_block
from ._comment import command_comment
from ._conversation.emoji import command_em
from .types import SerialCbData

Expand All @@ -37,6 +38,7 @@ class InteractApp(BaseApp):
BotCommand(command="block", description="黑名单管理"),
command_em,
command_block,
command_comment,
]

def __init__(self, conf: Settings) -> None:
Expand All @@ -51,13 +53,14 @@ def init_queue(self):
self.dyn_blockset = BlockSet(self.engine)

def init_hooks(self):
super().init_hooks()
from ._button import add_button_impls
from ._hook import add_qr_impls, add_up_impls

add_qr_impls(self)
add_up_impls(self)
add_button_impls(self)
super().init_hooks()
self.is_uin_blocked.add_impl(lambda uin: self.dyn_blockset.contains(uin))
JamzumSum marked this conversation as resolved.
Show resolved Hide resolved

async def __aenter__(self):
await super().__aenter__()
Expand Down Expand Up @@ -89,7 +92,10 @@ def register_handlers(self):
SerialCbData.filter(F.sub_command.regexp(r"-?\d+")),
)

self.dp.include_routers(_emoji_router(self), _button_router(self))
self.dp.include_routers(
_emoji_router(self),
_button_router(self),
)

async def set_commands(self):
try:
Expand Down Expand Up @@ -146,8 +152,6 @@ async def run(self):
self.set_commands(),
self.dyn_blockset.create(),
)
# 加载动态黑名单
self.blockset.update(await self.dyn_blockset.all())
return await super().run()

async def idle(self):
Expand Down Expand Up @@ -208,10 +212,11 @@ async def qr_login(self, message: Message, command: CommandObject):
await self.login.qr.new_cookie()

async def like(self, message: Message, command: CommandObject):
assert message
reply = message.reply_to_message
if not reply:
await message.reply(**Text("使用", CommandText("/like"), "时,您需要回复一条消息。").as_kwargs())
await message.reply(
**Text("使用", CommandText(f"/{command.command}"), "时,您需要回复一条消息。").as_kwargs()
)
return

async def query_likedata(mid: int):
Expand Down Expand Up @@ -299,4 +304,5 @@ async def _invalid_input(message: Message):
# --------------------------------
from ._block import block
from ._button import btn_like, btn_qr
from ._comment import comment
from ._conversation.emoji import em, input_eid
3 changes: 0 additions & 3 deletions src/qzone3tg/app/interact/_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ async def block(self: InteractApp, message: Message):
if feed is None:
await message.reply("uin not found. Try `/block add <uin>` instead.")
return
self.blockset.add(feed.uin)
await self.dyn_blockset.add(feed.uin)
await message.reply(f"{feed.uin} 已加入黑名单")
case ["rm", uin]:
Expand All @@ -40,7 +39,6 @@ async def block(self: InteractApp, message: Message):
except:
await message.reply(**BLOCK_CMD_HELP.as_kwargs())
return
self.blockset.discard(uin)
if await self.dyn_blockset.delete(uin):
await message.reply(f"{uin} 已从黑名单移除✅")
else:
Expand All @@ -52,7 +50,6 @@ async def block(self: InteractApp, message: Message):
await message.reply(**BLOCK_CMD_HELP.as_kwargs())
return

self.blockset.add(int(uin))
await self.dyn_blockset.add(int(uin))
await message.reply(f"{uin} 已加入黑名单")
case ["list"]:
Expand Down
63 changes: 63 additions & 0 deletions src/qzone3tg/app/interact/_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations

import typing as t

from aiogram.filters.command import CommandObject
from aiogram.types import BotCommand, Message
from aiogram.utils.formatting import BotCommand as CommandText
from aiogram.utils.formatting import Text, as_key_value, as_marked_section, as_numbered_section

if t.TYPE_CHECKING:
from . import InteractApp

COMMENT_CMD_HELP = as_marked_section(
"帮助:",
as_key_value(CommandText("/comment list"), "查看当前引用说说的评论"),
as_key_value(CommandText("/comment add <content>"), "回复引用的说说"),
as_key_value(CommandText("/comment add private <content>"), "私密回复引用的说说"),
)


async def comment(self: InteractApp, message: Message, command: CommandObject):
reply = message.reply_to_message
if not reply:
await message.reply(
**Text("使用", CommandText(f"/{command.command}"), "时,您需要回复一条消息。").as_kwargs()
)
return

async def query_fid(mid: int):
feed = await self.Mid2Feed(reply.message_id)
if not feed:
await message.reply(f"未找到该消息,可能已超出 {self.conf.bot.storage.keepdays} 天。")
return
return feed

if not command.args:
await message.reply(**COMMENT_CMD_HELP.as_kwargs())
return

match command.args.split():
case ["add", content]:
if orm := await query_fid(reply.message_id):
await self.qzone.add_comment(orm.uin, orm.fid, orm.appid, content)
case ["add", "private", content]:
if orm := await query_fid(reply.message_id):
await self.qzone.add_comment(orm.uin, orm.fid, orm.appid, content, private=True)
case ["list"]:
if orm := await query_fid(reply.message_id):
detail = await self.qzone.shuoshuo(orm.fid, orm.uin, orm.appid)
comments = sorted(detail.comment.comments, key=lambda comment: comment.commentid)
text = as_numbered_section(
"评论:",
*(
as_key_value(comment.user.nickname, comment.content)
for comment in comments
),
)
await message.reply(**text.as_kwargs())
case _:
await message.reply(**COMMENT_CMD_HELP.as_kwargs())


command_comment = BotCommand(command="comment", description="查看评论、发评论")
Loading