Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -763,13 +763,23 @@ async def update_profile(
|
|
| 763 |
raise HTTPException(401)
|
| 764 |
db = await get_db()
|
| 765 |
try:
|
|
|
|
| 766 |
await db.execute(
|
| 767 |
"UPDATE users SET display_name = ? WHERE id = ?",
|
| 768 |
-
(
|
| 769 |
)
|
| 770 |
await db.commit()
|
| 771 |
schedule_db_sync()
|
| 772 |
-
user['display_name'] =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 773 |
return {"user": user}
|
| 774 |
finally:
|
| 775 |
await db.close()
|
|
@@ -884,6 +894,16 @@ async def upload_avatar(
|
|
| 884 |
schedule_db_sync()
|
| 885 |
finally:
|
| 886 |
await db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 887 |
return {"avatar_path": remote_path}
|
| 888 |
|
| 889 |
|
|
@@ -1398,6 +1418,17 @@ class ConnectionManager:
|
|
| 1398 |
for uid, info in self.active.items()
|
| 1399 |
]
|
| 1400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1401 |
def is_online(self, user_id: int) -> bool:
|
| 1402 |
return user_id in self.active
|
| 1403 |
|
|
@@ -1883,12 +1914,10 @@ async def ws_endpoint(ws: WebSocket, token: str = Query(...)):
|
|
| 1883 |
except Exception as e:
|
| 1884 |
logger.error(f"WS error for {username}: {e}", exc_info=True)
|
| 1885 |
finally:
|
| 1886 |
-
|
| 1887 |
-
#
|
| 1888 |
-
if uid in manager.active and manager.active[uid]["connections"]:
|
| 1889 |
-
still_online = True
|
| 1890 |
manager.disconnect(ws, uid)
|
| 1891 |
-
if not
|
| 1892 |
db = await get_db()
|
| 1893 |
try:
|
| 1894 |
await db.execute(
|
|
|
|
| 763 |
raise HTTPException(401)
|
| 764 |
db = await get_db()
|
| 765 |
try:
|
| 766 |
+
updated_name = display_name.strip() or user['username']
|
| 767 |
await db.execute(
|
| 768 |
"UPDATE users SET display_name = ? WHERE id = ?",
|
| 769 |
+
(updated_name, user['id'])
|
| 770 |
)
|
| 771 |
await db.commit()
|
| 772 |
schedule_db_sync()
|
| 773 |
+
user['display_name'] = updated_name
|
| 774 |
+
# Live presence uses in-memory state; keep the online-users list fresh
|
| 775 |
+
manager.update_profile(user['id'], display_name=updated_name, avatar_path=user.get('avatar_path'))
|
| 776 |
+
await manager.broadcast({
|
| 777 |
+
"type": "profile_updated",
|
| 778 |
+
"user_id": user['id'],
|
| 779 |
+
"username": user['username'],
|
| 780 |
+
"display_name": updated_name,
|
| 781 |
+
"avatar_path": user.get('avatar_path'),
|
| 782 |
+
})
|
| 783 |
return {"user": user}
|
| 784 |
finally:
|
| 785 |
await db.close()
|
|
|
|
| 894 |
schedule_db_sync()
|
| 895 |
finally:
|
| 896 |
await db.close()
|
| 897 |
+
|
| 898 |
+
# Live presence uses in-memory state; keep the online-users list fresh
|
| 899 |
+
manager.update_profile(user['id'], avatar_path=remote_path)
|
| 900 |
+
await manager.broadcast({
|
| 901 |
+
"type": "profile_updated",
|
| 902 |
+
"user_id": user['id'],
|
| 903 |
+
"username": user['username'],
|
| 904 |
+
"display_name": user['display_name'],
|
| 905 |
+
"avatar_path": remote_path,
|
| 906 |
+
})
|
| 907 |
return {"avatar_path": remote_path}
|
| 908 |
|
| 909 |
|
|
|
|
| 1418 |
for uid, info in self.active.items()
|
| 1419 |
]
|
| 1420 |
|
| 1421 |
+
def update_profile(self, user_id: int, display_name: Optional[str] = None,
|
| 1422 |
+
avatar_path: Optional[str] = None):
|
| 1423 |
+
"""Keep the in-memory presence record in sync with profile changes."""
|
| 1424 |
+
if user_id not in self.active:
|
| 1425 |
+
return
|
| 1426 |
+
info = self.active[user_id]
|
| 1427 |
+
if display_name is not None and display_name != "":
|
| 1428 |
+
info["display_name"] = display_name
|
| 1429 |
+
if avatar_path is not None:
|
| 1430 |
+
info["avatar_path"] = avatar_path
|
| 1431 |
+
|
| 1432 |
def is_online(self, user_id: int) -> bool:
|
| 1433 |
return user_id in self.active
|
| 1434 |
|
|
|
|
| 1914 |
except Exception as e:
|
| 1915 |
logger.error(f"WS error for {username}: {e}", exc_info=True)
|
| 1916 |
finally:
|
| 1917 |
+
# Remove this socket first. If the user still has another connection
|
| 1918 |
+
# (multi-tab), they stay online; otherwise they are now fully offline.
|
|
|
|
|
|
|
| 1919 |
manager.disconnect(ws, uid)
|
| 1920 |
+
if uid not in manager.active:
|
| 1921 |
db = await get_db()
|
| 1922 |
try:
|
| 1923 |
await db.execute(
|