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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use js_int::UInt;
use ruma_macros::{Event, EventContent};
use serde::{Deserialize, Serialize};
use super::{EventKind, StaticEventContent};
use crate::{presence::PresenceState, OwnedMxcUri, OwnedUserId};
#[derive(Clone, Debug, Event)]
#[allow(clippy::exhaustive_structs)]
pub struct PresenceEvent {
pub content: PresenceEventContent,
pub sender: OwnedUserId,
}
#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.presence")]
pub struct PresenceEventContent {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "compat",
serde(default, deserialize_with = "crate::serde::empty_string_as_none")
)]
pub avatar_url: Option<OwnedMxcUri>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currently_active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub displayname: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_active_ago: Option<UInt>,
pub presence: PresenceState,
#[serde(skip_serializing_if = "Option::is_none")]
pub status_msg: Option<String>,
}
impl PresenceEventContent {
pub fn new(presence: PresenceState) -> Self {
Self {
avatar_url: None,
currently_active: None,
displayname: None,
last_active_ago: None,
presence,
status_msg: None,
}
}
}
impl StaticEventContent for PresenceEventContent {
const KIND: EventKind = EventKind::Presence;
const TYPE: &'static str = "m.presence";
}
#[cfg(test)]
mod tests {
use crate::{mxc_uri, presence::PresenceState, user_id};
use js_int::uint;
use matches::assert_matches;
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{PresenceEvent, PresenceEventContent};
#[test]
fn serialization() {
let event = PresenceEvent {
content: PresenceEventContent {
avatar_url: Some(mxc_uri!("mxc://localhost/wefuiwegh8742w").to_owned()),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(uint!(2_478_593)),
presence: PresenceState::Online,
status_msg: Some("Making cupcakes".into()),
},
sender: user_id!("@example:localhost").to_owned(),
};
let json = json!({
"content": {
"avatar_url": "mxc://localhost/wefuiwegh8742w",
"currently_active": false,
"last_active_ago": 2_478_593,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
});
assert_eq!(to_json_value(&event).unwrap(), json);
}
#[test]
fn deserialization() {
let json = json!({
"content": {
"avatar_url": "mxc://localhost/wefuiwegh8742w",
"currently_active": false,
"last_active_ago": 2_478_593,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
});
assert_matches!(
from_json_value::<PresenceEvent>(json).unwrap(),
PresenceEvent {
content: PresenceEventContent {
avatar_url: Some(avatar_url),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(last_active_ago),
presence: PresenceState::Online,
status_msg: Some(status_msg),
},
sender,
} if avatar_url == "mxc://localhost/wefuiwegh8742w"
&& status_msg == "Making cupcakes"
&& sender == "@example:localhost"
&& last_active_ago == uint!(2_478_593)
);
#[cfg(feature = "compat")]
assert_matches!(
from_json_value::<PresenceEvent>(json!({
"content": {
"avatar_url": "",
"currently_active": false,
"last_active_ago": 2_478_593,
"presence": "online",
"status_msg": "Making cupcakes"
},
"sender": "@example:localhost",
"type": "m.presence"
})).unwrap(),
PresenceEvent {
content: PresenceEventContent {
avatar_url: None,
currently_active: Some(false),
displayname: None,
last_active_ago: Some(last_active_ago),
presence: PresenceState::Online,
status_msg: Some(status_msg),
},
sender,
} if status_msg == "Making cupcakes"
&& sender == "@example:localhost"
&& last_active_ago == uint!(2_478_593)
);
}
}