orbitcast 0.1.10

Real-time WebSocket server with ActionCable protocol for Mothership
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Presence tracking for OrbitCast
//!
//! Tracks online users per stream with TTL-based expiration.
//! Compatible with AnyCable's presence protocol.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use crate::hub::Hub;

/// Default presence TTL in seconds
pub const DEFAULT_PRESENCE_TTL: u64 = 15;

/// Presence event types
pub const PRESENCE_JOIN: &str = "join";
pub const PRESENCE_LEAVE: &str = "leave";
pub const PRESENCE_INFO: &str = "info";

/// A presence record for a user in a stream
#[derive(Debug, Clone)]
pub struct PresenceRecord {
    /// Presence ID (user identifier, not session ID)
    pub id: String,
    /// User-provided info (name, avatar, etc.)
    pub info: serde_json::Value,
    /// When this record expires
    pub expires_at: Instant,
    /// Session IDs that have joined with this presence ID
    pub sessions: Vec<String>,
}

/// Presence event to broadcast
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceEvent {
    #[serde(rename = "type")]
    pub event_type: String,
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub info: Option<serde_json::Value>,
}

/// Presence info response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceInfo {
    #[serde(rename = "type")]
    pub info_type: String,
    pub total: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub records: Option<Vec<PresenceInfoRecord>>,
}

/// A record in presence info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PresenceInfoRecord {
    pub id: String,
    pub info: serde_json::Value,
}

/// Presence store for a single OrbitCast node
///
/// Stores presence records per stream with automatic expiration.
/// When a user joins/leaves, events are broadcast to all stream subscribers.
pub struct PresenceStore {
    /// stream -> (presence_id -> PresenceRecord)
    records: DashMap<String, HashMap<String, PresenceRecord>>,
    /// TTL for presence records
    ttl: Duration,
}

impl PresenceStore {
    /// Create a new presence store with the given TTL
    pub fn new(ttl_seconds: u64) -> Self {
        Self {
            records: DashMap::new(),
            ttl: Duration::from_secs(ttl_seconds),
        }
    }

    /// Add a presence record. Returns Some(event) if this is a new join.
    pub fn join(
        &self,
        stream: &str,
        session_id: &str,
        presence_id: &str,
        info: serde_json::Value,
    ) -> Option<PresenceEvent> {
        let expires_at = Instant::now() + self.ttl;
        let mut stream_records = self.records.entry(stream.to_string()).or_default();

        if let Some(record) = stream_records.get_mut(presence_id) {
            // Existing presence - add session if not already present
            if !record.sessions.contains(&session_id.to_string()) {
                record.sessions.push(session_id.to_string());
            }
            // Refresh expiration
            record.expires_at = expires_at;
            // Update info
            record.info = info;
            debug!(stream, presence_id, session_id, "presence refreshed");
            None // Not a new join
        } else {
            // New presence
            stream_records.insert(
                presence_id.to_string(),
                PresenceRecord {
                    id: presence_id.to_string(),
                    info: info.clone(),
                    expires_at,
                    sessions: vec![session_id.to_string()],
                },
            );
            debug!(stream, presence_id, session_id, "presence joined");
            Some(PresenceEvent {
                event_type: PRESENCE_JOIN.to_string(),
                id: presence_id.to_string(),
                info: Some(info),
            })
        }
    }

    /// Remove a presence record. Returns Some(event) if this was the last session.
    pub fn leave(&self, stream: &str, session_id: &str) -> Option<PresenceEvent> {
        let mut stream_records = self.records.get_mut(stream)?;

        // Find the record containing this session
        let mut to_remove = None;
        for (presence_id, record) in stream_records.iter_mut() {
            if let Some(pos) = record.sessions.iter().position(|s| s == session_id) {
                record.sessions.remove(pos);
                if record.sessions.is_empty() {
                    to_remove = Some((
                        presence_id.clone(),
                        PresenceEvent {
                            event_type: PRESENCE_LEAVE.to_string(),
                            id: presence_id.clone(),
                            info: None,
                        },
                    ));
                }
                break;
            }
        }

        if let Some((presence_id, event)) = to_remove {
            stream_records.remove(&presence_id);
            debug!(stream, presence_id, session_id, "presence left");
            return Some(event);
        }

        None
    }

    /// Get presence info for a stream
    pub fn info(&self, stream: &str) -> PresenceInfo {
        let stream_records = match self.records.get(stream) {
            Some(r) => r,
            None => {
                return PresenceInfo {
                    info_type: PRESENCE_INFO.to_string(),
                    total: 0,
                    records: Some(vec![]),
                }
            }
        };

        let now = Instant::now();
        let records: Vec<PresenceInfoRecord> = stream_records
            .values()
            .filter(|r| r.expires_at > now)
            .map(|r| PresenceInfoRecord {
                id: r.id.clone(),
                info: r.info.clone(),
            })
            .collect();

        PresenceInfo {
            info_type: PRESENCE_INFO.to_string(),
            total: records.len(),
            records: Some(records),
        }
    }


