jmap_client/principal/
helpers.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 crate::{
13    client::Client,
14    core::{
15        changes::{ChangesRequest, ChangesResponse},
16        get::GetRequest,
17        query::{Comparator, Filter, QueryRequest, QueryResponse},
18        query_changes::{QueryChangesRequest, QueryChangesResponse},
19        request::{Arguments, Request},
20        response::{PrincipalGetResponse, PrincipalSetResponse},
21        set::{SetObject, SetRequest},
22    },
23    Get, Method, Set,
24};
25
26use super::{Principal, Property, Type, DKIM};
27
28impl Client {
29    #[maybe_async::maybe_async]
30    pub async fn individual_create(
31        &self,
32        email: impl Into<String>,
33        secret: impl Into<String>,
34        name: impl Into<String>,
35    ) -> crate::Result<Principal> {
36        let mut request = self.build();
37        let id = request
38            .set_principal()
39            .create()
40            .name(name)
41            .secret(secret)
42            .email(email)
43            .ptype(Type::Individual)
44            .create_id()
45            .unwrap();
46        request
47            .send_single::<PrincipalSetResponse>()
48            .await?
49            .created(&id)
50    }
51
52    #[maybe_async::maybe_async]
53    pub async fn domain_create(&self, name: impl Into<String>) -> crate::Result<Principal> {
54        let mut request = self.build();
55        let id = request
56            .set_principal()
57            .create()
58            .name(name)
59            .ptype(Type::Domain)
60            .create_id()
61            .unwrap();
62        request
63            .send_single::<PrincipalSetResponse>()
64            .await?
65            .created(&id)
66    }
67
68    #[maybe_async::maybe_async]
69    pub async fn domain_enable_dkim(
70        &self,
71        id: &str,
72        key: impl Into<String>,
73        selector: impl Into<String>,
74        expiration: Option<i64>,
75    ) -> crate::Result<Option<Principal>> {
76        let mut request = self.build();
77        request
78            .set_principal()
79            .update(id)
80            .secret(key)
81            .dkim(DKIM::new(Some(selector), expiration));
82        request
83            .send_single::<PrincipalSetResponse>()
84            .await?
85            .updated(id)
86    }
87
88    #[maybe_async::maybe_async]
89    pub async fn list_create(
90        &self,
91        email: impl Into<String>,
92        name: impl Into<String>,
93        members: impl IntoIterator<Item = impl Into<String>>,
94    ) -> crate::Result<Principal> {
95        let mut request = self.build();
96        let id = request
97            .set_principal()
98            .create()
99            .name(name)
100            .email(email)
101            .ptype(Type::List)
102            .members(members.into())
103            .create_id()
104            .unwrap();
105        request
106            .send_single::<PrincipalSetResponse>()
107            .await?
108            .created(&id)
109    }
110
111    #[maybe_async::maybe_async]
112    pub async fn group_create(
113        &self,
114        email: impl Into<String>,
115        name: impl Into<String>,
116        members: impl IntoIterator<Item = impl Into<String>>,
117    ) -> crate::Result<Principal> {
118        let mut request = self.build();
119        let id = request
120            .set_principal()
121            .create()
122            .name(name)
123            .email(email)
124            .ptype(Type::Group)
125            .members(members.into())
126            .create_id()
127            .unwrap();
128        request
129            .send_single::<PrincipalSetResponse>()
130            .await?
131            .created(&id)
132    }
133
134    #[maybe_async::maybe_async]
135    pub async fn principal_set_name(
136        &self,
137        id: &str,
138        name: impl Into<String>,
139    ) -> crate::Result<Option<Principal>> {
140        let mut request = self.build();
141        request.set_principal().update(id).name(name);
142        request
143            .send_single::<PrincipalSetResponse>()
144            .await?
145            .updated(id)
146    }
147
148    #[maybe_async::maybe_async]
149    pub async fn principal_set_secret(
150        &self,
151        id: &str,
152        secret: impl Into<String>,
153    ) -> crate::Result<Option<Principal>> {
154        let mut request = self.build();
155        request.set_principal().update(id).secret(secret);
156        request
157            .send_single::<PrincipalSetResponse>()
158            .await?
159            .updated(id)
160    }
161
162    #[maybe_async::maybe_async]
163    pub async fn principal_set_email(
164        &self,
165        id: &str,
166        email: impl Into<String>,
167    ) -> crate::Result<Option<Principal>> {
168        let mut request = self.build();
169        request.set_principal().update(id).email(email);
170        request
171            .send_single::<PrincipalSetResponse>()
172            .await?
173            .updated(id)
174    }
175
176    #[maybe_async::maybe_async]
177    pub async fn principal_set_timezone(
178        &self,
179        id: &str,
180        timezone: Option<impl Into<String>>,
181    ) -> crate::Result<Option<Principal>> {
182        let mut request = self.build();
183        request.set_principal().update(id).timezone(timezone);
184        request
185            .send_single::<PrincipalSetResponse>()
186            .await?
187            .updated(id)
188    }
189
190    #[maybe_async::maybe_async]
191    pub async fn principal_set_members(
192        &self,
193        id: &str,
194        members: Option<impl IntoIterator<Item = impl Into<String>>>,
195    ) -> crate::Result<Option<Principal>> {
196        let mut request = self.build();
197        request.set_principal().update(id).members(members);
198        request
199            .send_single::<PrincipalSetResponse>()
200            .await?
201            .updated(id)
202    }
203
204    #[maybe_async::maybe_async]
205    pub async fn principal_set_aliases(
206        &self,
207        id: &str,
208        aliases: Option<impl IntoIterator<Item = impl Into<String>>>,
209    ) -> crate::Result<Option<Principal>> {
210        let mut request = self.build();
211        request.set_principal().update(id).aliases(aliases);
212        request
213            .send_single::<PrincipalSetResponse>()
214            .await?
215            .updated(id)
216    }
217
218    #[maybe_async::maybe_async]
219    pub async fn principal_set_capabilities(
220        &self,
221        id: &str,
222        capabilities: Option<impl IntoIterator<Item = impl Into<String>>>,
223    ) -> crate::Result<Option<Principal>> {
224        let mut request = self.build();
225        request
226            .set_principal()
227            .update(id)
228            .capabilities(capabilities);
229        request
230            .send_single::<PrincipalSetResponse>()
231            .await?
232            .updated(id)
233    }
234
235    #[maybe_async::maybe_async]
236    pub async fn principal_destroy(&self, id: &str) -> crate::Result<()> {
237        let mut request = self.build();
238        request.set_principal().destroy([id]).arguments();
239        request
240            .send_single::<PrincipalSetResponse>()
241            .await?
242            .destroyed(id)
243    }
244
245    #[maybe_async::maybe_async]
246    pub async fn principal_get(
247        &self,
248        id: &str,
249        properties: Option<impl IntoIterator<Item = Property>>,
250    ) -> crate::Result<Option<Principal>> {
251        let mut request = self.build();
252        let get_request = request.get_principal().ids([id]);
253        if let Some(properties) = properties {
254            get_request.properties(properties.into_iter());
255        }
256        request
257            .send_single::<PrincipalGetResponse>()
258            .await
259            .map(|mut r| r.take_list().pop())
260    }
261
262    #[maybe_async::maybe_async]
263    pub async fn principal_query(
264        &self,
265        filter: Option<impl Into<Filter<super::query::Filter>>>,
266        sort: Option<impl IntoIterator<Item = Comparator<super::query::Comparator>>>,
267    ) -> crate::Result<QueryResponse> {
268        let mut request = self.build();
269        let query_request = request.query_principal();
270        if let Some(filter) = filter {
271            query_request.filter(filter);
272        }
273        if let Some(sort) = sort {
274            query_request.sort(sort.into_iter());
275        }
276        request.send_single::<QueryResponse>().await
277    }
278
279    #[maybe_async::maybe_async]
280    pub async fn principal_changes(
281        &self,
282        since_state: impl Into<String>,
283        max_changes: usize,
284    ) -> crate::Result<ChangesResponse<Principal<Get>>> {
285        let mut request = self.build();
286        request
287            .changes_principal(since_state)
288            .max_changes(max_changes);
289        request.send_single().await
290    }
291}
292
293impl Request<'_> {
294    pub fn get_principal(&mut self) -> &mut GetRequest<Principal<Set>> {
295        self.add_method_call(
296            Method::GetPrincipal,
297            Arguments::principal_get(self.params(Method::GetPrincipal)),
298        )
299        .principal_get_mut()
300    }
301
302    #[maybe_async::maybe_async]
303    pub async fn send_get_principal(self) -> crate::Result<PrincipalGetResponse> {
304        self.send_single().await
305    }
306
307    pub fn changes_principal(&mut self, since_state: impl Into<String>) -> &mut ChangesRequest {
308        self.add_method_call(
309            Method::ChangesPrincipal,
310            Arguments::changes(self.params(Method::ChangesPrincipal), since_state.into()),
311        )
312        .changes_mut()
313    }
314
315    #[maybe_async::maybe_async]
316    pub async fn send_changes_principal(self) -> crate::Result<ChangesResponse<Principal<Get>>> {
317        self.send_single().await
318    }
319
320    pub fn query_principal(&mut self) -> &mut QueryRequest<Principal<Set>> {
321        self.add_method_call(
322            Method::QueryPrincipal,
323            Arguments::principal_query(self.params(Method::QueryPrincipal)),
324        )
325        .principal_query_mut()
326    }
327
328    #[maybe_async::maybe_async]
329    pub async fn send_query_principal(self) -> crate::Result<QueryResponse> {
330        self.send_single().await
331    }
332
333    pub fn query_principal_changes(
334        &mut self,
335        since_query_state: impl Into<String>,
336    ) -> &mut QueryChangesRequest<Principal<Set>> {
337        self.add_method_call(
338            Method::QueryChangesPrincipal,
339            Arguments::principal_query_changes(
340                self.params(Method::QueryChangesPrincipal),
341                since_query_state.into(),
342            ),
343        )
344        .principal_query_changes_mut()
345    }
346
347    #[maybe_async::maybe_async]
348    pub async fn send_query_principal_changes(self) -> crate::Result<QueryChangesResponse> {
349        self.send_single().await
350    }
351
352    pub fn set_principal(&mut self) -> &mut SetRequest<Principal<Set>> {
353        self.add_method_call(
354            Method::SetPrincipal,
355            Arguments::principal_set(self.params(Method::SetPrincipal)),
356        )
357        .principal_set_mut()
358    }
359
360    #[maybe_async::maybe_async]
361    pub async fn send_set_principal(self) -> crate::Result<PrincipalSetResponse> {
362        self.send_single().await
363    }
364}