ruma_events/policy/rule/
room.rs1use ruma_common::room_version_rules::RedactionRules;
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use super::{PolicyRuleEventContent, PossiblyRedactedPolicyRuleEventContent};
10use crate::{PossiblyRedactedStateEventContent, RedactContent, StateEventType, StaticEventContent};
11
12#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
16#[allow(clippy::exhaustive_structs)]
17#[ruma_event(type = "m.policy.rule.room", kind = State, state_key_type = String, custom_possibly_redacted)]
18pub struct PolicyRuleRoomEventContent(pub PolicyRuleEventContent);
19
20#[derive(Clone, Debug, Deserialize, Serialize)]
24#[allow(clippy::exhaustive_structs)]
25pub struct PossiblyRedactedPolicyRuleRoomEventContent(pub PossiblyRedactedPolicyRuleEventContent);
26
27impl PossiblyRedactedStateEventContent for PossiblyRedactedPolicyRuleRoomEventContent {
28 type StateKey = String;
29
30 fn event_type(&self) -> StateEventType {
31 StateEventType::PolicyRuleRoom
32 }
33}
34
35impl StaticEventContent for PossiblyRedactedPolicyRuleRoomEventContent {
36 const TYPE: &'static str = PolicyRuleRoomEventContent::TYPE;
37 type IsPrefix = <PolicyRuleRoomEventContent as StaticEventContent>::IsPrefix;
38}
39
40impl RedactContent for PossiblyRedactedPolicyRuleRoomEventContent {
41 type Redacted = Self;
42
43 fn redact(self, _rules: &RedactionRules) -> Self::Redacted {
44 Self(PossiblyRedactedPolicyRuleEventContent::empty())
45 }
46}
47
48impl From<PolicyRuleRoomEventContent> for PossiblyRedactedPolicyRuleRoomEventContent {
49 fn from(value: PolicyRuleRoomEventContent) -> Self {
50 let PolicyRuleRoomEventContent(policy) = value;
51 Self(policy.into())
52 }
53}
54
55impl From<RedactedPolicyRuleRoomEventContent> for PossiblyRedactedPolicyRuleRoomEventContent {
56 fn from(value: RedactedPolicyRuleRoomEventContent) -> Self {
57 let RedactedPolicyRuleRoomEventContent {} = value;
58 Self(PossiblyRedactedPolicyRuleEventContent::empty())
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use ruma_common::{canonical_json::assert_to_canonical_json_eq, serde::Raw};
65 use serde_json::{from_value as from_json_value, json};
66
67 use super::{OriginalPolicyRuleRoomEvent, PolicyRuleRoomEventContent};
68 use crate::policy::rule::{PolicyRuleEventContent, Recommendation};
69
70 #[test]
71 fn serialization() {
72 let content = PolicyRuleRoomEventContent(PolicyRuleEventContent {
73 entity: "#*:example.org".into(),
74 reason: "undesirable content".into(),
75 recommendation: Recommendation::Ban,
76 });
77
78 assert_to_canonical_json_eq!(
79 content,
80 json!({
81 "entity": "#*:example.org",
82 "reason": "undesirable content",
83 "recommendation": "m.ban",
84 }),
85 );
86 }
87
88 #[test]
89 fn deserialization() {
90 let json = json!({
91 "content": {
92 "entity": "#*:example.org",
93 "reason": "undesirable content",
94 "recommendation": "m.ban"
95 },
96 "event_id": "$143273582443PhrSn:example.org",
97 "origin_server_ts": 1_432_735_824_653_u64,
98 "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
99 "sender": "@example:example.org",
100 "state_key": "rule:#*:example.org",
101 "type": "m.policy.rule.room",
102 "unsigned": {
103 "age": 1234
104 }
105 });
106
107 from_json_value::<Raw<OriginalPolicyRuleRoomEvent>>(json).unwrap().deserialize().unwrap();
108 }
109}