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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! `GET /_matrix/client/*/presence/{userId}/status`
//!
//! Get presence status for this user.
pub mod v3 {
//! `/v3/` ([spec])
//!
//! [spec]: https://spec.matrix.org/v1.19/client-server-api/#get_matrixclientv3presenceuseridstatus
use std::time::Duration;
#[cfg(feature = "unstable-msc4532")]
use ruma_common::presence::PresenceStatus;
use ruma_common::{
OwnedUserId,
api::{auth_scheme::AccessToken, request, response},
metadata,
presence::PresenceState,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
metadata! {
method: GET,
rate_limited: false,
authentication: AccessToken,
history: {
1.0 => "/_matrix/client/r0/presence/{user_id}/status",
1.1 => "/_matrix/client/v3/presence/{user_id}/status",
}
}
/// Request type for the `get_presence` endpoint.
#[request]
pub struct Request {
/// The user whose presence state will be retrieved.
#[ruma_api(path)]
pub user_id: OwnedUserId,
/// Whether to use [MSC4532]'s revised presence states in responses (if the server supports
/// them).
///
/// Defaults to `true`.
///
/// This uses the unstable prefix defined in [MSC4532].
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg(feature = "unstable-msc4532")]
#[serde(
default,
skip_serializing_if = "ruma_common::serde::is_default",
rename = "org.continuwuity.presence_v2.msc4532.revised_presence"
)]
#[ruma_api(query)]
pub revised_presence: bool,
}
/// Response type for the `get_presence` endpoint.
#[response]
#[ruma_api(manual_body_serde)]
pub struct Response {
/// The state message for this user if one was set.
///
/// If the `unstable-msc4532` feature is enabled, this field will always have the same
/// value as `status.msg` after deserialization.
#[cfg_attr(
feature = "unstable-msc4532",
deprecated(note = "Deprecated when MSC4532 is enabled, use `status` instead")
)]
// required to prevent dead code warnings for the deprecated field on `ResponseBody`
#[allow(dead_code)]
pub status_msg: Option<String>,
/// The status information for this user's presence.
///
/// This field uses the unstable prefix defined in [MSC4532].
///
/// If this field is not present at deserialization, the value of `status_msg`
/// will be used instead.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg(feature = "unstable-msc4532")]
pub status: PresenceStatus,
/// Whether the user is currently active or not.
///
/// If the `unstable-msc4532` feature is enabled and `presence` is a [MSC4532]
/// presence state, this field will be ignored and will always have the same value
/// as [`PresenceState::currently_active`] after deserialization.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg_attr(
feature = "unstable-msc4532",
deprecated(
note = "Deprecated when MSC4532 is enabled, use `PresenceState::currently_active` instead"
)
)]
pub currently_active: Option<bool>,
/// The length of time in milliseconds since an action was performed by the user.
pub last_active_ago: Option<Duration>,
/// The user's presence state.
pub presence: PresenceState,
}
impl Request {
/// Creates a new `Request` with the given user ID.
pub fn new(user_id: OwnedUserId) -> Self {
Self {
user_id,
#[cfg(feature = "unstable-msc4532")]
revised_presence: true,
}
}
}
impl Response {
/// Creates a new `Response` with the given presence state.
pub fn new(presence: PresenceState) -> Self {
#[allow(deprecated)]
Self {
presence,
status_msg: None,
#[cfg(feature = "unstable-msc4532")]
status: PresenceStatus::default(),
currently_active: None,
last_active_ago: None,
}
}
}
#[derive(Serialize, Deserialize)]
struct ResponseBodyRepr {
/// The state message for this user if one was set.
#[serde(skip_serializing_if = "Option::is_none")]
status_msg: Option<String>,
/// The status information for this user's presence.
///
/// This field uses the unstable prefix defined in [MSC4532].
///
/// If this field is not present at deserialization, the value of `status_msg`
/// will be used instead.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[serde(
skip_serializing_if = "ruma_common::serde::is_default",
rename = "org.continuwuity.presence_v2.msc4532.status",
default
)]
#[cfg(feature = "unstable-msc4532")]
status: PresenceStatus,
/// Whether the user is currently active or not.
#[serde(skip_serializing_if = "Option::is_none")]
currently_active: Option<bool>,
/// The length of time in milliseconds since an action was performed by the user.
#[serde(
with = "ruma_common::serde::duration::opt_ms",
default,
skip_serializing_if = "Option::is_none"
)]
last_active_ago: Option<Duration>,
/// The user's presence state.
presence: PresenceState,
}
impl<'de> Deserialize<'de> for ResponseBody {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[allow(deprecated)]
let ResponseBodyRepr {
status_msg,
#[cfg(feature = "unstable-msc4532")]
status,
currently_active,
last_active_ago,
presence,
} = ResponseBodyRepr::deserialize(deserializer)?;
#[cfg(feature = "unstable-msc4532")]
let status = if status == PresenceStatus::default() {
PresenceStatus::new(status_msg)
} else {
status
};
#[cfg(feature = "unstable-msc4532")]
let status_msg = status.msg.clone();
#[cfg(feature = "unstable-msc4532")]
let currently_active = if presence.is_msc4532_state() {
// ignore the provided currently_active value and use the proposal's definition
Some(presence.currently_active())
} else {
currently_active
};
#[allow(deprecated)]
Ok(Self {
#[cfg(feature = "unstable-msc4532")]
status,
status_msg,
currently_active,
last_active_ago,
presence,
})
}
}
// Servers serialize the data as-is, clients are expected to handle old fields as needed.
impl Serialize for ResponseBody {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[allow(deprecated)]
let ResponseBody {
status_msg,
#[cfg(feature = "unstable-msc4532")]
status,
currently_active,
last_active_ago,
presence,
} = self.clone();
ResponseBodyRepr {
status_msg,
#[cfg(feature = "unstable-msc4532")]
status,
currently_active,
last_active_ago,
presence,
}
.serialize(serializer)
}
}
}