diff --git a/ktvmanager/account_checker.py b/ktvmanager/account_checker.py index 87289e1..9b772da 100644 --- a/ktvmanager/account_checker.py +++ b/ktvmanager/account_checker.py @@ -53,9 +53,10 @@ def send_expiry_notifications(app) -> None: for account in expiring_accounts: expiry_date = datetime.fromtimestamp(account['expiaryDate']) - days_to_expiry = (expiry_date - now).days + days_to_expiry = (expiry_date.date() - now.date()).days if days_to_expiry == 30 or days_to_expiry == 7: + print(f"Found expiring account: {account['username']}") user_id = account['user_id'] subscriptions = get_push_subscriptions(user_id) for sub in subscriptions: @@ -67,8 +68,11 @@ def send_expiry_notifications(app) -> None: if last_notified and last_notified.date() == now.date(): continue - message = f"Your account {account['username']} is due to expire in {days_to_expiry} days." - send_notification(sub['subscription_json'], message) + message = { + "title": "Account Expiry Warning", + "body": f"Your account {account['username']} is due to expire in {days_to_expiry} days." + } + send_notification(sub['subscription_json'], json.dumps(message)) # Update the last notified timestamp update_last_notified_query = "UPDATE push_subscriptions SET last_notified = %s WHERE id = %s" diff --git a/ktvmanager/lib/database.py b/ktvmanager/lib/database.py index f527c6c..4ff2c26 100644 --- a/ktvmanager/lib/database.py +++ b/ktvmanager/lib/database.py @@ -237,3 +237,14 @@ def get_push_subscriptions(user_id: Optional[int] = None) -> List[Dict[str, Any] else: query = "SELECT * FROM push_subscriptions" return _execute_query(query) + + +def delete_push_subscription(subscription_json: str) -> None: + """Deletes a push subscription from the database. + + Args: + subscription_json: The push subscription information as a JSON string. + """ + query = "DELETE FROM push_subscriptions WHERE subscription_json = %s" + params = (subscription_json,) + _execute_query(query, params) diff --git a/ktvmanager/lib/notifications.py b/ktvmanager/lib/notifications.py index 436c1d4..e68f60e 100644 --- a/ktvmanager/lib/notifications.py +++ b/ktvmanager/lib/notifications.py @@ -1,8 +1,11 @@ +import json from flask import current_app from pywebpush import webpush, WebPushException +from ktvmanager.lib.database import delete_push_subscription -def send_notification(subscription_info, message_body): +def send_notification(subscription_json, message_body): try: + subscription_info = json.loads(subscription_json) webpush( subscription_info=subscription_info, data=message_body, @@ -11,7 +14,6 @@ def send_notification(subscription_info, message_body): ) except WebPushException as ex: print(f"Web push error: {ex}") - # You might want to remove the subscription if it's invalid if ex.response and ex.response.status_code == 410: print("Subscription is no longer valid, removing from DB.") - # Add logic to remove the subscription from your database \ No newline at end of file + delete_push_subscription(subscription_json) \ No newline at end of file diff --git a/routes/api.py b/routes/api.py index 5698151..bec76a4 100644 --- a/routes/api.py +++ b/routes/api.py @@ -237,7 +237,7 @@ def send_test_notification_route(username: str, password: str) -> Response: for sub in subscriptions: try: - send_notification(json.loads(sub['subscription_json']), message_body) + send_notification(sub['subscription_json'], message_body) success_count += 1 except Exception as e: print(f"Error sending notification to subscription ID {sub.get('id', 'N/A')}: {e}")