fix(avatar): acknowledge BOXIM messages as read
This commit is contained in:
@@ -162,6 +162,27 @@ class BoxIMClient:
|
||||
raise BoxIMError("BOXIM 私聊消息格式不正确")
|
||||
return [item for item in data if isinstance(item, dict)]
|
||||
|
||||
async def mark_private_messages_read(
|
||||
self,
|
||||
access_token: str,
|
||||
friend_id: int | str,
|
||||
message_id: int | str,
|
||||
) -> None:
|
||||
"""Mark one private conversation read through its latest received message."""
|
||||
friend_id_text = str(friend_id).strip()
|
||||
message_id_text = str(message_id).strip()
|
||||
if not friend_id_text.isdigit() or not message_id_text.isdigit():
|
||||
raise BoxIMError("BOXIM 已读回执参数不正确")
|
||||
await self._request(
|
||||
"PUT",
|
||||
"/message/private/readed",
|
||||
access_token,
|
||||
params={
|
||||
"friendId": int(friend_id_text),
|
||||
"messageId": int(message_id_text),
|
||||
},
|
||||
)
|
||||
|
||||
async def send_private_message(
|
||||
self,
|
||||
access_token: str,
|
||||
|
||||
@@ -268,6 +268,7 @@ class TakeoverService:
|
||||
messages.sort(key=lambda item: (_numeric_id(item.get("id")), item.get("sendTime") or 0))
|
||||
priming = not bool(cursor.initialized)
|
||||
max_message_id = _numeric_id(cursor.last_message_id)
|
||||
read_receipts: dict[str, int] = {}
|
||||
for message in messages:
|
||||
self._record_message(
|
||||
db,
|
||||
@@ -276,7 +277,19 @@ class TakeoverService:
|
||||
message,
|
||||
schedule_reply=not priming,
|
||||
)
|
||||
max_message_id = max(max_message_id, _numeric_id(message.get("id")))
|
||||
message_id = _numeric_id(message.get("id"))
|
||||
max_message_id = max(max_message_id, message_id)
|
||||
send_id = str(message.get("sendId") or "")
|
||||
recv_id = str(message.get("recvId") or "")
|
||||
if recv_id == cursor.boxim_owner_id and send_id and message_id:
|
||||
read_receipts[send_id] = max(read_receipts.get(send_id, 0), message_id)
|
||||
|
||||
# BOXIM publishes this HTTP state change to connected socket clients.
|
||||
# Do it before advancing the cursor so a failed receipt is retried.
|
||||
for peer_id, message_id in read_receipts.items():
|
||||
await self.boxim.mark_private_messages_read(
|
||||
session["access_token"], peer_id, message_id
|
||||
)
|
||||
|
||||
cursor.last_message_id = str(max_message_id)
|
||||
cursor.initialized = True
|
||||
|
||||
Reference in New Issue
Block a user