Spaces:
Running
Running
Update router_messages.py
Browse files- router_messages.py +105 -172
router_messages.py
CHANGED
|
@@ -1,173 +1,106 @@
|
|
| 1 |
-
# router_messages.py
|
| 2 |
-
from fastapi import APIRouter
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
chats_db
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if modified or len(valid_msgs) != len(msgs):
|
| 108 |
-
chats_db[conv_id] = valid_msgs
|
| 109 |
-
db.save_data("chats.json", chats_db)
|
| 110 |
-
|
| 111 |
-
return {"status": "success", "data": valid_msgs}
|
| 112 |
-
|
| 113 |
-
@router.get("/api/messages/{account}")
|
| 114 |
-
async def get_messages(account: str):
|
| 115 |
-
msgs_db = db.load_data("messages.json", default_data={})
|
| 116 |
-
user_msgs = msgs_db.get(account, [])
|
| 117 |
-
|
| 118 |
-
# =========================================================
|
| 119 |
-
# 【核心新增】:系统公告懒加载注入逻辑 (Lazy Push)
|
| 120 |
-
# =========================================================
|
| 121 |
-
announcements_db = db.load_data("announcements.json", default_data=[])
|
| 122 |
-
# 获取用户已经拥有的消息 ID 集合,用于快速比对
|
| 123 |
-
user_msg_ids = {m.get("id") for m in user_msgs}
|
| 124 |
-
|
| 125 |
-
injected = False
|
| 126 |
-
for ann in announcements_db:
|
| 127 |
-
if ann.get("id") not in user_msg_ids:
|
| 128 |
-
# 如果是新公告,构造一条个人消息注入进去
|
| 129 |
-
new_sys_msg = dict(ann) # 浅拷贝,防止修改全局数据
|
| 130 |
-
new_sys_msg["is_read"] = False
|
| 131 |
-
new_sys_msg["receiver"] = account
|
| 132 |
-
user_msgs.append(new_sys_msg)
|
| 133 |
-
injected = True
|
| 134 |
-
|
| 135 |
-
if injected:
|
| 136 |
-
# 如果注入了新公告,按照时间重新倒序排列,确保最新的消息/公告在最上面
|
| 137 |
-
user_msgs.sort(key=lambda x: x.get("created_at", 0), reverse=True)
|
| 138 |
-
# =========================================================
|
| 139 |
-
|
| 140 |
-
now = int(time.time())
|
| 141 |
-
seven_days = 7 * 24 * 3600
|
| 142 |
-
valid = []
|
| 143 |
-
# 如果刚才注入了新公告,说明用户的消息列表被修改了,必须触发保存
|
| 144 |
-
modified = injected
|
| 145 |
-
|
| 146 |
-
for m in user_msgs:
|
| 147 |
-
if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days):
|
| 148 |
-
valid.append(m)
|
| 149 |
-
else:
|
| 150 |
-
modified = True
|
| 151 |
-
|
| 152 |
-
if modified:
|
| 153 |
-
msgs_db[account] = valid
|
| 154 |
-
db.save_data("messages.json", msgs_db)
|
| 155 |
-
|
| 156 |
-
return {"status": "success", "data": valid, "unread_count": sum(1 for m in valid if not m.get("is_read"))}
|
| 157 |
-
|
| 158 |
-
@router.post("/api/messages/{account}/read")
|
| 159 |
-
async def mark_messages_read(account: str):
|
| 160 |
-
msgs_db = db.load_data("messages.json", default_data={})
|
| 161 |
-
user_msgs = msgs_db.get(account, [])
|
| 162 |
-
modified = False
|
| 163 |
-
|
| 164 |
-
for m in user_msgs:
|
| 165 |
-
if not m.get("is_read"):
|
| 166 |
-
m["is_read"] = True
|
| 167 |
-
modified = True
|
| 168 |
-
|
| 169 |
-
if modified:
|
| 170 |
-
msgs_db[account] = user_msgs
|
| 171 |
-
db.save_data("messages.json", msgs_db)
|
| 172 |
-
|
| 173 |
return {"status": "success"}
|
|
|
|
| 1 |
+
# router_messages.py
|
| 2 |
+
from fastapi import APIRouter
|
| 3 |
+
import time
|
| 4 |
+
import uuid
|
| 5 |
+
import 数据库连接 as db
|
| 6 |
+
from notifications import add_notification
|
| 7 |
+
from models import PrivateMessage
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
@router.post("/api/messages/private")
|
| 12 |
+
async def send_private_message(msg: PrivateMessage):
|
| 13 |
+
chats_db = db.load_data("chats.json", default_data={})
|
| 14 |
+
conv_id = f"{min(msg.sender, msg.receiver)}_{max(msg.sender, msg.receiver)}"
|
| 15 |
+
if conv_id not in chats_db: chats_db[conv_id] = []
|
| 16 |
+
|
| 17 |
+
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}
|
| 18 |
+
chats_db[conv_id].append(chat_msg)
|
| 19 |
+
db.save_data("chats.json", chats_db)
|
| 20 |
+
|
| 21 |
+
add_notification(msg.receiver, {"type": "private", "from_user": msg.sender, "content": msg.content})
|
| 22 |
+
return {"status": "success"}
|
| 23 |
+
|
| 24 |
+
@router.get("/api/chats/{account}")
|
| 25 |
+
async def get_chat_list(account: str):
|
| 26 |
+
chats_db = db.load_data("chats.json", default_data={})
|
| 27 |
+
users_db = db.load_data("users.json", default_data={})
|
| 28 |
+
chat_list = []
|
| 29 |
+
now = int(time.time())
|
| 30 |
+
seven_days = 7 * 24 * 3600
|
| 31 |
+
|
| 32 |
+
for conv_id, msgs in chats_db.items():
|
| 33 |
+
accs = conv_id.split("_")
|
| 34 |
+
if account in accs and msgs:
|
| 35 |
+
# 列表中只统计有效消息
|
| 36 |
+
valid_msgs = [m for m in msgs if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days)]
|
| 37 |
+
if valid_msgs:
|
| 38 |
+
target = accs[0] if accs[1] == account else accs[1]
|
| 39 |
+
target_info = users_db.get(target, {})
|
| 40 |
+
chat_list.append({
|
| 41 |
+
"target_account": target, "target_name": target_info.get("name", target),
|
| 42 |
+
"last_message": valid_msgs[-1]["content"], "last_time": valid_msgs[-1]["created_at"],
|
| 43 |
+
"unread": sum(1 for m in valid_msgs if m["receiver"] == account and not m.get("is_read"))
|
| 44 |
+
})
|
| 45 |
+
chat_list.sort(key=lambda x: x["last_time"], reverse=True)
|
| 46 |
+
return {"status": "success", "data": chat_list}
|
| 47 |
+
|
| 48 |
+
@router.get("/api/chats/{account}/{target}")
|
| 49 |
+
async def get_chat_history(account: str, target: str):
|
| 50 |
+
chats_db = db.load_data("chats.json", default_data={})
|
| 51 |
+
conv_id = f"{min(account, target)}_{max(account, target)}"
|
| 52 |
+
msgs = chats_db.get(conv_id, [])
|
| 53 |
+
|
| 54 |
+
now = int(time.time())
|
| 55 |
+
seven_days = 7 * 24 * 3600
|
| 56 |
+
valid_msgs = []
|
| 57 |
+
modified = False
|
| 58 |
+
|
| 59 |
+
for m in msgs:
|
| 60 |
+
# 【核心策略】:未读的永远保留,已读的超过 7 天直接抛弃
|
| 61 |
+
if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days):
|
| 62 |
+
valid_msgs.append(m)
|
| 63 |
+
else:
|
| 64 |
+
modified = True
|
| 65 |
+
|
| 66 |
+
# 本次访问即为已读
|
| 67 |
+
if m["receiver"] == account and not m.get("is_read"):
|
| 68 |
+
m["is_read"] = True
|
| 69 |
+
modified = True
|
| 70 |
+
|
| 71 |
+
if modified or len(valid_msgs) != len(msgs):
|
| 72 |
+
chats_db[conv_id] = valid_msgs
|
| 73 |
+
db.save_data("chats.json", chats_db)
|
| 74 |
+
|
| 75 |
+
return {"status": "success", "data": valid_msgs}
|
| 76 |
+
|
| 77 |
+
@router.get("/api/messages/{account}")
|
| 78 |
+
async def get_messages(account: str):
|
| 79 |
+
msgs_db = db.load_data("messages.json", default_data={})
|
| 80 |
+
user_msgs = msgs_db.get(account, [])
|
| 81 |
+
|
| 82 |
+
now = int(time.time())
|
| 83 |
+
seven_days = 7 * 24 * 3600
|
| 84 |
+
valid = []
|
| 85 |
+
modified = False
|
| 86 |
+
|
| 87 |
+
for m in user_msgs:
|
| 88 |
+
if not m.get("is_read") or (now - m.get("created_at", 0) < seven_days):
|
| 89 |
+
valid.append(m)
|
| 90 |
+
else:
|
| 91 |
+
modified = True
|
| 92 |
+
|
| 93 |
+
if modified:
|
| 94 |
+
msgs_db[account] = valid
|
| 95 |
+
db.save_data("messages.json", msgs_db)
|
| 96 |
+
|
| 97 |
+
return {"status": "success", "data": valid, "unread_count": sum(1 for m in valid if not m.get("is_read"))}
|
| 98 |
+
|
| 99 |
+
@router.post("/api/messages/{account}/read")
|
| 100 |
+
async def mark_messages_read(account: str):
|
| 101 |
+
msgs_db = db.load_data("messages.json", default_data={})
|
| 102 |
+
user_msgs = msgs_db.get(account, [])
|
| 103 |
+
for m in user_msgs: m["is_read"] = True
|
| 104 |
+
msgs_db[account] = user_msgs
|
| 105 |
+
db.save_data("messages.json", msgs_db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return {"status": "success"}
|