Spaces:
Running
Running
优化消息
Browse files- router_messages.py +37 -2
router_messages.py
CHANGED
|
@@ -203,12 +203,48 @@ async def get_chat_history(account: str, target_account: str):
|
|
| 203 |
# ==========================================
|
| 204 |
# 改造:获取通知列表 (加入系统公告懒加载注入)
|
| 205 |
# 使用 atomic_update 避免并发覆盖问题
|
|
|
|
| 206 |
# ==========================================
|
| 207 |
@router.get("/api/messages/{account}")
|
| 208 |
async def get_messages(account: str):
|
| 209 |
-
# 公告是只读的,
|
| 210 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
result_container = [None]
|
| 213 |
|
| 214 |
def updater(data):
|
|
@@ -248,7 +284,6 @@ async def get_messages(account: str):
|
|
| 248 |
if not m.get("is_read"):
|
| 249 |
m["is_read"] = True
|
| 250 |
modified = True
|
| 251 |
-
|
| 252 |
# 原地修改 data,atomic_update 会自动保存
|
| 253 |
data[account] = valid
|
| 254 |
|
|
|
|
| 203 |
# ==========================================
|
| 204 |
# 改造:获取通知列表 (加入系统公告懒加载注入)
|
| 205 |
# 使用 atomic_update 避免并发覆盖问题
|
| 206 |
+
# 🔥 性能优化:先用只读方式检查是否有实际变更,避免无意义的写入和HF上传
|
| 207 |
# ==========================================
|
| 208 |
@router.get("/api/messages/{account}")
|
| 209 |
async def get_messages(account: str):
|
| 210 |
+
# 公告是只读的,先加载
|
| 211 |
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 212 |
|
| 213 |
+
# 🔥 性能优化:先用只读方式检查是否有实际变更需要写入
|
| 214 |
+
messages_db = db.load_data("messages.json", default_data={})
|
| 215 |
+
user_msgs = messages_db.get(account, [])
|
| 216 |
+
|
| 217 |
+
now = int(time.time())
|
| 218 |
+
seven_days = 7 * 24 * 3600
|
| 219 |
+
|
| 220 |
+
# 检查三个条件判断是否需要写入
|
| 221 |
+
# 1. 是否有新公告需要注入
|
| 222 |
+
user_msg_ids = {m.get("id") for m in user_msgs}
|
| 223 |
+
has_new_announcements = any(
|
| 224 |
+
ann.get("id") not in user_msg_ids
|
| 225 |
+
for ann in announcements_db
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# 2. 是否有未读消息需要标记已读
|
| 229 |
+
has_unread = any(not m.get("is_read") for m in user_msgs)
|
| 230 |
+
|
| 231 |
+
# 3. 是否有已读超过7天的消息需要清理
|
| 232 |
+
has_expired = any(
|
| 233 |
+
m.get("is_read") and (now - m.get("created_at", 0) >= seven_days)
|
| 234 |
+
for m in user_msgs
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
needs_update = has_new_announcements or has_unread or has_expired
|
| 238 |
+
|
| 239 |
+
if not needs_update:
|
| 240 |
+
# 无变更,直接返回只读数据,不触发写入和HF上传
|
| 241 |
+
return {
|
| 242 |
+
"status": "success",
|
| 243 |
+
"data": user_msgs,
|
| 244 |
+
"unread_count": 0 # 用户已查看消息列表,未读数设为0
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
# 有变更需要写入,使用 atomic_update 保证并发安全
|
| 248 |
result_container = [None]
|
| 249 |
|
| 250 |
def updater(data):
|
|
|
|
| 284 |
if not m.get("is_read"):
|
| 285 |
m["is_read"] = True
|
| 286 |
modified = True
|
|
|
|
| 287 |
# 原地修改 data,atomic_update 会自动保存
|
| 288 |
data[account] = valid
|
| 289 |
|