Skip to main content

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

1//! Move a Microsoft Graph message to a folder
2//! (`POST /me/messages/{id}/move`); creates a copy in the destination
3//! and returns it with a new id.
4//!
5//! <https://learn.microsoft.com/en-us/graph/api/message-move>
6
7use alloc::format;
8
9use io_http::rfc6750::bearer::HttpAuthBearer;
10use log::{debug, trace};
11use serde::Serialize;
12use url::Url;
13
14use crate::{
15    coroutine::*,
16    msgraph_try,
17    v1::{
18        rest::users::messages::MsgraphMessage,
19        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
20    },
21};
22
23#[derive(Debug, Serialize)]
24#[serde(rename_all = "camelCase")]
25struct MsgraphMessageMoveRequest<'a> {
26    destination_id: &'a str,
27}
28
29/// Moves a Microsoft Graph message into another folder.
30pub struct MsgraphMessageMove {
31    send: MsgraphSend<MsgraphMessage>,
32}
33
34impl MsgraphMessageMove {
35    /// Moves `id` into `destination` (a folder id or a well-known name
36    /// such as `archive`).
37    pub fn new(
38        auth: &HttpAuthBearer,
39        user_id: &str,
40        id: &str,
41        destination: &str,
42    ) -> Result<Self, MsgraphSendError> {
43        debug!("prepare microsoft graph message move");
44        trace!("id: {id:?}");
45        trace!("destination: {destination:?}");
46
47        let user = user_path(user_id);
48        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/messages/{id}/move"))?;
49        let body = MsgraphMessageMoveRequest {
50            destination_id: destination,
51        };
52        let send = MsgraphSend::post_json(auth, url, &body)?;
53
54        Ok(Self { send })
55    }
56}
57
58impl MsgraphCoroutine for MsgraphMessageMove {
59    type Yield = MsgraphYield;
60    type Return = Result<MsgraphSendOutput<MsgraphMessage>, MsgraphSendError>;
61
62    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
63        let out = msgraph_try!(&mut self.send, arg);
64        debug!("message moved");
65        trace!("out: {out:?}");
66        MsgraphCoroutineState::Complete(Ok(out))
67    }
68}