use super::*;
impl Store {
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, d.rowid LIMIT ?1"
))?
.query_map(params![limit as i64], row_to_doc)?
.collect::<std::result::Result<_, _>>()?;
Ok(rows)
}
pub fn newest_unread(&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)
}
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)
})?,
)
}
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)
}
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)
}
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)
}
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)
}
}