use alloc::{collections::BTreeMap, string::String, vec, vec::Vec};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, request::JmapBatch, send::*, session::JmapSession, set::*},
rfc8621::{
JMAP_MAIL_CAPABILITY,
mailbox::{JmapMailbox, JmapMailboxRole},
},
};
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapMailboxCreate {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<JmapMailboxRole>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_order: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_subscribed: Option<bool>,
}
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapMailboxUpdate {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<JmapMailboxRole>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_order: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_subscribed: Option<bool>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum JmapMailboxSetItemError {
MailboxHasChild {
description: Option<String>,
},
MailboxHasEmail {
description: Option<String>,
},
NotFound {
description: Option<String>,
},
InvalidPatch {
description: Option<String>,
},
WillDestroy {
description: Option<String>,
},
InvalidProperties {
description: Option<String>,
#[serde(default)]
properties: Vec<String>,
},
Singleton {
description: Option<String>,
},
#[serde(other)]
Unknown,
}
#[derive(Debug, Error)]
pub enum JmapMailboxSetError {
#[error("JMAP Mailbox/set failed: {0}")]
Send(#[from] JmapSendError),
#[error("JMAP Mailbox/set failed: serialize args: {0}")]
SerializeArgs(#[source] serde_json::Error),
#[error("JMAP Mailbox/set failed: {0}")]
Set(#[from] JmapSetError),
}
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapMailboxSetArgs {
#[serde(skip_serializing_if = "Option::is_none")]
pub create: Option<BTreeMap<String, JmapMailboxCreate>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update: Option<BTreeMap<String, JmapMailboxUpdate>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub destroy: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub on_destroy_remove_emails: Option<bool>,
}
#[derive(Clone, Debug)]
pub struct JmapMailboxSetOutput {
pub new_state: String,
pub created: BTreeMap<String, JmapMailbox>,
pub updated: BTreeMap<String, Option<JmapMailbox>>,
pub destroyed: Vec<String>,
pub not_created: BTreeMap<String, JmapMailboxSetItemError>,
pub not_updated: BTreeMap<String, JmapMailboxSetItemError>,
pub not_destroyed: BTreeMap<String, JmapMailboxSetItemError>,
pub keep_alive: bool,
}
pub struct JmapMailboxSet {
state: State,
}
impl JmapMailboxSet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
args: JmapMailboxSetArgs,
) -> Result<Self, JmapMailboxSetError> {
let account_id = session
.primary_accounts
.get(JMAP_MAIL_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let json_args = serde_json::to_value(MailboxSetRequest { account_id, args })
.map_err(JmapMailboxSetError::SerializeArgs)?;
let mut batch = JmapBatch::new();
batch.add("Mailbox/set", json_args);
let request = batch.into_request(vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_MAIL_CAPABILITY.into(),
]);
let send = JmapSend::new(http_auth, api_url, request)?;
Ok(Self {
state: State::Set(JmapSet::from_send(send)),
})
}
}
impl JmapCoroutine for JmapMailboxSet {
type Yield = JmapYield;
type Return = Result<JmapMailboxSetOutput, JmapMailboxSetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Set(set) => {
let JmapSetOutput {
new_state,
created,
updated,
destroyed,
not_created,
not_updated,
not_destroyed,
keep_alive,
} = jmap_try!(set, arg);
let parse = |map: BTreeMap<String, serde_json::Value>| {
map.into_iter()
.map(|(k, v)| {
let e = serde_json::from_value(v)
.unwrap_or(JmapMailboxSetItemError::Unknown);
(k, e)
})
.collect()
};
JmapCoroutineState::Complete(Ok(JmapMailboxSetOutput {
new_state,
created,
updated,
destroyed,
not_created: parse(not_created),
not_updated: parse(not_updated),
not_destroyed: parse(not_destroyed),
keep_alive,
}))
}
}
}
}
enum State {
Set(JmapSet<JmapMailbox>),
}
#[derive(Serialize)]
struct MailboxSetRequest {
#[serde(rename = "accountId")]
account_id: String,
#[serde(flatten)]
args: JmapMailboxSetArgs,
}