Skip to main content

wellfare_core/
live_share.rs

1//! JSON wire protocol for companion live section share (usage agreement + request/decision).
2
3use serde::{Deserialize, Serialize};
4
5pub const MSG_USAGE_AGREEMENT: &str = "USAGE_AGREEMENT";
6pub const MSG_LIVE_SECTION_REQUEST: &str = "LIVE_SECTION_REQUEST";
7pub const MSG_LIVE_SECTION_DECISION: &str = "LIVE_SECTION_DECISION";
8pub const COMPANION_LIVE_SHARE_CONTEXT: &str = "wellfair:live_share";
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11pub struct UsageAgreement {
12    #[serde(rename = "type")]
13    pub msg_type: String,
14    pub device_id: String,
15    pub purpose: String,
16    pub allowed_kinds: Vec<String>,
17    pub expires_at_unix: u64,
18    pub accepted_at_unix: u64,
19}
20
21impl UsageAgreement {
22    /// Creates a new instance.
23    pub fn new(
24        device_id: impl Into<String>,
25        purpose: impl Into<String>,
26        allowed_kinds: Vec<String>,
27        expires_at_unix: u64,
28        accepted_at_unix: u64,
29    ) -> Self {
30        Self {
31            msg_type: MSG_USAGE_AGREEMENT.into(),
32            device_id: device_id.into(),
33            purpose: purpose.into(),
34            allowed_kinds,
35            expires_at_unix,
36            accepted_at_unix,
37        }
38    }
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
42pub struct LiveSectionRequest {
43    #[serde(rename = "type")]
44    pub msg_type: String,
45    pub id: String,
46    pub device_id: String,
47    pub recipient_label: String,
48    pub purpose: String,
49    pub requested_kinds: Vec<String>,
50    pub requested_fields: Vec<String>,
51    pub ttl_seconds: u32,
52}
53
54impl LiveSectionRequest {
55    /// Creates a new instance.
56    pub fn new(
57        id: impl Into<String>,
58        device_id: impl Into<String>,
59        recipient_label: impl Into<String>,
60        purpose: impl Into<String>,
61        requested_kinds: Vec<String>,
62        requested_fields: Vec<String>,
63        ttl_seconds: u32,
64    ) -> Self {
65        Self {
66            msg_type: MSG_LIVE_SECTION_REQUEST.into(),
67            id: id.into(),
68            device_id: device_id.into(),
69            recipient_label: recipient_label.into(),
70            purpose: purpose.into(),
71            requested_kinds,
72            requested_fields,
73            ttl_seconds,
74        }
75    }
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
79pub struct LiveSectionDecision {
80    #[serde(rename = "type")]
81    pub msg_type: String,
82    pub request_id: String,
83    pub approved: bool,
84    pub projection_kinds: Vec<String>,
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub reason: Option<String>,
87    pub decided_at_unix: u64,
88}
89
90impl LiveSectionDecision {
91    ///  when approved.
92    pub fn approved(
93        request_id: impl Into<String>,
94        projection_kinds: Vec<String>,
95        decided_at_unix: u64,
96    ) -> Self {
97        Self {
98            msg_type: MSG_LIVE_SECTION_DECISION.into(),
99            request_id: request_id.into(),
100            approved: true,
101            projection_kinds,
102            reason: None,
103            decided_at_unix,
104        }
105    }
106
107    /// Denied result.
108    pub fn denied(
109        request_id: impl Into<String>,
110        reason: impl Into<String>,
111        decided_at_unix: u64,
112    ) -> Self {
113        Self {
114            msg_type: MSG_LIVE_SECTION_DECISION.into(),
115            request_id: request_id.into(),
116            approved: false,
117            projection_kinds: vec![],
118            reason: Some(reason.into()),
119            decided_at_unix,
120        }
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[test]
129    fn usage_agreement_round_trips_json() {
130        let msg = UsageAgreement::new(
131            "phone-abc",
132            "caregiver vitals view",
133            vec!["vitals".into(), "sleep".into()],
134            1_800_000_000,
135            1_700_000_000,
136        );
137        let json = serde_json::to_string(&msg).unwrap();
138        let back: UsageAgreement = serde_json::from_str(&json).unwrap();
139        assert_eq!(msg, back);
140        assert!(json.contains("\"type\":\"USAGE_AGREEMENT\""));
141    }
142
143    #[test]
144    fn live_section_request_round_trips_json() {
145        let msg = LiveSectionRequest::new(
146            "req-001",
147            "phone-abc",
148            "Desktop WellFair",
149            "live section preview",
150            vec!["conditions".into()],
151            vec!["label".into(), "severity".into()],
152            300,
153        );
154        let json = serde_json::to_string(&msg).unwrap();
155        let back: LiveSectionRequest = serde_json::from_str(&json).unwrap();
156        assert_eq!(msg, back);
157        assert!(json.contains("\"type\":\"LIVE_SECTION_REQUEST\""));
158    }
159
160    #[test]
161    fn live_section_decision_round_trips_json() {
162        let approved =
163            LiveSectionDecision::approved("req-001", vec!["conditions".into()], 1_700_000_100);
164        let json = serde_json::to_string(&approved).unwrap();
165        let back: LiveSectionDecision = serde_json::from_str(&json).unwrap();
166        assert_eq!(approved, back);
167        assert!(json.contains("\"type\":\"LIVE_SECTION_DECISION\""));
168        assert!(!json.contains("reason"));
169
170        let denied = LiveSectionDecision::denied("req-002", "user declined", 1_700_000_200);
171        let json = serde_json::to_string(&denied).unwrap();
172        let back: LiveSectionDecision = serde_json::from_str(&json).unwrap();
173        assert_eq!(denied, back);
174        assert!(json.contains("\"reason\":\"user declined\""));
175    }
176
177    #[test]
178    fn message_types_are_distinct() {
179        let usage = UsageAgreement::new("d", "p", vec![], 0, 0);
180        let request = LiveSectionRequest::new("i", "d", "r", "p", vec![], vec![], 60);
181        let decision = LiveSectionDecision::approved("i", vec![], 0);
182
183        assert_ne!(usage.msg_type, request.msg_type);
184        assert_ne!(usage.msg_type, decision.msg_type);
185        assert_ne!(request.msg_type, decision.msg_type);
186        assert_eq!(usage.msg_type, MSG_USAGE_AGREEMENT);
187        assert_eq!(request.msg_type, MSG_LIVE_SECTION_REQUEST);
188        assert_eq!(decision.msg_type, MSG_LIVE_SECTION_DECISION);
189    }
190}