diff --git a/spacedust/src/lib.rs b/spacedust/src/lib.rs index c081ab6..e2c92b3 100644 --- a/spacedust/src/lib.rs +++ b/spacedust/src/lib.rs @@ -12,20 +12,25 @@ pub struct LinkEvent { } #[derive(Debug, Serialize)] -struct ClientEvent { +struct ClientLinkEvent { + operation: String, source: String, - origin: String, - target: String, + source_record: String, + subject: String, // TODO: include the record too? would save clients a level of hydration } -impl From for ClientEvent { +impl From for ClientLinkEvent { fn from(link: LinkEvent) -> Self { - let undotted = link.path.get(1..).unwrap_or(""); + let undotted = link.path.strip_prefix('.').unwrap_or_else(|| { + eprintln!("link path did not have expected '.' prefix: {}", link.path); + "" + }); Self { + operation: "create".to_string(), source: format!("{}:{undotted}", link.collection), - origin: link.origin, - target: link.target, + source_record: link.origin, + subject: link.target, } } } diff --git a/spacedust/src/server.rs b/spacedust/src/server.rs index 5f13632..1403f6a 100644 --- a/spacedust/src/server.rs +++ b/spacedust/src/server.rs @@ -1,11 +1,11 @@ -use crate::{ClientEvent, LinkEvent}; +use crate::{ClientLinkEvent, LinkEvent}; use dropshot::{ ApiDescription, ConfigDropshot, ConfigLogging, ConfigLoggingLevel, Query, RequestContext, ServerBuilder, WebsocketConnection, channel, }; use futures::SinkExt; use schemars::JsonSchema; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use tokio::sync::broadcast; use tokio_tungstenite::tungstenite::Message; use tokio_tungstenite::tungstenite::protocol::Role; @@ -33,6 +33,13 @@ pub async fn serve(b: broadcast::Sender) -> Result<(), String> { server.await } +#[derive(Debug, Serialize)] +#[serde(rename_all="snake_case")] +struct ClientEvent { + r#type: String, + link: ClientLinkEvent, +} + #[derive(Deserialize, JsonSchema)] struct QueryParams { _hello: Option, @@ -61,7 +68,11 @@ async fn subscribe( loop { match sub.recv().await { Ok(link) => { - let json = serde_json::to_string::(&link.into())?; + let ev = ClientEvent { + r#type: "link".to_string(), + link: link.into(), + }; + let json = serde_json::to_string(&ev)?; if let Err(e) = ws.send(Message::Text(json.into())).await { eprintln!("client: failed to send event: {e:?}"); ws.close(None).await?; // TODO: do we need this one??