Added wiki removal function and associated pub/sub command

This commit is contained in:
Frisk 2024-09-10 11:50:39 +02:00
parent 56f6ef1887
commit c6e9a92c94
2 changed files with 27 additions and 3 deletions

View file

@ -84,6 +84,14 @@ class DomainManager:
elif split_payload[0] == "UPDATE": elif split_payload[0] == "UPDATE":
await self.return_domain(self.get_domain(split_payload[1])).get_wiki(split_payload[1]).update_targets() 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])) 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": elif split_payload[0] == "DEBUG":
asyncio.current_task().set_name("webhook_update") asyncio.current_task().set_name("webhook_update")
if split_payload[1] == "INFO": if split_payload[1] == "INFO":

View file

@ -500,7 +500,8 @@ class Wiki:
if not send_reason: if not send_reason:
return return
lang = "en" 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: if webhook_url in webhooks:
lang = combination.lang lang = combination.lang
lang = langs[lang]["wiki"] lang = langs[lang]["wiki"]
@ -511,8 +512,23 @@ class Wiki:
except: except:
logger.exception("Webhook removal send_reason failure.") logger.exception("Webhook removal send_reason failure.")
async def remove_wiki_from_db(self, reason: str): async def remove_wiki_from_db(self, reason: str, send_reason=False):
raise NotImplementedError # TODO 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]: async def fetch_discussions(self, params: dict) -> tuple[aiohttp.ClientResponse, dict]:
header = settings["header"] header = settings["header"]