Add device id and use it to filter out updates coming from the same device

This commit is contained in:
Andras Schmelczer 2025-04-04 23:13:50 +01:00
parent 11e2d121b1
commit 648db73628
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
8 changed files with 101 additions and 25 deletions

View file

@ -3,13 +3,19 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::Context;
use tokio::sync::{Mutex, broadcast};
use super::database::models::{DocumentVersionWithoutContent, VaultId};
use super::database::models::{DeviceId, DocumentVersionWithoutContent, VaultId};
use crate::{config::server_config::ServerConfig, errors::server_error};
#[derive(Debug, Clone)]
pub struct Broadcasts {
max_clients_per_vault: usize,
tx: Arc<Mutex<HashMap<VaultId, broadcast::Sender<DocumentVersionWithoutContent>>>>,
tx: Arc<Mutex<HashMap<VaultId, broadcast::Sender<VaultUpdate>>>>,
}
#[derive(Debug, Clone)]
pub struct VaultUpdate {
pub origin_device_id: Option<DeviceId>,
pub document: DocumentVersionWithoutContent,
}
impl Broadcasts {
@ -20,10 +26,7 @@ impl Broadcasts {
}
}
pub async fn get_receiver(
&self,
vault: VaultId,
) -> broadcast::Receiver<DocumentVersionWithoutContent> {
pub async fn get_receiver(&self, vault: VaultId) -> broadcast::Receiver<VaultUpdate> {
let tx = self.get_or_create(vault).await;
tx.subscribe()
@ -31,7 +34,7 @@ impl Broadcasts {
/// Sent a document update to all clients subscribed to the vault.
/// We ignore & log failures.
pub async fn send(&self, vault: VaultId, document: DocumentVersionWithoutContent) {
pub async fn send(&self, vault: VaultId, document: VaultUpdate) {
let tx = self.get_or_create(vault).await;
let result = tx
@ -44,10 +47,7 @@ impl Broadcasts {
}
}
async fn get_or_create(
&self,
vault: VaultId,
) -> broadcast::Sender<DocumentVersionWithoutContent> {
async fn get_or_create(&self, vault: VaultId) -> broadcast::Sender<VaultUpdate> {
let mut tx = self.tx.lock().await;
tx.entry(vault)

View file

@ -6,6 +6,7 @@ use sync_lib::bytes_to_base64;
pub type VaultId = String;
pub type VaultUpdateId = i64;
pub type DocumentId = uuid::Uuid;
pub type DeviceId = String;
#[derive(Debug, Clone)]
pub struct StoredDocumentVersion {