ruma_events/
invite_permission_config.rs

1//! Types for the [`m.invite_permission_config`] account data event.
2//!
3//! [`m.invite_permission_config`]: https://github.com/matrix-org/matrix-spec-proposals/pull/4380
4
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8/// The content of an `m.invite_permission_config` event.
9///
10/// A single property: `block_all`.
11#[derive(Clone, Debug, Default, Deserialize, Serialize, EventContent)]
12#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
13#[ruma_event(
14    kind = GlobalAccountData,
15    type = "org.matrix.msc4380.invite_permission_config",
16    alias = "m.invite_permission_config",
17)]
18pub struct InvitePermissionConfigEventContent {
19    /// When set to true, indicates that the user does not wish to receive *any* room invites, and
20    /// they should be blocked.
21    #[serde(default)]
22    #[serde(deserialize_with = "ruma_common::serde::default_on_error")]
23    pub block_all: bool,
24}
25
26impl InvitePermissionConfigEventContent {
27    /// Creates a new `InvitePermissionConfigEventContent` from the desired boolean state.
28    pub fn new(block_all: bool) -> Self {
29        Self { block_all }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use assert_matches2::assert_matches;
36    use ruma_common::canonical_json::assert_to_canonical_json_eq;
37    use serde_json::{from_value as from_json_value, json};
38
39    use super::InvitePermissionConfigEventContent;
40    use crate::AnyGlobalAccountDataEvent;
41
42    #[test]
43    fn serialization() {
44        let invite_permission_config = InvitePermissionConfigEventContent::new(true);
45
46        assert_to_canonical_json_eq!(
47            invite_permission_config,
48            json!({
49                "block_all": true,
50            }),
51        );
52    }
53
54    #[test]
55    fn deserialization() {
56        let json = json!({
57            "content": {
58                "block_all": true
59            },
60            "type": "m.invite_permission_config"
61        });
62
63        assert_matches!(
64            from_json_value::<AnyGlobalAccountDataEvent>(json),
65            Ok(AnyGlobalAccountDataEvent::InvitePermissionConfig(ev))
66        );
67        assert!(ev.content.block_all);
68    }
69}