From 2f68a82c3361c5707f337295f078ff8a7c48bd1e Mon Sep 17 00:00:00 2001 From: Karl Date: Thu, 17 Jul 2025 16:22:56 +0100 Subject: [PATCH] notification support --- app.py | 22 ++++++++++++++++++++++ requirements.txt | Bin 198 -> 220 bytes templates/base.html | 24 ++++++++++++++---------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index a72ba23..b465736 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ import requests.auth import os import base64 from typing import Dict, Any, Tuple, Union +from pywebpush import webpush, WebPushException from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired from lib.reqs import (get_urls, get_user_accounts, add_user_account, @@ -90,6 +91,27 @@ def index() -> Union[Response, str]: return redirect(url_for("home")) return render_template("index.html") +@app.route('/vapid-public-key') +def vapid_public_key(): + """Provides the VAPID public key to the client.""" + return jsonify({'public_key': app.config['VAPID_PUBLIC_KEY']}) + +@app.route('/save-subscription', methods=['POST']) +def save_subscription(): + """Saves a push notification subscription.""" + if not session.get("logged_in"): + return jsonify({'error': 'Unauthorized'}), 401 + + subscription_data = request.get_json() + if not subscription_data: + return jsonify({'error': 'No subscription data received'}), 400 + + # Here you would typically save the subscription to a database + # For this example, we'll just store it in the session + session['push_subscription'] = subscription_data + print(f"Subscription saved for user: {session['username']}") + return jsonify({'success': True}) + @app.route("/home") @cache.cached(timeout=60) def home() -> str: diff --git a/requirements.txt b/requirements.txt index e37a2d42720295005cafacd55e2693fc7cfcbb5f..67890d400c567735bb5799ebf1c517ebc1c3f486 100644 GIT binary patch delta 29 jcmX@cc!zPqF)>~SE`|bzN``WVRE8uVtCXRbA%g(`dC> response.json()) + .then(data => { + const applicationServerKey = urlB64ToUint8Array(data.public_key); + registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: applicationServerKey + }).then(function(subscription) { + console.log('User is subscribed.'); + saveSubscription(subscription); + }).catch(function(err) { + console.log('Failed to subscribe the user: ', err); + }); + }); } function saveSubscription(subscription) {