ruma_events/policy/rule/
room.rs

1//! Types for the [`m.policy.rule.room`] event.
2//!
3//! [`m.policy.rule.room`]: https://spec.matrix.org/latest/client-server-api/#mpolicyruleroom
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::{PolicyRuleEventContent, PossiblyRedactedPolicyRuleEventContent};
9use crate::{PossiblyRedactedStateEventContent, StateEventType, StaticEventContent};
10
11/// The content of an `m.policy.rule.room` event.
12///
13/// This event type is used to apply rules to room entities.
14#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
15#[allow(clippy::exhaustive_structs)]
16#[ruma_event(type = "m.policy.rule.room", kind = State, state_key_type = String, custom_possibly_redacted)]
17pub struct PolicyRuleRoomEventContent(pub PolicyRuleEventContent);
18
19/// The possibly redacted form of [`PolicyRuleRoomEventContent`].
20///
21/// This type is used when it's not obvious whether the content is redacted or not.
22#[derive(Clone, Debug, Deserialize, Serialize)]
23#[allow(clippy::exhaustive_structs)]
24pub struct PossiblyRedactedPolicyRuleRoomEventContent(pub PossiblyRedactedPolicyRuleEventContent);
25
26impl PossiblyRedactedStateEventContent for PossiblyRedactedPolicyRuleRoomEventContent {
27    type StateKey = String;
28
29    fn event_type(&self) -> StateEventType {
30        StateEventType::PolicyRuleRoom
31    }
32}
33
34impl StaticEventContent for PossiblyRedactedPolicyRuleRoomEventContent {
35    const TYPE: &'static str = PolicyRuleRoomEventContent::TYPE;
36    type IsPrefix = <PolicyRuleRoomEventContent as StaticEventContent>::IsPrefix;
37}
38
39#[cfg(test)]
40mod tests {
41    use ruma_common::serde::Raw;
42    use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
43
44    use super::{OriginalPolicyRuleRoomEvent, PolicyRuleRoomEventContent};
45    use crate::policy::rule::{PolicyRuleEventContent, Recommendation};
46
47    #[test]
48    fn serialization() {
49        let content = PolicyRuleRoomEventContent(PolicyRuleEventContent {
50            entity: "#*:example.org".into(),
51            reason: "undesirable content".into(),
52            recommendation: Recommendation::Ban,
53        });
54
55        let json = json!({
56            "entity": "#*:example.org",
57            "reason": "undesirable content",
58            "recommendation": "m.ban"
59        });
60
61        assert_eq!(to_json_value(content).unwrap(), json);
62    }
63
64    #[test]
65    fn deserialization() {
66        let json = json!({
67            "content": {
68                "entity": "#*:example.org",
69                "reason": "undesirable content",
70                "recommendation": "m.ban"
71            },
72            "event_id": "$143273582443PhrSn:example.org",
73            "origin_server_ts": 1_432_735_824_653_u64,
74            "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
75            "sender": "@example:example.org",
76            "state_key": "rule:#*:example.org",
77            "type": "m.policy.rule.room",
78            "unsigned": {
79                "age": 1234
80            }
81        });
82
83        from_json_value::<Raw<OriginalPolicyRuleRoomEvent>>(json).unwrap().deserialize().unwrap();
84    }
85}