use std::path::Path;
use std::sync::Arc;
use crate::client::{HaiClient, HaiClientOptions};
use crate::error::Result;
#[cfg(feature = "jacs-crate")]
use crate::jacs_local::LocalJacsProvider;
use crate::types::{
Contact, EmailMessage, EmailStatus, ListMessagesOptions, SearchOptions, SendEmailOptions,
SendEmailResult,
};
use crate::validation;
pub struct Agent {
pub email: EmailNamespace,
}
#[cfg(feature = "jacs-crate")]
impl Agent {
pub async fn from_config(config_path: Option<&Path>) -> Result<Self> {
Self::from_config_with_options(config_path, HaiClientOptions::default()).await
}
pub async fn from_config_with_options(
config_path: Option<&Path>,
options: HaiClientOptions,
) -> Result<Self> {
let provider = LocalJacsProvider::from_config_path(config_path, None)?;
let client = HaiClient::new(provider, options)?;
let client = Arc::new(tokio::sync::RwLock::new(client));
Ok(Self {
email: EmailNamespace {
client: Arc::clone(&client),
},
})
}
pub fn client(&self) -> &Arc<tokio::sync::RwLock<HaiClient<LocalJacsProvider>>> {
&self.email.client
}
}
pub struct EmailNamespace {
#[cfg(feature = "jacs-crate")]
client: Arc<tokio::sync::RwLock<HaiClient<LocalJacsProvider>>>,
}
#[cfg(feature = "jacs-crate")]
impl EmailNamespace {
pub async fn send(&self, options: SendEmailOptions) -> Result<SendEmailResult> {
validation::validate_send_email(&options)?;
let client = self.client.read().await;
client.send_signed_email(&options).await
}
pub async fn inbox(&self, options: ListMessagesOptions) -> Result<Vec<EmailMessage>> {
let mut opts = options;
opts.direction = Some("inbound".to_string());
let client = self.client.read().await;
client.list_messages(&opts).await
}
pub async fn outbox(&self, options: ListMessagesOptions) -> Result<Vec<EmailMessage>> {
let mut opts = options;
opts.direction = Some("outbound".to_string());
let client = self.client.read().await;
client.list_messages(&opts).await
}
pub async fn get(&self, message_id: &str) -> Result<EmailMessage> {
let client = self.client.read().await;
client.get_message(message_id).await
}
pub async fn search(&self, options: SearchOptions) -> Result<Vec<EmailMessage>> {
let client = self.client.read().await;
client.search_messages(&options).await
}
pub async fn status(&self) -> Result<EmailStatus> {
let client = self.client.read().await;
client.get_email_status().await
}
pub async fn unread_count(&self) -> Result<u64> {
let client = self.client.read().await;
client.get_unread_count().await
}
pub async fn delete(&self, message_id: &str) -> Result<()> {
let client = self.client.read().await;
client.delete_message(message_id).await
}
pub async fn mark_read(&self, message_id: &str) -> Result<()> {
let client = self.client.read().await;
client.mark_read(message_id).await
}
pub async fn mark_unread(&self, message_id: &str) -> Result<()> {
let client = self.client.read().await;
client.mark_unread(message_id).await
}
pub async fn reply(
&self,
message_id: &str,
body: &str,
subject_override: Option<&str>,
) -> Result<SendEmailResult> {
let client = self.client.read().await;
client.reply(message_id, body, subject_override).await
}
pub async fn forward(
&self,
message_id: &str,
to: &str,
comment: Option<&str>,
) -> Result<SendEmailResult> {
let client = self.client.read().await;
client.forward(message_id, to, comment).await
}
pub async fn archive(&self, message_id: &str) -> Result<()> {
let client = self.client.read().await;
client.archive(message_id).await
}
pub async fn unarchive(&self, message_id: &str) -> Result<()> {
let client = self.client.read().await;
client.unarchive(message_id).await
}
pub async fn contacts(&self) -> Result<Vec<Contact>> {
let client = self.client.read().await;
client.contacts().await
}
}