misskey2telegram/models/services/revoke.py

62 lines
1.9 KiB
Python
Raw Normal View History

2023-07-21 14:00:49 +00:00
import base64
from cashews import cache
from pyrogram.types import Message
from init import bot
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
text = f"{cid}:{','.join(ids)}"
return base64.b64encode(text.encode()).decode()
@staticmethod
def decode_messages(text: str) -> tuple[int, list[int]]:
text = base64.b64decode(text.encode()).decode()
cid, ids = text.split(":")
return int(cid), [int(mid) for mid in ids.split(",")]
@staticmethod
2023-08-07 12:48:02 +00:00
async def push(uid: int, note_id: str, messages: Message | list[Message]):
2023-08-10 03:53:28 +00:00
if not messages:
return
2023-08-07 12:48:02 +00:00
messages = [messages] if isinstance(messages, Message) else messages
2023-07-21 14:00:49 +00:00
await cache.set(
f"sub:{uid}:{note_id}",
RevokeAction.encode_messages(messages),
expire=60 * 60 * RevokeAction.HOURS,
)
@staticmethod
async def get(uid: int, note_id: str) -> tuple[int, list[int]]:
text = await cache.get(f"sub:{uid}:{note_id}")
if text is None:
raise ValueError("No such sub note: {}".format(note_id))
return RevokeAction.decode_messages(text)
@staticmethod
async def get_all_subs(uid: int) -> list[str]:
keys = []
async for key in cache.scan(f"sub:{uid}:*"):
key: str
2023-07-24 12:03:44 +00:00
keys.append(key.split(":")[-1])
2023-07-21 14:00:49 +00:00
return keys
@staticmethod
async def process_delete_note(uid: int, note_id: str):
try:
cid, msgs = await RevokeAction.get(uid, note_id)
except ValueError:
return
await RevokeAction._delete_message(cid, msgs)
await cache.delete(f"sub:{uid}:{note_id}")
@staticmethod
async def _delete_message(cid: int, msgs: list[int]):
await bot.delete_messages(cid, msgs)