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 serde_json::{from_value as from_json_value, json, to_value as to_json_value};
37
38    use super::InvitePermissionConfigEventContent;
39    use crate::AnyGlobalAccountDataEvent;
40
41    #[test]
42    fn serialization() {
43        let invite_permission_config = InvitePermissionConfigEventContent::new(true);
44
45        let json = json!({
46            "block_all": true
47        });
48
49        assert_eq!(to_json_value(invite_permission_config).unwrap(), json);
50    }
51
52    #[test]
53    fn deserialization() {
54        let json = json!({
55            "content": {
56                "block_all": true
57            },
58            "type": "m.invite_permission_config"
59        });
60
61        assert_matches!(
62            from_json_value::<AnyGlobalAccountDataEvent>(json),
63            Ok(AnyGlobalAccountDataEvent::InvitePermissionConfig(ev))
64        );
65        assert!(ev.content.block_all);
66    }
67}