Spaces:
Running
Running
Upload routers.py
Browse files- routers.py +52 -13
routers.py
CHANGED
|
@@ -215,14 +215,14 @@ async def soft_delete_comment(item_id: str, comment_id: str, author: str):
|
|
| 215 |
return {"status": "success"}
|
| 216 |
raise HTTPException(status_code=404, detail="找不到该评论")
|
| 217 |
|
| 218 |
-
# --- 4. 专属聊天信箱聚合 ---
|
| 219 |
@router.post("/api/messages/private")
|
| 220 |
async def send_private_message(msg: PrivateMessage):
|
| 221 |
chats_db = db.load_data("chats.json", default_data={})
|
| 222 |
conv_id = f"{min(msg.sender, msg.receiver)}_{max(msg.sender, msg.receiver)}"
|
| 223 |
if conv_id not in chats_db: chats_db[conv_id] = []
|
| 224 |
|
| 225 |
-
chat_msg = {"id": f"chat_{int(time.time())}", "sender": msg.sender, "receiver": msg.receiver, "content": msg.content, "created_at": int(time.time()), "is_read": False}
|
| 226 |
chats_db[conv_id].append(chat_msg)
|
| 227 |
db.save_data("chats.json", chats_db)
|
| 228 |
|
|
@@ -234,16 +234,22 @@ async def get_chat_list(account: str):
|
|
| 234 |
chats_db = db.load_data("chats.json", default_data={})
|
| 235 |
users_db = db.load_data("users.json", default_data={})
|
| 236 |
chat_list = []
|
|
|
|
|
|
|
|
|
|
| 237 |
for conv_id, msgs in chats_db.items():
|
| 238 |
accs = conv_id.split("_")
|
| 239 |
if account in accs and msgs:
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
chat_list.sort(key=lambda x: x["last_time"], reverse=True)
|
| 248 |
return {"status": "success", "data": chat_list}
|
| 249 |
|
|
@@ -253,17 +259,50 @@ async def get_chat_history(account: str, target: str):
|
|
| 253 |
conv_id = f"{min(account, target)}_{max(account, target)}"
|
| 254 |
msgs = chats_db.get(conv_id, [])
|
| 255 |
|
|
|
|
|
|
|
|
|
|
| 256 |
modified = False
|
|
|
|
| 257 |
for m in msgs:
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
@router.get("/api/messages/{account}")
|
| 263 |
async def get_messages(account: str):
|
| 264 |
msgs_db = db.load_data("messages.json", default_data={})
|
| 265 |
user_msgs = msgs_db.get(account, [])
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
@router.post("/api/messages/{account}/read")
|
| 269 |
async def mark_messages_read(account: str):
|
|
|
|
| 215 |
return {"status": "success"}
|
| 216 |
raise HTTPException(status_code=404, detail="找不到该评论")
|
| 217 |
|
| 218 |
+
# --- 4. 专属聊天信箱聚合 (【核心更新】:已读超过7天自动释放) ---
|
| 219 |
@router.post("/api/messages/private")
|
| 220 |
async def send_private_message(msg: PrivateMessage):
|
| 221 |
chats_db = db.load_data("chats.json", default_data={})
|
| 222 |
conv_id = f"{min(msg.sender, msg.receiver)}_{max(msg.sender, msg.receiver)}"
|
| 223 |
if conv_id not in chats_db: chats_db[conv_id] = []
|
| 224 |
|
| 225 |
+
chat_msg = {"id": f"chat_{int(time.time())}_{uuid.uuid4().hex[:6]}", "sender": msg.sender, "receiver": msg.receiver, "content": msg.content, "created_at": int(time.time()), "is_read": False}
|
| 226 |
chats_db[conv_id].append(chat_msg)
|
| 227 |
db.save_data("chats.json", chats_db)
|
| 228 |
|
|
|
|
| 234 |
chats_db = db.load_data("chats.json", default_data={})
|
| 235 |
users_db = db.load_data("users.json", default_data={})
|
| 236 |
chat_list = []
|
| 237 |
+
now = int(time.time())
|
| 238 |
+
seven_days = 7 * 24 * 3600
|
| 239 |
+
|
| 240 |
for conv_id, msgs in chats_db.items():
|
| 241 |
accs = conv_id.split("_")
|
| 242 |
if account in accs and msgs:
|
| 243 |
+
# 列表中只统计有效消息
|
| 244 |
+
valid_msgs = [m for m in msgs if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days)]
|
| 245 |
+
if valid_msgs:
|
| 246 |
+
target = accs[0] if accs[1] == account else accs[1]
|
| 247 |
+
target_info = users_db.get(target, {})
|
| 248 |
+
chat_list.append({
|
| 249 |
+
"target_account": target, "target_name": target_info.get("name", target),
|
| 250 |
+
"last_message": valid_msgs[-1]["content"], "last_time": valid_msgs[-1]["created_at"],
|
| 251 |
+
"unread": sum(1 for m in valid_msgs if m["receiver"] == account and not m.get("is_read"))
|
| 252 |
+
})
|
| 253 |
chat_list.sort(key=lambda x: x["last_time"], reverse=True)
|
| 254 |
return {"status": "success", "data": chat_list}
|
| 255 |
|
|
|
|
| 259 |
conv_id = f"{min(account, target)}_{max(account, target)}"
|
| 260 |
msgs = chats_db.get(conv_id, [])
|
| 261 |
|
| 262 |
+
now = int(time.time())
|
| 263 |
+
seven_days = 7 * 24 * 3600
|
| 264 |
+
valid_msgs = []
|
| 265 |
modified = False
|
| 266 |
+
|
| 267 |
for m in msgs:
|
| 268 |
+
# 【核心策略】:未读的永远保留,已读的超过 7 天直接抛弃
|
| 269 |
+
if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days):
|
| 270 |
+
valid_msgs.append(m)
|
| 271 |
+
else:
|
| 272 |
+
modified = True
|
| 273 |
+
|
| 274 |
+
# 本次访问即为已读
|
| 275 |
+
if m["receiver"] == account and not m.get("is_read"):
|
| 276 |
+
m["is_read"] = True
|
| 277 |
+
modified = True
|
| 278 |
+
|
| 279 |
+
if modified or len(valid_msgs) != len(msgs):
|
| 280 |
+
chats_db[conv_id] = valid_msgs
|
| 281 |
+
db.save_data("chats.json", chats_db)
|
| 282 |
+
|
| 283 |
+
return {"status": "success", "data": valid_msgs}
|
| 284 |
|
| 285 |
@router.get("/api/messages/{account}")
|
| 286 |
async def get_messages(account: str):
|
| 287 |
msgs_db = db.load_data("messages.json", default_data={})
|
| 288 |
user_msgs = msgs_db.get(account, [])
|
| 289 |
+
|
| 290 |
+
now = int(time.time())
|
| 291 |
+
seven_days = 7 * 24 * 3600
|
| 292 |
+
valid = []
|
| 293 |
+
modified = False
|
| 294 |
+
|
| 295 |
+
for m in user_msgs:
|
| 296 |
+
if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days):
|
| 297 |
+
valid.append(m)
|
| 298 |
+
else:
|
| 299 |
+
modified = True
|
| 300 |
+
|
| 301 |
+
if modified:
|
| 302 |
+
msgs_db[account] = valid
|
| 303 |
+
db.save_data("messages.json", msgs_db)
|
| 304 |
+
|
| 305 |
+
return {"status": "success", "data": valid, "unread_count": sum(1 for m in valid if not m.get("is_read"))}
|
| 306 |
|
| 307 |
@router.post("/api/messages/{account}/read")
|
| 308 |
async def mark_messages_read(account: str):
|