a9 commited on
Commit
6ce8b8f
·
verified ·
1 Parent(s): 92d5821

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -0
app.py CHANGED
@@ -29,6 +29,9 @@ class ConnectionManager:
29
  if user_id in self.active_connections:
30
  del self.active_connections[user_id]
31
 
 
 
 
32
  async def send_personal_message(self, message: Dict, user_id: int):
33
  """Sends a JSON message to a single connected user."""
34
  if user_id in self.active_connections:
@@ -163,6 +166,13 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
163
  online_status = {"type": "status_update", "user_id": user_id, "is_online": True}
164
  await manager.broadcast(online_status)
165
 
 
 
 
 
 
 
 
166
  # 3. <--- NEW IMPLEMENTATION HERE --->
167
  # Attempt to send any messages that accumulated while the user was offline.
168
  await send_undelivered_messages(user_id)
 
29
  if user_id in self.active_connections:
30
  del self.active_connections[user_id]
31
 
32
+ def isOnline(self, user_id: int):
33
+ return user_id in self.active_connections
34
+
35
  async def send_personal_message(self, message: Dict, user_id: int):
36
  """Sends a JSON message to a single connected user."""
37
  if user_id in self.active_connections:
 
166
  online_status = {"type": "status_update", "user_id": user_id, "is_online": True}
167
  await manager.broadcast(online_status)
168
 
169
+ # Send the other user's status to the newly connected user
170
+ for other_user_id in VALID_USER_IDS:
171
+ if other_user_id != user_id:
172
+ if not manager.isOnline(other_user_id):
173
+ offline_status = {"type": "status_update", "user_id": other_user_id, "is_online": False, "last_seen": userLastOnlineTime.get(other_user_id, 0.0)}
174
+ await manager.send_personal_message(offline_status, user_id)
175
+
176
  # 3. <--- NEW IMPLEMENTATION HERE --->
177
  # Attempt to send any messages that accumulated while the user was offline.
178
  await send_undelivered_messages(user_id)