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::{
18            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
19            user_path,
20        },
21    },
22};
23
24/// Updates a Microsoft Graph message.
25pub struct MsgraphMessageUpdate {
26    send: MsgraphSend<MsgraphMessage>,
27}
28
29impl MsgraphMessageUpdate {
30    /// Patches the message `id` with the set fields of `message`.
31    pub fn new(
32        auth: &HttpAuthBearer,
33        user_id: &str,
34        id: &str,
35        message: &MsgraphMessage,
36    ) -> Result<Self, MsgraphSendError> {
37        debug!("prepare microsoft graph message update");
38        trace!("id: {id:?}");
39        trace!("message: {message:?}");
40
41        let user = user_path(user_id)?;
42        let url = Url::parse(MSGRAPH_API_BASE)?
43            .join(&format!("{user}/messages/{}", path_segment(id)?))?;
44        let send = MsgraphSend::patch_json(auth, url, message)?;
45
46        Ok(Self { send })
47    }
48}
49
50impl MsgraphCoroutine for MsgraphMessageUpdate {
51    type Yield = MsgraphYield;
52    type Return = Result<MsgraphSendOutput<MsgraphMessage>, MsgraphSendError>;
53
54    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
55        let out = msgraph_try!(&mut self.send, arg);
56        debug!("message updated");
57        trace!("out: {out:?}");
58        MsgraphCoroutineState::Complete(Ok(out))
59    }
60}