From 256aa8f315cbb184eba0256c2ec818abbdd2d408 Mon Sep 17 00:00:00 2001 From: Sophie Tauchert Date: Sat, 21 Jan 2023 16:54:33 +0100 Subject: [PATCH] Cancel unnecessary subscription tasks --- ntfy/bot.py | 3 +++ ntfy/db.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/ntfy/bot.py b/ntfy/bot.py index 131af7f..057785a 100644 --- a/ntfy/bot.py +++ b/ntfy/bot.py @@ -111,6 +111,9 @@ class NtfyBot(Plugin): await evt.reply("This room is not subscribed to %s/%s", server, topic) return await self.db.remove_subscription(db_topic.id, evt.room_id) + if not await self.db.get_subscriptions(db_topic.id): + self.tasks[db_topic.id].cancel() + await self.db.clear_topic_id(db_topic.id) await evt.reply("Unsubscribed this room from %s/%s", server, topic) await evt.react(WHITE_CHECK_MARK) diff --git a/ntfy/db.py b/ntfy/db.py index 0a5bb1c..7b0c36b 100644 --- a/ntfy/db.py +++ b/ntfy/db.py @@ -111,6 +111,12 @@ class DB: """ await self.db.execute(query, topic_id, event_id) + async def clear_topic_id(self, topic_id: int) -> None: + query = """ + UPDATE topics SET last_event_id=NULL where id=$1 + """ + await self.db.execute(query, topic_id) + async def create_topic(self, topic: Topic) -> Topic: query = """ INSERT INTO topics (server, topic, last_event_id)