Sfoglia il codice sorgente

sorted_threads_by_date api endpoint fix with limit and offset

Yurii Sokolovskyi 2 mesi fa
parent
commit
bbd293f949
1 ha cambiato i file con 32 aggiunte e 5 eliminazioni
  1. 32 5
      src/server.rs

+ 32 - 5
src/server.rs

@@ -231,11 +231,38 @@ async fn get_threads(folder: String, limit: usize, offset: usize, file_name: Str
         Err(_) => return serde_json::to_string(&Vec::<(String, Vec<u32>)>::new()).unwrap(),
     };
     
-    let result: Vec<(String, Vec<u32>)> = messages
-        .into_iter()
-        .skip(offset)
-        .take(limit)
-        .collect();
+    // filtering elements with limit and offset
+    let mut result = Vec::new();
+    let mut current_offset = offset;
+    let mut remaining_limit = limit;
+
+    for (msg, data) in messages.into_iter() {
+        // Skip the inner data until we reach the required offset
+        if current_offset >= data.len() {
+            current_offset -= data.len();
+            continue;
+        }
+
+        // Take the required elements from the current vector
+        let paginated_data: Vec<u32> = data.into_iter()
+            .skip(current_offset)
+            .take(remaining_limit)
+            .collect();
+
+        // Push the result for the current message
+        if !paginated_data.is_empty() {
+            result.push((msg, paginated_data));
+        }
+
+        // Update the remaining limit and reset the offset for the next iterations
+        remaining_limit -= result.last().unwrap().1.len();
+        current_offset = 0;
+
+        // If we've collected enough elements, break out of the loop
+        if remaining_limit == 0 {
+            break;
+        }
+    }
 
     serde_json::to_string(&result).unwrap()
 }