WIP add search

This commit is contained in:
2024-07-21 09:05:03 -07:00
parent 0bf865fdef
commit dd09bc3168
6 changed files with 170 additions and 56 deletions

View File

@@ -4,11 +4,15 @@ use std::{
time::Instant,
};
use async_graphql::connection::{self, Connection, Edge};
use log::info;
use notmuch::Notmuch;
use shared::Message;
use crate::{error, graphql::Tag};
use crate::{
error,
graphql::{Tag, ThreadSummary},
};
// TODO(wathiede): decide good error type
pub fn threadset_to_messages(
@@ -20,6 +24,67 @@ pub fn threadset_to_messages(
Ok(Vec::new())
}
pub async fn search(
nm: &Notmuch,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
query: String,
) -> Result<Connection<usize, ThreadSummary>, async_graphql::Error> {
connection::query(
after,
before,
first,
last,
|after, before, first, last| async move {
let total = nm.count(&query)?;
let (first, last) = if let (None, None) = (first, last) {
info!("neither first nor last set, defaulting first to 20");
(Some(20), None)
} else {
(first, last)
};
let mut start = after.map(|after| after + 1).unwrap_or(0);
let mut end = before.unwrap_or(total);
if let Some(first) = first {
end = (start + first).min(end);
}
if let Some(last) = last {
start = if last > end - start { end } else { end - last };
}
let count = end - start;
let slice: Vec<ThreadSummary> = nm
.search(&query, start, count)?
.0
.into_iter()
.map(|ts| ThreadSummary {
thread: ts.thread,
timestamp: ts.timestamp,
date_relative: ts.date_relative,
matched: ts.matched,
total: ts.total,
authors: ts.authors,
subject: ts.subject,
tags: ts.tags,
})
.collect();
let mut connection = Connection::new(start > 0, end < total);
connection.edges.extend(
slice
.into_iter()
.enumerate()
.map(|(idx, item)| Edge::new(start + idx, item)),
);
Ok::<_, async_graphql::Error>(connection)
},
)
.await
}
pub fn tags(nm: &Notmuch, needs_unread: bool) -> Result<Vec<Tag>, error::ServerError> {
let now = Instant::now();
let unread_msg_cnt: HashMap<String, usize> = if needs_unread {