Skip to main content

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

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