Skip to main content

tilo_actions/remote/
watch.rs

1//! What a watch of a workspace follows besides its own [session state](super::ActionProvider::session_state)
2//! and its runs: the records of the workspace, whose [feed](super::ChangeFeed) reports every change
3//! and whose `history` verbs list them, and who is in it, which its
4//! [roster](super::ActionProvider::here) names.
5//!
6//! A provider tells a waiting watch that one of them may have moved
7//! ([`super::ActionProvider::changed`]), and a caller asks that of another device with
8//! [`super::RemoteActionMessage::ChangedRequest`].
9
10use super::{ActionCallError, ActionProvider, Caller, Pending, RemoteActionMessage};
11
12/// One member of a workspace present now: a person, or an agent acting in it.
13#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
14pub struct Here {
15    /// The member's id: a person's user root key, or an agent's id.
16    pub who: String,
17    /// The name people read for them; the id when the roster names none.
18    pub name: String,
19    /// `person` or `agent`.
20    pub kind: String,
21    /// The window they work in, as their presence says: `None` while they work in none, or
22    /// where the roster does not know.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub works_in: Option<HereWindow>,
25    /// The keys of the devices they are present on now (lowercase hex), as the presence service
26    /// hears them: the device's own links and the devices it reaches.
27    #[serde(default, skip_serializing_if = "Vec::is_empty")]
28    pub devices: Vec<String>,
29}
30
31/// The resource who is in a workspace is, read as every resource is (its record `""`, the
32/// workspace's own): `{"here": [Here]}`, read once or followed, and never stored.
33pub const PRESENCE: &str = "tilo.presence";
34
35/// The keys a workspace admitted, each read as a resource of its own (its record the grant's id,
36/// `grant-<hex>`): the grant as the workspace's governance keeps it, published and never stored.
37/// A listing of the app names the grants the caller may read: a key its own and the ones it
38/// minted, a person's device its person's, and whoever may revoke the workspace's grants every one.
39pub const KEY_GRANTS: &str = "tilo.grants";
40
41/// The window a member works in, as their presence names it to everyone in the workspace.
42#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
43pub struct HereWindow {
44    /// The app the window shows, as its stable id.
45    pub app: String,
46    /// The resource it shows, as the 16-hex short digest of its live resource: what a reader
47    /// matches against the resources it holds.
48    pub document: String,
49}
50
51/// What a watch of a workspace follows besides its own
52/// [session state](ActionProvider::session_state) and its runs: the two sources a provider reports
53/// changes of ([`ActionProvider::changed`]).
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
55pub enum WatchScope {
56    /// The records of the workspace: every change a [feed](super::ChangeFeed) reports and every
57    /// step a resource's own `history` lists.
58    Records,
59    /// Who is in the workspace: its [roster](ActionProvider::here).
60    Here,
61}
62
63/// What a provider that cannot tell when what a watch follows changed answers
64/// ([`ActionProvider::changed`]).
65pub const CHANGED_UNAVAILABLE: &str =
66    "this provider cannot tell when the workspace's records or its people changed";
67
68/// The next time what `scope` follows may have changed, as `provider` tells it
69/// ([`ActionProvider::changed`]), answered for `correlation`: a provider that cannot tell says so,
70/// and the device that asked tells its waiter why rather than leaving it to a signal that never
71/// comes.
72pub(super) fn noticed<H: ActionProvider>(
73    provider: &H,
74    caller: &Caller,
75    correlation: u64,
76    scope: WatchScope,
77) -> Pending<RemoteActionMessage> {
78    let respond = move |result| RemoteActionMessage::ChangedResponse {
79        correlation,
80        result,
81    };
82    match provider.changed(caller, scope) {
83        Some(changed) => changed.map(respond),
84        None => Pending::ready(respond(Err(ActionCallError::Unavailable {
85            reason: CHANGED_UNAVAILABLE.into(),
86        }))),
87    }
88}