ruma_events/room/
aliases.rs

1//! Types for the `m.room.aliases` event.
2
3use ruma_common::{room_version_rules::RedactionRules, OwnedRoomAliasId, OwnedServerName};
4use ruma_macros::EventContent;
5use serde::{Deserialize, Serialize};
6
7use crate::{EventContent, RedactContent, RedactedStateEventContent, StateEventType};
8
9/// The content of an `m.room.aliases` event.
10///
11/// Informs the room about what room aliases it has been given.
12#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
13#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
14#[ruma_event(type = "m.room.aliases", kind = State, state_key_type = OwnedServerName, custom_redacted)]
15pub struct RoomAliasesEventContent {
16    /// A list of room aliases.
17    pub aliases: Vec<OwnedRoomAliasId>,
18}
19
20impl RoomAliasesEventContent {
21    /// Create an `RoomAliasesEventContent` from the given aliases.
22    pub fn new(aliases: Vec<OwnedRoomAliasId>) -> Self {
23        Self { aliases }
24    }
25}
26
27impl RedactContent for RoomAliasesEventContent {
28    type Redacted = RedactedRoomAliasesEventContent;
29
30    fn redact(self, rules: &RedactionRules) -> RedactedRoomAliasesEventContent {
31        let aliases = rules.keep_room_aliases_aliases.then_some(self.aliases);
32        RedactedRoomAliasesEventContent { aliases }
33    }
34}
35
36/// An aliases event that has been redacted.
37#[derive(Clone, Debug, Default, Deserialize, Serialize)]
38#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
39pub struct RedactedRoomAliasesEventContent {
40    /// A list of room aliases.
41    ///
42    /// According to the Matrix spec version 1 redaction rules allowed this field to be
43    /// kept after redaction, this was changed in version 6.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub aliases: Option<Vec<OwnedRoomAliasId>>,
46}
47
48impl RedactedRoomAliasesEventContent {
49    /// Create a `RedactedAliasesEventContent` with the given aliases.
50    ///
51    /// This is only valid for room version 5 and below.
52    pub fn new_v1(aliases: Vec<OwnedRoomAliasId>) -> Self {
53        Self { aliases: Some(aliases) }
54    }
55
56    /// Create a `RedactedAliasesEventContent` with the given aliases.
57    ///
58    /// This is only valid for room version 6 and above.
59    pub fn new_v6() -> Self {
60        Self::default()
61    }
62}
63
64impl EventContent for RedactedRoomAliasesEventContent {
65    type EventType = StateEventType;
66
67    fn event_type(&self) -> StateEventType {
68        StateEventType::RoomAliases
69    }
70}
71
72impl RedactedStateEventContent for RedactedRoomAliasesEventContent {
73    type StateKey = OwnedServerName;
74}