cancel subscription task if authentication error occurs
This commit is contained in:
parent
a55124c23a
commit
acc3a4cd47
25
ntfy/bot.py
25
ntfy/bot.py
|
|
@ -160,7 +160,9 @@ class NtfyBot(Plugin):
|
|||
line = line.decode("utf-8").strip()
|
||||
self.log.trace("Received notification: %s", line)
|
||||
message = json.loads(line)
|
||||
if message["event"] != "message":
|
||||
if not "event" in message or message["event"] != "message":
|
||||
if "error" in message:
|
||||
await self.handle_subscription_error(message, topic, self.tasks[topic.id])
|
||||
continue
|
||||
self.log.debug("Received message event: %s", line)
|
||||
# persist the received message id
|
||||
|
|
@ -186,6 +188,27 @@ class NtfyBot(Plugin):
|
|||
self.log.exception(
|
||||
"Failed to send matrix message!", exc_info=exc)
|
||||
|
||||
async def handle_subscription_error(self, message: dict, topic: Topic,
|
||||
task: asyncio.Task) -> None:
|
||||
self.log.error(message["error"])
|
||||
code = message["code"] if "code" in message else None
|
||||
match code:
|
||||
case 42909:
|
||||
if task.cancel():
|
||||
for sub in await self.db.get_subscriptions(topic.id):
|
||||
html_content = "<span>Authentication failed, please check the access token. Subscription has been cancelled.</span>"
|
||||
text_content = await parse_html(html_content.strip())
|
||||
content = TextMessageEventContent(
|
||||
msgtype=MessageType.TEXT,
|
||||
format=Format.HTML,
|
||||
formatted_body=html_content,
|
||||
body=text_content,
|
||||
)
|
||||
await self.client.send_message(sub.room_id, content)
|
||||
else:
|
||||
self.log.error("Failed to cancel subscription task")
|
||||
|
||||
|
||||
def build_message_content(self, server: str, message) -> str:
|
||||
topic = message["topic"]
|
||||
body = message["message"]
|
||||
|
|
|
|||
Loading…
Reference in New Issue