|
@@ -1,3 +1,4 @@
|
|
|
|
+use std::fs;
|
|
use std::fs::{File, OpenOptions};
|
|
use std::fs::{File, OpenOptions};
|
|
use serde::{Deserialize, Serialize};
|
|
use serde::{Deserialize, Serialize};
|
|
use crate::config::Config;
|
|
use crate::config::Config;
|
|
@@ -66,19 +67,63 @@ pub async fn check_server(){
|
|
|
|
|
|
pub fn ingest_new_message(message: StrMessage) -> anyhow::Result<()>{
|
|
pub fn ingest_new_message(message: StrMessage) -> anyhow::Result<()>{
|
|
// ingest text from the message to the sonic server
|
|
// ingest text from the message to the sonic server
|
|
- // TODO add attachments
|
|
|
|
match IngestSonic::new(){
|
|
match IngestSonic::new(){
|
|
Ok(mut ingest_channel) => {
|
|
Ok(mut ingest_channel) => {
|
|
let data = message.to_ingest();
|
|
let data = message.to_ingest();
|
|
- match ingest_channel.ingest_document("emails", &*message.list.clone(), &*message.id.clone(), &*data) {
|
|
|
|
|
|
+ // TODO move "emails" to config
|
|
|
|
+ match ingest_channel.ingest_document("emails", &*message.list.clone().to_lowercase(), &*message.id.clone(), &*data) {
|
|
Ok(_) => {}
|
|
Ok(_) => {}
|
|
- Err(_) => write_to_queue(message)?
|
|
|
|
|
|
+ Err(_) => write_to_queue(message.clone())?
|
|
|
|
+ };
|
|
|
|
+ let _ = ingest_channel.stop_ingest();
|
|
|
|
+
|
|
|
|
+ // checking for saved attachments
|
|
|
|
+ let attachments_path = Config::global().out_dir.clone()
|
|
|
|
+ .join(message.list.clone())
|
|
|
|
+ .join(".attachments")
|
|
|
|
+ .join(message.id.clone());
|
|
|
|
+
|
|
|
|
+ match fs::read_dir(attachments_path) {
|
|
|
|
+ Ok(entries) => {
|
|
|
|
+ for entry in entries {
|
|
|
|
+ if let Ok(entry) = entry {
|
|
|
|
+ ingest_new_attachment(message.list.clone(), message.id.clone(), entry.file_name().to_str().unwrap().to_string())?;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Err(_) => {},
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Err(_) => write_to_queue(message.clone())?
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ Ok(())
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Serialize, Deserialize)]
|
|
|
|
+pub struct AttachmentSearchInfo{
|
|
|
|
+ pub name: String,
|
|
|
|
+ pub email_id: String,
|
|
|
|
+ pub mailbox: String
|
|
|
|
+}
|
|
|
|
+pub fn ingest_new_attachment(mailbox: String, email_id: String, name: String) -> anyhow::Result<()> {
|
|
|
|
+ println!("{:?}", name.clone());
|
|
|
|
+ match IngestSonic::new(){
|
|
|
|
+ Ok(mut ingest_channel) => {
|
|
|
|
+ let obj = AttachmentSearchInfo{
|
|
|
|
+ name: name.clone(),
|
|
|
|
+ email_id,
|
|
|
|
+ mailbox: mailbox.clone()
|
|
|
|
+ };
|
|
|
|
+ match ingest_channel.ingest_document("attachments", &*mailbox.clone().to_lowercase(), &*serde_json::to_string(&obj)?, &*name.clone()) {
|
|
|
|
+ Ok(_) => {}
|
|
|
|
+ Err(_) => {}
|
|
};
|
|
};
|
|
let _ = ingest_channel.stop_ingest();
|
|
let _ = ingest_channel.stop_ingest();
|
|
}
|
|
}
|
|
- Err(_) => write_to_queue(message)?
|
|
|
|
|
|
+ Err(_) => {}
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
Ok(())
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
|