teoat commited on
Commit
cedf681
·
verified ·
1 Parent(s): 731fe8f

Upload app/routers/realtime_sync.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app/routers/realtime_sync.py +53 -17
app/routers/realtime_sync.py CHANGED
@@ -27,7 +27,9 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
27
  token = query_params.get("token")
28
 
29
  if not token:
30
- await websocket.send_json({"type": "error", "message": "Authentication token required"})
 
 
31
  await websocket.close(code=1008, reason="Authentication required")
32
  return
33
 
@@ -40,20 +42,26 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
40
 
41
  # Verify token belongs to requested user
42
  if token_user_id != user_id:
43
- await websocket.send_json({"type": "error", "message": "Token does not match user ID"})
 
 
44
  await websocket.close(code=1008, reason="Authentication failed")
45
  return
46
 
47
  # Check MFA if required
48
  mfa_verified = payload.get("mfa_verified", False)
49
  if not mfa_verified:
50
- await websocket.send_json({"type": "error", "message": "MFA verification required"})
 
 
51
  await websocket.close(code=1008, reason="MFA required")
52
  return
53
 
54
  except Exception as e:
55
  logger.error(f"WebSocket authentication failed: {e}")
56
- await websocket.send_json({"type": "error", "message": "Invalid authentication token"})
 
 
57
  await websocket.close(code=1008, reason="Authentication failed")
58
  return
59
  try:
@@ -64,25 +72,33 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
64
 
65
  # Verify token belongs to requested user
66
  if token_user_id != user_id:
67
- await websocket.send_json({"type": "error", "message": "Token does not match user ID"})
 
 
68
  await websocket.close(code=1008, reason="Authentication failed")
69
  return
70
 
71
  # Check MFA if required
72
  mfa_verified = payload.get("mfa_verified", False)
73
  if not mfa_verified:
74
- await websocket.send_json({"type": "error", "message": "MFA verification required"})
 
 
75
  await websocket.close(code=1008, reason="MFA required")
76
  return
77
 
78
  except Exception as e:
79
  logger.error(f"WebSocket authentication failed: {e}")
80
- await websocket.send_json({"type": "error", "message": "Invalid authentication token"})
 
 
81
  await websocket.close(code=1008, reason="Authentication failed")
82
  return
83
  else:
84
  # No token provided - require authentication
85
- await websocket.send_json({"type": "error", "message": "Authentication token required"})
 
 
86
  await websocket.close(code=1008, reason="Authentication required")
87
  return
88
 
@@ -143,7 +159,9 @@ async def handle_websocket_message(client_id: str, message: dict[str, Any]):
143
  operation_data = message.get("operation")
144
 
145
  if document_id and operation_data:
146
- await sync_manager.handle_operation(client_id, document_id, operation_data)
 
 
147
 
148
  elif message_type == "sync":
149
  # Sync client with latest state
@@ -151,11 +169,15 @@ async def handle_websocket_message(client_id: str, message: dict[str, Any]):
151
  client_vector_clock = message.get("vector_clock", {})
152
 
153
  if document_id:
154
- await sync_manager.sync_client(client_id, document_id, client_vector_clock)
 
 
155
 
156
  elif message_type == "ping":
157
  # Respond to ping
158
- await sync_manager._send_to_client(client_id, {"type": "pong", "timestamp": datetime.now().isoformat()})
 
 
159
 
160
  else:
161
  logger.warning(f"Unknown message type: {message_type}")
@@ -205,7 +227,9 @@ async def get_document(document_id: str):
205
  """Get specific document details"""
206
  try:
207
  if document_id not in sync_manager.documents:
208
- raise HTTPException(status_code=404, detail=f"Document {document_id} not found")
 
 
209
 
210
  document = sync_manager.documents[document_id]
211
  state = document.get_state()
@@ -213,8 +237,14 @@ async def get_document(document_id: str):
213
  # Add additional metadata
214
  state.update(
215
  {
216
- "subscribers_count": sum(1 for subs in sync_manager.client_subscriptions.values() if document_id in subs),
217
- "operations": [asdict(op) for op in document.operations[-10:]], # Last 10 operations
 
 
 
 
 
 
218
  }
219
  )
220
 
@@ -275,7 +305,9 @@ async def create_operation(document_id: str, operation_data: dict[str, Any]):
275
  raise
276
  except Exception as e:
277
  logger.error(f"Error creating operation: {e!s}")
278
- raise HTTPException(status_code=500, detail=f"Failed to create operation: {e!s}")
 
 
279
 
280
 
281
  @router.get("/stats")
@@ -288,7 +320,9 @@ async def get_sync_stats():
288
  stats.update(
289
  {
290
  "active_documents": len(sync_manager.documents),
291
- "total_operations": sum(len(doc.operations) for doc in sync_manager.documents.values()),
 
 
292
  "server_timestamp": datetime.now().isoformat(),
293
  }
294
  )
