Fix of the notification subscription

This commit is contained in:
2026-07-29 11:35:04 +02:00
parent 4d9bb62907
commit a49ed98ed7
3 changed files with 50 additions and 52 deletions
+11 -15
View File
@@ -122,15 +122,15 @@ def get_user_subscriptions(username):
def save_push_subscription(username, subscription_obj):
"""
Save a new push subscription for a user with field-level encryption.
"""
"""Save a new push subscription for a user with field-level encryption."""
try:
endpoint = subscription_obj.get('endpoint')
if not endpoint:
logger.warning('Invalid subscription object: missing endpoint')
keys = subscription_obj.get('keys', {})
if not endpoint or not keys.get('p256dh') or not keys.get('auth'):
logger.warning(f'Invalid subscription object for {username}: missing endpoint or keys')
return False
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
db = client[cfg.MONGODB_DB]
subs_col = get_push_subscriptions_collection(db)
@@ -184,27 +184,23 @@ def save_push_subscription(username, subscription_obj):
def remove_push_subscription(username, endpoint):
"""
Remove a push subscription by making it inactive.
"""
try:
client = MongoClient(cfg.MONGODB_HOST, cfg.MONGODB_PORT)
db = client[cfg.MONGODB_DB]
subs_col = get_push_subscriptions_collection(db)
# Recreate the deterministic hash to find the specific subscription
sub_hash = hashlib.shake_256(
f"{username}:{endpoint}".encode('utf-8')
).hexdigest()
result = subs_col.update_one(
{'SubscriptionHash': sub_hash},
{'$set': {'IsActive': False}}
)
client.close()
return result.modified_count > 0
return result.matched_count > 0
except Exception as e:
logger.error(f'Error removing push subscription: {e}')
return False