diff --git a/Cargo.lock b/Cargo.lock index cf640848..80b4dcbf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -579,6 +579,7 @@ dependencies = [ "collab-rt-entity", "collab-rt-protocol", "collab-stream", + "collab-user", "dashmap", "database", "database-entity", @@ -1954,6 +1955,7 @@ dependencies = [ "gotrue-entity", "infra", "shared-entity", + "uuid", ] [[package]] @@ -1969,6 +1971,7 @@ dependencies = [ "collab-document", "collab-entity", "collab-folder", + "collab-user", "database-entity", "dotenvy", "futures", @@ -2038,7 +2041,7 @@ dependencies = [ [[package]] name = "collab" version = "0.2.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=46b9594#46b9594a20d93ebe476b5c3b183862882f296c3f" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2ba00c1e430f6157a2b6cbda89992d3b154ea6fb#2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" dependencies = [ "anyhow", "arc-swap", @@ -2063,7 +2066,7 @@ dependencies = [ [[package]] name = "collab-document" version = "0.2.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=46b9594#46b9594a20d93ebe476b5c3b183862882f296c3f" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2ba00c1e430f6157a2b6cbda89992d3b154ea6fb#2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" dependencies = [ "anyhow", "arc-swap", @@ -2083,7 +2086,7 @@ dependencies = [ [[package]] name = "collab-entity" version = "0.2.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=46b9594#46b9594a20d93ebe476b5c3b183862882f296c3f" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2ba00c1e430f6157a2b6cbda89992d3b154ea6fb#2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" dependencies = [ "anyhow", "bytes", @@ -2102,7 +2105,7 @@ dependencies = [ [[package]] name = "collab-folder" version = "0.2.0" -source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=46b9594#46b9594a20d93ebe476b5c3b183862882f296c3f" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2ba00c1e430f6157a2b6cbda89992d3b154ea6fb#2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" dependencies = [ "anyhow", "arc-swap", @@ -2183,6 +2186,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "collab-user" +version = "0.2.0" +source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2ba00c1e430f6157a2b6cbda89992d3b154ea6fb#2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" +dependencies = [ + "anyhow", + "collab", + "collab-entity", + "getrandom 0.2.15", + "serde", + "serde_json", + "tokio", + "tokio-stream", + "tracing", +] + [[package]] name = "color_quant" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 740d612e..8342cf6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -114,6 +114,7 @@ collab = { workspace = true } collab-document = { workspace = true } collab-entity = { workspace = true } collab-folder = { workspace = true } +collab-user = { workspace = true } collab-rt-protocol.workspace = true #Local crate @@ -230,7 +231,7 @@ serde_repr = "0.1.18" serde = { version = "1.0.195", features = ["derive"] } bytes = "1.5.0" workspace-template = { path = "libs/workspace-template" } -uuid = { version = "1.6.1", features = ["v4"] } +uuid = { version = "1.6.1", features = ["v4", "v5"] } anyhow = "1.0.79" actix = "0.13.3" actix-web = { version = "4.5.1", default-features = false, features = [ @@ -267,6 +268,7 @@ collab = { version = "0.2.0" } collab-entity = { version = "0.2.0" } collab-folder = { version = "0.2.0" } collab-document = { version = "0.2.0" } +collab-user = { version = "0.2.0" } [profile.release] lto = true @@ -280,10 +282,11 @@ debug = true [patch.crates-io] # It's diffcult to resovle different version with the same crate used in AppFlowy Frontend and the Client-API crate. # So using patch to workaround this issue. -collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "46b9594" } -collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "46b9594" } -collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "46b9594" } -collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "46b9594" } +collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" } +collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" } +collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" } +collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" } +collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2ba00c1e430f6157a2b6cbda89992d3b154ea6fb" } [features] history = [] diff --git a/libs/client-api-entity/Cargo.toml b/libs/client-api-entity/Cargo.toml index c4c50173..bf748283 100644 --- a/libs/client-api-entity/Cargo.toml +++ b/libs/client-api-entity/Cargo.toml @@ -11,6 +11,7 @@ gotrue-entity = { workspace = true } shared-entity = { workspace = true } collab-rt-entity = { workspace = true } database-entity.workspace = true +uuid.workspace = true infra = { workspace = true, optional = true } diff --git a/libs/client-api-entity/src/id.rs b/libs/client-api-entity/src/id.rs new file mode 100644 index 00000000..1f50373a --- /dev/null +++ b/libs/client-api-entity/src/id.rs @@ -0,0 +1,8 @@ +use uuid::Uuid; + +pub fn user_awareness_object_id(user_uuid: &Uuid, workspace_id: &str) -> Uuid { + Uuid::new_v5( + user_uuid, + format!("user_awareness:{}", workspace_id).as_bytes(), + ) +} diff --git a/libs/client-api-entity/src/lib.rs b/libs/client-api-entity/src/lib.rs index 4d2fd53c..c7cb3ac3 100644 --- a/libs/client-api-entity/src/lib.rs +++ b/libs/client-api-entity/src/lib.rs @@ -1,3 +1,5 @@ +pub mod id; + pub use collab_entity::*; pub use collab_rt_entity::user::*; pub use database_entity::dto::*; diff --git a/libs/client-api-test/Cargo.toml b/libs/client-api-test/Cargo.toml index 7f95b817..00f70e2d 100644 --- a/libs/client-api-test/Cargo.toml +++ b/libs/client-api-test/Cargo.toml @@ -15,6 +15,7 @@ tracing.workspace = true collab-folder.workspace = true collab = { workspace = true } collab-document.workspace = true +collab-user.workspace = true client-api = { path = "../client-api", features = ["test_util"] } once_cell = "1.19.0" tempfile = "3.9.0" diff --git a/libs/client-api-test/src/test_client.rs b/libs/client-api-test/src/test_client.rs index 5aadb626..3e1637af 100644 --- a/libs/client-api-test/src/test_client.rs +++ b/libs/client-api-test/src/test_client.rs @@ -16,6 +16,7 @@ use collab::entity::EncodedCollab; use collab::preclude::{Collab, Prelim}; use collab_entity::CollabType; use collab_folder::Folder; +use collab_user::core::UserAwareness; use mime::Mime; use serde::Deserialize; use serde_json::{json, Value}; @@ -27,12 +28,13 @@ use uuid::Uuid; #[cfg(feature = "collab-sync")] use client_api::collab_sync::{SinkConfig, SyncObject, SyncPlugin}; +use client_api::entity::id::user_awareness_object_id; use client_api::entity::QueryWorkspaceMember; use client_api::ws::{WSClient, WSClientConfig}; use database_entity::dto::{ - AFAccessLevel, AFRole, AFSnapshotMeta, AFSnapshotMetas, AFUserWorkspaceInfo, AFWorkspace, - AFWorkspaceInvitationStatus, AFWorkspaceMember, BatchQueryCollabResult, CollabParams, - CreateCollabParams, InsertCollabMemberParams, QueryCollab, QueryCollabParams, + AFAccessLevel, AFRole, AFSnapshotMeta, AFSnapshotMetas, AFUserProfile, AFUserWorkspaceInfo, + AFWorkspace, AFWorkspaceInvitationStatus, AFWorkspaceMember, BatchQueryCollabResult, + CollabParams, CreateCollabParams, InsertCollabMemberParams, QueryCollab, QueryCollabParams, QuerySnapshotParams, SnapshotData, UpdateCollabMemberParams, }; use shared_entity::dto::workspace_dto::{ @@ -188,6 +190,31 @@ impl TestClient { .unwrap() } + pub async fn get_user_awareness(&self) -> UserAwareness { + let workspace_id = self.workspace_id().await; + let profile = self.get_user_profile().await; + let awareness_object_id = user_awareness_object_id(&profile.uuid, &workspace_id).to_string(); + let data = self + .api_client + .get_collab(QueryCollabParams::new( + &awareness_object_id, + CollabType::UserAwareness, + &workspace_id, + )) + .await + .unwrap(); + let collab = Collab::new_with_source( + CollabOrigin::Empty, + &awareness_object_id, + DataSource::DocStateV1(data.encode_collab.doc_state.to_vec()), + vec![], + false, + ) + .unwrap(); + + UserAwareness::open(collab, None) + } + pub async fn try_update_workspace_member( &self, workspace_id: &str, @@ -405,6 +432,10 @@ impl TestClient { self.api_client.get_profile().await.unwrap().uid } + pub async fn get_user_profile(&self) -> AFUserProfile { + self.api_client.get_profile().await.unwrap() + } + pub async fn get_snapshot( &self, workspace_id: &str, diff --git a/services/appflowy-collaborate/src/indexer/document_indexer.rs b/services/appflowy-collaborate/src/indexer/document_indexer.rs index c83a7a2e..b904cc51 100644 --- a/services/appflowy-collaborate/src/indexer/document_indexer.rs +++ b/services/appflowy-collaborate/src/indexer/document_indexer.rs @@ -65,7 +65,9 @@ impl Indexer for DocumentIndexer { let mut params = match Self::get_document_contents(&document) { Ok(result) => result, Err(err) => { - tracing::warn!("failed to get document data: {}", err); + if cfg!(debug_assertions) { + tracing::warn!("failed to get document:{} error:{}", object_id, err); + } return Ok(None); }, }; diff --git a/src/biz/user/user_init.rs b/src/biz/user/user_init.rs index 141292bb..03d503ea 100644 --- a/src/biz/user/user_init.rs +++ b/src/biz/user/user_init.rs @@ -1,17 +1,18 @@ use std::sync::Arc; +use app_error::AppError; +use appflowy_collaborate::collab::storage::CollabAccessControlStorage; use collab::core::origin::CollabOrigin; use collab::preclude::{ArrayPrelim, Collab, Map}; use collab_entity::define::WORKSPACE_DATABASES; use collab_entity::CollabType; -use sqlx::Transaction; -use tracing::{debug, instrument}; - -use app_error::AppError; -use appflowy_collaborate::collab::storage::CollabAccessControlStorage; +use collab_user::core::UserAwareness; use database::collab::CollabStorage; use database::pg_row::AFWorkspaceRow; use database_entity::dto::CollabParams; +use sqlx::Transaction; +use tracing::{debug, error, instrument, trace}; +use uuid::Uuid; use workspace_template::{WorkspaceTemplate, WorkspaceTemplateBuilder}; /// This function generates templates for a workspace and stores them in the database. @@ -19,6 +20,7 @@ use workspace_template::{WorkspaceTemplate, WorkspaceTemplateBuilder}; #[instrument(level = "debug", skip_all, err)] pub async fn initialize_workspace_for_user( uid: i64, + user_uuid: &Uuid, row: &AFWorkspaceRow, txn: &mut Transaction<'_, sqlx::Postgres>, templates: Vec, @@ -45,6 +47,16 @@ where txn, ) .await?; + + match create_user_awareness(&uid, user_uuid, &workspace_id, collab_storage, txn).await { + Ok(object_id) => trace!("User awareness created successfully: {}", object_id), + Err(err) => { + error!( + "Failed to create user awareness for workspace: {}, {}", + workspace_id, err + ); + }, + } } else { return Err(AppError::Internal(anyhow::anyhow!( "Workspace database object id is missing" @@ -76,6 +88,42 @@ where Ok(()) } +async fn create_user_awareness( + uid: &i64, + user_uuid: &Uuid, + workspace_id: &str, + storage: &Arc, + txn: &mut Transaction<'_, sqlx::Postgres>, +) -> Result { + let object_id = user_awareness_object_id(user_uuid, workspace_id).to_string(); + let collab_type = CollabType::UserAwareness; + let collab = Collab::new_with_origin(CollabOrigin::Empty, object_id.clone(), vec![], false); + + // TODO(nathan): Maybe using hardcode encoded collab + let user_awareness = UserAwareness::open(collab, None); + let encode_collab = user_awareness + .encode_collab_v1(|collab| collab_type.validate_require_data(collab)) + .map_err(AppError::Internal)?; + let encoded_collab_v1 = encode_collab + .encode_to_bytes() + .map_err(|err| AppError::Internal(anyhow::Error::from(err)))?; + + storage + .insert_new_collab_with_transaction( + workspace_id, + uid, + CollabParams { + object_id: object_id.to_string(), + encoded_collab_v1, + collab_type, + embeddings: None, + }, + txn, + ) + .await?; + Ok(object_id) +} + async fn create_workspace_database_collab( workspace_id: &str, uid: &i64, @@ -86,7 +134,7 @@ async fn create_workspace_database_collab( let collab_type = CollabType::WorkspaceDatabase; let mut collab = Collab::new_with_origin(CollabOrigin::Empty, object_id, vec![], false); { - let mut txn = collab.context.transact_mut(); + let mut txn = collab.transact_mut(); collab .data .insert(&mut txn, WORKSPACE_DATABASES, ArrayPrelim::default()); @@ -116,3 +164,10 @@ async fn create_workspace_database_collab( Ok(()) } + +pub fn user_awareness_object_id(user_uuid: &Uuid, workspace_id: &str) -> Uuid { + Uuid::new_v5( + user_uuid, + format!("user_awareness:{}", workspace_id).as_bytes(), + ) +} diff --git a/src/biz/user/user_verify.rs b/src/biz/user/user_verify.rs index 6107a12e..e1f79ec9 100644 --- a/src/biz/user/user_verify.rs +++ b/src/biz/user/user_verify.rs @@ -46,6 +46,7 @@ pub async fn verify_token(access_token: &str, state: &AppState) -> Result