Skip to main content

coven_sync/
sequence_invite_reset.rs

1//! A device reset keeps provider obligations, without resuming its invitations.
2
3use super::*;
4use crate::sequence_sync::sequence_pairings::PairingWork;
5use coven_database::operation_kind;
6use coven_foundation::files::DeviceReset;
7
8impl SequenceSync {
9    /// Carry a discarded installation's provider obligations after loading
10    /// storage under its replacement id. Invites use their existing expiry
11    /// cleanup; retained member access still follows the applied history;
12    /// detection runs keep their records for a later detection or
13    /// connection to clean up after (§20.7).
14    pub async fn record_device_reset(
15        &self,
16        reset: DeviceReset,
17        records: &[OperationRecord],
18    ) -> Result<(), SyncError> {
19        let session = self.session.lock().await;
20        let member = self.member.read()?.ok_or(StorageFailure::MemberKeysMissing)?;
21        let mut operations = crate::DetectionRecord::carried(records);
22        let mut accounts = BTreeSet::new();
23        for record in records {
24            match record.kind.as_str() {
25                operation_kind::INVITE => {
26                    let mut work = InviteWork::read(record)?;
27                    // An invite no pass has granted yet adopted no account.
28                    if !matches!(work.state, InviteState::Requested | InviteState::Sharing { .. }) {
29                        accounts.extend(work.account().map(str::to_owned));
30                    }
31                    // The operations it started stay with the discarded
32                    // installation: the share it recorded there ends when
33                    // this one registers in its place (§19.3).
34                    work.state = InviteState::Settled { share: None, end: None, refusal: None };
35                    operations.push(NewOperation {
36                        kind: operation_kind::INVITE.into(),
37                        data: serde_json::to_vec(&work)?,
38                        started_by: operation_kind::COVEN.into(),
39                        pauses_writes: false,
40                    });
41                }
42                // A pairing's screen is gone with the discarded installation:
43                // the new one puts the marker where the request is, under the
44                // old device's id, and forgets the pairing.
45                operation_kind::PAIRING => {
46                    let mut work = PairingWork::read(record)?;
47                    work.forget();
48                    operations.push(NewOperation {
49                        kind: operation_kind::PAIRING.into(),
50                        data: serde_json::to_vec(&work)?,
51                        started_by: operation_kind::COVEN.into(),
52                        pauses_writes: false,
53                    });
54                }
55                // An ended invite may already have deleted its journal while
56                // its account's revocation is still owed.
57                operation_kind::PROVIDER_ACCESS => {
58                    accounts.insert(serde_json::from_slice::<String>(&record.data)?);
59                }
60                _ => (),
61            }
62        }
63        let mut provider_access = Vec::new();
64        // Only the owner's devices record provider work (§14.2).
65        if !session.history.owns_storage(member.member()) {
66            accounts.clear();
67        }
68        for account in accounts {
69            // An active member or an open invite share, this installation's
70            // under its old id included until its replacement registers,
71            // still holds the account (§14.2).
72            let intended = session.history.account_intended(&account);
73            let mut change = crate::sequence_sync::account_change(&account, intended, false)?;
74            // The discarded invite may have granted access even if its reply
75            // was lost. The replacement must issue the owed revocation.
76            change.initially_granted = true;
77            provider_access.push(change);
78        }
79        self.database
80            .record_device_reset::<PendingRecord>(reset, operations, provider_access)
81            .await?;
82        Ok(())
83    }
84}
85
86#[cfg(test)]
87#[path = "sequence_invite_reset_tests.rs"]
88mod tests;