use std::collections::HashMap;
use jmap_types::{Id, PatchObject, State};
use super::{ChangesResponse, GetResponse, SetResponse};
impl super::SessionClient {
pub async fn identity_get(
&self,
ids: Option<&[Id]>,
properties: Option<&[&str]>,
) -> Result<GetResponse<jmap_mail_types::Identity>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({ "accountId": account_id });
if let Some(id_slice) = ids {
args["ids"] = serde_json::to_value(id_slice).expect("Id slice Serialize is infallible");
}
if let Some(props) = properties {
args["properties"] = serde_json::Value::Array(
props.iter().copied().map(serde_json::Value::from).collect(),
);
}
let req = super::build_request("Identity/get", args, super::USING_MAIL);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn identity_changes(
&self,
since_state: &State,
max_changes: Option<u64>,
) -> Result<ChangesResponse, jmap_base_client::ClientError> {
if since_state.as_ref().is_empty() {
return Err(jmap_base_client::ClientError::InvalidArgument(
"identity_changes: since_state may not be empty".into(),
));
}
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
"sinceState": since_state,
});
if let Some(mc) = max_changes {
args["maxChanges"] = mc.into();
}
let req = super::build_request("Identity/changes", args, super::USING_MAIL);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
pub async fn identity_set(
&self,
create: Option<serde_json::Value>,
update: Option<HashMap<Id, PatchObject>>,
destroy: Option<Vec<Id>>,
) -> Result<SetResponse<jmap_mail_types::Identity>, jmap_base_client::ClientError> {
let (api_url, account_id) = self.session_parts()?;
let mut args = serde_json::json!({
"accountId": account_id,
});
if let Some(c) = create {
args["create"] = c;
}
if let Some(u) = update {
args["update"] = serde_json::to_value(&u).map_err(|e| {
jmap_base_client::ClientError::InvalidArgument(format!(
"identity_set: serializing update map failed: {e}"
))
})?;
}
if let Some(d) = destroy {
args["destroy"] = serde_json::to_value(&d).expect("Id Vec Serialize is infallible");
}
let req = super::build_request("Identity/set", args, super::USING_MAIL);
let resp = self.call_internal(api_url, &req).await?;
jmap_base_client::extract_response(&resp, super::CALL_ID)
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
#[test]
fn identity_get_response_deserializes() {
let json = json!({
"accountId": "acc1",
"state": "s1",
"list": [
{
"id": "ident1",
"name": "Jane Doe",
"email": "jane@example.com",
"textSignature": "-- \nJane",
"htmlSignature": "<p>Jane</p>",
"mayDelete": true
}
],
"notFound": []
});
use super::super::GetResponse;
let resp: GetResponse<jmap_mail_types::Identity> =
serde_json::from_value(json).expect("must deserialize Identity GetResponse");
assert_eq!(resp.list.len(), 1);
assert_eq!(resp.list[0].name, "Jane Doe");
assert_eq!(resp.list[0].email, "jane@example.com");
assert!(resp.list[0].may_delete);
}
}