File size: 939 Bytes
82f9be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
消息结构定义
"""

from dataclasses import dataclass
from typing import Dict, Any
from datetime import datetime


@dataclass
class Message:
    """消息结构"""
    sender_id: str  # 发送者UUID
    receiver_id: str  # 接收者UUID
    timestamp: datetime  # 时间戳
    content: str  # 消息内容
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'sender_id': self.sender_id,
            'receiver_id': self.receiver_id,
            'timestamp': self.timestamp.isoformat(),
            'content': self.content
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Message':
        """从字典创建消息对象"""
        return cls(
            sender_id=data['sender_id'],
            receiver_id=data['receiver_id'],
            timestamp=datetime.fromisoformat(data['timestamp']),
            content=data['content']
        )