use alloc::{collections::BTreeMap, string::String, vec};
use secrecy::SecretString;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{
JMAP_CORE_CAPABILITY, error::JmapMethodError, request::JmapBatch, send::*,
session::JmapSession,
},
rfc8621::{
JMAP_MAIL_CAPABILITY,
vacation_response::{JMAP_VACATION_RESPONSE_CAPABILITY, JmapVacationResponse},
},
};
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct JmapVacationResponseUpdate {
#[serde(skip_serializing_if = "Option::is_none")]
pub is_enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub from_date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub to_date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub subject: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text_body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub html_body: Option<String>,
}
#[derive(Debug, Error)]
pub enum JmapVacationResponseSetError {
#[error("JMAP VacationResponse/set failed: missing response in method_responses")]
MissingResponse,
#[error("JMAP VacationResponse/set failed: {0}")]
Send(#[from] JmapSendError),
#[error("JMAP VacationResponse/set failed: serialize args: {0}")]
SerializeArgs(#[source] serde_json::Error),
#[error("JMAP VacationResponse/set failed: parse response: {0}")]
ParseResponse(#[source] serde_json::Error),
#[error("JMAP VacationResponse/set failed: {0}")]
Method(#[from] JmapMethodError),
}
#[derive(Clone, Debug)]
pub struct JmapVacationResponseSetOutput {
pub new_state: String,
pub updated: Option<JmapVacationResponse>,
pub keep_alive: bool,
}
pub struct JmapVacationResponseSet {
state: State,
}
impl JmapVacationResponseSet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
patch: JmapVacationResponseUpdate,
) -> Result<Self, JmapVacationResponseSetError> {
let account_id = session
.primary_accounts
.get(JMAP_VACATION_RESPONSE_CAPABILITY)
.or_else(|| session.primary_accounts.get(JMAP_MAIL_CAPABILITY))
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let args = serde_json::to_value(VacationResponseSetArgs {
account_id,
update: BTreeMap::from([("singleton", patch)]),
})
.map_err(JmapVacationResponseSetError::SerializeArgs)?;
let mut using = vec![JMAP_CORE_CAPABILITY.into(), JMAP_MAIL_CAPABILITY.into()];
if session
.capabilities
.contains_key(JMAP_VACATION_RESPONSE_CAPABILITY)
{
using.push(JMAP_VACATION_RESPONSE_CAPABILITY.into());
}
let mut batch = JmapBatch::new();
batch.add("VacationResponse/set", args);
let request = batch.into_request(using);
Ok(Self {
state: State::Send(JmapSend::new(http_auth, api_url, request)?),
})
}
}
impl JmapCoroutine for JmapVacationResponseSet {
type Yield = JmapYield;
type Return = Result<JmapVacationResponseSetOutput, JmapVacationResponseSetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Send(send) => {
let JmapSendOutput {
response,
keep_alive,
} = jmap_try!(send, arg);
let Some((name, args, _)) = response.method_responses.into_iter().next() else {
return JmapCoroutineState::Complete(Err(
JmapVacationResponseSetError::MissingResponse,
));
};
if name == "error" {
let err = serde_json::from_value::<JmapMethodError>(args)
.unwrap_or(JmapMethodError::Unknown);
return JmapCoroutineState::Complete(Err(err.into()));
}
match serde_json::from_value::<VacationResponseSetResponse>(args) {
Ok(r) => JmapCoroutineState::Complete(Ok(JmapVacationResponseSetOutput {
new_state: r.new_state,
updated: r.updated.unwrap_or_default().into_values().flatten().next(),
keep_alive,
})),
Err(err) => JmapCoroutineState::Complete(Err(
JmapVacationResponseSetError::ParseResponse(err),
)),
}
}
}
}
}
enum State {
Send(JmapSend),
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct VacationResponseSetArgs {
account_id: String,
update: BTreeMap<&'static str, JmapVacationResponseUpdate>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct VacationResponseSetResponse {
new_state: String,
#[serde(default)]
updated: Option<BTreeMap<String, Option<JmapVacationResponse>>>,
}