Add backend support for users
This commit is contained in:
@@ -5,7 +5,6 @@ from model import SessionLocal, Subscription, Settings, Notification
|
||||
import json
|
||||
|
||||
# Constants
|
||||
|
||||
NTFY_TOKEN = os.getenv("NTFY_TOKEN")
|
||||
|
||||
|
||||
@@ -34,14 +33,11 @@ def fetch_ntfy_notifications(base_url, subscriptions):
|
||||
notifications.append(notification)
|
||||
|
||||
print(f"Fetched {len(notifications)} notifications")
|
||||
print(notifications)
|
||||
|
||||
return notifications
|
||||
|
||||
|
||||
def save_notifications_to_db(notifications, topic_to_subscription, db):
|
||||
"""Save the fetched notifications to the database and update last_message_id."""
|
||||
db = SessionLocal()
|
||||
last_message_ids = {}
|
||||
for notification in notifications:
|
||||
topic = notification["topic"]
|
||||
@@ -67,33 +63,26 @@ def save_notifications_to_db(notifications, topic_to_subscription, db):
|
||||
if subscription:
|
||||
subscription.last_message_id = message_id
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to fetch and save notifications."""
|
||||
db = SessionLocal()
|
||||
|
||||
# Get the ntfy base URL from settings
|
||||
settings = db.query(Settings).filter(Settings.user == "default").first()
|
||||
if not settings:
|
||||
print("Default user settings not found.")
|
||||
return
|
||||
|
||||
ntfy_url = settings.ntfy_url
|
||||
def process_user_notifications(user_settings, db):
|
||||
"""Process notifications for a specific user's subscriptions."""
|
||||
ntfy_url = user_settings.ntfy_url
|
||||
|
||||
if not ntfy_url:
|
||||
print("Ntfy URL not found in settings.")
|
||||
print(f"Ntfy URL not found for user ID {user_settings.user_id}. Skipping...")
|
||||
return
|
||||
|
||||
# Get all subscribed topics
|
||||
subscriptions = db.query(Subscription).all()
|
||||
# Get all subscriptions for the user
|
||||
subscriptions = (
|
||||
db.query(Subscription)
|
||||
.filter(Subscription.user_id == user_settings.user_id)
|
||||
.all()
|
||||
)
|
||||
topic_to_subscription = {
|
||||
subscription.topic: subscription.id for subscription in subscriptions
|
||||
}
|
||||
|
||||
db.close()
|
||||
|
||||
# Fetch notifications from ntfy.sh
|
||||
notifications = fetch_ntfy_notifications(ntfy_url, subscriptions)
|
||||
|
||||
@@ -101,5 +90,24 @@ def main():
|
||||
save_notifications_to_db(notifications, topic_to_subscription, db)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to fetch and save notifications for all users."""
|
||||
db = SessionLocal()
|
||||
|
||||
# Get all user settings
|
||||
user_settings_list = db.query(Settings).all()
|
||||
|
||||
if not user_settings_list:
|
||||
print("No user settings found.")
|
||||
return
|
||||
|
||||
# Process notifications for each user
|
||||
for user_settings in user_settings_list:
|
||||
print(f"Processing notifications for user ID {user_settings.user_id}")
|
||||
process_user_notifications(user_settings, db)
|
||||
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user