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::{
20            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
21            user_path,
22        },
23    },
24};
25
26#[derive(Debug, Serialize)]
27#[serde(rename_all = "camelCase")]
28struct MsgraphMessageMoveRequest<'a> {
29    destination_id: &'a str,
30}
31
32/// Moves a Microsoft Graph message into another folder.
33pub struct MsgraphMessageMove {
34    send: MsgraphSend<MsgraphMessage>,
35}
36
37impl MsgraphMessageMove {
38    /// Moves `id` into `destination` (a folder id or a well-known name
39    /// such as `archive`).
40    pub fn new(
41        auth: &HttpAuthBearer,
42        user_id: &str,
43        id: &str,
44        destination: &str,
45    ) -> Result<Self, MsgraphSendError> {
46        debug!("prepare microsoft graph message move");
47        trace!("id: {id:?}");
48        trace!("destination: {destination:?}");
49
50        let user = user_path(user_id)?;
51        let url = Url::parse(MSGRAPH_API_BASE)?
52            .join(&format!("{user}/messages/{}/move", path_segment(id)?))?;
53        let body = MsgraphMessageMoveRequest {
54            destination_id: destination,
55        };
56        let send = MsgraphSend::post_json(auth, url, &body)?;
57
58        Ok(Self { send })
59    }
60}
61
62impl MsgraphCoroutine for MsgraphMessageMove {
63    type Yield = MsgraphYield;
64    type Return = Result<MsgraphSendOutput<MsgraphMessage>, MsgraphSendError>;
65
66    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
67        let out = msgraph_try!(&mut self.send, arg);
68        debug!("message moved");
69        trace!("out: {out:?}");
70        MsgraphCoroutineState::Complete(Ok(out))
71    }
72}