jmap_client/mailbox/
set.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use super::{ACLPatch, Mailbox, Role, SetArguments};
13use crate::{core::set::SetObject, principal::ACL, Get, Set};
14use ahash::AHashMap;
15
16impl Mailbox<Set> {
17    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
18        self.name = Some(name.into());
19        self
20    }
21
22    pub fn parent_id(&mut self, parent_id: Option<impl Into<String>>) -> &mut Self {
23        self.parent_id = parent_id.map(|s| s.into());
24        self
25    }
26
27    pub fn parent_id_ref(&mut self, parent_id_ref: &str) -> &mut Self {
28        self.parent_id = format!("#{}", parent_id_ref).into();
29        self
30    }
31
32    pub fn role(&mut self, role: Role) -> &mut Self {
33        if !matches!(role, Role::None) {
34            self.role = Some(role);
35        } else {
36            self.role = None;
37        }
38        self
39    }
40
41    pub fn sort_order(&mut self, sort_order: u32) -> &mut Self {
42        self.sort_order = sort_order.into();
43        self
44    }
45
46    pub fn is_subscribed(&mut self, is_subscribed: bool) -> &mut Self {
47        self.is_subscribed = is_subscribed.into();
48        self
49    }
50
51    pub fn acls<T, U, V>(&mut self, acls: T) -> &mut Self
52    where
53        T: IntoIterator<Item = (U, V)>,
54        U: Into<String>,
55        V: IntoIterator<Item = ACL>,
56    {
57        self.share_with = Some(
58            acls.into_iter()
59                .map(|(id, acls)| (id.into(), acls.into_iter().map(|acl| (acl, true)).collect()))
60                .collect(),
61        );
62        self
63    }
64
65    pub fn acl(&mut self, id: &str, acl: impl IntoIterator<Item = ACL>) -> &mut Self {
66        self.acl_patch.get_or_insert_with(AHashMap::new).insert(
67            format!("shareWith/{}", id),
68            ACLPatch::Replace(acl.into_iter().map(|acl| (acl, true)).collect()),
69        );
70        self
71    }
72
73    pub fn acl_set(&mut self, id: &str, acl: ACL, set: bool) -> &mut Self {
74        self.acl_patch
75            .get_or_insert_with(AHashMap::new)
76            .insert(format!("shareWith/{}/{}", id, acl), ACLPatch::Set(set));
77        self
78    }
79}
80
81pub fn role_not_set(role: &Option<Role>) -> bool {
82    matches!(role, Some(Role::None))
83}
84
85impl SetObject for Mailbox<Set> {
86    type SetArguments = SetArguments;
87
88    fn new(_create_id: Option<usize>) -> Self {
89        Mailbox {
90            _create_id,
91            _state: Default::default(),
92            id: None,
93            name: None,
94            parent_id: "".to_string().into(),
95            role: Role::None.into(),
96            sort_order: None,
97            total_emails: None,
98            unread_emails: None,
99            total_threads: None,
100            unread_threads: None,
101            my_rights: None,
102            is_subscribed: None,
103            share_with: AHashMap::with_capacity(0).into(),
104            acl_patch: None,
105        }
106    }
107
108    fn create_id(&self) -> Option<String> {
109        self._create_id.map(|id| format!("c{}", id))
110    }
111}
112
113impl SetObject for Mailbox<Get> {
114    type SetArguments = SetArguments;
115
116    fn new(_create_id: Option<usize>) -> Self {
117        unimplemented!()
118    }
119
120    fn create_id(&self) -> Option<String> {
121        None
122    }
123}
124
125impl SetArguments {
126    pub fn on_destroy_remove_emails(&mut self, value: bool) -> &mut Self {
127        self.on_destroy_remove_emails = value.into();
128        self
129    }
130}