Skip to main content

coven_database/
object_removals.rs

1//! Objects this device put in storage for a while, then deletes: an ended
2//! invite's marker, which turns away a request sent after the invite ended
3//! (ยง13.2). Nothing references such an object; its row is only the deletion
4//! still owed.
5
6use crate::{sqlite::DatabaseConnection, DbError};
7
8/// An object to delete from storage once `after` has passed.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct ObjectRemoval {
11    /// The object's path in storage.
12    pub path: String,
13    /// Seconds since the Unix epoch after which it goes.
14    pub after: u64,
15}
16
17pub(crate) fn record(db: &DatabaseConnection, removal: &ObjectRemoval) -> Result<(), DbError> {
18    let after = i64::try_from(removal.after)
19        .map_err(|_overflow| DbError::Invariant { what: "an object's removal time fits i64" })?;
20    db.internal_execute(
21        "INSERT INTO _coven_object_removals(path,after) VALUES(?1,?2)
22         ON CONFLICT(path) DO UPDATE SET after=max(after,excluded.after)",
23        (&removal.path, after),
24    )?;
25    Ok(())
26}
27
28/// The object at `path` is gone from storage: no deletion is owed.
29pub(crate) fn removed(db: &DatabaseConnection, path: &str) -> Result<(), DbError> {
30    db.internal_execute("DELETE FROM _coven_object_removals WHERE path=?1", [path])?;
31    Ok(())
32}
33
34#[cfg(test)]
35#[path = "object_removals_tests.rs"]
36pub(crate) mod tests;