jmap_client/thread/
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        request::{Arguments, Request},
18        response::ThreadGetResponse,
19    },
20    Method,
21};
22
23use super::Thread;
24
25impl Client {
26    #[maybe_async::maybe_async]
27    pub async fn thread_get(&self, id: &str) -> crate::Result<Option<Thread>> {
28        let mut request = self.build();
29        request.get_thread().ids([id]);
30        request
31            .send_single::<ThreadGetResponse>()
32            .await
33            .map(|mut r| r.take_list().pop())
34    }
35}
36
37impl Request<'_> {
38    pub fn get_thread(&mut self) -> &mut GetRequest<Thread> {
39        self.add_method_call(
40            Method::GetThread,
41            Arguments::thread_get(self.params(Method::GetThread)),
42        )
43        .thread_get_mut()
44    }
45
46    #[maybe_async::maybe_async]
47    pub async fn send_get_thread(self) -> crate::Result<ThreadGetResponse> {
48        self.send_single().await
49    }
50
51    pub fn changes_thread(&mut self, since_state: impl Into<String>) -> &mut ChangesRequest {
52        self.add_method_call(
53            Method::ChangesThread,
54            Arguments::changes(self.params(Method::ChangesThread), since_state.into()),
55        )
56        .changes_mut()
57    }
58
59    #[maybe_async::maybe_async]
60    pub async fn send_changes_thread(self) -> crate::Result<ChangesResponse<Thread>> {
61        self.send_single().await
62    }
63}