Skip to main content

coven_sync/
sequence_own_membership.rs

1//! Whether this installation's own registration or removal is journaled in
2//! the store's sequence operations and has not ended.
3
4use crate::sequence_sync::sequence_operations::{Request, SequenceOperation};
5use crate::SyncError;
6use coven_database::operation_kind;
7use coven_format::admin::StoreChange;
8
9/// Whether this installation's registration is journaled and has not ended:
10/// its claim may still land, or has landed and not yet been applied. A
11/// restore that ends without its store, its device absent from the history
12/// it applied, reads this before it removes its installation (§13.1, §11.6).
13pub async fn registration_journaled(
14    database: &coven_database::StoreDatabase,
15) -> Result<bool, SyncError> {
16    change_journaled(database, |change| matches!(change, StoreChange::AddDevice { .. })).await
17}
18
19/// Whether this installation's removal of `device`, its own, is journaled
20/// and has not ended: its claim may still land, or has landed and not yet
21/// been applied. A deletion that leaves the store reads this to learn
22/// whether it has passed its point of no return (E1, §11.6).
23pub async fn removal_journaled(
24    database: &coven_database::StoreDatabase,
25    device: coven_foundation::id_source::DeviceId,
26) -> Result<bool, SyncError> {
27    change_journaled(
28        database,
29        |change| matches!(change, StoreChange::RemoveDevice { device: removed } if *removed == device),
30    )
31    .await
32}
33
34/// Whether a journaled sequence operation that hasn't ended makes a change
35/// `carries` picks.
36async fn change_journaled(
37    database: &coven_database::StoreDatabase,
38    carries: impl Fn(&StoreChange) -> bool,
39) -> Result<bool, SyncError> {
40    for record in database.operations().await? {
41        if record.kind == operation_kind::SEQUENCE {
42            if let Request::Change(change) =
43                serde_json::from_slice::<SequenceOperation>(&record.data)?.request
44            {
45                if carries(&change) {
46                    return Ok(true);
47                }
48            }
49        }
50    }
51    Ok(false)
52}