1use serde::{Deserialize, Serialize};
2use chrono::{DateTime, Utc};
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct UserId(pub String);
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10pub struct RoomId(pub String);
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct EventId(pub String);
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
18pub struct DeviceId(pub String);
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct MessageContent {
23 pub msgtype: String,
24 pub body: String,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 pub formatted_body: Option<String>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 pub format: Option<String>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct Event {
34 pub event_id: EventId,
35 pub room_id: RoomId,
36 pub sender: UserId,
37 pub event_type: String,
38 pub content: serde_json::Value,
39 pub timestamp: DateTime<Utc>,
40 #[serde(skip_serializing_if = "Option::is_none")]
41 pub unsigned: Option<serde_json::Value>,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
46pub enum MembershipState {
47 #[serde(rename = "invite")]
48 Invite,
49 #[serde(rename = "join")]
50 Join,
51 #[serde(rename = "leave")]
52 Leave,
53 #[serde(rename = "ban")]
54 Ban,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct RoomMember {
60 pub user_id: UserId,
61 pub membership: MembershipState,
62 #[serde(skip_serializing_if = "Option::is_none")]
63 pub display_name: Option<String>,
64 #[serde(skip_serializing_if = "Option::is_none")]
65 pub avatar_url: Option<String>,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct Room {
71 pub room_id: RoomId,
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub name: Option<String>,
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub topic: Option<String>,
76 pub members: Vec<RoomMember>,
77 pub is_encrypted: bool,
78 pub created_at: DateTime<Utc>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct Device {
84 pub device_id: DeviceId,
85 pub user_id: UserId,
86 pub display_name: Option<String>,
87 pub last_seen_ip: Option<String>,
88 pub last_seen_ts: Option<DateTime<Utc>>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct EncryptionKey {
94 pub algorithm: String,
95 pub key: String,
96 #[serde(skip_serializing_if = "Option::is_none")]
97 pub signatures: Option<serde_json::Value>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct Session {
103 pub user_id: UserId,
104 pub device_id: DeviceId,
105 pub access_token: String,
106 pub created_at: DateTime<Utc>,
107 pub expires_at: Option<DateTime<Utc>>,
108}
109
110impl UserId {
111 pub fn new(id: impl Into<String>) -> Self {
112 Self(id.into())
113 }
114
115 pub fn as_str(&self) -> &str {
116 &self.0
117 }
118}
119
120impl RoomId {
121 pub fn new(id: impl Into<String>) -> Self {
122 Self(id.into())
123 }
124
125 pub fn as_str(&self) -> &str {
126 &self.0
127 }
128}
129
130impl EventId {
131 pub fn new(id: impl Into<String>) -> Self {
132 Self(id.into())
133 }
134
135 pub fn as_str(&self) -> &str {
136 &self.0
137 }
138}
139
140impl DeviceId {
141 pub fn new(id: impl Into<String>) -> Self {
142 Self(id.into())
143 }
144
145 pub fn as_str(&self) -> &str {
146 &self.0
147 }
148}