79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
"""Small upstream client extension for visible, date-bounded history sync."""
|
|
|
|
from larkx.client import LarkClient
|
|
|
|
from larkx.proto import decoders
|
|
|
|
|
|
class SnifferLarkClient(LarkClient):
|
|
def pull_history(
|
|
self,
|
|
chat_id: str,
|
|
positions,
|
|
save_storage=None,
|
|
visible_only=False,
|
|
thread_chat=None,
|
|
):
|
|
request = {"chatId": str(chat_id), "positions": [int(p) for p in positions]}
|
|
if visible_only:
|
|
request.update(
|
|
{
|
|
"type": "CHAT",
|
|
"onlyVisibleMessages": True,
|
|
"needNextPosition": True,
|
|
"direction": "UP",
|
|
}
|
|
)
|
|
response = self.api("messages.PullMessagesByPositionsRequest", request)
|
|
messages = (response or {}).get("messages") or {}
|
|
if thread_chat is None:
|
|
thread_chat = self.is_thread_chat(chat_id)
|
|
result = []
|
|
for position_key, value in sorted(messages.items(), key=lambda item: int(item[0])):
|
|
message_type = decoders.enum_to_int(value.get("type", 0))
|
|
chat_type = decoders.enum_to_int(value.get("chatType", 0))
|
|
summary, data = decoders.decode_message_content(
|
|
message_type, value.get("content", b"")
|
|
)
|
|
root_id = value.get("rootId") or ""
|
|
scope, anchor = decoders.resolve_scope(
|
|
value.get("id"),
|
|
message_type,
|
|
chat_type,
|
|
root_id,
|
|
value.get("threadId") or "",
|
|
thread_chat,
|
|
)
|
|
message = {
|
|
"msg_id": value.get("id"),
|
|
"msg_type": message_type,
|
|
"msg_type_name": decoders.MSG_TYPE_NAMES.get(
|
|
message_type, str(message_type)
|
|
),
|
|
"from_id": value.get("fromId"),
|
|
"chat_id": value.get("chatId") or value.get("channelId") or str(chat_id),
|
|
"chat_type": chat_type,
|
|
"chat_type_name": decoders.CHAT_TYPE_NAMES.get(
|
|
chat_type, str(chat_type)
|
|
),
|
|
"scope": scope,
|
|
"anchor": anchor,
|
|
"at_me": False,
|
|
"root_id": root_id,
|
|
"parent_id": value.get("parentId") or "",
|
|
"cid": value.get("cid") or "",
|
|
"position": value.get("position") or int(position_key),
|
|
"create_time": value.get("createTime"),
|
|
"content": summary,
|
|
"content_data": data,
|
|
}
|
|
result.append(message)
|
|
if save_storage is not None:
|
|
direction = (
|
|
"out"
|
|
if str(value.get("fromId") or "") == str(self.auth.user_id or "")
|
|
else "in"
|
|
)
|
|
save_storage.save_message(message, direction=direction)
|
|
return result
|