Fix of the notification subscription
This commit is contained in:
+20
-31
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user