use alloc::{format, string::String, vec::Vec};
use io_http::rfc6750::bearer::HttpAuthBearer;
use log::{debug, trace};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{
coroutine::*,
msgraph_try,
v1::{
query::to_query_pairs,
rest::users::{contacts::delta::MsgraphRemoved, messages::MsgraphMessage},
send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
},
};
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphMessagesDeltaResponse {
#[serde(default)]
pub value: Vec<MsgraphMessageDelta>,
#[serde(default, rename = "@odata.nextLink")]
pub next_link: Option<String>,
#[serde(default, rename = "@odata.deltaLink")]
pub delta_link: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MsgraphMessageDelta {
#[serde(flatten)]
pub message: MsgraphMessage,
#[serde(default, rename = "@removed", skip_serializing_if = "Option::is_none")]
pub removed: Option<MsgraphRemoved>,
}
#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
pub struct MsgraphMessagesDeltaParams<'a> {
#[serde(rename = "$select")]
pub select: Option<&'a str>,
#[serde(rename = "$filter")]
pub filter: Option<&'a str>,
#[serde(skip)]
pub max_page_size: Option<u32>,
}
pub struct MsgraphMessagesDelta {
send: MsgraphSend<MsgraphMessagesDeltaResponse>,
}
impl MsgraphMessagesDelta {
pub fn new(
auth: &HttpAuthBearer,
user_id: &str,
folder: Option<&str>,
select: Option<&str>,
) -> Result<Self, MsgraphSendError> {
let params = MsgraphMessagesDeltaParams {
select,
..Default::default()
};
Self::with_params(auth, user_id, folder, ¶ms)
}
pub fn with_params(
auth: &HttpAuthBearer,
user_id: &str,
folder: Option<&str>,
params: &MsgraphMessagesDeltaParams,
) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph messages delta");
trace!("folder: {folder:?}");
trace!("params: {params:?}");
let user = user_path(user_id);
let path = match folder {
Some(folder) => format!("{user}/mailFolders/{folder}/messages/delta"),
None => format!("{user}/messages/delta"),
};
let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&path)?;
url.query_pairs_mut().extend_pairs(to_query_pairs(params));
let mut send = MsgraphSend::get(auth, url);
if let Some(size) = params.max_page_size {
send = send.max_page_size(size);
}
Ok(Self { send })
}
pub fn from_link(auth: &HttpAuthBearer, link: &str) -> Result<Self, MsgraphSendError> {
debug!("prepare microsoft graph messages delta from link");
trace!("link: {link:?}");
let url = Url::parse(link)?;
let send = MsgraphSend::get(auth, url);
Ok(Self { send })
}
pub fn max_page_size(mut self, size: u32) -> Self {
self.send = self.send.max_page_size(size);
self
}
}
impl MsgraphCoroutine for MsgraphMessagesDelta {
type Yield = MsgraphYield;
type Return = Result<MsgraphSendOutput<MsgraphMessagesDeltaResponse>, MsgraphSendError>;
fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
let out = msgraph_try!(&mut self.send, arg);
debug!("messages delta page received");
trace!("out: {out:?}");
MsgraphCoroutineState::Complete(Ok(out))
}
}