Skip to main content

io_msgraph/v1/rest/users/todo/lists/
delta.rs

1//! Track changes to Microsoft To Do task lists
2//! (`GET /me/todo/lists/delta`).
3//!
4//! An initial request enumerates every list and ends with an
5//! `@odata.deltaLink`; following that link returns only what changed
6//! since, removals arriving as `@removed`-marked rows. An expired link
7//! answers HTTP 410; the consumer falls back to an initial request.
8//!
9//! <https://learn.microsoft.com/en-us/graph/api/todotasklist-delta>
10
11use alloc::{format, string::String, vec::Vec};
12
13use io_http::rfc6750::bearer::HttpAuthBearer;
14use log::{debug, trace};
15use serde::{Deserialize, Serialize};
16use url::Url;
17
18use crate::{
19    coroutine::*,
20    msgraph_try,
21    v1::{
22        rest::users::{contacts::delta::MsgraphRemoved, todo::lists::MsgraphTodoList},
23        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
24    },
25};
26
27/// One page of a task lists delta round.
28///
29/// More pages follow through `next_link`; the round ends when
30/// `delta_link` arrives (the token of the next round).
31#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
32pub struct MsgraphTodoListsDeltaResponse {
33    /// The changed lists of the page.
34    #[serde(default)]
35    pub value: Vec<MsgraphTodoListDelta>,
36    /// The URL of the next page of the round, when one exists.
37    #[serde(default, rename = "@odata.nextLink")]
38    pub next_link: Option<String>,
39    /// The URL closing the round, carrying the next round's token.
40    #[serde(default, rename = "@odata.deltaLink")]
41    pub delta_link: Option<String>,
42}
43
44/// One list row of a delta page: the list (only its id when the row is
45/// a removal), plus the `@removed` marker.
46#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
47pub struct MsgraphTodoListDelta {
48    /// The changed list.
49    #[serde(flatten)]
50    pub list: MsgraphTodoList,
51    /// The removal marker, present when the row is a removal.
52    #[serde(default, rename = "@removed", skip_serializing_if = "Option::is_none")]
53    pub removed: Option<MsgraphRemoved>,
54}
55
56/// I/O-free coroutine for a task lists delta request, opening a round
57/// with [`new`](Self::new) or continuing one with
58/// [`from_link`](Self::from_link).
59pub struct MsgraphTodoListsDelta {
60    send: MsgraphSend<MsgraphTodoListsDeltaResponse>,
61}
62
63impl MsgraphTodoListsDelta {
64    /// Starts a delta round over the user's task lists.
65    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, MsgraphSendError> {
66        debug!("prepare microsoft graph to do lists delta");
67
68        let user = user_path(user_id);
69        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/todo/lists/delta"))?;
70        let send = MsgraphSend::get(auth, url);
71
72        Ok(Self { send })
73    }
74
75    /// Continues a round from an `@odata.nextLink`, or starts the next
76    /// round from a saved `@odata.deltaLink`, sent as-is.
77    pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
78        debug!("prepare microsoft graph to do lists delta from link");
79        trace!("link: {link}");
80
81        let url = Url::parse(link)?;
82        let send = MsgraphSend::get(auth, url);
83
84        Ok(Self { send })
85    }
86
87    /// Asks Graph for at most `size` items per page
88    /// (`Prefer: odata.maxpagesize={size}`), see
89    /// [`MsgraphSend::max_page_size`]. Graph keeps no memory of it
90    /// across pages: chain it on every page request, the ones built
91    /// from an `@odata.nextLink` included.
92    pub fn max_page_size(mut self, size: u32) -> Self {
93        self.send = self.send.max_page_size(size);
94        self
95    }
96}
97
98impl MsgraphCoroutine for MsgraphTodoListsDelta {
99    type Yield = MsgraphYield;
100    type Return = Result<MsgraphSendOutput<MsgraphTodoListsDeltaResponse>, MsgraphSendError>;
101
102    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
103        let out = msgraph_try!(&mut self.send, arg);
104        debug!("to do lists delta page received");
105        trace!("out: {out:?}");
106        MsgraphCoroutineState::Complete(Ok(out))
107    }
108}