Fix of the notification subscription
This commit is contained in:
+32
-28
@@ -10479,45 +10479,49 @@ def mark_notification_read(notification_id):
|
||||
return redirect(url_for('notifications_view'))
|
||||
|
||||
|
||||
@app.route('/notifications/mark_all_read', methods=['POST'])
|
||||
@app.route('/notifications/mark-all-read', methods=['POST'])
|
||||
def mark_all_notifications_read():
|
||||
"""Mark all visible notifications as read for the current user."""
|
||||
if 'username' not in session:
|
||||
flash('Bitte melden Sie sich an.', 'error')
|
||||
return redirect(url_for('login'))
|
||||
|
||||
username = session['username']
|
||||
is_admin_user = False
|
||||
|
||||
current_permissions = us.get_effective_permissions(session['username'])
|
||||
|
||||
if not current_permissions['actions'].get('can_manage_settings', False):
|
||||
is_admin_user = True
|
||||
else:
|
||||
is_admin_user = False
|
||||
|
||||
query = {
|
||||
'$or': [
|
||||
{'Audience': 'user', 'TargetUser': username},
|
||||
]
|
||||
}
|
||||
if is_admin_user:
|
||||
query['$or'].append({'Audience': 'admin'})
|
||||
current_permissions = us.get_effective_permissions(username)
|
||||
is_admin_user = current_permissions['actions'].get('can_manage_settings', False)
|
||||
|
||||
client = None
|
||||
try:
|
||||
client = MongoClient(MONGODB_HOST, MONGODB_PORT)
|
||||
db = client[MONGODB_DB]
|
||||
result = db['notifications'].update_many(
|
||||
|
||||
# Filter-Logik: Welche Benachrichtigungen sollen als gelesen markiert werden?
|
||||
# Wenn Sie 'Audience' nutzen (wie in Ihrer notifications_view Route):
|
||||
query = {}
|
||||
if is_admin_user:
|
||||
# Ein Admin sieht sowohl an ihn gerichtete als auch allgemeine Admin-Meldungen
|
||||
query['$or'] = [
|
||||
{'TargetUser': username},
|
||||
{'Audience': 'admin'}
|
||||
]
|
||||
else:
|
||||
# Normale User sehen nur Meldungen, die an sie oder alle User gerichtet sind
|
||||
query['$or'] = [
|
||||
{'TargetUser': username},
|
||||
{'Audience': 'user'}
|
||||
]
|
||||
|
||||
# WICHTIG: Fügt den Benutzernamen per $addToSet in das ReadBy-Array ein.
|
||||
# $addToSet verhindert, dass der Name doppelt eingetragen wird, falls er schon drinsteht.
|
||||
db['notifications'].update_many(
|
||||
query,
|
||||
{
|
||||
'$addToSet': {'ReadBy': username},
|
||||
'$set': {'UpdatedAt': datetime.datetime.now()}
|
||||
}
|
||||
{'$addToSet': {'ReadBy': username}}
|
||||
)
|
||||
if result.modified_count > 0:
|
||||
_bump_notification_version(f'user:{username}')
|
||||
|
||||
flash('Alle Benachrichtigungen wurden als gelesen markiert.', 'success')
|
||||
except Exception as exc:
|
||||
app.logger.warning(f"Could not mark all notifications as read for {encrypt_text(username)}: {exc}")
|
||||
app.logger.error(f"Error marking all notifications as read: {exc}")
|
||||
flash('Fehler beim Aktualisieren der Benachrichtigungen.', 'error')
|
||||
finally:
|
||||
if client:
|
||||
client.close()
|
||||
@@ -12145,7 +12149,7 @@ def subscribe_to_push():
|
||||
return jsonify({'success': False, 'error': 'Not authenticated'}), 401
|
||||
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
data = request.get_json(silent=True) or {}
|
||||
subscription = data.get('subscription')
|
||||
|
||||
if not subscription or not subscription.get('endpoint'):
|
||||
@@ -12182,7 +12186,7 @@ def unsubscribe_from_push():
|
||||
return jsonify({'success': False, 'error': 'Not authenticated'}), 401
|
||||
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
data = request.get_json(silent=True) or {}
|
||||
endpoint = data.get('endpoint')
|
||||
|
||||
if not endpoint:
|
||||
@@ -12250,7 +12254,7 @@ def test_push_notification():
|
||||
return jsonify({'success': False, 'error': 'Admin access required'}), 403
|
||||
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
data = request.get_json(silent=True) or {}
|
||||
target_user = data.get('target_user', session['username'])
|
||||
|
||||
sent = pn.send_push_notification(
|
||||
|
||||
Reference in New Issue
Block a user