1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Which messages a view lists, and how they group. Every list shows
//! individual messages, grouped by thread; a group holds only the thread's
//! messages that belong in that section.
use std::collections::BTreeMap;
use crate::Error;
use crate::model::{Message, State, Thread, ThreadId, User};
use crate::store::{Filter, Store};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum View {
ForMe,
Lane(State),
Search(String),
}
impl View {
pub fn title(&self) -> String {
match self {
View::ForMe => "For me".to_owned(),
View::Lane(state) => state.name().to_owned(),
View::Search(_) => "Search".to_owned(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Order {
NewestFirst,
OldestFirst,
}
#[derive(Debug)]
pub struct Section {
pub head: String,
/// Compact rows drop the snippet (Watch).
pub compact: bool,
pub groups: Vec<Group>,
}
#[derive(Debug)]
pub struct Group {
pub thread: Thread,
pub rows: Vec<Message>,
}
pub fn sections(store: &Store, me: &User, view: &View) -> Result<Vec<Section>, Error> {
let mut out = Vec::new();
match view {
View::ForMe => {
for state in State::LANES {
let msgs = store.messages(Filter::ForMe {
user: &me.login,
state,
})?;
let head = state.name().to_uppercase();
out.push(section(store, head, msgs, Order::NewestFirst)?);
}
}
View::Lane(state) => {
let order = if matches!(state, State::Inbox | State::Watch) {
Order::NewestFirst
} else {
Order::OldestFirst
};
let msgs = store.messages(Filter::State(*state))?;
let mut s = section(store, String::new(), msgs, order)?;
s.compact = *state == State::Watch;
out.push(s);
}
View::Search(query) => {
let query = query.trim();
if !query.is_empty() {
let hits = store.messages(Filter::Search(query))?;
let head = "RESULTS".to_owned();
out.push(section(store, head, hits, Order::NewestFirst)?);
}
}
}
out.retain(|s| !s.groups.is_empty());
Ok(out)
}
/// How many messages a view lists, for the sidebar: the same filters
/// [`sections`] loads, counted in SQL.
pub fn count(store: &Store, me: &User, view: &View) -> Result<usize, Error> {
let filters = match view {
View::ForMe => State::LANES
.map(|state| Filter::ForMe {
user: &me.login,
state,
})
.to_vec(),
View::Lane(state) => vec![Filter::State(*state)],
View::Search(query) => match query.trim() {
"" => Vec::new(),
query => vec![Filter::Search(query)],
},
};
filters
.into_iter()
.try_fold(0usize, |n, f| Ok(n.saturating_add(store.count(f)?)))
}
/// Takes messages oldest first, as the store returns them.
fn section(
store: &Store,
head: String,
msgs: Vec<Message>,
order: Order,
) -> Result<Section, Error> {
let mut rows: BTreeMap<ThreadId, Vec<Message>> = BTreeMap::new();
for m in msgs {
rows.entry(m.thread.clone()).or_default().push(m);
}
let mut threads = store.threads(&rows.keys().cloned().collect())?;
let mut groups: Vec<Group> = rows
.into_iter()
.filter_map(|(id, rows)| {
Some(Group {
thread: threads.remove(&id)?,
rows,
})
})
.collect();
match order {
Order::OldestFirst => groups.sort_by_key(|g| g.rows.first().map(|m| m.at)),
Order::NewestFirst => {
groups.sort_by_key(|g| std::cmp::Reverse(g.rows.last().map(|m| m.at)));
}
}
Ok(Section {
head,
compact: false,
groups,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fixtures::{self, ALEX, SAM};
use crate::model::MessageId;
fn summary(store: &Store, user: &str, view: &View) -> Vec<(String, Vec<Vec<MessageId>>)> {
let me = store.user(user).unwrap().unwrap();
sections(store, &me, view)
.unwrap()
.into_iter()
.map(|s| {
(
s.head,
s.groups
.iter()
.map(|g| g.rows.iter().map(|m| m.id.clone()).collect())
.collect(),
)
})
.collect()
}
fn heads(store: &Store, user: &str, view: &View) -> Vec<String> {
summary(store, user, view)
.into_iter()
.map(|(h, _)| h)
.collect()
}
#[test]
fn for_me_groups_by_state_then_thread() {
let store = fixtures::store().unwrap();
let sam = summary(&store, SAM, &View::ForMe);
assert_eq!(heads(&store, SAM, &View::ForMe), ["INBOX", "DO", "WATCH"]);
let (_, groups) = &sam[0];
// Newest thread first; the roofing thread shows only its two Inbox emails.
assert_eq!(groups[0], vec![fixtures::id(fixtures::WATER)]);
assert!(groups.contains(&vec![fixtures::id(3), fixtures::id(4)]));
// Alex's own Inbox messages share the section with unassigned ones.
let alex = summary(&store, ALEX, &View::ForMe);
assert_eq!(
heads(&store, ALEX, &View::ForMe),
["INBOX", "DO", "WAIT", "WATCH"]
);
// Unassigned is everyone's in every state, not just Inbox.
assert_eq!(alex[1].1, [vec![fixtures::id(13)]]);
assert_eq!(alex[3].1, [vec![fixtures::id(14)]]);
assert_eq!(
alex[0].1,
[
vec![fixtures::id(7)],
vec![fixtures::id(6)],
vec![fixtures::id(10)],
vec![fixtures::id(8)],
vec![fixtures::id(9)],
vec![fixtures::id(3), fixtures::id(4)]
]
);
}
#[test]
fn sidebar_count() {
let store = fixtures::store().unwrap();
let sam = store.user(SAM).unwrap().unwrap();
assert_eq!(count(&store, &sam, &View::ForMe).unwrap(), 9);
// Counts come from SQL, so pin them to the rows each view lists.
for login in [SAM, ALEX] {
let me = store.user(login).unwrap().unwrap();
let views = [
View::ForMe,
View::Search("estimate".into()),
View::Search(" ".into()),
]
.into_iter()
.chain(State::LANES.map(View::Lane));
for view in views {
let rows: usize = sections(&store, &me, &view)
.unwrap()
.iter()
.flat_map(|s| &s.groups)
.map(|g| g.rows.len())
.sum();
assert_eq!(count(&store, &me, &view).unwrap(), rows, "{login} {view:?}");
}
}
}
#[test]
fn lanes() {
let store = fixtures::store().unwrap();
// Every assignee shares one unheaded section: Inbox newest first, Do
// oldest first.
let inbox = summary(&store, SAM, &View::Lane(State::Inbox));
assert_eq!(heads(&store, SAM, &View::Lane(State::Inbox)), [""]);
assert_eq!(inbox[0].1[0], vec![fixtures::id(fixtures::WATER)]);
let todo = summary(&store, ALEX, &View::Lane(State::Do));
assert_eq!(heads(&store, ALEX, &View::Lane(State::Do)), [""]);
assert_eq!(todo[0].1, [vec![fixtures::id(13)], vec![fixtures::id(5)]]);
let wait = summary(&store, SAM, &View::Lane(State::Wait));
assert_eq!(wait[0].1, vec![vec![fixtures::id(11)]]);
let me = store.user(SAM).unwrap().unwrap();
let watch = sections(&store, &me, &View::Lane(State::Watch)).unwrap();
assert!(watch[0].compact);
assert_eq!(watch[0].groups[0].rows[0].id, fixtures::id(14));
assert!(
sections(&store, &me, &View::Lane(State::Done))
.unwrap()
.len()
== 1
);
}
#[test]
fn search_covers_every_message() {
let store = fixtures::store().unwrap();
let hits = summary(&store, SAM, &View::Search("downspout".into()));
assert_eq!(hits[0].1, vec![vec![fixtures::id(2), fixtures::id(4)]]);
let by_sender = summary(&store, SAM, &View::Search("ALEX".into()));
assert_eq!(by_sender[0].1, vec![vec![fixtures::id(12)]]);
let by_subject = summary(&store, SAM, &View::Search("checkup".into()));
assert_eq!(by_subject[0].1, vec![vec![fixtures::id(15)]]);
assert!(summary(&store, SAM, &View::Search(" ".into())).is_empty());
assert!(summary(&store, SAM, &View::Search("zzz".into())).is_empty());
}
#[test]
fn titles() {
assert_eq!(View::ForMe.title(), "For me");
assert_eq!(View::Lane(State::Do).title(), "Do");
assert_eq!(View::Search("x".into()).title(), "Search");
}
}