From c6e9a92c94f6d9c82cf1eef6e4a0b3f16532f8f7 Mon Sep 17 00:00:00 2001 From: Frisk Date: Tue, 10 Sep 2024 11:50:39 +0200 Subject: [PATCH] Added wiki removal function and associated pub/sub command --- src/domain_manager.py | 8 ++++++++ src/wiki.py | 22 +++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/domain_manager.py b/src/domain_manager.py index facff1d..63c9dcf 100644 --- a/src/domain_manager.py +++ b/src/domain_manager.py @@ -84,6 +84,14 @@ class DomainManager: elif split_payload[0] == "UPDATE": await self.return_domain(self.get_domain(split_payload[1])).get_wiki(split_payload[1]).update_targets() logger.info("Successfully force updated information about {}".format(split_payload[1])) + elif split_payload[0] == "ERASE" and len(split_payload) > 2: + logger.info(f"Received {' '.join(split_payload)} on pub/sub.") + domain = self.return_domain(self.get_domain(split_payload[1])) + wiki = domain.get_wiki(split_payload[1]) + reason = " ".join(split_payload[2:]) + if wiki is not None: + logger.debug("Wiki specified in pub/sub message has been found. Erasing the wiki from DB.") + await wiki.remove_wiki_from_db(reason, send_reason=True if reason else False) elif split_payload[0] == "DEBUG": asyncio.current_task().set_name("webhook_update") if split_payload[1] == "INFO": diff --git a/src/wiki.py b/src/wiki.py index 23bc22f..eed4774 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -500,7 +500,8 @@ class Wiki: if not send_reason: return lang = "en" - for combination, webhooks in self.rc_targets.items(): + rc_targets_long_lived = self.rc_targets.copy() # Create a copy since those will be updated and we live in async world + for combination, webhooks in rc_targets_long_lived.items(): if webhook_url in webhooks: lang = combination.lang lang = langs[lang]["wiki"] @@ -511,8 +512,23 @@ class Wiki: except: logger.exception("Webhook removal send_reason failure.") - async def remove_wiki_from_db(self, reason: str): - raise NotImplementedError # TODO + async def remove_wiki_from_db(self, reason: str, send_reason=False): + logger.info(f"Removing a wiki with script_url of {self.script_url} from the database due to {reason}.") + dbmanager.add(("DELETE FROM rcgcdb WHERE wiki = $1", (self.script_url,))) + if not send_reason: + return + rc_targets_long_lived = self.rc_targets.copy() # Create a copy since those will be updated and we live in async world + for combination, webhooks in rc_targets_long_lived.items(): + lang = combination.lang + try: # This is best effort scenario, but I don't plan to add re-tries to this + dc_msg = DiscordMessage("compact", "custom/webhook_removal", + webhooks, content=lang.gettext( + "This recent changes webhook has been removed for `{reason}`!".format(reason=reason))) + for webhook in webhooks: + await send_to_discord_webhook(dc_msg, webhook, "POST") + except: + logger.exception("Webhook removal send_reason failure.") + async def fetch_discussions(self, params: dict) -> tuple[aiohttp.ClientResponse, dict]: header = settings["header"]