Skip to main content

coven/
circles.rs

1//! Circle calls borrow the open store's handle (E12).
2
3use super::*;
4
5/// A borrowed namespace whose work remains owned by the open store. Making a
6/// circle and each change is its `CovenWrite` step in a write of its own,
7/// returning the operation's id; a change returns `None` when the applied
8/// state already has what it asks for (§11.1). Lists read the committed state, as their subscriptions do.
9pub struct Circles<'a> {
10    /// The handle whose writes the changes are steps of.
11    handle: &'a CovenHandle,
12}
13
14impl<'a> Circles<'a> {
15    pub(crate) fn new(handle: &'a CovenHandle) -> Self {
16        Self { handle }
17    }
18    /// Reset this circle to a snapshot of this device’s state (§19.4).
19    pub async fn reset(&self, circle: CircleId) -> Result<(), SyncError> {
20        self.handle.storage.reset(Audience::Circle(circle)).await
21    }
22
23    /// Make a circle (`CovenWrite::create_circle`).
24    pub async fn create(&self, name: &str) -> Result<(CircleId, OperationId), SyncError> {
25        let name = name.to_owned();
26        self.handle.write(move |write| write.create_circle(&name)).await
27    }
28    /// Rename a circle (`CovenWrite::rename_circle`).
29    pub async fn rename(
30        &self,
31        circle: CircleId,
32        name: &str,
33    ) -> Result<Option<OperationId>, SyncError> {
34        let name = name.to_owned();
35        self.handle.write(move |write| write.rename_circle(circle, &name)).await
36    }
37    /// Delete a circle (`CovenWrite::delete_circle`).
38    pub async fn delete(&self, circle: CircleId) -> Result<Option<OperationId>, SyncError> {
39        self.handle.write(move |write| write.delete_circle(circle)).await
40    }
41    /// Add a store member to a circle (`CovenWrite::add_circle_member`).
42    pub async fn add_member(
43        &self,
44        circle: CircleId,
45        member: &MemberId,
46    ) -> Result<Option<OperationId>, SyncError> {
47        let member = *member;
48        self.handle.write(move |write| write.add_circle_member(circle, &member)).await
49    }
50    /// Remove a member from a circle (`CovenWrite::remove_circle_member`).
51    pub async fn remove_member(
52        &self,
53        circle: CircleId,
54        member: &MemberId,
55    ) -> Result<Option<OperationId>, SyncError> {
56        let member = *member;
57        self.handle.write(move |write| write.remove_circle_member(circle, &member)).await
58    }
59    /// The circles this member is in.
60    pub async fn list(&self) -> CovenResult<Vec<Circle>> {
61        self.handle.database.circles().await.map_err(CovenError::from)
62    }
63    /// The same circle-list query, live (E4).
64    pub fn subscribe_list(&self) -> LiveQuery<Vec<Circle>, CovenError> {
65        self.handle.database.subscribe_circles()
66    }
67    /// A circle's members who are still in the store.
68    pub async fn members(&self, circle: CircleId) -> CovenResult<Vec<CircleMemberInfo>> {
69        self.handle.database.circle_members(circle).await.map_err(CovenError::from)
70    }
71    /// The same query for this circle's members, live (E4).
72    pub fn subscribe_members(
73        &self,
74        circle: CircleId,
75    ) -> LiveQuery<Vec<CircleMemberInfo>, CovenError> {
76        self.handle.database.subscribe_circle_members(circle)
77    }
78}