From 372c5bb3e7cb48921afb88c9a880ed959c49bba2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= <tlovinator@gmail.com>
Date: Mon, 19 Dec 2022 17:23:55 +0100
Subject: [PATCH] Add a function for get_webhooks()

---
 discord_rss_bot/main.py     | 16 +++-------------
 discord_rss_bot/settings.py | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/discord_rss_bot/main.py b/discord_rss_bot/main.py
index 0a83c2f..c288235 100644
--- a/discord_rss_bot/main.py
+++ b/discord_rss_bot/main.py
@@ -51,7 +51,7 @@ from starlette.templating import _TemplateResponse
 
 from discord_rss_bot.feeds import send_to_discord
 from discord_rss_bot.search import create_html_for_search_results
-from discord_rss_bot.settings import get_reader
+from discord_rss_bot.settings import get_reader, get_webhooks
 
 app: FastAPI = FastAPI()
 app.mount("/static", StaticFiles(directory="discord_rss_bot/static"), name="static")
@@ -94,12 +94,7 @@ async def add_webhook(webhook_name: str = Form(), webhook_url: str = Form()) ->
     clean_webhook_url: str = webhook_url.strip()
 
     # Get current webhooks from the database if they exist otherwise use an empty list.
-    webhooks: list[dict[str, str]] = []
-    if reader.get_tags(()) is not None:
-        for tag in reader.get_tag_keys(()):
-            if tag == "webhooks":
-                webhooks = reader.get_tag((), "webhooks")
-            break
+    webhooks = get_webhooks(reader)
 
     # Only add the webhook if it doesn't already exist.
     if not any(webhook["name"] == clean_webhook_name for webhook in webhooks):
@@ -134,12 +129,7 @@ async def delete_webhook(webhook_url: str = Form()) -> RedirectResponse | dict[s
     clean_webhook_url: str = webhook_url.strip()
 
     # Get current webhooks from the database if they exist otherwise use an empty list.
-    webhooks: list[dict[str, str]] = []
-    if reader.get_tags(()) is not None:
-        for tag in reader.get_tag_keys(()):
-            if tag == "webhooks":
-                webhooks = reader.get_tag((), "webhooks")
-            break
+    webhooks = get_webhooks(reader)
 
     # Only add the webhook if it doesn't already exist.
     for webhook in webhooks:
diff --git a/discord_rss_bot/settings.py b/discord_rss_bot/settings.py
index 4e6e421..d58d01d 100644
--- a/discord_rss_bot/settings.py
+++ b/discord_rss_bot/settings.py
@@ -45,3 +45,22 @@ def get_reader(custom_location: str = "") -> Reader:
     """
     db_location: str = get_db_location(custom_location)
     return make_reader(url=db_location)
+
+
+def get_webhooks(reader: Reader) -> list[dict[str, str]]:
+    """
+    Get current webhooks from the database if they exist otherwise use an empty list.
+
+    Args:
+        reader: The reader to use.
+
+    Returns:
+        list[dict[str, str]]: The webhooks.
+    """
+    webhooks: list[dict[str, str]] = []
+    if reader.get_tags(()) is not None:
+        for tag in reader.get_tag_keys(()):
+            if tag == "webhooks":
+                webhooks = reader.get_tag((), "webhooks")
+            break
+    return webhooks