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;
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        page::{MsgraphPage, link_url},
23        rest::users::{contacts::delta::MsgraphRemoved, todo::lists::MsgraphTodoList},
24        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
25    },
26};
27
28/// One page of a task lists delta round.
29///
30/// More pages follow through `next_link`; the round ends when
31/// `delta_link` arrives (the token of the next round).
32pub type MsgraphTodoListsDeltaResponse = MsgraphPage<MsgraphTodoListDelta>;
33
34/// One list row of a delta page: the list (only its id when the row is
35/// a removal), plus the `@removed` marker.
36#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
37pub struct MsgraphTodoListDelta {
38    /// The changed list.
39    #[serde(flatten)]
40    pub list: MsgraphTodoList,
41    /// The removal marker, present when the row is a removal.
42    #[serde(default, rename = "@removed", skip_serializing_if = "Option::is_none")]
43    pub removed: Option<MsgraphRemoved>,
44}
45
46/// I/O-free coroutine for a task lists delta request, opening a round
47/// with [`new`](Self::new) or continuing one with
48/// [`from_link`](Self::from_link).
49pub struct MsgraphTodoListsDelta {
50    send: MsgraphSend<MsgraphTodoListsDeltaResponse>,
51}
52
53impl MsgraphTodoListsDelta {
54    /// Starts a delta round over the user's task lists.
55    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, MsgraphSendError> {
56        debug!("prepare microsoft graph to do lists delta");
57
58        let user = user_path(user_id)?;
59        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/todo/lists/delta"))?;
60        let send = MsgraphSend::get(auth, url);
61
62        Ok(Self { send })
63    }
64
65    /// Continues a round from an `@odata.nextLink`, or starts the next
66    /// round from a saved `@odata.deltaLink`, sent as-is. A link off the
67    /// Microsoft Graph origin is refused.
68    pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
69        debug!("prepare microsoft graph to do lists delta from link");
70        trace!("link: {link}");
71
72        let url = link_url(link)?;
73        let send = MsgraphSend::get(auth, url);
74
75        Ok(Self { send })
76    }
77
78    /// Asks Graph for at most `size` items per page
79    /// (`Prefer: odata.maxpagesize={size}`), see
80    /// [`MsgraphSend::max_page_size`]. Graph keeps no memory of it
81    /// across pages: chain it on every page request, the ones built
82    /// from an `@odata.nextLink` included.
83    pub fn max_page_size(mut self, size: u32) -> Self {
84        self.send = self.send.max_page_size(size);
85        self
86    }
87}
88
89impl MsgraphCoroutine for MsgraphTodoListsDelta {
90    type Yield = MsgraphYield;
91    type Return = Result<MsgraphSendOutput<MsgraphTodoListsDeltaResponse>, MsgraphSendError>;
92
93    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
94        let out = msgraph_try!(&mut self.send, arg);
95        debug!("to do lists delta page received");
96        trace!("out: {out:?}");
97        MsgraphCoroutineState::Complete(Ok(out))
98    }
99}