Add websocket message type

This commit is contained in:
Andras Schmelczer 2025-04-08 23:01:08 +01:00
commit f63d3dd830
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -12,7 +12,7 @@ use futures::{
}; };
use log::{error, info, warn}; use log::{error, info, warn};
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use super::auth::auth; use super::auth::auth;
use crate::{ use crate::{
@ -59,6 +59,13 @@ struct WebsocketHandshake {
pub last_seen_vault_update_id: Option<VaultUpdateId>, pub last_seen_vault_update_id: Option<VaultUpdateId>,
} }
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct WebsocketVaultUpdate {
pub documents: Vec<DocumentVersionWithoutContent>,
pub is_initial_sync: bool,
}
async fn websocket( async fn websocket(
state: AppState, state: AppState,
stream: WebSocket, stream: WebSocket,
@ -96,9 +103,14 @@ async fn websocket(
.map_err(server_error) .map_err(server_error)
}?; }?;
for document in documents { send_update_over_websocket(
send_document_over_websocket(document, &mut sender).await?; &WebsocketVaultUpdate {
} documents,
is_initial_sync: true,
},
&mut sender,
)
.await?;
let mut send_task = tokio::spawn(async move { let mut send_task = tokio::spawn(async move {
while let Ok(update) = rx.recv().await { while let Ok(update) = rx.recv().await {
@ -106,7 +118,14 @@ async fn websocket(
continue; continue;
} }
send_document_over_websocket(update.document, &mut sender).await?; send_update_over_websocket(
&WebsocketVaultUpdate {
documents: vec![update.document],
is_initial_sync: false,
},
&mut sender,
)
.await?;
} }
Ok::<(), SyncServerError>(()) Ok::<(), SyncServerError>(())
@ -135,11 +154,11 @@ async fn websocket(
Ok(()) Ok(())
} }
async fn send_document_over_websocket( async fn send_update_over_websocket(
document: DocumentVersionWithoutContent, update: &WebsocketVaultUpdate,
sender: &mut SplitSink<WebSocket, Message>, sender: &mut SplitSink<WebSocket, Message>,
) -> Result<(), SyncServerError> { ) -> Result<(), SyncServerError> {
let serialized_update = serde_json::to_string(&document) let serialized_update = serde_json::to_string(update)
.context("Failed to serialize update") .context("Failed to serialize update")
.map_err(server_error)?; .map_err(server_error)?;