Added support for incremental merging

This commit is contained in:
fschuh
2025-04-11 00:15:07 -04:00
parent b3ce22ddbc
commit 1207b1e0cc
3 changed files with 132 additions and 2 deletions

View File

@@ -213,6 +213,18 @@ class ChatStore:
'status': self.status,
'messages': {id: msg.to_json() for id, msg in self._messages.items()}
}
@classmethod
def from_json(cls, data):
chat = cls(data.get("type"), data.get("name"))
chat.my_avatar = data.get("my_avatar")
chat.their_avatar = data.get("their_avatar")
chat.their_avatar_thumb = data.get("their_avatar_thumb")
chat.status = data.get("status")
for id, msg_data in data.get("messages", {}).items():
message = Message.from_json(msg_data)
chat.add_message(id, message)
return chat
def get_last_message(self) -> 'Message':
"""Get the most recent message in the chat."""
@@ -230,6 +242,20 @@ class ChatStore:
"""Get all message keys in the chat."""
return self._messages.keys()
def merge_with(self, other):
if not isinstance(other, ChatStore):
raise TypeError("Can only merge with another ChatStore object")
# Update fields if they are not None in the other ChatStore
self.name = other.name or self.name
self.type = other.type or self.type
self.my_avatar = other.my_avatar or self.my_avatar
self.their_avatar = other.their_avatar or self.their_avatar
self.their_avatar_thumb = other.their_avatar_thumb or self.their_avatar_thumb
self.status = other.status or self.status
# Merge messages
self.messages.update(other.messages)
class Message:
"""
@@ -310,4 +336,25 @@ class Message:
'caption': self.caption,
'thumb': self.thumb,
'sticker': self.sticker
}
}
@classmethod
def from_json(cls, data):
message = cls(
data["from_me"],
data["timestamp"],
data["time"],
data["key_id"]
)
message.media = data.get("media")
message.meta = data.get("meta")
message.data = data.get("data")
message.sender = data.get("sender")
message.safe = data.get("safe")
message.mime = data.get("mime")
message.reply = data.get("reply")
message.quoted_data = data.get("quoted_data")
message.caption = data.get("caption")
message.thumb = data.get("thumb")
message.sticker = data.get("sticker")
return message