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