use super::*;
use crate::backends::BackendOp;
use crate::error::Result;
use std::cell::Cell;
use std::sync::{Arc, RwLock};
#[derive(Debug, Clone)]
pub struct JmapOp {
hash: EnvelopeHash,
connection: Arc<JmapConnection>,
store: Arc<RwLock<Store>>,
bytes: Option<String>,
flags: Cell<Option<Flag>>,
headers: Option<String>,
body: Option<String>,
}
impl JmapOp {
pub fn new(
hash: EnvelopeHash,
connection: Arc<JmapConnection>,
store: Arc<RwLock<Store>>,
) -> Self {
JmapOp {
hash,
connection,
store,
bytes: None,
headers: None,
body: None,
flags: Cell::new(None),
}
}
}
impl BackendOp for JmapOp {
fn description(&self) -> String {
self.store
.try_read()
.and_then(|store_lck| Ok(store_lck.id_store[&self.hash].clone()))
.unwrap_or(String::new())
}
fn as_bytes(&mut self) -> Result<&[u8]> {
if self.bytes.is_none() {
let mut store_lck = self.store.write().unwrap();
if !(store_lck.byte_cache.contains_key(&self.hash)
&& store_lck.byte_cache[&self.hash].bytes.is_some())
{
let blob_id = &store_lck.blob_id_store[&self.hash];
let res = self
.connection
.client
.lock()
.unwrap()
.get(&download_request_format(
&self.connection.session,
self.connection.mail_account_id(),
blob_id,
None,
))
.basic_auth(
&self.connection.server_conf.server_username,
Some(&self.connection.server_conf.server_password),
)
.send();
let res_text = res?.text()?;
store_lck.byte_cache.entry(self.hash).or_default().bytes = Some(res_text);
}
self.bytes = store_lck.byte_cache[&self.hash].bytes.clone();
}
Ok(&self.bytes.as_ref().unwrap().as_bytes())
}
fn fetch_flags(&self) -> Flag {
Flag::default()
}
fn set_flag(&mut self, _envelope: &mut Envelope, _f: Flag, _value: bool) -> Result<()> {
Ok(())
}
fn set_tag(&mut self, _envelope: &mut Envelope, _tag: String, _value: bool) -> Result<()> {
Ok(())
}
}