@@ -331,7 +365,9 @@ async def delete_document(document_id: str):
331
  """Delete a collaborative document"""
332
  try:
333
  if document_id not in sync_manager.documents:
334
- raise HTTPException(status_code=404, detail=f"Document {document_id} not found")
 
 
335
 
336
  # Notify all subscribers
337
  message = {
 
27
  token = query_params.get("token")
28
 
29
  if not token:
30
+ await websocket.send_json(
31
+ {"type": "error", "message": "Authentication token required"}
32
+ )
33
  await websocket.close(code=1008, reason="Authentication required")
34
  return
35
 
 
42
 
43
  # Verify token belongs to requested user
44
  if token_user_id != user_id:
45
+ await websocket.send_json(
46
+ {"type": "error", "message": "Token does not match user ID"}
47
+ )
48
  await websocket.close(code=1008, reason="Authentication failed")
49
  return
50
 
51
  # Check MFA if required
52
  mfa_verified = payload.get("mfa_verified", False)
53
  if not mfa_verified:
54
+ await websocket.send_json(
55
+ {"type": "error", "message": "MFA verification required"}
56
+ )
57
  await websocket.close(code=1008, reason="MFA required")
58
  return
59
 
60
  except Exception as e:
61
  logger.error(f"WebSocket authentication failed: {e}")
62
+ await websocket.send_json(
63
+ {"type": "error", "message": "Invalid authentication token"}
64
+ )
65
  await websocket.close(code=1008, reason="Authentication failed")
66
  return
67
  try:
 
72
 
73
  # Verify token belongs to requested user
74
  if token_user_id != user_id:
75
+ await websocket.send_json(
76
+ {"type": "error", "message": "Token does not match user ID"}
77
+ )
78
  await websocket.close(code=1008, reason="Authentication failed")
79
  return
80
 
81
  # Check MFA if required
82
  mfa_verified = payload.get("mfa_verified", False)
83
  if not mfa_verified:
84
+ await websocket.send_json(
85
+ {"type": "error", "message": "MFA verification required"}
86
+ )
87
  await websocket.close(code=1008, reason="MFA required")
88
  return
89
 
90
  except Exception as e:
91
  logger.error(f"WebSocket authentication failed: {e}")
92
+ await websocket.send_json(
93
+ {"type": "error", "message": "Invalid authentication token"}
94
+ )
95
  await websocket.close(code=1008, reason="Authentication failed")
96
  return
97
  else:
98
  # No token provided - require authentication
99
+ await websocket.send_json(
100
+ {"type": "error", "message": "Authentication token required"}
101
+ )
102
  await websocket.close(code=1008, reason="Authentication required")
103
  return
104
 
 
159
  operation_data = message.get("operation")
160
 
161
  if document_id and operation_data:
162
+ await sync_manager.handle_operation(
163
+ client_id, document_id, operation_data
164
+ )
165
 
166
  elif message_type == "sync":
167
  # Sync client with latest state
 
169
  client_vector_clock = message.get("vector_clock", {})
170
 
171
  if document_id:
172
+ await sync_manager.sync_client(
173
+ client_id, document_id, client_vector_clock
174
+ )
175
 
176
  elif message_type == "ping":
177
  # Respond to ping
178
+ await sync_manager._send_to_client(
179
+ client_id, {"type": "pong", "timestamp": datetime.now().isoformat()}
180
+ )
181
 
182
  else:
183
  logger.warning(f"Unknown message type: {message_type}")
 
227
  """Get specific document details"""
228
  try:
229
  if document_id not in sync_manager.documents:
230
+ raise HTTPException(
231
+ status_code=404, detail=f"Document {document_id} not found"
232
+ )
233
 
234
  document = sync_manager.documents[document_id]
235
  state = document.get_state()
 
237
  # Add additional metadata
238
  state.update(
239
  {
240
+ "subscribers_count": sum(
241
+ 1
242
+ for subs in sync_manager.client_subscriptions.values()
243
+ if document_id in subs
244
+ ),
245
+ "operations": [
246
+ asdict(op) for op in document.operations[-10:]
247
+ ], # Last 10 operations
248
  }
249
  )
250
 
 
305
  raise
306
  except Exception as e:
307
  logger.error(f"Error creating operation: {e!s}")
308
+ raise HTTPException(
309
+ status_code=500, detail=f"Failed to create operation: {e!s}"
310
+ )
311
 
312
 
313
  @router.get("/stats")
 
320
  stats.update(
321
  {
322
  "active_documents": len(sync_manager.documents),
323
+ "total_operations": sum(
324
+ len(doc.operations) for doc in sync_manager.documents.values()
325
+ ),
326
  "server_timestamp": datetime.now().isoformat(),
327
  }
328
  )
 
365
  """Delete a collaborative document"""
366
  try:
367
  if document_id not in sync_manager.documents:
368
+ raise HTTPException(
369
+ status_code=404, detail=f"Document {document_id} not found"
370
+ )
371
 
372
  # Notify all subscribers
373
  message = {