From 8e6dd1724356aaee1115410dc9c07b7aa452ae12 Mon Sep 17 00:00:00 2001 From: AIIrondev Date: Wed, 29 Jul 2026 11:57:34 +0200 Subject: [PATCH] Fix of the notification subscription --- Web/app.py | 51 ++++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/Web/app.py b/Web/app.py index 548bdf3..8ec6d4c 100755 --- a/Web/app.py +++ b/Web/app.py @@ -12134,44 +12134,33 @@ def get_optimal_image_quality(img, target_size_kb=80): def health_check(): return 'OK', 200 + @app.route('/api/push/subscribe', methods=['POST']) def subscribe_to_push(): - """ - Subscribe a user to push notifications - - Expects JSON payload: - { - 'subscription': { - 'endpoint': '...', - 'keys': {'p256dh': '...', 'auth': '...'} - } - } - """ if 'username' not in session: - return jsonify({'success': False, 'error': 'Not authenticated'}), 401 - + return jsonify({'success': False, 'error': 'Unauthorized'}), 401 + + data = request.get_json(silent=True) or {} + + subscription_obj = data.get('subscription') if 'subscription' in data else data + + if not subscription_obj or not isinstance(subscription_obj, dict): + app.logger.error("Invalid subscription payload received") + return jsonify({'success': False, 'error': 'Invalid payload'}), 400 + + username = session['username'] + try: - data = request.get_json(silent=True) or {} - subscription = data.get('subscription') - - if not subscription or not subscription.get('endpoint'): - return jsonify({'success': False, 'error': 'Invalid subscription'}), 400 - - username = session['username'] - success = pn.save_push_subscription(username, subscription) - + success = pn.save_push_subscription(username, subscription_obj) + if success: - app.logger.info(f'Push subscription saved for {encrypt_text(username)}') - return jsonify({ - 'success': True, - 'message': 'Successfully subscribed to push notifications' - }) + return jsonify({'success': True}) else: - return jsonify({'success': False, 'error': 'Failed to save subscription'}), 500 - + return jsonify({'success': False, 'error': 'Failed to save in DB'}), 400 + except Exception as e: - app.logger.error(f'Error subscribing to push: {e}') - return jsonify({'success': False}), 500 + app.logger.error(f"Error in subscribe_to_push route: {e}") + return jsonify({'success': False, 'error': 'Internal server error'}), 500 @app.route('/api/push/unsubscribe', methods=['POST'])