server: add unread field to tag query.

Optionally fill out unread, as it's expensive.
This commit is contained in:
2023-11-21 13:15:57 -08:00
parent 64912be4eb
commit ff6675b08f
3 changed files with 15 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ use async_graphql::{
};
use log::info;
use notmuch::Notmuch;
use rayon::prelude::*;
pub struct QueryRoot;
@@ -36,6 +37,7 @@ struct Tag {
name: String,
fg_color: String,
bg_color: String,
unread: usize,
}
#[Object]
@@ -129,15 +131,21 @@ impl QueryRoot {
let nm = ctx.data_unchecked::<Notmuch>();
Ok(nm
.tags()?
.into_iter()
.into_par_iter()
.map(|tag| {
let mut hasher = DefaultHasher::new();
tag.hash(&mut hasher);
let hex = format!("#{:06x}", hasher.finish() % (1 << 24));
let unread = if ctx.look_ahead().field("unread").exists() {
nm.count(&format!("tag:{tag} is:unread")).unwrap_or(0)
} else {
0
};
Tag {
name: tag,
fg_color: "white".to_string(),
bg_color: hex,
unread,
}
})
.collect())