WIP reading news from app

This commit is contained in:
2024-07-21 07:53:02 -07:00
parent 5c0c45b99f
commit 0bf865fdef
11 changed files with 734 additions and 62 deletions

View File

@@ -1,6 +1,14 @@
use std::{
collections::HashMap,
hash::{DefaultHasher, Hash, Hasher},
time::Instant,
};
use log::info;
use notmuch::Notmuch;
use shared::Message;
use crate::error;
use crate::{error, graphql::Tag};
// TODO(wathiede): decide good error type
pub fn threadset_to_messages(
@@ -11,3 +19,44 @@ pub fn threadset_to_messages(
}
Ok(Vec::new())
}
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 {
// 10000 is an arbitrary number, if there's more than 10k unread messages, we'll
// get an inaccurate count.
nm.search("is:unread", 0, 10000)?
.0
.iter()
.fold(HashMap::new(), |mut m, ts| {
ts.tags.iter().for_each(|t| {
m.entry(t.clone()).and_modify(|c| *c += 1).or_insert(1);
});
m
})
} else {
HashMap::new()
};
let tags = nm
.tags()?
.into_iter()
.map(|tag| {
let mut hasher = DefaultHasher::new();
tag.hash(&mut hasher);
let hex = format!("#{:06x}", hasher.finish() % (1 << 24));
let unread = if needs_unread {
*unread_msg_cnt.get(&tag).unwrap_or(&0)
} else {
0
};
Tag {
name: tag,
fg_color: "white".to_string(),
bg_color: hex,
unread,
}
})
.collect();
info!("Fetching tags took {} seconds", now.elapsed().as_secs_f32());
Ok(tags)
}