Spaces:
Running
Running
修复消息提醒红点提示
Browse files- router_messages.py +22 -3
router_messages.py
CHANGED
|
@@ -210,7 +210,21 @@ async def get_chat_history(account: str, target_account: str):
|
|
| 210 |
# 🔥 性能优化:先用只读方式检查是否有实际变更,避免无意义的写入和HF上传
|
| 211 |
# ==========================================
|
| 212 |
@router.get("/api/messages/{account}")
|
| 213 |
-
async def get_messages(account: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# 公告是只读的,先加载
|
| 215 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 216 |
|
|
@@ -240,12 +254,17 @@ async def get_messages(account: str):
|
|
| 240 |
|
| 241 |
needs_update = has_new_announcements or has_unread or has_expired
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
if not needs_update:
|
| 244 |
# 无变更,直接返回只读数据,不触发写入和HF上传
|
| 245 |
return {
|
| 246 |
"status": "success",
|
| 247 |
"data": user_msgs,
|
| 248 |
-
"unread_count":
|
| 249 |
}
|
| 250 |
|
| 251 |
# 有变更需要写入,使用 atomic_update 保证并发安全
|
|
@@ -295,7 +314,7 @@ async def get_messages(account: str):
|
|
| 295 |
result_container[0] = {
|
| 296 |
"status": "success",
|
| 297 |
"data": valid,
|
| 298 |
-
"unread_count":
|
| 299 |
}
|
| 300 |
|
| 301 |
db.atomic_update("messages.json", updater, default_data={})
|
|
|
|
| 210 |
# 🔥 性能优化:先用只读方式检查是否有实际变更,避免无意义的写入和HF上传
|
| 211 |
# ==========================================
|
| 212 |
@router.get("/api/messages/{account}")
|
| 213 |
+
async def get_messages(account: str, count_only: bool = False, current_user: str = Depends(require_auth)):
|
| 214 |
+
# 🔥 count_only 模式:轻量级轮询,只返回未读数,不标记已读
|
| 215 |
+
if count_only:
|
| 216 |
+
messages_db = db.load_data("messages.json", default_data={})
|
| 217 |
+
user_msgs = messages_db.get(account, [])
|
| 218 |
+
|
| 219 |
+
now = int(time.time())
|
| 220 |
+
seven_days = 7 * 24 * 3600
|
| 221 |
+
|
| 222 |
+
# 只统计未过期的未读消息
|
| 223 |
+
unread = sum(1 for m in user_msgs
|
| 224 |
+
if not m.get("is_read")
|
| 225 |
+
and (now - m.get("created_at", 0) < seven_days))
|
| 226 |
+
return {"status": "success", "unread_count": unread}
|
| 227 |
+
|
| 228 |
# 公告是只读的,先加载
|
| 229 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 230 |
|
|
|
|
| 254 |
|
| 255 |
needs_update = has_new_announcements or has_unread or has_expired
|
| 256 |
|
| 257 |
+
# 🔥 修复:在标记已读之前先计算真实的未读数
|
| 258 |
+
unread_before_mark = sum(1 for m in user_msgs
|
| 259 |
+
if not m.get("is_read")
|
| 260 |
+
and (now - m.get("created_at", 0) < seven_days))
|
| 261 |
+
|
| 262 |
if not needs_update:
|
| 263 |
# 无变更,直接返回只读数据,不触发写入和HF上传
|
| 264 |
return {
|
| 265 |
"status": "success",
|
| 266 |
"data": user_msgs,
|
| 267 |
+
"unread_count": unread_before_mark # 🔥 修复:返回真实的未读数
|
| 268 |
}
|
| 269 |
|
| 270 |
# 有变更需要写入,使用 atomic_update 保证并发安全
|
|
|
|
| 314 |
result_container[0] = {
|
| 315 |
"status": "success",
|
| 316 |
"data": valid,
|
| 317 |
+
"unread_count": unread_before_mark # 🔥 修复:返回标记已读前的真实未读数
|
| 318 |
}
|
| 319 |
|
| 320 |
db.atomic_update("messages.json", updater, default_data={})
|