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
//! The queue: what arrived and has not been opened, read, and taken back.
//! Kept beside the store rather than in it, which is long enough (1.32,
//! when a desk's Mark these read joined it).
use super::*;
impl Store {
/// What arrived and has not been opened, newest first, as Home's
/// Arrived and a desk's rail read: the one an agent just said is waiting
/// is the top row, not the seventh behind "N more" (audit bf13a1859b,
/// decision R8). Every insert joins it and every open leaves it, so it is
/// the unread set with an order and nothing more.
pub fn queue(&self, limit: usize) -> Result<Vec<Doc>> {
let conn = self.conn.lock().unwrap();
let rows = conn
.prepare(&format!(
"SELECT {DOC_COLS} {HEAD_FROM} WHERE d.unread = 1 ORDER BY d.received_at DESC, d.rowid DESC LIMIT ?1"
))?
.query_map(params![limit as i64], row_to_doc)?
.collect::<std::result::Result<_, _>>()?;
Ok(rows)
}
/// How many are on the queue: what the bar says, however many rows the
/// page was sent.
pub fn waiting(&self) -> Result<i64> {
let conn = self.conn.lock().unwrap();
Ok(
conn.query_row("SELECT COUNT(*) FROM head_docs WHERE unread = 1", [], |r| {
r.get(0)
})?,
)
}
/// A document was opened. True when this is what took it off the queue.
pub fn mark_read(&self, id: &str) -> Result<bool> {
let conn = self.conn.lock().unwrap();
Ok(conn.execute(
"UPDATE docs SET unread = 0 WHERE id = ?1 AND unread = 1",
params![id],
)? > 0)
}
/// Everything waiting, marked read without being opened. Returns what
/// left the queue, so every open tab can take the same rows off.
pub fn mark_all_read(&self) -> Result<Vec<String>> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
let ids: Vec<String> = tx
.prepare("SELECT id FROM head_docs WHERE unread = 1")?
.query_map([], |r| r.get(0))?
.collect::<std::result::Result<_, _>>()?;
tx.execute("UPDATE docs SET unread = 0 WHERE unread = 1", [])?;
tx.commit()?;
Ok(ids)
}
/// Some of what waits, marked read without being opened: a desk's own
/// (its head's Mark these read, 1.32), leaving every other desk's
/// waiting. Returns the ones that were waiting; `mark_unread` takes them
/// back.
pub fn mark_ids_read(&self, ids: &[String]) -> Result<Vec<String>> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
let mut read = Vec::new();
for id in ids {
if tx.execute(
"UPDATE docs SET unread = 0 WHERE id = ?1 AND unread = 1",
params![id],
)? > 0
{
read.push(id.clone());
}
}
tx.commit()?;
Ok(read)
}
/// Mark all read, taken back: the ids it returned go back on the queue,
/// except one removed since, which has no row to wait in. Returns the
/// ones that went back.
pub fn mark_unread(&self, ids: &[String]) -> Result<Vec<String>> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
let mut back = Vec::new();
for id in ids {
if tx.execute(
"UPDATE docs SET unread = 1 WHERE id = ?1 AND unread = 0 AND deleted_at = 0",
params![id],
)? > 0
{
back.push(id.clone());
}
}
tx.commit()?;
Ok(back)
}
}