ruma_events/lib.rs
1#![doc(html_favicon_url = "https://ruma.dev/favicon.ico")]
2#![doc(html_logo_url = "https://ruma.dev/images/logo.png")]
3//! (De)serializable types for the events in the [Matrix](https://matrix.org) specification.
4//! These types are used by other Ruma crates.
5//!
6//! All data exchanged over Matrix is expressed as an event.
7//! Different event types represent different actions, such as joining a room or sending a message.
8//! Events are stored and transmitted as simple JSON structures.
9//! While anyone can create a new event type for their own purposes, the Matrix specification
10//! defines a number of event types which are considered core to the protocol.
11//! This module contains Rust types for all of the event types defined by the specification and
12//! facilities for extending the event system for custom event types.
13//!
14//! # Core event types
15//!
16//! This module includes Rust types for all event types in the Matrix specification.
17//! To better organize the crate, these types live in separate modules with a hierarchy that matches
18//! the reverse domain name notation of the event type. For example, the `m.room.message` event
19//! lives at `ruma::events::room::message::RoomMessageEvent`. Each type's module also contains a
20//! Rust type for that event type's `content` field, and any other supporting types required by the
21//! event's other fields.
22//!
23//! # Extending Ruma with custom events
24//!
25//! For our examples we will start with a simple custom state event. `ruma_event`
26//! specifies the state event's `type` and its `kind`.
27//!
28//! ```rust
29//! use ruma_events::macros::EventContent;
30//! use serde::{Deserialize, Serialize};
31//!
32//! #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
33//! #[ruma_event(type = "org.example.event", kind = State, state_key_type = String)]
34//! pub struct ExampleContent {
35//! field: String,
36//! }
37//! ```
38//!
39//! This can be used with events structs, such as passing it into
40//! `ruma::api::client::state::send_state_event`'s `Request`.
41//!
42//! As a more advanced example we create a reaction message event. For this event we will use a
43//! [`OriginalSyncMessageLikeEvent`] struct but any [`OriginalMessageLikeEvent`] struct would work.
44//!
45//! ```rust
46//! use ruma_common::OwnedEventId;
47//! use ruma_events::{macros::EventContent, OriginalSyncMessageLikeEvent};
48//! use serde::{Deserialize, Serialize};
49//!
50//! #[derive(Clone, Debug, Deserialize, Serialize)]
51//! #[serde(tag = "rel_type")]
52//! pub enum RelatesTo {
53//! #[serde(rename = "m.annotation")]
54//! Annotation {
55//! /// The event this reaction relates to.
56//! event_id: OwnedEventId,
57//! /// The displayable content of the reaction.
58//! key: String,
59//! },
60//!
61//! /// Since this event is not fully specified in the Matrix spec
62//! /// it may change or types may be added, we are ready!
63//! #[serde(rename = "m.whatever")]
64//! Whatever,
65//! }
66//!
67//! /// The payload for our reaction event.
68//! #[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
69//! #[ruma_event(type = "m.reaction", kind = MessageLike)]
70//! pub struct ReactionEventContent {
71//! #[serde(rename = "m.relates_to")]
72//! pub relates_to: RelatesTo,
73//! }
74//!
75//! let json = serde_json::json!({
76//! "content": {
77//! "m.relates_to": {
78//! "event_id": "$xxxx-xxxx",
79//! "key": "👍",
80//! "rel_type": "m.annotation"
81//! }
82//! },
83//! "event_id": "$xxxx-xxxx",
84//! "origin_server_ts": 1,
85//! "sender": "@someone:example.org",
86//! "type": "m.reaction",
87//! "unsigned": {
88//! "age": 85
89//! }
90//! });
91//!
92//! // The downside of this event is we cannot use it with event enums,
93//! // but could be deserialized from a `Raw<_>` that has failed to deserialize.
94//! assert!(matches!(
95//! serde_json::from_value::<OriginalSyncMessageLikeEvent<ReactionEventContent>>(json),
96//! Ok(OriginalSyncMessageLikeEvent {
97//! content: ReactionEventContent {
98//! relates_to: RelatesTo::Annotation { key, .. },
99//! },
100//! ..
101//! }) if key == "👍"
102//! ));
103//! ```
104
105#![warn(missing_docs)]
106
107#[cfg(feature = "unstable-uniffi")]
108uniffi::setup_scaffolding!();
109
110use std::collections::BTreeSet;
111
112use ruma_common::{EventEncryptionAlgorithm, OwnedUserId, room_version_rules::RedactionRules};
113use serde::{Deserialize, Serialize, Serializer, de::IgnoredAny};
114
115// Needs to be public for trybuild tests
116#[doc(hidden)]
117pub mod _custom;
118mod content;
119mod enums;
120mod kinds;
121mod state_key;
122mod unsigned;
123
124// So event macros work inside this crate.
125extern crate self as ruma_events;
126
127/// Re-exports used by macro-generated code.
128///
129/// It is not considered part of this module's public API.
130#[doc(hidden)]
131pub mod exports {
132 pub use ruma_common;
133 pub use ruma_macros;
134 pub use serde;
135 pub use serde_json;
136}
137
138/// Re-export of all the derives needed to create your own event types.
139pub mod macros {
140 pub use ruma_macros::{Event, EventContent};
141}
142
143#[cfg(feature = "unstable-msc3927")]
144pub mod audio;
145#[cfg(feature = "unstable-msc3489")]
146pub mod beacon;
147#[cfg(feature = "unstable-msc3489")]
148pub mod beacon_info;
149pub mod call;
150pub mod direct;
151#[cfg(feature = "unstable-msc4359")]
152pub mod do_not_disturb;
153pub mod dummy;
154#[cfg(feature = "unstable-msc3954")]
155pub mod emote;
156#[cfg(feature = "unstable-msc3956")]
157pub mod encrypted;
158#[cfg(feature = "unstable-msc3551")]
159pub mod file;
160pub mod forwarded_room_key;
161pub mod fully_read;
162pub mod identity_server;
163pub mod ignored_user_list;
164#[cfg(feature = "unstable-msc3552")]
165pub mod image;
166#[cfg(feature = "unstable-msc2545")]
167pub mod image_pack;
168pub mod invite_permission_config;
169pub mod key;
170pub mod key_backup;
171#[cfg(feature = "unstable-msc3488")]
172pub mod location;
173pub mod marked_unread;
174#[cfg(feature = "unstable-msc4278")]
175pub mod media_preview_config;
176#[cfg(feature = "unstable-msc4171")]
177pub mod member_hints;
178pub mod message;
179pub mod policy;
180#[cfg(feature = "unstable-msc3381")]
181pub mod poll;
182pub mod presence;
183pub mod push_rules;
184pub mod reaction;
185pub mod receipt;
186pub mod recent_emoji;
187pub mod relation;
188pub mod room;
189pub mod room_key;
190#[cfg(feature = "unstable-msc4268")]
191pub mod room_key_bundle;
192pub mod room_key_request;
193#[cfg(feature = "unstable-msc4310")]
194pub mod rtc;
195pub mod secret;
196pub mod secret_storage;
197pub mod space;
198#[cfg(feature = "unstable-msc3230")]
199pub mod space_order;
200pub mod sticker;
201pub mod tag;
202pub mod typing;
203#[cfg(feature = "unstable-msc3553")]
204pub mod video;
205#[cfg(feature = "unstable-msc3245")]
206pub mod voice;
207
208pub use self::{
209 content::*,
210 enums::*,
211 kinds::*,
212 relation::{BundledMessageLikeRelations, BundledStateRelations},
213 state_key::EmptyStateKey,
214 unsigned::{MessageLikeUnsigned, RedactedUnsigned, StateUnsigned, UnsignedRoomRedactionEvent},
215};
216
217/// Trait to define the behavior of redact an event's content object.
218pub trait RedactContent {
219 /// The redacted form of the event's content.
220 type Redacted;
221
222 /// Transform `self` into a redacted form (removing most or all fields) according to the spec.
223 ///
224 /// A small number of events have room-version specific redaction behavior, so a
225 /// [`RedactionRules`] has to be specified.
226 fn redact(self, rules: &RedactionRules) -> Self::Redacted;
227}
228
229/// Helper struct to determine the event kind from a `serde_json::value::RawValue`.
230#[doc(hidden)]
231#[derive(Deserialize)]
232#[allow(clippy::exhaustive_structs)]
233pub struct EventTypeDeHelper<'a> {
234 #[serde(borrow, rename = "type")]
235 pub ev_type: std::borrow::Cow<'a, str>,
236}
237
238/// Helper struct to determine if an event has been redacted.
239#[doc(hidden)]
240#[derive(Deserialize)]
241#[allow(clippy::exhaustive_structs)]
242pub struct RedactionDeHelper {
243 /// Used to check whether redacted_because exists.
244 pub unsigned: Option<UnsignedDeHelper>,
245}
246
247#[doc(hidden)]
248#[derive(Deserialize)]
249#[allow(clippy::exhaustive_structs)]
250pub struct UnsignedDeHelper {
251 /// This is the field that signals an event has been redacted.
252 pub redacted_because: Option<IgnoredAny>,
253}
254
255/// Helper function for erroring when trying to serialize an event enum _Custom variant that can
256/// only be created by deserializing from an unknown event type.
257#[doc(hidden)]
258#[allow(clippy::ptr_arg)]
259pub fn serialize_custom_event_error<T, S: Serializer>(_: &T, _: S) -> Result<S::Ok, S::Error> {
260 Err(serde::ser::Error::custom(
261 "Failed to serialize event [content] enum: Unknown event type.\n\
262 To send custom events, turn them into `Raw<EnumType>` by going through
263 `serde_json::value::to_raw_value` and `Raw::from_json`.",
264 ))
265}
266
267/// Describes whether the event mentions other users or the room.
268#[derive(Clone, Debug, Default, Serialize, Deserialize)]
269#[non_exhaustive]
270pub struct Mentions {
271 /// The list of mentioned users.
272 ///
273 /// Defaults to an empty `BTreeSet`.
274 #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
275 pub user_ids: BTreeSet<OwnedUserId>,
276
277 /// Whether the whole room is mentioned.
278 ///
279 /// Defaults to `false`.
280 #[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
281 pub room: bool,
282}
283
284impl Mentions {
285 /// Create a `Mentions` with the default values.
286 pub fn new() -> Self {
287 Self::default()
288 }
289
290 /// Create a `Mentions` for the given user IDs.
291 pub fn with_user_ids(user_ids: impl IntoIterator<Item = OwnedUserId>) -> Self {
292 Self { user_ids: BTreeSet::from_iter(user_ids), ..Default::default() }
293 }
294
295 /// Create a `Mentions` for a room mention.
296 pub fn with_room_mention() -> Self {
297 Self { room: true, ..Default::default() }
298 }
299
300 fn add(&mut self, mentions: Self) {
301 self.user_ids.extend(mentions.user_ids);
302 self.room |= mentions.room;
303 }
304}
305
306ruma_common::priv_owned_str!(uniffi);