diff --git a/defs/announcement.py b/defs/announcement.py index 7d89c70..a1f82f3 100644 --- a/defs/announcement.py +++ b/defs/announcement.py @@ -1,11 +1,13 @@ -from datetime import datetime -from typing import Optional +from typing import TYPE_CHECKING from mipac import Route from init import bot from misskey_init import MisskeyBot +if TYPE_CHECKING: + from mipac.models.announcement import Announcement + announcement_template = """Misskey Announcement {0} @@ -13,28 +15,16 @@ announcement_template = """Misskey Announcement {1}""" -class Announcement: - def __init__(self, data, misskey_bot: MisskeyBot): +class UnreadAnnouncement: + def __init__(self, data: "Announcement", misskey_bot: MisskeyBot): self.misskey_bot = misskey_bot - self.id = data["id"] - self.title = data["title"] - self.text = data["text"] - self.is_read = data["is_read"] - self.image_url = data["image_url"] - self._created_at = data["created_at"] - self._updated_at = data["updated_at"] - - @property - def created_at(self) -> datetime: - return datetime.strptime(self._created_at, "%Y-%m-%dT%H:%M:%S.%fZ") - - @property - def updated_at(self) -> Optional[datetime]: - return ( - datetime.strptime(self._updated_at, "%Y-%m-%dT%H:%M:%S.%fZ") - if self._updated_at - else None - ) + self.id = data.id + self.title = data.title + self.text = data.text + self.is_read = data.is_read + self.image_url = data.image_url + self.created_at = data.created_at + self.updated_at = data.updated_at async def send_notice(self): if not self.image_url: @@ -67,17 +57,3 @@ class Announcement: auth=True, lower=True, ) - - -async def get_unread_announcements(misskey_bot: MisskeyBot): - data = { - "limit": 10, - "withUnreads": True, - } - req = await misskey_bot.core.http.request( - Route("POST", "/api/announcements"), - json=data, - auth=True, - lower=True, - ) - return [Announcement(i, misskey_bot) for i in req] diff --git a/modules/announcement.py b/modules/announcement.py deleted file mode 100644 index 4cdab32..0000000 --- a/modules/announcement.py +++ /dev/null @@ -1,14 +0,0 @@ -from defs.announcement import get_unread_announcements -from misskey_init import misskey_bot_map -from models.services.scheduler import scheduler - - -# @scheduler.scheduled_job("interval", minutes=15, id="check_announcement") -async def announcement(): - for bot in misskey_bot_map.values(): - data = await get_unread_announcements(bot) - for an in data: - try: - await an.send_notice() - finally: - await an.mark_as_read() diff --git a/modules/timeline.py b/modules/timeline.py index e2de1eb..6d94199 100644 --- a/modules/timeline.py +++ b/modules/timeline.py @@ -1,14 +1,35 @@ import contextlib +from typing import TYPE_CHECKING + +from defs.announcement import UnreadAnnouncement from init import bot from misskey_init import init_misskey_bot, misskey_bot_map from models.services.scheduler import scheduler +if TYPE_CHECKING: + from mipac import MeDetailed + from misskey_init import MisskeyBot + bot.loop.create_task(init_misskey_bot()) -@scheduler.scheduled_job("cron", hour=1, id="daily_status") +async def get_unread_announcements(me: "MeDetailed", m_bot: "MisskeyBot"): + if not me: + return + un = [UnreadAnnouncement(an, m_bot) for an in me.unread_announcements] + for an in un: + try: + await an.send_notice() + finally: + await an.mark_as_read() + + +@scheduler.scheduled_job("cron", minute=30, id="daily_status") async def daily_status(): for m_bot in misskey_bot_map.values(): with contextlib.suppress(Exception): - await m_bot.core.api.get_me() + me = await m_bot.core.api.get_me() + await get_unread_announcements(me, m_bot) + +