Feature/add or remove #7

Merged
florian merged 6 commits from feature/add-or-remove into main 2026-11-01 08:49:48 +00:00
Showing only changes of commit 4dfa2b3b7c - Show all commits
+19 -15
View File
@@ -5,18 +5,20 @@ from contextlib import asynccontextmanager
import uvicorn import uvicorn
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from requests.exceptions import RequestException
from auth_config import Caller, auth from auth_config import Caller, auth
from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread
from feed_errors import FeedError from feed_errors import FeedCheckFailedError, FeedError
from feed_handler import ( from feed_handler import (
build_gitea_runner, build_gitea_runner,
ensure_feed_exists, ensure_feed_exists,
fetch_latest,
grab_gitea_release_information, grab_gitea_release_information,
grab_royal_road_chapter_information, grab_royal_road_chapter_information,
) )
from metrics_server import REQUEST_COUNTER, metrics_api from metrics_server import REQUEST_COUNTER, metrics_api
from models import FeedEntry, fetch_latest from models import FeedEntry
from secret_manager import cleanup_secret_files, get_secret from secret_manager import cleanup_secret_files, get_secret
from send_notifications import NotificationClient, NotificationError, NotificationUnavailableError from send_notifications import NotificationClient, NotificationError, NotificationUnavailableError
from service_auth import UserId from service_auth import UserId
@@ -112,12 +114,12 @@ def get_release_information(request: Request, db = Depends(get_db)):
results["checked"] += 1 results["checked"] += 1
try: try:
gitea_release_information = grab_gitea_release_information(rss_feed) gitea_release_information = grab_gitea_release_information(rss_feed)
logger.debug(f"[FastAPI] ID:{feed_id},RSS Feed {rss_feed}: db={last_entry}, online={gitea_release_information.link}") logger.debug(f"[FastAPI] ID:{feed_id},RSS Feed {rss_feed}: db={last_entry}, online={gitea_release_information.entry_id}")
if gitea_release_information.link == last_entry: if gitea_release_information.entry_id == last_entry:
continue continue
logger.info(f"[FastAPI] Release change for ID {feed_id}: {gitea_release_information.release_title}") logger.info(f"[FastAPI] Release change for ID {feed_id}: {gitea_release_information.body_title}")
with db.cursor() as cursor: with db.cursor() as cursor:
cursor.execute( cursor.execute(
@@ -130,12 +132,12 @@ def get_release_information(request: Request, db = Depends(get_db)):
sent = 0 sent = 0
failed = 0 failed = 0
repo = gitea_release_information.link.split("/releases")[0].removeprefix("https://")
for user_id,feed_name in subscribers: for user_id,feed_name in subscribers:
try: try:
repo = gitea_release_information.link.split("/releases")[0].removeprefix("https://")
request.app.state.notifier.send( request.app.state.notifier.send(
title=feed_name or repo, title=feed_name or repo,
body=gitea_release_information.release_title, body=gitea_release_information.body_title,
link=gitea_release_information.link, link=gitea_release_information.link,
category="utility", category="utility",
user_id=user_id, user_id=user_id,
@@ -157,14 +159,14 @@ def get_release_information(request: Request, db = Depends(get_db)):
with db.cursor() as cursor: with db.cursor() as cursor:
cursor.execute( cursor.execute(
"UPDATE rss_feeds SET last_entry = %s WHERE id = %s", "UPDATE rss_feeds SET last_entry = %s WHERE id = %s",
(gitea_release_information.link, feed_id), (gitea_release_information.entry_id, feed_id),
) )
db.commit() db.commit()
logger.debug(f"[FastAPI] Notification sent and database updated for Gitea feed ID {feed_id}") logger.debug(f"[FastAPI] Notification sent and database updated for Gitea feed ID {feed_id}")
results["new"] += 1 results["new"] += 1
if gitea_release_information.link.startswith("https://gitea.com/gitea/runner/releases/tag/v"): if gitea_release_information.link.startswith("https://gitea.com/gitea/runner/releases/tag/v"):
gitea_build_success = build_gitea_runner(gitea_release_information.release_title, request.app.state.gitea_token) gitea_build_success = build_gitea_runner(gitea_release_information.body_title, request.app.state.gitea_token)
logger.info(f"[FastAPI] Gitea build ran successfully:{gitea_build_success}") logger.info(f"[FastAPI] Gitea build ran successfully:{gitea_build_success}")
except Exception as e: except Exception as e:
@@ -285,24 +287,26 @@ def add_feed(
): ):
url = feed_entry.rss_feed_url url = feed_entry.rss_feed_url
logger.info(f"[FastAPI] Subscribe request: RSS URL={url}, user_id={user_id}") logger.info(f"[FastAPI] Subscribe request: RSS URL={url}, user_id={user_id}")
with db.cursor() as cursor: with db.cursor() as cursor:
cursor.execute("SELECT id FROM rss_feeds WHERE rss_feed = %s", (feed_entry.rss_feed_url,),) cursor.execute("SELECT id FROM rss_feeds WHERE rss_feed = %s", (url,),)
known = cursor.fetchone() known = cursor.fetchone()
last_entry = None last_entry_id = None
if known is None: if known is None:
ensure_feed_exists(url) ensure_feed_exists(url)
logger.info(f"[FastApi] Adding new RSS feed, type={feed_entry.type}, rss_feed_url={url}") logger.info(f"[FastApi] Adding new RSS feed, type={feed_entry.type}, rss_feed_url={url}")
try: try:
last_entry = fetch_latest(feed_entry.type, url) last_entry_id = fetch_latest(feed_entry.type, url).entry_id
except ValueError: except (ValueError, RequestException) as e:
logger.warning(f"[FastAPI] Could not read latest entry for {url}, storing none") logger.warning(f"[FastAPI] Could not read latest entry for {url}, storing none")
raise FeedCheckFailedError(url) from e
try: try:
with db.cursor() as cursor: with db.cursor() as cursor:
cursor.execute( cursor.execute(
"INSERT into rss_feeds (type,rss_feed,last_entry) VALUES (%s,%s,%s) " "INSERT into rss_feeds (type,rss_feed,last_entry) VALUES (%s,%s,%s) "
"ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), active = 1", "ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), active = 1",
(feed_entry.type, url, last_entry.entry_id), (feed_entry.type, url, last_entry_id),
) )
feed_id = cursor.lastrowid feed_id = cursor.lastrowid
@@ -319,7 +323,7 @@ def add_feed(
raise HTTPException(status_code=500) from e raise HTTPException(status_code=500) from e
logger.info(f"[FastAPI] user_id={user_id} subscribed to feed_id={feed_id}, url={url}") logger.info(f"[FastAPI] user_id={user_id} subscribed to feed_id={feed_id}, url={url}")
return {"status":"ok","detail":"Subscribed to feed","feed_id":{feed_id}} return {"status":"ok","detail":"Subscribed to feed","feed_id":feed_id}
api.include_router(user_router) api.include_router(user_router)
api.include_router(scheduler_router) api.include_router(scheduler_router)