ruma_events/room/
tombstone.rs

1//! Types for the [`m.room.tombstone`] event.
2//!
3//! [`m.room.tombstone`]: https://spec.matrix.org/latest/client-server-api/#mroomtombstone
4
5use ruma_common::OwnedRoomId;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use crate::{
10    EmptyStateKey, PossiblyRedactedStateEventContent, RedactContent, StateEventType,
11    StaticEventContent,
12};
13
14/// The content of an `m.room.tombstone` event.
15///
16/// A state event signifying that a room has been upgraded to a different room version, and that
17/// clients should go there.
18#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
19#[ruma_event(
20    type = "m.room.tombstone",
21    kind = State,
22    state_key_type = EmptyStateKey,
23    custom_possibly_redacted,
24)]
25#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
26pub struct RoomTombstoneEventContent {
27    /// A server-defined message.
28    ///
29    /// If the `compat-optional` feature is enabled, this field being absent in JSON will result
30    /// in an empty string instead of an error when deserializing.
31    #[cfg_attr(feature = "compat-optional", serde(default))]
32    pub body: String,
33
34    /// The new room the client should be visiting.
35    pub replacement_room: OwnedRoomId,
36}
37
38impl RoomTombstoneEventContent {
39    /// Creates a new `RoomTombstoneEventContent` with the given body and replacement room ID.
40    pub fn new(body: String, replacement_room: OwnedRoomId) -> Self {
41        Self { body, replacement_room }
42    }
43}
44
45/// The possibly redacted form of [`RoomTombstoneEventContent`].
46///
47/// This type is used when it's not obvious whether the content is redacted or not.
48#[derive(Clone, Debug, Deserialize, Serialize)]
49#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
50pub struct PossiblyRedactedRoomTombstoneEventContent {
51    /// A server-defined message.
52    pub body: Option<String>,
53
54    /// The new room the client should be visiting.
55    pub replacement_room: Option<OwnedRoomId>,
56}
57
58impl PossiblyRedactedStateEventContent for PossiblyRedactedRoomTombstoneEventContent {
59    type StateKey = EmptyStateKey;
60
61    fn event_type(&self) -> StateEventType {
62        StateEventType::RoomTombstone
63    }
64}
65
66impl StaticEventContent for PossiblyRedactedRoomTombstoneEventContent {
67    const TYPE: &'static str = RoomTombstoneEventContent::TYPE;
68    type IsPrefix = <RoomTombstoneEventContent as StaticEventContent>::IsPrefix;
69}
70
71impl RedactContent for PossiblyRedactedRoomTombstoneEventContent {
72    type Redacted = Self;
73
74    fn redact(self, _rules: &ruma_common::room_version_rules::RedactionRules) -> Self::Redacted {
75        Self { body: None, replacement_room: None }
76    }
77}
78
79impl From<RoomTombstoneEventContent> for PossiblyRedactedRoomTombstoneEventContent {
80    fn from(value: RoomTombstoneEventContent) -> Self {
81        let RoomTombstoneEventContent { body, replacement_room } = value;
82        Self { body: Some(body), replacement_room: Some(replacement_room) }
83    }
84}
85
86impl From<RedactedRoomTombstoneEventContent> for PossiblyRedactedRoomTombstoneEventContent {
87    fn from(_value: RedactedRoomTombstoneEventContent) -> Self {
88        Self { body: None, replacement_room: None }
89    }
90}