use alloc::{string::String, vec, vec::Vec};
use secrecy::SecretString;
use thiserror::Error;
use crate::{
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, get::*, session::JmapSession},
rfc8621::{JMAP_MAIL_CAPABILITY, thread::JmapThread},
};
#[derive(Debug, Error)]
pub enum JmapThreadGetError {
#[error("JMAP Thread/get failed: {0}")]
Get(#[from] JmapGetError),
}
#[derive(Clone, Debug)]
pub struct JmapThreadGetOutput {
pub threads: Vec<JmapThread>,
pub not_found: Vec<String>,
pub new_state: String,
pub keep_alive: bool,
}
pub struct JmapThreadGet {
state: State,
}
impl JmapThreadGet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
ids: Vec<String>,
) -> Result<Self, JmapThreadGetError> {
let account_id = session
.primary_accounts
.get(JMAP_MAIL_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
Ok(Self {
state: State::Get(JmapGet::new(
account_id,
http_auth,
api_url,
"Thread/get",
vec![JMAP_CORE_CAPABILITY.into(), JMAP_MAIL_CAPABILITY.into()],
JmapGetOptions {
ids: Some(ids),
properties: None,
},
)?),
})
}
}
impl JmapCoroutine for JmapThreadGet {
type Yield = JmapYield;
type Return = Result<JmapThreadGetOutput, JmapThreadGetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Get(get) => {
let JmapGetOutput {
list,
not_found,
state,
keep_alive,
} = jmap_try!(get, arg);
JmapCoroutineState::Complete(Ok(JmapThreadGetOutput {
threads: list,
not_found,
new_state: state,
keep_alive,
}))
}
}
}
}
enum State {
Get(JmapGet<JmapThread>),
}