Skip to main content

zulip_api/message/
group_permission_setting.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_default, impl_display};
4
5/// Represents configuration for a group permission setting specifying the groups to which the setting can be set to and the default values for the setting.
6///
7/// **Changes**: Removed `allow_owners_group` field in Zulip 10.0 (feature level 326), as we now support anonymous user groups.
8/// Previously, the `role:owners` system group was not offered when `allow_owners_group` was false.
9///
10/// Removed unnecessary `id_field_name` field in Zulip 10.0 (feature level 326).
11/// Previously, this always had the value of `\"{setting_name}_id\"`;
12/// it was an internal implementation detail of the server not intended to be included in the API.
13#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
14pub struct GroupPermissionSetting {
15    #[serde(skip_serializing_if = "Option::is_none")]
16    require_system_group: Option<bool>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    allow_internet_group: Option<bool>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    allow_nobody_group: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    allow_everyone_group: Option<bool>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    default_group_name: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    default_for_system_groups: Option<String>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    allowed_system_groups: Option<Vec<String>>,
29}
30
31impl GroupPermissionSetting {
32    /// Creates a new [GroupPermissionSetting].
33    pub const fn new() -> Self {
34        Self {
35            require_system_group: None,
36            allow_internet_group: None,
37            allow_nobody_group: None,
38            allow_everyone_group: None,
39            default_group_name: None,
40            default_for_system_groups: None,
41            allowed_system_groups: None,
42        }
43    }
44}
45
46field_access! {
47    GroupPermissionSetting {
48        /// Whether the setting can only be set to a system user group.
49        require_system_group: option { bool },
50        /// Whether the setting can be set to `role:internet` system group.
51        allow_internet_group: option { bool },
52        /// Whether the setting can be set to `role:nobody` system group.
53        allow_nobody_group: option { bool },
54        /// Whether the setting can be set to `role:everyone` system group.
55        ///
56        /// If false, guest users cannot exercise this permission even if they are part of the [group-setting value](https://zulip.com/api/group-setting-values) for this setting.
57        allow_everyone_group: option { bool },
58    }
59}
60
61field_access! {
62    GroupPermissionSetting {
63        /// Name of the default system group for the setting.
64        ///
65        /// For some channel settings, this can also be `channel_creator`.
66        ///
67        /// In that case:
68        ///
69        /// - If the channel's [`creator_id`](https://zulip.com/api/get-stream-by-id#response) is not `null`, default for the setting is an anonymous group with the channel creator as the only member.
70        /// - If the channel's [`creator_id`](https://zulip.com/api/get-stream-by-id#response) is `null`, default for the setting is `role:nobody` system group.
71        ///
72        /// **Changes**: In Zulip 12.0 (feature level 427), renamed `stream_creator_or_nobody` value to `channel_creator`.
73        default_group_name: option_cow_deref { str, String },
74        /// Name of the default group for the setting for system groups.  This is non-null only for group-level settings.
75        default_for_system_groups: option_cow_deref { str, String },
76        /// An array of names of system groups to which the setting can be set to.
77        ///
78        /// If the list is empty, the setting can be set to system groups based on the other boolean fields.
79        ///
80        /// **Changes**: New in Zulip 8.0 (feature level 225).
81        allowed_system_groups: option_cow_deref { [String], Vec<String> },
82    }
83}
84
85impl_default!(GroupPermissionSetting);
86impl_display!(GroupPermissionSetting);
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn test_group_setting() {
94        let require_system_group = true;
95        let allow_internet_group = false;
96        let allow_nobody_group = true;
97        let allow_everyone_group = false;
98        let default_group_name = "test default group name";
99        let default_for_system_groups = "test default system groups";
100        let allowed_system_groups = ["test allowed system group".to_string()];
101
102        let setting = GroupPermissionSetting::new()
103            .with_require_system_group(require_system_group)
104            .with_allow_internet_group(allow_internet_group)
105            .with_allow_nobody_group(allow_nobody_group)
106            .with_allow_everyone_group(allow_everyone_group)
107            .with_default_group_name(default_group_name)
108            .with_default_for_system_groups(default_for_system_groups)
109            .with_allowed_system_groups(allowed_system_groups.as_ref());
110        let setting_json = setting.to_string();
111
112        assert_eq!(serde_json::to_string(&setting).unwrap(), setting_json);
113        assert_eq!(
114            serde_json::from_str::<GroupPermissionSetting>(&setting_json).unwrap(),
115            setting
116        );
117
118        assert_eq!(setting.require_system_group(), Some(require_system_group));
119        assert_eq!(setting.allow_internet_group(), Some(allow_internet_group));
120        assert_eq!(setting.allow_nobody_group(), Some(allow_nobody_group));
121        assert_eq!(setting.allow_everyone_group(), Some(allow_everyone_group));
122        assert_eq!(setting.default_group_name(), Some(default_group_name));
123        assert_eq!(
124            setting.default_for_system_groups(),
125            Some(default_for_system_groups)
126        );
127        assert_eq!(
128            setting.allowed_system_groups(),
129            Some(allowed_system_groups.as_ref())
130        );
131    }
132}