rairo commited on
Commit
8b451a8
·
verified ·
1 Parent(s): 0e49d4b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +81 -6
main.py CHANGED
@@ -1103,20 +1103,95 @@ def admin_send_notification():
1103
  @app.route('/api/user/notifications', methods=['GET'])
1104
  def get_user_notifications():
1105
  try:
1106
- token = request.headers.get('Authorization', '').split(' ')[1]
 
 
 
 
 
 
 
 
 
 
 
 
 
1107
  uid = verify_token(token)
1108
- if not uid: return jsonify({'error': 'Unauthorized'}), 401
 
 
 
 
1109
 
 
1110
  notifications_ref = db.reference(f'notifications/{uid}')
1111
- user_notifications = notifications_ref.order_by_child('created_at').get() or {}
1112
 
1113
- # Sort descending (newest first)
1114
- sorted_notifications = sorted(user_notifications.values(), key=lambda item: item['created_at'], reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1116
  return jsonify(sorted_notifications), 200
 
1117
  except Exception as e:
1118
  logger.error(f"CRITICAL ERROR getting notifications: {traceback.format_exc()}")
1119
- return jsonify({'error': str(e)}), 500
 
 
1120
 
1121
  @app.route('/api/user/notifications/<string:notification_id>/read', methods=['POST'])
1122
  def mark_notification_read(notification_id):
 
1103
  @app.route('/api/user/notifications', methods=['GET'])
1104
  def get_user_notifications():
1105
  try:
1106
+ logger.info("Getting user notifications - start")
1107
+
1108
+ # Get and validate authorization token
1109
+ auth_header = request.headers.get('Authorization', '')
1110
+ logger.info(f"Authorization header present: {bool(auth_header)}")
1111
+
1112
+ if not auth_header or not auth_header.startswith('Bearer '):
1113
+ logger.warning("Missing or invalid Authorization header format")
1114
+ return jsonify({'error': 'Missing or invalid authorization header'}), 401
1115
+
1116
+ token = auth_header.split(' ')[1]
1117
+ logger.info(f"Token extracted, length: {len(token) if token else 0}")
1118
+
1119
+ # Verify token and get user ID
1120
  uid = verify_token(token)
1121
+ if not uid:
1122
+ logger.warning(f"Token verification failed for token: {token[:20]}...")
1123
+ return jsonify({'error': 'Unauthorized'}), 401
1124
+
1125
+ logger.info(f"User authenticated: {uid}")
1126
 
1127
+ # Get notifications reference
1128
  notifications_ref = db.reference(f'notifications/{uid}')
1129
+ logger.info(f"Notifications reference created for path: notifications/{uid}")
1130
 
1131
+ # Try to get notifications with error handling
1132
+ try:
1133
+ user_notifications = notifications_ref.order_by_child('created_at').get()
1134
+ logger.info(f"Notifications query successful, raw result type: {type(user_notifications)}")
1135
+ logger.info(f"Raw notifications data: {user_notifications}")
1136
+ except Exception as db_error:
1137
+ logger.error(f"Database query failed: {str(db_error)}")
1138
+ # Fallback to unordered query
1139
+ logger.info("Attempting fallback to unordered query")
1140
+ user_notifications = notifications_ref.get()
1141
+ logger.info(f"Fallback query result: {user_notifications}")
1142
+
1143
+ # Handle empty or None results
1144
+ if not user_notifications:
1145
+ logger.info("No notifications found for user")
1146
+ return jsonify([]), 200
1147
+
1148
+ # Convert to list and handle different data structures
1149
+ notifications_list = []
1150
+
1151
+ if isinstance(user_notifications, dict):
1152
+ logger.info(f"Processing dict with {len(user_notifications)} items")
1153
+ for key, notification in user_notifications.items():
1154
+ if isinstance(notification, dict):
1155
+ # Add the key as id if not present
1156
+ notification_copy = notification.copy()
1157
+ if 'id' not in notification_copy:
1158
+ notification_copy['id'] = key
1159
+ notifications_list.append(notification_copy)
1160
+ logger.debug(f"Added notification: {key}")
1161
+ else:
1162
+ logger.warning(f"Unexpected notification format for key {key}: {type(notification)}")
1163
+ else:
1164
+ logger.warning(f"Unexpected notifications data type: {type(user_notifications)}")
1165
+ return jsonify({'error': 'Unexpected data format'}), 500
1166
+
1167
+ logger.info(f"Processed {len(notifications_list)} notifications")
1168
 
1169
+ # Sort notifications by created_at (newest first)
1170
+ try:
1171
+ sorted_notifications = sorted(
1172
+ notifications_list,
1173
+ key=lambda item: item.get('created_at', 0),
1174
+ reverse=True
1175
+ )
1176
+ logger.info(f"Notifications sorted successfully, count: {len(sorted_notifications)}")
1177
+
1178
+ # Log first notification for debugging
1179
+ if sorted_notifications:
1180
+ logger.debug(f"First notification: {sorted_notifications[0]}")
1181
+
1182
+ except Exception as sort_error:
1183
+ logger.error(f"Error sorting notifications: {str(sort_error)}")
1184
+ # Return unsorted if sorting fails
1185
+ sorted_notifications = notifications_list
1186
+
1187
+ logger.info(f"Returning {len(sorted_notifications)} notifications")
1188
  return jsonify(sorted_notifications), 200
1189
+
1190
  except Exception as e:
1191
  logger.error(f"CRITICAL ERROR getting notifications: {traceback.format_exc()}")
1192
+ logger.error(f"Exception type: {type(e).__name__}")
1193
+ logger.error(f"Exception message: {str(e)}")
1194
+ return jsonify({'error': 'Internal server error'}), 500
1195
 
1196
  @app.route('/api/user/notifications/<string:notification_id>/read', methods=['POST'])
1197
  def mark_notification_read(notification_id):