diff --git a/defs/notice.py b/defs/notice.py index 696970c..f82494b 100644 --- a/defs/notice.py +++ b/defs/notice.py @@ -8,7 +8,7 @@ from mipac.models.notification import ( NotificationAchievement, NotificationNote, ) -from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton +from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message from init import bot @@ -119,8 +119,8 @@ async def send_note_mention( chat_id: int, notice: NotificationNote, topic_id: int, -): - await bot.send_message( +) -> Message: + return await bot.send_message( chat_id, mention_template.format( get_note_link(host, notice.note), diff --git a/misskey_init.py b/misskey_init.py index fcc13f1..680bdc3 100644 --- a/misskey_init.py +++ b/misskey_init.py @@ -5,7 +5,6 @@ from typing import Optional, Union from aiohttp import ClientConnectorError from mipa.exception import WebSocketNotConnected from mipa.ext import commands -from mipa.router import Router from mipac import ( Note, NotificationFollow, @@ -58,12 +57,12 @@ class MisskeyBot(commands.Bot): await self.process_note(note, notice=False) logs.info(f"{self.tg_user.user_id} 处理完成最近十条时间线") - async def when_start(self, ws): - await Router(ws).connect_channel(["main", "home"]) + async def when_start(self, _): + await self._router.connect_channel(["main", "home"]) await self.fetch_offline_notes() subs = await RevokeAction.get_all_subs(self.tg_user.user_id) for sub in subs: - await Router(ws).capture_message(sub) + await self._router.capture_message(sub) async def on_ready(self, ws): try: @@ -191,12 +190,14 @@ class MisskeyBot(commands.Bot): async def on_mention(self, notice: NotificationNote): if self.tg_user.chat_id != 0 and self.tg_user.notice_topic != 0: - await send_note_mention( + msg = await send_note_mention( self.tg_user.host, self.tg_user.chat_id, notice, self.tg_user.notice_topic, ) + await RevokeAction.push_extend(self.tg_user.user_id, notice.note.id, msg) + await self._router.capture_message(notice.note.id) @staticmethod async def __on_error(event_method: str) -> None: diff --git a/models/services/revoke.py b/models/services/revoke.py index 95a0467..cdd31a7 100644 --- a/models/services/revoke.py +++ b/models/services/revoke.py @@ -1,4 +1,6 @@ import base64 +import contextlib + from cashews import cache from pyrogram.types import Message @@ -9,9 +11,7 @@ class RevokeAction: HOURS: int = 2 @staticmethod - def encode_messages(messages: list[Message]) -> str: - ids = [str(message.id) for message in messages] - cid = messages[0].chat.id + def encode_messages(cid: int, ids: list[str]) -> str: text = f"{cid}:{','.join(ids)}" return base64.b64encode(text.encode()).decode() @@ -21,16 +21,35 @@ class RevokeAction: cid, ids = text.split(":") return int(cid), [int(mid) for mid in ids.split(",")] + @staticmethod + async def _push(uid: int, note_id: str, cid: int, messages: list[int]): + ids = [str(message) for message in messages] + await cache.set( + f"sub:{uid}:{note_id}", + RevokeAction.encode_messages(cid, ids), + expire=60 * 60 * RevokeAction.HOURS, + ) + @staticmethod async def push(uid: int, note_id: str, messages: Message | list[Message]): if not messages: return messages = [messages] if isinstance(messages, Message) else messages - await cache.set( - f"sub:{uid}:{note_id}", - RevokeAction.encode_messages(messages), - expire=60 * 60 * RevokeAction.HOURS, - ) + cid = messages[0].chat.id + mids = [message.id for message in messages] + await RevokeAction._push(uid, note_id, cid, mids) + + @staticmethod + async def push_extend(uid: int, note_id: str, messages: Message | list[Message]): + if not messages: + return + messages = [messages] if isinstance(messages, Message) else messages + try: + cid, mids = await RevokeAction.get(uid, note_id) + except ValueError: + cid, mids = messages[0].chat.id, [] + mids.extend([message.id for message in messages]) + await RevokeAction._push(uid, note_id, cid, mids) @staticmethod async def get(uid: int, note_id: str) -> tuple[int, list[int]]: @@ -53,7 +72,8 @@ class RevokeAction: cid, msgs = await RevokeAction.get(uid, note_id) except ValueError: return - await RevokeAction._delete_message(cid, msgs) + with contextlib.suppress(Exception): + await RevokeAction._delete_message(cid, msgs) await cache.delete(f"sub:{uid}:{note_id}") @staticmethod diff --git a/requirements.txt b/requirements.txt index 7b3832e..09e325d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,13 @@ Pyrogram==2.0.106 PyrotgCrypto==1.2.6 -httpx==0.24.1 -apscheduler==3.10.1 -sqlalchemy==1.4.41 -sqlmodel==0.0.8 +httpx==0.26.0 +apscheduler==3.10.4 +sqlalchemy==2.0.27 +sqlmodel==0.0.16 aiosqlite==0.19.0 PyYAML==6.0.1 -aiofiles==23.1.0 +aiofiles==23.2.1 pillow==10.2.0 -cashews[redis]==6.2.0 -alembic==1.11.2 -sentry-sdk==1.29.2 +cashews[redis]==7.0.0 +alembic==1.13.1 +sentry-sdk==1.40.4