-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
41 lines (34 loc) · 1.67 KB
/
utils.py
File metadata and controls
41 lines (34 loc) · 1.67 KB
1
2
3
4
5
6
7
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
import json
from models import MessageContent, MessageContainer, Profile
from exceptions import EncodingError, DecodingError
class SimplexEncoder(json.JSONEncoder):
def default(self, obj):
# If the object is an instance of any of our data classes, return its dictionary representation
if isinstance(obj, (Profile, MessageContent, MessageContainer)):
return obj.__dict__
# Otherwise, let the base class handle the object
return super().default(obj)
def encode_message(message_obj):
"""Encode a message for transmission using the SimpleX Chat Protocol."""
if message_obj is None:
raise EncodingError("Message object is None.")
try:
return json.dumps(message_obj, cls=SimplexEncoder)
except Exception as e:
raise EncodingError(f"Error encoding message: {e}")
def decode_message(encoded_message):
"""Decode a received message using the SimpleX Chat Protocol."""
try:
message_dict = json.loads(encoded_message)
if "content" not in message_dict:
raise DecodingError("The 'content' key is missing from the encoded message.")
content_data = message_dict["content"]
if not isinstance(content_data, dict):
raise DecodingError("The 'content' key should contain a dictionary.")
content = MessageContent(**content_data)
return MessageContainer(content=content,
file=message_dict.get("file"),
quote=message_dict.get("quote"),
forward=message_dict.get("forward", False))
except Exception as e:
raise DecodingError(f"Error decoding message: {e}")