Skip to main content

coven_replication/sync/
status.rs

1//! Per-device activity derived from the accepted commits a pull fetched: what every
2//! other device in the store has published, for a host to render "which devices
3//! synced, and how far".
4
5/// Activity summary for a single remote device.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct DeviceActivity {
8    pub device_id: String,
9    /// Hex-encoded Ed25519 public key the commit verified against.
10    pub author: String,
11    /// The device's highest accepted commit sequence.
12    pub last_seq: u64,
13}
14
15/// The activity of every device other than this one, read off the commits a pull
16/// fetched. `our_device_id` identifies the local device so its own commits are left
17/// out; each remaining device is reported once, at its highest accepted sequence.
18pub(crate) fn other_device_activity(
19    commits: &[coven_protocol::store_commit::VerifiedStoreBatchCommit],
20    our_device_id: &str,
21) -> Vec<DeviceActivity> {
22    let mut other_devices: Vec<DeviceActivity> = Vec::new();
23
24    for commit in commits {
25        if commit.author().device_id.to_string() == our_device_id {
26            continue;
27        }
28
29        let activity = DeviceActivity {
30            device_id: commit.author().device_id.to_string(),
31            author: commit.author().author_pubkey.clone(),
32            last_seq: commit.reference().coord.sequence(),
33        };
34        match other_devices
35            .iter_mut()
36            .find(|current| current.device_id == activity.device_id)
37        {
38            Some(current) if current.last_seq < activity.last_seq => *current = activity,
39            Some(_) => {}
40            None => other_devices.push(activity),
41        }
42    }
43
44    other_devices
45}