Skip to main content

io_msgraph/v1/rest/users/messages/
update.rs

1//! Update a Microsoft Graph message (`PATCH /me/messages/{id}`); used to
2//! change `isRead`, the follow-up flag, categories, etc.
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/message-update>
5
6use alloc::format;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        rest::users::messages::MsgraphMessage,
17        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
18    },
19};
20
21/// Updates a Microsoft Graph message.
22pub struct MsgraphMessageUpdate {
23    send: MsgraphSend<MsgraphMessage>,
24}
25
26impl MsgraphMessageUpdate {
27    /// Patches the message `id` with the set fields of `message`.
28    pub fn new(
29        auth: &HttpAuthBearer,
30        user_id: &str,
31        id: &str,
32        message: &MsgraphMessage,
33    ) -> Result<Self, MsgraphSendError> {
34        debug!("prepare microsoft graph message update");
35        trace!("id: {id:?}");
36        trace!("message: {message:?}");
37
38        let user = user_path(user_id);
39        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/messages/{id}"))?;
40        let send = MsgraphSend::patch_json(auth, url, message)?;
41
42        Ok(Self { send })
43    }
44}
45
46impl MsgraphCoroutine for MsgraphMessageUpdate {
47    type Yield = MsgraphYield;
48    type Return = Result<MsgraphSendOutput<MsgraphMessage>, MsgraphSendError>;
49
50    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
51        let out = msgraph_try!(&mut self.send, arg);
52        debug!("message updated");
53        trace!("out: {out:?}");
54        MsgraphCoroutineState::Complete(Ok(out))
55    }
56}