Преглед на файлове

/get_threads_by_message endpoint added

Yurii Sokolovskyi преди 1 месец
родител
ревизия
32bb954312
променени са 2 файла, в които са добавени 31 реда и са изтрити 9 реда
  1. 15 0
      src/indexes.rs
  2. 16 9
      src/server.rs

+ 15 - 0
src/indexes.rs

@@ -402,6 +402,7 @@ impl Indexes {
     }
 
     pub fn delete_from_messages(message_id: String) -> anyhow::Result<SerializableMessage> {
+        // TODO change to get_messages();
         let path = Config::global().out_dir.clone().join("messages.json");
         let file = File::open(&path)?;
         let reader = BufReader::new(file);
@@ -419,6 +420,7 @@ impl Indexes {
     }
 
     pub fn delete_from_threads(message: SerializableMessage) -> anyhow::Result<()> {
+        // TODO change to get_threads();
         let path = Config::global().out_dir.clone().join("threads.json");
         let file = File::open(&path)?;
         let reader = BufReader::new(file);
@@ -458,6 +460,19 @@ impl Indexes {
 
         folders
     }
+    
+    pub fn get_threads_with_message(id: String) -> anyhow::Result<Vec<u32>>{
+        let threads = Indexes::get_threads()?;
+        let mut result: Vec<u32> = vec![];
+
+        for thread in threads {
+            if thread.messages.contains(&id) { 
+                result.push(thread.id.clone())
+            }
+        }
+        
+        Ok(result)
+    }
 }
 
 

+ 16 - 9
src/server.rs

@@ -30,15 +30,6 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
     Arc::new(Mutex::new(None))
 });
 
-/// folders -> GET, returns list of all mailboxes
-/// sorted_threads_by_date -> GET, returns hashmap with date: list of thread ids sorted by date
-/// get_thread -> GET, returns all information about one thread
-/// get_thread_messages -> GET, returns a list of SerializableMessage in the thread
-/// email -> GET, returns html for the message
-/// search -> GET, returns a list of message ids where search found matches
-/// create_folder -> POST, creates a new mailbox
-/// rename_folder -> POST, renames a mailbox
-/// delete_folder -> POST, deletes a mailbox
 pub async fn run_api() {
     let cors = warp::cors()
         .allow_any_origin()
@@ -76,6 +67,10 @@ pub async fn run_api() {
             .and(warp::get())
             .and(warp::query::<SearchQuery>())
             .and_then(search_handle))
+        .or(warp::path("get_threads_by_message")
+            .and(warp::get())
+            .and(warp::query::<GetThreadsByMessageQuery>())
+            .and_then(get_threads_by_message_handle))
         .or(warp::path("create_folder")
             .and(warp::post())
             .and(warp::body::json())
@@ -566,6 +561,18 @@ async fn search_handle(query: SearchQuery) -> Result<impl warp::Reply, warp::Rej
     }
 }
 
+#[derive(Deserialize)]
+struct GetThreadsByMessageQuery {
+    id: String
+}
+
+async fn get_threads_by_message_handle(query: GetThreadsByMessageQuery) -> Result<impl warp::Reply, warp::Rejection> {
+    match Indexes::get_threads_with_message(query.id){
+        Ok(result) => Ok(warp::reply::json(&result)),
+        Err(_) => Ok(warp::reply::json(&Vec::<String>::new()))
+    }
+}
+
 #[derive(Deserialize, Serialize)]
 struct IsLoggedInResponse{
     is_logged_in: bool