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
//! Common types for the [presence module][presence].
//!
//! [presence]: https://spec.matrix.org/v1.19/client-server-api/#presence
#[cfg(feature = "unstable-msc4532")]
use std::borrow::Cow;
#[cfg(feature = "unstable-msc4532")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "unstable-msc4532")]
use crate::serde::JsonObject;
use crate::{PrivOwnedStr, serde::StringEnum};
/// A description of a user's connectivity and availability for chat.
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, Default, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PresenceState {
/// Disconnected from the service.
Offline,
/// Connected to the service.
#[cfg_attr(not(feature = "unstable-msc4532"), default)]
#[cfg_attr(
feature = "unstable-msc4532",
deprecated(note = "Deprecated when MSC4532 is enabled, use a revised state instead")
)]
Online,
/// Connected to the service but not available for chat.
#[cfg_attr(
feature = "unstable-msc4532",
deprecated(note = "Deprecated when MSC4532 is enabled, use a revised state instead")
)]
Unavailable,
/// The user is available to reply.
///
/// This uses the unstable prefix defined in [MSC4532].
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg(feature = "unstable-msc4532")]
#[default]
#[ruma_enum(rename = "org.continuwuity.presence_v2.msc4532.active")]
Active,
/// The user has a connected client and may reply (potentially unreachable).
///
/// This uses the unstable prefix defined in [MSC4532].
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg(feature = "unstable-msc4532")]
#[ruma_enum(rename = "org.continuwuity.presence_v2.msc4532.idle")]
Idle,
/// The user is unavailable to reply (fully unreachable).
///
/// This uses the unstable prefix defined in [MSC4532].
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[cfg(feature = "unstable-msc4532")]
#[ruma_enum(rename = "org.continuwuity.presence_v2.msc4532.busy")]
Busy,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
#[cfg(feature = "unstable-msc4532")]
impl PresenceState {
/// If this [`PresenceState`] is a legacy (non-[MSC4532]) state, return the equivalent MSC4532
/// state.
///
/// This uses the behavior map as defined in the proposal.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[allow(deprecated)]
pub fn as_msc4532_state(&self, currently_active: bool) -> Self {
match (self, currently_active) {
(Self::Online, true) => Self::Active,
(Self::Online, false) => Self::Idle,
(Self::Unavailable, _) => Self::Idle,
(state, _) => state.clone(),
}
}
/// If this [`PresenceState`] is an MSC4532 state, return the equivalent legacy (non-MSC4532)
/// state.
///
/// This uses the behavior map as defined in the proposal.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
#[allow(deprecated)]
pub fn as_legacy_state(&self) -> Self {
match self {
Self::Active => Self::Online,
Self::Idle => Self::Unavailable,
Self::Busy => Self::Unavailable,
state => state.clone(),
}
}
/// Returns `true` if this [`PresenceState`] is a [MSC4532] state.
///
/// This uses the behavior map as defined in the proposal.
///
/// [MSC4532]: https://github.com/matrix-org/matrix-spec-proposals/pull/4532
pub fn is_msc4532_state(&self) -> bool {
matches!(self, Self::Active | Self::Idle | Self::Busy | Self::Offline)
}
/// Return a reasonable `currently_active` value for this [`PresenceState`].
#[expect(deprecated)]
pub fn currently_active(&self) -> bool {
matches!(self, Self::Active | Self::Online)
}
}
impl Default for &'_ PresenceState {
fn default() -> Self {
#[cfg(feature = "unstable-msc4532")]
{
&PresenceState::Active
}
#[cfg(not(feature = "unstable-msc4532"))]
{
&PresenceState::Online
}
}
}
/// A user's extensible status.
#[cfg(feature = "unstable-msc4532")]
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
pub struct PresenceStatus {
/// An optional description to accompany the presence.
#[serde(skip_serializing_if = "Option::is_none")]
pub msg: Option<String>,
/// Remaining content.
#[serde(flatten)]
data: JsonObject,
}
#[cfg(feature = "unstable-msc4532")]
impl PresenceStatus {
/// Creates a new `PresenceStatus` with the given message.
pub fn new(msg: Option<String>) -> Self {
Self { msg, data: JsonObject::new() }
}
/// Creates a new `PresenceStatus` with the given message and additional data.
pub fn with_data(msg: Option<String>, mut data: JsonObject) -> Self {
data.remove("msg");
Self { msg, data }
}
/// The data of this `PresenceStatus`.
///
/// The returned JSON object won't contain the `msg` field; use [`Self::msg`] to access it.
///
/// Prefer to use the public fields when possible; this method is meant to be used to access
/// unsupported fields only.
pub fn data(&self) -> Cow<'_, JsonObject> {
Cow::Borrowed(&self.data)
}
}