Skip to main content

io_gmail/v1/rest/threads/
list.rs

1//! List the Gmail threads (`users.threads.list`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.threads/list>
4
5use alloc::{format, string::String, vec::Vec};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::{
16        query::{is_false, to_query_pairs},
17        rest::threads::GmailThreadSummary,
18        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
19    },
20};
21
22/// Query parameters for listing threads (`users.threads.list`).
23#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
24#[serde(rename_all = "camelCase")]
25pub struct GmailThreadsListParams<'a> {
26    /// Search query filtering threads, using the Gmail search box syntax.
27    pub q: Option<&'a str>,
28    /// Label ids that returned threads must all carry.
29    pub label_ids: &'a [String],
30    /// Maximum number of threads to return per page.
31    pub max_results: Option<u32>,
32    /// Page token from a previous listing response.
33    pub page_token: Option<&'a str>,
34    /// Whether to include threads from SPAM and TRASH.
35    #[serde(skip_serializing_if = "is_false")]
36    pub include_spam_trash: bool,
37}
38
39/// Gmail REST thread listing response (one page of thread summaries).
40#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
41#[serde(rename_all = "camelCase")]
42pub struct GmailThreadsListResponse {
43    /// Thread summaries of the current page.
44    #[serde(default)]
45    pub threads: Vec<GmailThreadSummary>,
46    /// Token to fetch the next page, absent on the last page.
47    #[serde(default)]
48    pub next_page_token: Option<String>,
49    /// Estimated total number of results.
50    #[serde(default)]
51    pub result_size_estimate: Option<u64>,
52}
53
54/// Gmail REST thread listing, wrapping a page of thread summaries.
55pub struct GmailThreadsList {
56    send: GmailSend<GmailThreadsListResponse>,
57}
58
59impl GmailThreadsList {
60    /// Builds the `users.threads.list` request from the given query
61    /// parameters; `user_id` is the mailbox owner (usually `me`).
62    pub fn new(
63        auth: &HttpAuthBearer,
64        user_id: &str,
65        params: &GmailThreadsListParams,
66    ) -> Result<Self, GmailSendError> {
67        debug!("prepare gmail threads listing");
68        trace!("params: {params:?}");
69
70        let mut url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/threads"))?;
71        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
72
73        let send = GmailSend::get(auth, url);
74
75        Ok(Self { send })
76    }
77}
78
79impl GmailCoroutine for GmailThreadsList {
80    type Yield = GmailYield;
81    type Return = Result<GmailSendOutput<GmailThreadsListResponse>, GmailSendError>;
82
83    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
84        let out = gmail_try!(&mut self.send, arg);
85        debug!("threads listed");
86        trace!("out: {out:?}");
87        GmailCoroutineState::Complete(Ok(out))
88    }
89}