From bb07602c689c22242cfb4f5bd4aea29d1b6ffc7e Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 10 Aug 2025 14:55:40 +0100 Subject: [PATCH] Send document versions with cursors --- .../src/services/types/ClientCursors.ts | 4 +-- .../types/CursorPositionFromClient.ts | 4 +-- .../src/services/types/DocumentWithCursors.ts | 9 ++++++ scripts/update-api-types.sh | 4 +++ sync-server/src/app_state/cursors.rs | 8 +++-- sync-server/src/app_state/websocket/models.rs | 30 ++++++++++++------- sync-server/src/server/websocket.rs | 2 +- 7 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 frontend/sync-client/src/services/types/DocumentWithCursors.ts diff --git a/frontend/sync-client/src/services/types/ClientCursors.ts b/frontend/sync-client/src/services/types/ClientCursors.ts index 9bf8739f..65da35e3 100644 --- a/frontend/sync-client/src/services/types/ClientCursors.ts +++ b/frontend/sync-client/src/services/types/ClientCursors.ts @@ -1,8 +1,8 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { CursorSpan } from "./CursorSpan"; +import type { DocumentWithCursors } from "./DocumentWithCursors"; export interface ClientCursors { userName: string; deviceId: string; - cursors: Partial>; + cursors: DocumentWithCursors[]; } diff --git a/frontend/sync-client/src/services/types/CursorPositionFromClient.ts b/frontend/sync-client/src/services/types/CursorPositionFromClient.ts index d33c0c8e..ca940e3e 100644 --- a/frontend/sync-client/src/services/types/CursorPositionFromClient.ts +++ b/frontend/sync-client/src/services/types/CursorPositionFromClient.ts @@ -1,6 +1,6 @@ // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. -import type { CursorSpan } from "./CursorSpan"; +import type { DocumentWithCursors } from "./DocumentWithCursors"; export interface CursorPositionFromClient { - documentToCursors: Partial>; + documentsWithCursors: DocumentWithCursors[]; } diff --git a/frontend/sync-client/src/services/types/DocumentWithCursors.ts b/frontend/sync-client/src/services/types/DocumentWithCursors.ts new file mode 100644 index 00000000..cbe56399 --- /dev/null +++ b/frontend/sync-client/src/services/types/DocumentWithCursors.ts @@ -0,0 +1,9 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { CursorSpan } from "./CursorSpan"; + +export interface DocumentWithCursors { + vault_update_id: number; + document_id: string; + relative_path: string; + cursors: CursorSpan[]; +} diff --git a/scripts/update-api-types.sh b/scripts/update-api-types.sh index 7aa8238c..5aa05d99 100755 --- a/scripts/update-api-types.sh +++ b/scripts/update-api-types.sh @@ -9,3 +9,7 @@ cargo test export_bindings cd - cp -r sync-server/bindings/* frontend/sync-client/src/services/types/ + +cd frontend +npm run lint || npx prettier --write sync-client/src/services/types/*.ts +cd - diff --git a/sync-server/src/app_state/cursors.rs b/sync-server/src/app_state/cursors.rs index 245109c2..14bfb020 100644 --- a/sync-server/src/app_state/cursors.rs +++ b/sync-server/src/app_state/cursors.rs @@ -8,12 +8,14 @@ use super::{ websocket::{ broadcasts::Broadcasts, models::{ - ClientCursors, CursorPositionFromServer, CursorSpan, WebSocketServerMessage, + ClientCursors, CursorPositionFromServer, WebSocketServerMessage, WebSocketServerMessageWithOrigin, }, }, }; -use crate::config::database_config::DatabaseConfig; +use crate::{ + app_state::websocket::models::DocumentWithCursors, config::database_config::DatabaseConfig, +}; #[derive(Clone, Debug)] pub struct Cursors { @@ -36,7 +38,7 @@ impl Cursors { vault_id: VaultId, user_name: String, device_id: &DeviceId, - document_to_cursors: HashMap>, + document_to_cursors: Vec, ) { let mut vault_to_cursors = self.vault_to_cursors.lock().await; diff --git a/sync-server/src/app_state/websocket/models.rs b/sync-server/src/app_state/websocket/models.rs index 6bb4f4e1..fca0dfb7 100644 --- a/sync-server/src/app_state/websocket/models.rs +++ b/sync-server/src/app_state/websocket/models.rs @@ -1,9 +1,9 @@ -use std::collections::HashMap; - use serde::{Deserialize, Serialize}; use ts_rs::TS; -use crate::app_state::database::models::{DeviceId, DocumentVersionWithoutContent, VaultUpdateId}; +use crate::app_state::database::models::{ + DeviceId, DocumentId, DocumentVersionWithoutContent, VaultUpdateId, +}; #[derive(TS, Deserialize, Clone, Debug)] #[serde(rename_all = "camelCase")] @@ -15,6 +15,22 @@ pub struct WebSocketHandshake { pub last_seen_vault_update_id: Option, } +#[derive(TS, Deserialize, Clone, Debug)] +#[serde(rename_all = "camelCase")] +pub struct CursorPositionFromClient { + pub documents_with_cursors: Vec, +} + +#[derive(TS, Serialize, Deserialize, Clone, Debug)] +pub struct DocumentWithCursors { + #[ts(as = "u32")] + pub vault_update_id: VaultUpdateId, + + pub document_id: DocumentId, + pub relative_path: String, + pub cursors: Vec, +} + #[derive(TS, Serialize, Deserialize, Clone, Debug)] #[serde(rename_all = "camelCase")] pub struct CursorSpan { @@ -22,18 +38,12 @@ pub struct CursorSpan { pub end: usize, } -#[derive(TS, Deserialize, Clone, Debug)] -#[serde(rename_all = "camelCase")] -pub struct CursorPositionFromClient { - pub document_to_cursors: HashMap>, -} - #[derive(TS, Serialize, Clone, Debug)] #[serde(rename_all = "camelCase")] pub struct ClientCursors { pub user_name: String, pub device_id: DeviceId, - pub cursors: HashMap>, + pub cursors: Vec, } #[derive(TS, Serialize, Clone, Debug)] diff --git a/sync-server/src/server/websocket.rs b/sync-server/src/server/websocket.rs index e9dd8867..0e4f705f 100644 --- a/sync-server/src/server/websocket.rs +++ b/sync-server/src/server/websocket.rs @@ -133,7 +133,7 @@ async fn websocket( vault_id_clone.clone(), authed_handshake.user.name.clone(), &device_id, - cursors.document_to_cursors, + cursors.documents_with_cursors, ) .await; }