use jmap_types::{Id, State};
use super::{ChangesResponse, GetResponse};
impl super::SessionClient {
pub async fn thread_get(
&self,
ids: Option<&[Id]>,
properties: Option<&[&str]>,
) -> Result<GetResponse<jmap_mail_types::Thread>, 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::to_value(props).expect("&[&str] Serialize is infallible");
}
let req = super::build_request("Thread/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 thread_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(
"thread_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("Thread/changes", 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 thread_get_response_deserializes() {
let json = json!({
"accountId": "acc1",
"state": "s5",
"list": [
{
"id": "t1",
"emailIds": ["e1", "e2", "e3"]
}
],
"notFound": []
});
use super::super::GetResponse;
let resp: GetResponse<jmap_mail_types::Thread> =
serde_json::from_value(json).expect("must deserialize Thread GetResponse");
assert_eq!(resp.list.len(), 1);
assert_eq!(resp.list[0].id.as_ref(), "t1");
assert_eq!(resp.list[0].email_ids.len(), 3);
}
}