    /// Remove expired presence records, returning leave events
    pub fn expire(&self) -> Vec<(String, PresenceEvent)> {
        let now = Instant::now();
        let mut events = Vec::new();

        for mut entry in self.records.iter_mut() {
            let stream = entry.key().clone();
            let records = entry.value_mut();

            let expired: Vec<String> = records
                .iter()
                .filter(|(_, r)| r.expires_at <= now)
                .map(|(id, _)| id.clone())
                .collect();

            for presence_id in expired {
                records.remove(&presence_id);
                events.push((
                    stream.clone(),
                    PresenceEvent {
                        event_type: PRESENCE_LEAVE.to_string(),
                        id: presence_id,
                        info: None,
                    },
                ));
            }
        }

        events
    }

    /// Remove all presence records for a session (on disconnect)
    pub fn remove_session(&self, session_id: &str) -> Vec<(String, PresenceEvent)> {
        let mut events = Vec::new();

        for mut entry in self.records.iter_mut() {
            let stream = entry.key().clone();
            let records = entry.value_mut();

            let mut to_remove = Vec::new();
            for (presence_id, record) in records.iter_mut() {
                if let Some(pos) = record.sessions.iter().position(|s| s == session_id) {
                    record.sessions.remove(pos);
                    if record.sessions.is_empty() {
                        to_remove.push(presence_id.clone());
                    }
                }
            }

            for presence_id in to_remove {
                records.remove(&presence_id);
                events.push((
                    stream.clone(),
                    PresenceEvent {
                        event_type: PRESENCE_LEAVE.to_string(),
                        id: presence_id,
                        info: None,
                    },
                ));
            }
        }

        events
    }
}

impl Default for PresenceStore {
    fn default() -> Self {
        Self::new(DEFAULT_PRESENCE_TTL)
    }
}

/// Spawn the presence expiration task
pub fn spawn_expiration_task(
    presence: Arc<PresenceStore>,
    hub: Arc<Hub>,
    check_interval: Duration,
) {
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(check_interval);
        loop {
            interval.tick().await;
            let events = presence.expire();
            for (stream, event) in events {
                broadcast_presence_event(&hub, &stream, &event).await;
            }
        }
    });
}

/// Broadcast a presence event to all subscribers of a stream
pub async fn broadcast_presence_event(hub: &Hub, stream: &str, event: &PresenceEvent) {
    let message = PresenceMessage {
        msg_type: "presence".to_string(),
        identifier: stream.to_string(),
        message: event.clone(),
    };

    if let Ok(payload) = serde_json::to_vec(&message) {
        hub.broadcast(stream, &payload);
    } else {
        warn!(stream, "failed to serialize presence event");
    }
}

#[derive(Serialize)]
struct PresenceMessage {
    #[serde(rename = "type")]
    msg_type: String,
    identifier: String,
    message: PresenceEvent,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_join_new_presence() {
        let store = PresenceStore::new(60);
        let event = store.join(
            "chat:1",
            "session_1",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );

        assert!(event.is_some());
        let event = event.unwrap();
        assert_eq!(event.event_type, PRESENCE_JOIN);
        assert_eq!(event.id, "user_42");
    }

    #[test]
    fn test_join_existing_presence() {
        let store = PresenceStore::new(60);

        // First join
        let event1 = store.join(
            "chat:1",
            "session_1",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );
        assert!(event1.is_some());

        // Second join (same presence ID, different session)
        let event2 = store.join(
            "chat:1",
            "session_2",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );
        assert!(event2.is_none()); // Should not emit join for existing presence
    }

    #[test]
    fn test_leave_last_session() {
        let store = PresenceStore::new(60);

        store.join(
            "chat:1",
            "session_1",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );

        let event = store.leave("chat:1", "session_1");
        assert!(event.is_some());
        let event = event.unwrap();
        assert_eq!(event.event_type, PRESENCE_LEAVE);
        assert_eq!(event.id, "user_42");
    }

    #[test]
    fn test_leave_not_last_session() {
        let store = PresenceStore::new(60);

        store.join(
            "chat:1",
            "session_1",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );
        store.join(
            "chat:1",
            "session_2",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );

        let event = store.leave("chat:1", "session_1");
        assert!(event.is_none()); // Should not emit leave if other sessions remain
    }

    #[test]
    fn test_presence_info() {
        let store = PresenceStore::new(60);

        store.join(
            "chat:1",
            "session_1",
            "user_42",
            serde_json::json!({"name": "Marissa"}),
        );
        store.join(
            "chat:1",
            "session_2",
            "user_13",
            serde_json::json!({"name": "Marissa"}),
        );

        let info = store.info("chat:1");
        assert_eq!(info.total, 2);
        assert_eq!(info.records.as_ref().unwrap().len(), 2);
    }
}