Skip to main content

io_gmail/v1/rest/users/
watch.rs

1//! Set up Gmail push notifications (`users.watch`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users/watch>
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::send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
16};
17
18/// Push-notification watch request body (`users.watch`).
19#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
20#[serde(rename_all = "camelCase")]
21pub struct GmailWatchRequest {
22    /// The fully qualified Cloud Pub/Sub topic to publish
23    /// notifications to.
24    pub topic_name: String,
25    /// The label ids to restrict notifications about.
26    #[serde(default, skip_serializing_if = "Vec::is_empty")]
27    pub label_ids: Vec<String>,
28    /// The filtering behavior applied to the label ids.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub label_filter_behavior: Option<GmailLabelFilterBehavior>,
31}
32
33/// Whether a watch includes or excludes its label IDs.
34#[derive(Debug, Clone, Copy, Deserialize, Serialize, Eq, PartialEq)]
35#[serde(rename_all = "camelCase")]
36pub enum GmailLabelFilterBehavior {
37    /// Only changes on the listed labels trigger a notification.
38    Include,
39    /// Changes on the listed labels never trigger a notification.
40    Exclude,
41}
42
43/// Result of establishing a watch (`users.watch`).
44#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
45#[serde(rename_all = "camelCase")]
46pub struct GmailWatchResponse {
47    /// The id of the current history record of the mailbox.
48    #[serde(default)]
49    pub history_id: Option<String>,
50    /// The expiration time of the watch as epoch milliseconds.
51    #[serde(default)]
52    pub expiration: Option<String>,
53}
54
55/// I/O-free coroutine setting up Gmail push notifications (`users.watch`).
56pub struct GmailWatch {
57    send: GmailSend<GmailWatchResponse>,
58}
59
60impl GmailWatch {
61    /// Builds the `users.watch` request from the given [`GmailWatchRequest`].
62    pub fn new(
63        auth: &HttpAuthBearer,
64        user_id: &str,
65        request: &GmailWatchRequest,
66    ) -> Result<Self, GmailSendError> {
67        debug!("prepare gmail watch");
68        trace!("request: {request:?}");
69
70        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/watch"))?;
71        let send = GmailSend::post_json(auth, url, request)?;
72
73        Ok(Self { send })
74    }
75}
76
77impl GmailCoroutine for GmailWatch {
78    type Yield = GmailYield;
79    type Return = Result<GmailSendOutput<GmailWatchResponse>, GmailSendError>;
80
81    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
82        let out = gmail_try!(&mut self.send, arg);
83        debug!("watch established");
84        trace!("out: {out:?}");
85        GmailCoroutineState::Complete(Ok(out))
86    }
87}