|
| 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 | +
|
| 5 | +use ruma_macros::EventContent; |
| 6 | +use 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 = "m.invite_permission_config", |
| 16 | + alias = "org.matrix.msc4380.invite_permission_config", |
| 17 | +)] |
| 18 | +pub 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 | + pub block_all: bool, |
| 22 | +} |
| 23 | + |
| 24 | +impl InvitePermissionConfigEventContent { |
| 25 | + /// Creates a new `InvitePermissionConfigEventContent` from the desired boolean state. |
| 26 | + pub fn new(block_all: bool) -> Self { |
| 27 | + Self { block_all } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +#[cfg(test)] |
| 32 | +mod tests { |
| 33 | + use assert_matches2::assert_matches; |
| 34 | + use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; |
| 35 | + |
| 36 | + use super::InvitePermissionConfigEventContent; |
| 37 | + use crate::AnyGlobalAccountDataEvent; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn serialization() { |
| 41 | + let invite_permission_config = InvitePermissionConfigEventContent::new(true); |
| 42 | + |
| 43 | + let json = json!({ |
| 44 | + "block_all": true |
| 45 | + }); |
| 46 | + |
| 47 | + assert_eq!(to_json_value(invite_permission_config).unwrap(), json); |
| 48 | + } |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn deserialization() { |
| 52 | + let json = json!({ |
| 53 | + "content": { |
| 54 | + "block_all": true |
| 55 | + }, |
| 56 | + "type": "m.invite_permission_config" |
| 57 | + }); |
| 58 | + |
| 59 | + assert_matches!( |
| 60 | + from_json_value::<AnyGlobalAccountDataEvent>(json), |
| 61 | + Ok(AnyGlobalAccountDataEvent::InvitePermissionConfig(ev)) |
| 62 | + ); |
| 63 | + assert!(ev.content.block_all); |
| 64 | + } |
| 65 | +} |
0 commit comments