use crate::{
email::{address::StrBuild, parser::BytesExt, *},
SortField, SortOrder, UnixTimestamp,
};
mod iterators;
use std::{
cmp::Ordering,
collections::{HashMap, HashSet, VecDeque},
iter::FromIterator,
ops::Index,
string::ToString,
sync::{Arc, RwLock},
};
pub use iterators::*;
use smallvec::SmallVec;
use uuid::Uuid;
use crate::text::grapheme_clusters::*;
type Envelopes = Arc<RwLock<HashMap<EnvelopeHash, Envelope>>>;
macro_rules! uuid_hash_type {
($n:ident) => {
#[derive(PartialEq, Hash, Eq, Copy, Clone, Serialize, Deserialize, Default)]
pub struct $n(Uuid);
impl std::fmt::Debug for $n {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.to_string())
}
}
impl std::fmt::Display for $n {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.to_string())
}
}
impl<B: AsRef<[u8]>> From<B> for $n {
fn from(val: B) -> Self {
let val = val.as_ref();
$n(Uuid::new_v5(&Uuid::NAMESPACE_URL, val))
}
}
impl $n {
pub fn new() -> Self {
$n(Uuid::new_v4())
}
pub const fn null() -> Self {
$n(Uuid::nil())
}
}
};
}
uuid_hash_type!(ThreadNodeHash);
uuid_hash_type!(ThreadHash);
macro_rules! remove_from_parent {
($buf:expr, $idx:expr) => {{
let mut parent: Option<ThreadNodeHash> = None;
let entry_parent = $buf.entry($idx).or_default().parent;
if let Some(p) = entry_parent {
parent = Some(p);
if let Some(pos) = $buf[&p].children.iter().position(|c| *c == $idx) {
$buf.entry(p).and_modify(|e| {
e.children.remove(pos);
});
}
}
$buf.entry($idx).and_modify(|e| e.parent = None);
parent
}};
}
macro_rules! make {
(($p:expr)parent of($c:expr), $threads:expr) => {{
let old_group_hash = $threads.find_group($threads.thread_nodes[&$c].group);
let parent_group_hash = $threads.find_group($threads.thread_nodes[&$p].group);
if old_group_hash != parent_group_hash {
if let Some(old_env_hashes) = $threads.thread_to_envelope.get(&old_group_hash).cloned()
{
for &env_hash in &old_env_hashes {
*$threads.envelope_to_thread.entry(env_hash).or_default() = parent_group_hash;
}
$threads
.thread_to_envelope
.entry(parent_group_hash)
.or_default()
.extend(old_env_hashes.into_iter());
}
let prev_parent = remove_from_parent!(&mut $threads.thread_nodes, $c);
if !($threads.thread_nodes[&$p]).children.contains(&$c) {
$threads
.thread_nodes
.entry($p)
.and_modify(|e| e.children.push($c));
}
$threads.thread_nodes.entry($c).and_modify(|e| {
e.parent = Some($p);
});
let old_group = std::mem::replace(
$threads.groups.entry(old_group_hash).or_default(),
ThreadGroup::Node {
parent: Arc::new(RwLock::new(parent_group_hash)),
},
);
$threads.thread_nodes.entry($c).and_modify(|e| {
e.group = parent_group_hash;
});
$threads.thread_nodes.entry($p).and_modify(|e| {
e.group = parent_group_hash;
});
{
let parent_group = $threads.thread_ref_mut(parent_group_hash);
match (parent_group, old_group) {
(
Thread {
ref mut date,
ref mut len,
ref mut unseen,
ref mut snoozed,
ref mut attachments,
..
},
ThreadGroup::Root(Thread {
date: old_date,
len: old_len,
unseen: old_unseen,
snoozed: old_snoozed,
attachments: old_attachments,
..
}),
) => {
*date = std::cmp::max(old_date, *date);
*len += old_len;
*unseen += old_unseen;
*attachments += old_attachments;
*snoozed |= old_snoozed;
}
_ => unreachable!(),
}
}
prev_parent
} else {
None
}
}};
}
pub trait SubjectPrefix {
const USUAL_PREFIXES: &'static [&'static str] = &[
"Re:",
"RE:",
"Fwd:",
"Fw:",
"回复:",
"回覆:",
"Antw:",
"Doorst:",
"VL:",
"REF:",
"TR:",
"AW:",
"WG:",
"ΑΠ:",
"Απ:",
"απ:",
"ΠΡΘ:",
"Πρθ:",
"πρθ:",
"ΣΧΕΤ:",
"Σχετ:",
"σχετ:",
"ΠΡΘ:",
"Πρθ:",
"πρθ:",
"Vá:",
"Továbbítás:",
"R:",
"I:",
"RIF:",
"SV:",
"Sv:",
"FS:",
"Fs:",
"BLS:",
"TRS:",
"VS:",
"Vs:",
"VB:",
"Vb:",
"RV:",
"Rv:",
"RES:",
"Res:",
"ENC:",
"Odp:",
"PD:",
"YNT:",
"İLT:",
"ATB:",
"YML:",
];
fn is_a_reply(&self) -> bool;
fn strip_prefixes(&mut self) -> &mut Self;
fn strip_prefixes_from_list(&mut self, list: &[&str], times: Option<u8>) -> &mut Self;
}
impl SubjectPrefix for &[u8] {
fn is_a_reply(&self) -> bool {
let self_ = self.trim();
self_.starts_with(b"RE: ")
|| self_.starts_with(b"Re: ")
|| self_.starts_with(b"RES: ")
|| self_.starts_with(b"Res: ")
|| self_.starts_with(b"FW: ")
|| self_.starts_with(b"Fw: ")
}
fn strip_prefixes(&mut self) -> &mut Self {
let result = {
let mut slice = self.trim();
loop {
if slice.starts_with(b"RE: ")
|| slice.starts_with(b"Re: ")
|| slice.starts_with(b"FW: ")
|| slice.starts_with(b"Fw: ")
{
slice = &slice[b"RE: ".len()..];
continue;
}
if slice.starts_with(b"FWD: ")
|| slice.starts_with(b"Fwd: ")
|| slice.starts_with(b"fwd: ")
{
slice = &slice[b"FWD: ".len()..];
continue;
}
if slice.starts_with(b" ") || slice.starts_with(b"\t") || slice.starts_with(b"\r") {
slice = &slice[b" ".len()..];
continue;
}
if slice.starts_with(b"[")
&& !(slice.starts_with(b"[PATCH") || slice.starts_with(b"[RFC"))
{
if let Some(pos) = slice.find(b"]") {
slice = &slice[pos + 1..];
continue;
}
}
break;
}
slice
};
*self = result;
self
}
fn strip_prefixes_from_list(&mut self, list: &[&str], mut times: Option<u8>) -> &mut Self {
let result = {
let mut slice = self.trim();
'outer: loop {
let len = slice.len();
for prefix in list.iter() {
if slice
.get(0..prefix.as_bytes().len())
.map(|p| p.eq_ignore_ascii_case(prefix.as_bytes()))
.unwrap_or(false)
{
slice = &slice[prefix.len()..];
slice = slice.trim();
times = times.map(|u| u.saturating_sub(1));
if times == Some(0) {
break 'outer;
}
}
}
if slice.len() == len || times == Some(0) {
break;
}
}
slice
};
*self = result;
self
}
}
impl SubjectPrefix for &str {
fn is_a_reply(&self) -> bool {
self.as_bytes().is_a_reply()
}
fn strip_prefixes(&mut self) -> &mut Self {
let result = {
let mut slice = self.trim();
loop {
if slice.starts_with("RE: ")
|| slice.starts_with("Re: ")
|| slice.starts_with("FW: ")
|| slice.starts_with("Fw: ")
{
slice = &slice["RE: ".len()..];
continue;
}
if slice.starts_with("FWD: ")
|| slice.starts_with("Fwd: ")
|| slice.starts_with("fwd: ")
{
slice = &slice["FWD: ".len()..];
continue;
}
if slice.starts_with(' ') || slice.starts_with('\t') || slice.starts_with('\r') {
slice = &slice[1..];
continue;
}
if slice.starts_with('[')
&& !(slice.starts_with("[PATCH") || slice.starts_with("[RFC"))
{
if let Some(pos) = slice.find(']') {
slice = &slice[pos + 1..];
continue;
}
}
break;
}
slice
};
*self = result;
self
}
fn strip_prefixes_from_list(&mut self, list: &[&str], mut times: Option<u8>) -> &mut Self {
let result = {
let mut slice = self.trim();
'outer: loop {
let len = slice.len();
for prefix in list.iter() {
if slice
.get(0..prefix.as_bytes().len())
.map(|p| p.eq_ignore_ascii_case(prefix))
.unwrap_or(false)
{
slice = &slice[prefix.len()..];
slice = slice.trim();
times = times.map(|u| u.saturating_sub(1));
if times == Some(0) {
break 'outer;
}
}
}
if slice.len() == len || times == Some(0) {
break;
}
}
slice
};
*self = result;
self
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Thread {
pub root: ThreadNodeHash,
pub date: UnixTimestamp,
pub len: usize,
pub unseen: usize,
pub attachments: usize,
pub snoozed: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ThreadGroup {
Root(Thread),
Node { parent: Arc<RwLock<ThreadHash>> },
}
impl Default for ThreadGroup {
fn default() -> Self {
Self::Root(Thread::default())
}
}
impl ThreadGroup {
pub fn root(&self) -> Option<&Thread> {
if let Self::Root(ref root) = self {
Some(root)
} else {
None
}
}
}
macro_rules! property {
($name:ident: $t:ty) => {
pub fn $name(&self) -> $t {
(self.$name).into()
}
};
}
impl Thread {
property!(root: ThreadNodeHash);
property!(len: usize);
property!(unseen: usize);
property!(snoozed: bool);
property!(date: UnixTimestamp);
pub fn has_attachments(&self) -> bool {
self.attachments > 0
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn set_snoozed(&mut self, val: bool) {
self.snoozed = val;
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ThreadNode {
pub message: Option<EnvelopeHash>,
pub other_mailbox: bool,
pub parent: Option<ThreadNodeHash>,
pub children: Vec<ThreadNodeHash>,
pub date: UnixTimestamp,
pub show_subject: bool,
pub group: ThreadHash,
pub unseen: bool,
}
impl Default for ThreadNode {
fn default() -> Self {
Self {
message: None,
parent: None,
other_mailbox: false,
children: Vec::new(),
date: UnixTimestamp::default(),
show_subject: true,
group: ThreadHash::new(),
unseen: false,
}
}
}
impl ThreadNode {
fn new() -> Self {
Self::default()
}
pub fn show_subject(&self) -> bool {
self.show_subject
}
pub fn unseen(&self) -> bool {
self.unseen
}
pub fn set_unseen(&mut self, new_val: bool) {
self.unseen = new_val;
}
pub fn date(&self) -> UnixTimestamp {
self.date
}
pub fn is_empty(&self) -> bool {
self.parent.is_none() && self.message.is_none() && self.children.is_empty()
}
pub fn message(&self) -> Option<EnvelopeHash> {
self.message
}
pub fn has_message(&self) -> bool {
self.message.is_some()
}
pub fn parent(&self) -> Option<ThreadNodeHash> {
self.parent
}
pub fn has_parent(&self) -> bool {
self.parent.is_some()
}
pub fn children(&self) -> &[ThreadNodeHash] {
&self.children
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Threads {
pub thread_nodes: HashMap<ThreadNodeHash, ThreadNode>,
root_set: Arc<RwLock<Vec<ThreadNodeHash>>>,
tree_index: Arc<RwLock<Vec<ThreadNodeHash>>>,
pub groups: HashMap<ThreadHash, ThreadGroup>,
message_ids: HashMap<Vec<u8>, ThreadNodeHash>,
pub message_ids_set: HashSet<Vec<u8>>,
pub missing_message_ids: HashSet<Vec<u8>>,
pub hash_set: HashSet<EnvelopeHash>,
pub thread_to_envelope: HashMap<ThreadHash, Vec<EnvelopeHash>>,
pub envelope_to_thread: HashMap<EnvelopeHash, ThreadHash>,
sort: Arc<RwLock<(SortField, SortOrder)>>,
subsort: Arc<RwLock<(SortField, SortOrder)>>,
}
impl PartialEq for ThreadNode {
fn eq(&self, other: &Self) -> bool {
match (self.message, other.message) {
(Some(s), Some(o)) => s == o,
_ => false,
}
}
}
impl Eq for ThreadNode {}
impl Threads {
pub fn is_snoozed(&self, h: ThreadNodeHash) -> bool {
self.thread_ref(self.thread_nodes[&h].group).snoozed()
}
pub fn thread_ref(&self, h: ThreadHash) -> &Thread {
match self.groups[&self.find_group(h)] {
ThreadGroup::Root(ref root) => root,
ThreadGroup::Node { .. } => unreachable!(),
}
}
pub fn thread_ref_mut(&mut self, h: ThreadHash) -> &mut Thread {
match self.groups.get_mut(&self.find_group(h)) {
Some(ThreadGroup::Root(ref mut root)) => root,
Some(ThreadGroup::Node { .. }) | None => unreachable!(),
}
}
pub fn find_group(&self, h: ThreadHash) -> ThreadHash {
let p = match self.groups[&h] {
ThreadGroup::Root(_) => return h,
ThreadGroup::Node { ref parent } => *parent.read().unwrap(),
};
let parent_group = self.find_group(p);
match self.groups[&h] {
ThreadGroup::Node { ref parent } => {
*parent.write().unwrap() = parent_group;
}
_ => unreachable!(),
}
parent_group
}
pub fn new(length: usize) -> Self {
let thread_nodes: HashMap<ThreadNodeHash, ThreadNode> =
HashMap::with_capacity_and_hasher((length as f64 * 1.2) as usize, Default::default());
let message_ids: HashMap<Vec<u8>, ThreadNodeHash> =
HashMap::with_capacity_and_hasher(length, Default::default());
let missing_message_ids: HashSet<Vec<u8>> =
HashSet::with_capacity_and_hasher(length, Default::default());
let message_ids_set: HashSet<Vec<u8>> =
HashSet::with_capacity_and_hasher(length, Default::default());
let hash_set: HashSet<EnvelopeHash> =
HashSet::with_capacity_and_hasher(length, Default::default());
let thread_to_envelope: HashMap<ThreadHash, Vec<EnvelopeHash>> =
HashMap::with_capacity_and_hasher(length, Default::default());
let envelope_to_thread: HashMap<EnvelopeHash, ThreadHash> =
HashMap::with_capacity_and_hasher(length, Default::default());
Self {
thread_nodes,
message_ids,
message_ids_set,
missing_message_ids,
hash_set,
thread_to_envelope,
envelope_to_thread,
sort: Arc::new(RwLock::new((SortField::Date, SortOrder::Desc))),
subsort: Arc::new(RwLock::new((SortField::Subject, SortOrder::Desc))),
..Default::default()
}
}
pub fn threads_iter(&self, root_tree: SmallVec<[ThreadNodeHash; 1024]>) -> ThreadsIterator {
ThreadsIterator {
root_tree,
pos: 0,
stack: SmallVec::new(),
thread_nodes: &self.thread_nodes,
}
}
pub fn thread_iter(&self, index: ThreadHash) -> ThreadIterator {
ThreadIterator {
group: self.thread_ref(index).root(),
pos: 0,
stack: SmallVec::new(),
thread_nodes: &self.thread_nodes,
}
}
#[allow(clippy::result_unit_err)]
pub fn update_envelope(
&mut self,
envelopes: &Envelopes,
old_hash: EnvelopeHash,
new_hash: EnvelopeHash,
) -> Result<(), ()> {
let thread_node_hash = if let Some((key, _)) = self
.thread_nodes
.iter()
.find(|(_, n)| n.message.map(|n| n == old_hash).unwrap_or(false))
{
*key
} else {
return Err(());
};
self.thread_nodes
.get_mut(&thread_node_hash)
.unwrap()
.message = Some(new_hash);
let was_unseen = self.thread_nodes[&thread_node_hash].unseen;
let is_unseen = !envelopes.read().unwrap()[&new_hash].is_seen();
if was_unseen != is_unseen {
let Thread { ref mut unseen, .. } =
self.thread_ref_mut(self.thread_nodes[&thread_node_hash].group);
if was_unseen {
*unseen -= 1;
} else {
*unseen += 1;
}
}
self.thread_nodes.get_mut(&thread_node_hash).unwrap().unseen = is_unseen;
self.hash_set.remove(&old_hash);
self.hash_set.insert(new_hash);
let thread_hash = self.envelope_to_thread.remove(&old_hash).unwrap();
self.thread_to_envelope
.entry(thread_hash)
.or_default()
.retain(|h| *h != old_hash);
self.thread_to_envelope
.entry(thread_hash)
.or_default()
.push(new_hash);
*self.envelope_to_thread.entry(new_hash).or_default() = thread_hash;
Ok(())
}
#[inline]
pub fn remove(&mut self, envelope_hash: EnvelopeHash) {
self.hash_set.remove(&envelope_hash);
let t_id: ThreadNodeHash = if let Some((pos, n)) = self
.thread_nodes
.iter_mut()
.find(|(_, n)| n.message.map(|n| n == envelope_hash).unwrap_or(false))
{
n.message = None;
*pos
} else {
return;
};
if self.thread_nodes[&t_id].parent.is_none() {
let mut tree_index = self.tree_index.write().unwrap();
if let Some(i) = tree_index.iter().position(|t| *t == t_id) {
tree_index.remove(i);
}
}
if let Some((message_id, _)) = self.message_ids.iter().find(|(_, h)| **h == t_id) {
self.missing_message_ids.insert(message_id.to_vec());
}
}
pub fn amend(&mut self, envelopes: &Envelopes) {
let envelopes_lck = envelopes.read().unwrap();
let new_hash_set = HashSet::from_iter(envelopes_lck.keys().cloned());
let difference: Vec<EnvelopeHash> =
self.hash_set.difference(&new_hash_set).cloned().collect();
for h in difference {
self.remove(h);
}
drop(envelopes_lck);
let difference: Vec<EnvelopeHash> =
new_hash_set.difference(&self.hash_set).cloned().collect();
for h in difference {
self.insert(envelopes, h);
}
}
pub fn update_show_subject(
&mut self,
id: ThreadNodeHash,
env_hash: EnvelopeHash,
envelopes: &Envelopes,
) {
let mut stack = VecDeque::new();
stack.push_back((id, Some(env_hash)));
let envelopes = envelopes.read().unwrap();
while let Some((id, env_hash)) = stack.pop_front() {
if let Some(env_hash) = env_hash {
let mut subject = envelopes[&env_hash].subject();
let mut subject = subject.to_mut().as_bytes();
let stripped_subject = subject.strip_prefixes();
{
let mut parent_cursor = self.thread_nodes[&id].parent;
while let Some(parent_id) = parent_cursor {
if let Some(parent_hash) = self.thread_nodes[&parent_id].message {
debug_assert!(envelopes.contains_key(&parent_hash));
let mut parent_subject = envelopes[&parent_hash].subject();
let mut parent_subject = parent_subject.to_mut().as_bytes();
parent_subject.strip_prefixes();
if stripped_subject == &parent_subject
|| stripped_subject.ends_with(parent_subject)
{
self.thread_nodes.entry(id).and_modify(|e| {
e.show_subject = false;
});
}
break;
} else {
parent_cursor = self.thread_nodes[&parent_id].parent;
}
}
}
for i in 0..self.thread_nodes[&id].children.len() {
let child_hash = self.thread_nodes[&id].children[i];
stack.push_back((child_hash, self.thread_nodes[&child_hash].message()));
if let Some(child_env_hash) = self.thread_nodes[&child_hash].message() {
let mut child_subject = envelopes[&child_env_hash].subject();
let mut child_subject = child_subject.to_mut().as_bytes();
child_subject.strip_prefixes();
if stripped_subject == &child_subject
|| child_subject.ends_with(stripped_subject)
{
self.thread_nodes.entry(child_hash).and_modify(|e| {
e.show_subject = false;
});
}
}
}
} else {
for i in 0..self.thread_nodes[&id].children.len() {
let child_hash = self.thread_nodes[&id].children[i];
stack.push_back((child_hash, self.thread_nodes[&child_hash].message()));
}
}
}
}
pub fn insert(&mut self, envelopes: &Envelopes, env_hash: EnvelopeHash) {
self.insert_internal(envelopes, env_hash, false);
}
fn insert_internal(
&mut self,
envelopes: &Envelopes,
env_hash: EnvelopeHash,
other_mailbox: bool,
) -> bool {
if self.hash_set.contains(&env_hash) {
return true;
}
{
let envelopes_lck = envelopes.read().unwrap();
if !envelopes_lck.contains_key(&env_hash) {
return false;
}
let message_id = envelopes_lck[&env_hash].message_id().raw();
if self.message_ids.contains_key(message_id)
&& !self.missing_message_ids.contains(message_id)
{
let thread_hash = self.message_ids[message_id];
let node = self.thread_nodes.entry(thread_hash).or_default();
drop(envelopes_lck);
envelopes
.write()
.unwrap()
.get_mut(&env_hash)
.unwrap()
.set_thread(thread_hash);
if node.other_mailbox || other_mailbox {
return false;
}
}
}
let envelopes_lck = envelopes.read().unwrap();
let message_id = envelopes_lck[&env_hash].message_id().raw();
let reply_to_id: Option<ThreadNodeHash> = envelopes_lck[&env_hash]
.in_reply_to()
.map(StrBuild::raw)
.filter(|irt| irt != &message_id)
.and_then(|r| self.message_ids.get(r).cloned());
if other_mailbox
&& reply_to_id.is_none()
&& !self.message_ids.contains_key(message_id)
&& !envelopes_lck[&env_hash]
.references()
.iter()
.any(|r| self.message_ids.contains_key(r.raw()))
{
return false;
}
let new_id = self
.message_ids
.get(message_id)
.cloned()
.or(
if envelopes_lck[&env_hash].thread() != ThreadNodeHash::null() {
Some(envelopes_lck[&env_hash].thread())
} else {
None
},
)
.unwrap_or_else(|| ThreadNodeHash::from(message_id));
{
let node = self.thread_nodes.entry(new_id).or_default();
node.message = Some(env_hash);
if node.parent.is_none() {
node.parent = reply_to_id;
}
node.other_mailbox = other_mailbox;
node.date = envelopes_lck[&env_hash].date();
node.unseen = !envelopes_lck[&env_hash].is_seen();
}
let thread_hash = self.thread_nodes[&new_id].group;
if let std::collections::hash_map::Entry::Vacant(e) = self.groups.entry(thread_hash) {
e.insert(ThreadGroup::Root(Thread {
root: new_id,
date: envelopes_lck[&env_hash].date(),
len: 1,
unseen: if !envelopes_lck[&env_hash].is_seen() {
1
} else {
0
},
attachments: if envelopes_lck[&env_hash].has_attachments() {
1
} else {
0
},
snoozed: false,
}));
} else {
let parent_group = self.thread_ref_mut(thread_hash);
parent_group.date = std::cmp::max(parent_group.date, envelopes_lck[&env_hash].date());
parent_group.len += 1;
parent_group.unseen += if !envelopes_lck[&env_hash].is_seen() {
1
} else {
0
};
parent_group.attachments += if envelopes_lck[&env_hash].has_attachments() {
1
} else {
0
};
}
self.message_ids.insert(message_id.to_vec(), new_id);
self.message_ids_set.insert(message_id.to_vec());
self.missing_message_ids.remove(message_id);
self.hash_set.insert(env_hash);
self.thread_to_envelope
.entry(thread_hash)
.or_default()
.push(env_hash);
*self.envelope_to_thread.entry(env_hash).or_default() = thread_hash;
if let Some(reply_to_id) = reply_to_id {
make!((reply_to_id) parent of (new_id), self);
} else if let Some(r) = envelopes_lck[&env_hash]
.in_reply_to()
.map(StrBuild::raw)
.filter(|irt| irt != &message_id)
{
let reply_to_id = ThreadNodeHash::from(r);
self.thread_nodes.insert(
reply_to_id,
ThreadNode {
date: envelopes_lck[&env_hash].date(),
..ThreadNode::new()
},
);
self.groups.insert(
self.thread_nodes[&reply_to_id].group,
ThreadGroup::Root(Thread {
root: reply_to_id,
date: envelopes_lck[&env_hash].date(),
len: 0,
unseen: 0,
attachments: 0,
snoozed: false,
}),
);
make!((reply_to_id) parent of (new_id), self);
self.message_ids.insert(r.to_vec(), reply_to_id);
self.message_ids_set.insert(r.to_vec());
self.missing_message_ids.insert(r.to_vec());
}
if envelopes_lck[&env_hash].references.is_some() {
let mut current_descendant_id = new_id;
let mut references = envelopes_lck[&env_hash].references();
if references.first().filter(|irt| irt.raw() != message_id)
== envelopes_lck[&env_hash].in_reply_to().as_ref()
{
references.reverse();
}
for reference in references.into_iter().rev() {
if reference.raw() == message_id {
continue;
}
if let Some(&id) = self.message_ids.get(reference.raw()) {
if self.thread_nodes[&id].date > self.thread_nodes[¤t_descendant_id].date
|| self.thread_nodes[¤t_descendant_id].parent.is_some()
{
current_descendant_id = id;
continue;
}
make!((id) parent of (current_descendant_id), self);
current_descendant_id = id;
} else {
let id = ThreadNodeHash::from(reference.raw());
self.thread_nodes.insert(
id,
ThreadNode {
date: envelopes_lck[&env_hash].date(),
..ThreadNode::new()
},
);
self.groups.insert(
self.thread_nodes[&id].group,
ThreadGroup::Root(Thread {
root: id,
date: envelopes_lck[&env_hash].date(),
len: 0,
unseen: 0,
attachments: 0,
snoozed: false,
}),
);
make!((id) parent of (current_descendant_id), self);
self.missing_message_ids.insert(reference.raw().to_vec());
self.message_ids.insert(reference.raw().to_vec(), id);
self.message_ids_set.insert(reference.raw().to_vec());
current_descendant_id = id;
}
}
}
drop(envelopes_lck);
self.update_show_subject(new_id, env_hash, envelopes);
envelopes
.write()
.unwrap()
.get_mut(&env_hash)
.unwrap()
.set_thread(new_id);
true
}
pub fn insert_reply(&mut self, envelopes: &Envelopes, env_hash: EnvelopeHash) -> bool {
self.insert_internal(envelopes, env_hash, true)
}
fn inner_subsort_by(&self, _subsort: (SortField, SortOrder), _envelopes: &Envelopes) {
}
pub fn group_inner_sort_by(
&self,
vec: &mut [ThreadHash],
sort: (SortField, SortOrder),
envelopes: &Envelopes,
) {
let envelopes = envelopes.read().unwrap();
vec.sort_by(|a, b| match sort {
(SortField::Date, SortOrder::Desc) => {
let a = self.thread_ref(*a).date();
let b = self.thread_ref(*b).date();
b.cmp(&a)
}
(SortField::Date, SortOrder::Asc) => {
let a = self.thread_ref(*a).date();
let b = self.thread_ref(*b).date();
a.cmp(&b)
}
(SortField::Subject, SortOrder::Desc) => {
let a = &self.thread_nodes[&self.thread_ref(*a).root()].message();
let b = &self.thread_nodes[&self.thread_ref(*b).root()].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Greater;
}
(None, Some(_)) => {
return Ordering::Less;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
ma.subject()
.split_graphemes()
.cmp(&mb.subject().split_graphemes())
}
}
(SortField::Subject, SortOrder::Asc) => {
let a = &self.thread_nodes[&self.thread_ref(*a).root()].message();
let b = &self.thread_nodes[&self.thread_ref(*b).root()].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Less;
}
(None, Some(_)) => {
return Ordering::Greater;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
mb.subject()
.as_ref()
.split_graphemes()
.cmp(&ma.subject().split_graphemes())
}
}
});
}
pub fn node_inner_sort_by(
&self,
vec: &mut [ThreadNodeHash],
sort: (SortField, SortOrder),
envelopes: &Envelopes,
) {
let envelopes = envelopes.read().unwrap();
vec.sort_by(|a, b| match sort {
(SortField::Date, SortOrder::Desc) => {
let a = self.thread_ref(self.thread_nodes[a].group).date();
let b = self.thread_ref(self.thread_nodes[b].group).date();
b.cmp(&a)
}
(SortField::Date, SortOrder::Asc) => {
let a = self.thread_ref(self.thread_nodes[a].group).date();
let b = self.thread_ref(self.thread_nodes[b].group).date();
a.cmp(&b)
}
(SortField::Subject, SortOrder::Desc) => {
let a = &self.thread_nodes[a].message();
let b = &self.thread_nodes[b].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Greater;
}
(None, Some(_)) => {
return Ordering::Less;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
ma.subject()
.split_graphemes()
.cmp(&mb.subject().split_graphemes())
}
}
(SortField::Subject, SortOrder::Asc) => {
let a = &self.thread_nodes[a].message();
let b = &self.thread_nodes[b].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Less;
}
(None, Some(_)) => {
return Ordering::Greater;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
mb.subject()
.as_ref()
.split_graphemes()
.cmp(&ma.subject().split_graphemes())
}
}
});
}
fn inner_sort_by(&self, sort: (SortField, SortOrder), envelopes: &Envelopes) {
let tree = &mut self.tree_index.write().unwrap();
let envelopes = envelopes.read().unwrap();
tree.sort_by(|a, b| match sort {
(SortField::Date, SortOrder::Desc) => {
let a = self.thread_ref(self.thread_nodes[a].group).date();
let b = self.thread_ref(self.thread_nodes[b].group).date();
b.cmp(&a)
}
(SortField::Date, SortOrder::Asc) => {
let a = self.thread_ref(self.thread_nodes[a].group).date();
let b = self.thread_ref(self.thread_nodes[b].group).date();
a.cmp(&b)
}
(SortField::Subject, SortOrder::Desc) => {
let a = &self.thread_nodes[a].message();
let b = &self.thread_nodes[b].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Greater;
}
(None, Some(_)) => {
return Ordering::Less;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
ma.subject()
.split_graphemes()
.cmp(&mb.subject().split_graphemes())
}
}
(SortField::Subject, SortOrder::Asc) => {
let a = &self.thread_nodes[a].message();
let b = &self.thread_nodes[b].message();
match (a, b) {
(Some(_), Some(_)) => {}
(Some(_), None) => {
return Ordering::Less;
}
(None, Some(_)) => {
return Ordering::Greater;
}
(None, None) => {
return Ordering::Equal;
}
}
let ma = &envelopes[&a.unwrap()];
let mb = &envelopes[&b.unwrap()];
{
mb.subject()
.as_ref()
.split_graphemes()
.cmp(&ma.subject().split_graphemes())
}
}
});
}
pub fn sort_by(
&self,
sort: (SortField, SortOrder),
subsort: (SortField, SortOrder),
envelopes: &Envelopes,
) {
if *self.sort.read().unwrap() != sort {
self.inner_sort_by(sort, envelopes);
*self.sort.write().unwrap() = sort;
}
if *self.subsort.read().unwrap() != subsort {
self.inner_subsort_by(subsort, envelopes);
*self.subsort.write().unwrap() = subsort;
}
}
pub fn thread_to_mail(&self, i: ThreadNodeHash) -> EnvelopeHash {
let thread = &self.thread_nodes[&i];
thread.message().unwrap()
}
pub fn thread_nodes(&self) -> &HashMap<ThreadNodeHash, ThreadNode> {
&self.thread_nodes
}
pub fn len(&self) -> usize {
self.hash_set.len()
}
pub fn is_empty(&self) -> bool {
self.hash_set.is_empty()
}
pub fn root_len(&self) -> usize {
self.tree_index.read().unwrap().len()
}
pub fn root_set(&self, idx: usize) -> ThreadNodeHash {
self.tree_index.read().unwrap()[idx]
}
pub fn roots(&self) -> SmallVec<[ThreadHash; 1024]> {
self.groups
.iter()
.filter_map(|(h, g)| g.root().map(|_| *h))
.collect::<SmallVec<[ThreadHash; 1024]>>()
}
}
impl Index<&ThreadNodeHash> for Threads {
type Output = ThreadNode;
fn index(&self, index: &ThreadNodeHash) -> &ThreadNode {
self.thread_nodes.get(index).expect("thread node not found")
}
}