m8chaa commited on
Commit
2e5034f
·
verified ·
1 Parent(s): efc2807

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -4
app.py CHANGED
@@ -1004,9 +1004,49 @@ async def shutdown_event():
1004
  @app.post("/notifications")
1005
  async def notifications(request: Request):
1006
  try:
1007
- body = await request.body()
1008
- logging.info("Notification received: %s", body)
1009
- return {"message": "Notification received"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1010
  except Exception as e:
 
1011
  raise HTTPException(status_code=500, detail=str(e))
1012
-
 
1004
  @app.post("/notifications")
1005
  async def notifications(request: Request):
1006
  try:
1007
+ # Extract relevant headers
1008
+ channel_id = request.headers.get('X-Goog-Channel-ID')
1009
+ resource_id = request.headers.get('X-Goog-Resource-ID')
1010
+ resource_state = request.headers.get('X-Goog-Resource-State')
1011
+ resource_uri = request.headers.get('X-Goog-Resource-URI')
1012
+ changed = request.headers.get('X-Goog-Changed')
1013
+
1014
+ # Log the notification details
1015
+ logging.info(f"Notification received:")
1016
+ logging.info(f"Channel ID: {channel_id}")
1017
+ logging.info(f"Resource ID: {resource_id}")
1018
+ logging.info(f"Resource State: {resource_state}")
1019
+ logging.info(f"Resource URI: {resource_uri}")
1020
+ logging.info(f"Changed: {changed}")
1021
+
1022
+ # Determine the user associated with this notification
1023
+ user_id = None
1024
+ folder_id = None
1025
+ if channel_id in app.state.drive_watcher.channels:
1026
+ channel_info = app.state.drive_watcher.channels[channel_id]
1027
+ user_id = channel_info['user_id']
1028
+ folder_id = channel_info['folder_id']
1029
+
1030
+ if user_id and folder_id:
1031
+ logging.info(f"Notification is for user: {user_id}, folder: {folder_id}")
1032
+
1033
+ # Here you can add logic to process the notification based on the resource_state
1034
+ if resource_state == 'sync':
1035
+ logging.info("Initial sync notification")
1036
+ elif resource_state in ['add', 'update', 'change']:
1037
+ logging.info(f"Resource {resource_state}d")
1038
+ # Add logic to process new or updated files
1039
+ elif resource_state in ['trash', 'untrash']:
1040
+ logging.info(f"Resource {resource_state}ed")
1041
+ # Add logic to handle trashed or untrashed files
1042
+ elif resource_state == 'remove':
1043
+ logging.info("Resource removed")
1044
+ # Add logic to handle removed files
1045
+
1046
+ else:
1047
+ logging.warning(f"Received notification for unknown channel: {channel_id}")
1048
+
1049
+ return {"message": "Notification processed successfully"}
1050
  except Exception as e:
1051
+ logging.error(f"Error processing notification: {str(e)}")
1052
  raise HTTPException(status_code=500, detail=str(e))