feat: note mention notice delete

This commit is contained in:
xtaodada 2024-02-19 11:26:00 +08:00
parent 83b52128bf
commit 56b0cd5a96
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 46 additions and 25 deletions

View File

@ -8,7 +8,7 @@ from mipac.models.notification import (
NotificationAchievement, NotificationAchievement,
NotificationNote, NotificationNote,
) )
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, Message
from init import bot from init import bot
@ -119,8 +119,8 @@ async def send_note_mention(
chat_id: int, chat_id: int,
notice: NotificationNote, notice: NotificationNote,
topic_id: int, topic_id: int,
): ) -> Message:
await bot.send_message( return await bot.send_message(
chat_id, chat_id,
mention_template.format( mention_template.format(
get_note_link(host, notice.note), get_note_link(host, notice.note),

View File

@ -5,7 +5,6 @@ from typing import Optional, Union
from aiohttp import ClientConnectorError from aiohttp import ClientConnectorError
from mipa.exception import WebSocketNotConnected from mipa.exception import WebSocketNotConnected
from mipa.ext import commands from mipa.ext import commands
from mipa.router import Router
from mipac import ( from mipac import (
Note, Note,
NotificationFollow, NotificationFollow,
@ -58,12 +57,12 @@ class MisskeyBot(commands.Bot):
await self.process_note(note, notice=False) await self.process_note(note, notice=False)
logs.info(f"{self.tg_user.user_id} 处理完成最近十条时间线") logs.info(f"{self.tg_user.user_id} 处理完成最近十条时间线")
async def when_start(self, ws): async def when_start(self, _):
await Router(ws).connect_channel(["main", "home"]) await self._router.connect_channel(["main", "home"])
await self.fetch_offline_notes() await self.fetch_offline_notes()
subs = await RevokeAction.get_all_subs(self.tg_user.user_id) subs = await RevokeAction.get_all_subs(self.tg_user.user_id)
for sub in subs: for sub in subs:
await Router(ws).capture_message(sub) await self._router.capture_message(sub)
async def on_ready(self, ws): async def on_ready(self, ws):
try: try:
@ -191,12 +190,14 @@ class MisskeyBot(commands.Bot):
async def on_mention(self, notice: NotificationNote): async def on_mention(self, notice: NotificationNote):
if self.tg_user.chat_id != 0 and self.tg_user.notice_topic != 0: 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.host,
self.tg_user.chat_id, self.tg_user.chat_id,
notice, notice,
self.tg_user.notice_topic, 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 @staticmethod
async def __on_error(event_method: str) -> None: async def __on_error(event_method: str) -> None:

View File

@ -1,4 +1,6 @@
import base64 import base64
import contextlib
from cashews import cache from cashews import cache
from pyrogram.types import Message from pyrogram.types import Message
@ -9,9 +11,7 @@ class RevokeAction:
HOURS: int = 2 HOURS: int = 2
@staticmethod @staticmethod
def encode_messages(messages: list[Message]) -> str: def encode_messages(cid: int, ids: list[str]) -> str:
ids = [str(message.id) for message in messages]
cid = messages[0].chat.id
text = f"{cid}:{','.join(ids)}" text = f"{cid}:{','.join(ids)}"
return base64.b64encode(text.encode()).decode() return base64.b64encode(text.encode()).decode()
@ -21,16 +21,35 @@ class RevokeAction:
cid, ids = text.split(":") cid, ids = text.split(":")
return int(cid), [int(mid) for mid in ids.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 @staticmethod
async def push(uid: int, note_id: str, messages: Message | list[Message]): async def push(uid: int, note_id: str, messages: Message | list[Message]):
if not messages: if not messages:
return return
messages = [messages] if isinstance(messages, Message) else messages messages = [messages] if isinstance(messages, Message) else messages
await cache.set( cid = messages[0].chat.id
f"sub:{uid}:{note_id}", mids = [message.id for message in messages]
RevokeAction.encode_messages(messages), await RevokeAction._push(uid, note_id, cid, mids)
expire=60 * 60 * RevokeAction.HOURS,
) @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 @staticmethod
async def get(uid: int, note_id: str) -> tuple[int, list[int]]: 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) cid, msgs = await RevokeAction.get(uid, note_id)
except ValueError: except ValueError:
return 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}") await cache.delete(f"sub:{uid}:{note_id}")
@staticmethod @staticmethod

View File

@ -1,13 +1,13 @@
Pyrogram==2.0.106 Pyrogram==2.0.106
PyrotgCrypto==1.2.6 PyrotgCrypto==1.2.6
httpx==0.24.1 httpx==0.26.0
apscheduler==3.10.1 apscheduler==3.10.4
sqlalchemy==1.4.41 sqlalchemy==2.0.27
sqlmodel==0.0.8 sqlmodel==0.0.16
aiosqlite==0.19.0 aiosqlite==0.19.0
PyYAML==6.0.1 PyYAML==6.0.1
aiofiles==23.1.0 aiofiles==23.2.1
pillow==10.2.0 pillow==10.2.0
cashews[redis]==6.2.0 cashews[redis]==7.0.0
alembic==1.11.2 alembic==1.13.1
sentry-sdk==1.29.2 sentry-sdk==1.40.4