From b3ab6da5c05ddcd87dbd002bc7ed3adb26af2e5b Mon Sep 17 00:00:00 2001 From: khorshuheng Date: Sat, 18 Jan 2025 17:38:01 +0800 Subject: [PATCH] chore: remove collab member CRUD --- ...624f2ffc9c958ed3d06b26162eb1c249c62e9.json | 16 -- ...3ddbb1b566b6c064d8d397a4e073433a86c11.json | 23 --- ...c252bedb826c473bf94d9f3945c1b15327acb.json | 22 --- libs/client-api/src/http_member.rs | 103 +---------- libs/database-entity/src/dto.rs | 31 ---- libs/database/src/collab/collab_db_ops.rs | 160 +----------------- src/api/workspace.rs | 106 ------------ src/biz/collab/ops.rs | 137 +-------------- src/biz/workspace/ops.rs | 13 +- tests/collab/member_crud.rs | 155 ----------------- tests/collab/mod.rs | 1 - 11 files changed, 7 insertions(+), 760 deletions(-) delete mode 100644 .sqlx/query-19fd96e0183b93b1751440a2562624f2ffc9c958ed3d06b26162eb1c249c62e9.json delete mode 100644 .sqlx/query-4fffbac63c56402626b50f8f50e3ddbb1b566b6c064d8d397a4e073433a86c11.json delete mode 100644 .sqlx/query-5752d8de85c68896b5d85d52941c252bedb826c473bf94d9f3945c1b15327acb.json delete mode 100644 tests/collab/member_crud.rs diff --git a/.sqlx/query-19fd96e0183b93b1751440a2562624f2ffc9c958ed3d06b26162eb1c249c62e9.json b/.sqlx/query-19fd96e0183b93b1751440a2562624f2ffc9c958ed3d06b26162eb1c249c62e9.json deleted file mode 100644 index 94bb8dc19..000000000 --- a/.sqlx/query-19fd96e0183b93b1751440a2562624f2ffc9c958ed3d06b26162eb1c249c62e9.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n INSERT INTO af_collab_member (uid, oid, permission_id)\n VALUES ($1, $2, $3)\n ON CONFLICT (uid, oid)\n DO UPDATE\n SET permission_id = excluded.permission_id;\n ", - "describe": { - "columns": [], - "parameters": { - "Left": [ - "Int8", - "Text", - "Int4" - ] - }, - "nullable": [] - }, - "hash": "19fd96e0183b93b1751440a2562624f2ffc9c958ed3d06b26162eb1c249c62e9" -} diff --git a/.sqlx/query-4fffbac63c56402626b50f8f50e3ddbb1b566b6c064d8d397a4e073433a86c11.json b/.sqlx/query-4fffbac63c56402626b50f8f50e3ddbb1b566b6c064d8d397a4e073433a86c11.json deleted file mode 100644 index e719ed770..000000000 --- a/.sqlx/query-4fffbac63c56402626b50f8f50e3ddbb1b566b6c064d8d397a4e073433a86c11.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT EXISTS (SELECT 1 FROM af_collab_member WHERE oid = $1 AND uid = $2 LIMIT 1)\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "exists", - "type_info": "Bool" - } - ], - "parameters": { - "Left": [ - "Text", - "Int8" - ] - }, - "nullable": [ - null - ] - }, - "hash": "4fffbac63c56402626b50f8f50e3ddbb1b566b6c064d8d397a4e073433a86c11" -} diff --git a/.sqlx/query-5752d8de85c68896b5d85d52941c252bedb826c473bf94d9f3945c1b15327acb.json b/.sqlx/query-5752d8de85c68896b5d85d52941c252bedb826c473bf94d9f3945c1b15327acb.json deleted file mode 100644 index 7e3a2651e..000000000 --- a/.sqlx/query-5752d8de85c68896b5d85d52941c252bedb826c473bf94d9f3945c1b15327acb.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n SELECT id\n FROM af_permissions\n WHERE access_level = $1\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int4" - } - ], - "parameters": { - "Left": [ - "Int4" - ] - }, - "nullable": [ - false - ] - }, - "hash": "5752d8de85c68896b5d85d52941c252bedb826c473bf94d9f3945c1b15327acb" -} diff --git a/libs/client-api/src/http_member.rs b/libs/client-api/src/http_member.rs index e07d28bf1..a4d8faceb 100644 --- a/libs/client-api/src/http_member.rs +++ b/libs/client-api/src/http_member.rs @@ -1,9 +1,7 @@ use crate::http::log_request_id; use crate::Client; use client_api_entity::{ - AFCollabMember, AFCollabMembers, AFWorkspaceInvitation, AFWorkspaceInvitationStatus, - AFWorkspaceMember, InsertCollabMemberParams, QueryCollabMembers, QueryWorkspaceMember, - UpdateCollabMemberParams, WorkspaceCollabIdentify, + AFWorkspaceInvitation, AFWorkspaceInvitationStatus, AFWorkspaceMember, QueryWorkspaceMember, }; use reqwest::Method; use shared_entity::dto::workspace_dto::{ @@ -183,105 +181,6 @@ impl Client { Ok(()) } - #[instrument(level = "info", skip_all, err)] - pub async fn add_collab_member( - &self, - params: InsertCollabMemberParams, - ) -> Result<(), AppResponseError> { - let url = format!( - "{}/api/workspace/{}/collab/{}/member", - self.base_url, params.workspace_id, ¶ms.object_id - ); - let resp = self - .http_client_with_auth(Method::POST, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::<()>::from_response(resp).await?.into_error() - } - - #[instrument(level = "info", skip_all, err)] - pub async fn get_collab_member( - &self, - params: WorkspaceCollabIdentify, - ) -> Result { - let url = format!( - "{}/api/workspace/{}/collab/{}/member", - self.base_url, params.workspace_id, ¶ms.object_id - ); - let resp = self - .http_client_with_auth(Method::GET, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::::from_response(resp) - .await? - .into_data() - } - - #[instrument(level = "info", skip_all, err)] - pub async fn update_collab_member( - &self, - params: UpdateCollabMemberParams, - ) -> Result<(), AppResponseError> { - let url = format!( - "{}/api/workspace/{}/collab/{}/member", - self.base_url, params.workspace_id, ¶ms.object_id - ); - let resp = self - .http_client_with_auth(Method::PUT, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::<()>::from_response(resp).await?.into_error() - } - - #[instrument(level = "info", skip_all, err)] - pub async fn remove_collab_member( - &self, - params: WorkspaceCollabIdentify, - ) -> Result<(), AppResponseError> { - let url = format!( - "{}/api/workspace/{}/collab/{}/member", - self.base_url, params.workspace_id, ¶ms.object_id - ); - let resp = self - .http_client_with_auth(Method::DELETE, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::<()>::from_response(resp).await?.into_error() - } - - #[instrument(level = "info", skip_all, err)] - pub async fn get_collab_members( - &self, - params: QueryCollabMembers, - ) -> Result { - let url = format!( - "{}/api/workspace/{}/collab/{}/member/list", - self.base_url, params.workspace_id, ¶ms.object_id - ); - let resp = self - .http_client_with_auth(Method::GET, &url) - .await? - .json(¶ms) - .send() - .await?; - log_request_id(&resp); - AppResponse::::from_response(resp) - .await? - .into_data() - } - #[instrument(level = "info", skip_all, err)] pub async fn get_workspace_member( &self, diff --git a/libs/database-entity/src/dto.rs b/libs/database-entity/src/dto.rs index e08aa347a..7c792fbef 100644 --- a/libs/database-entity/src/dto.rs +++ b/libs/database-entity/src/dto.rs @@ -353,27 +353,6 @@ pub struct WorkspaceUsage { pub total_document_size: i64, } -#[derive(Debug, Clone, Validate, Serialize, Deserialize)] -pub struct InsertCollabMemberParams { - pub uid: i64, - #[validate(custom(function = "validate_not_empty_str"))] - pub workspace_id: String, - #[validate(custom(function = "validate_not_empty_str"))] - pub object_id: String, - pub access_level: AFAccessLevel, -} - -pub type UpdateCollabMemberParams = InsertCollabMemberParams; - -#[derive(Debug, Clone, Validate, Serialize, Deserialize)] -pub struct WorkspaceCollabIdentify { - pub uid: i64, - #[validate(custom(function = "validate_not_empty_str"))] - pub workspace_id: String, - #[validate(custom(function = "validate_not_empty_str"))] - pub object_id: String, -} - #[derive(Serialize, Deserialize)] pub struct UpdatePublishNamespace { pub old_namespace: String, @@ -407,13 +386,6 @@ pub struct QueryWorkspaceMember { pub uid: i64, } -#[derive(Serialize, Deserialize)] -pub struct AFCollabMember { - pub uid: i64, - pub oid: String, - pub permission: AFPermission, -} - #[derive(Debug, Serialize, Deserialize)] pub struct AFCollabEmbedInfo { pub object_id: String, @@ -609,9 +581,6 @@ impl Ord for AFAccessLevel { } } -#[derive(Serialize, Deserialize)] -pub struct AFCollabMembers(pub Vec); - pub type RawData = Vec; #[derive(Serialize, Deserialize)] diff --git a/libs/database/src/collab/collab_db_ops.rs b/libs/database/src/collab/collab_db_ops.rs index 6be3d3315..1349f6fa5 100644 --- a/libs/database/src/collab/collab_db_ops.rs +++ b/libs/database/src/collab/collab_db_ops.rs @@ -1,8 +1,8 @@ use anyhow::{anyhow, Context}; use collab_entity::CollabType; use database_entity::dto::{ - AFAccessLevel, AFCollabEmbedInfo, AFCollabMember, AFPermission, AFSnapshotMeta, AFSnapshotMetas, - CollabParams, QueryCollab, QueryCollabResult, RawData, RepeatedAFCollabEmbedInfo, + AFCollabEmbedInfo, AFSnapshotMeta, AFSnapshotMetas, CollabParams, QueryCollab, QueryCollabResult, + RawData, RepeatedAFCollabEmbedInfo, }; use shared_entity::dto::workspace_dto::{DatabaseRowUpdatedItem, EmbeddedCollabQuery}; @@ -12,8 +12,7 @@ use crate::pg_row::AFSnapshotRow; use app_error::AppError; use chrono::{DateTime, Duration, Utc}; -use sqlx::postgres::PgRow; -use sqlx::{Error, Executor, PgPool, Postgres, Row, Transaction}; +use sqlx::{Error, Executor, PgPool, Postgres, Transaction}; use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::{ops::DerefMut, str::FromStr}; @@ -487,159 +486,6 @@ pub async fn get_all_collab_snapshot_meta( Ok(AFSnapshotMetas(snapshots)) } -#[inline] -#[instrument(level = "trace", skip(txn), err)] -pub async fn upsert_collab_member_with_txn + Debug>( - uid: i64, - oid: T, - access_level: &AFAccessLevel, - txn: &mut Transaction<'_, sqlx::Postgres>, -) -> Result<(), AppError> { - let oid = oid.as_ref(); - let access_level: i32 = (*access_level).into(); - let permission_id = sqlx::query_scalar!( - r#" - SELECT id - FROM af_permissions - WHERE access_level = $1 - "#, - access_level - ) - .fetch_one(txn.deref_mut()) - .await - .context("Get permission id from access level fail")?; - - sqlx::query!( - r#" - INSERT INTO af_collab_member (uid, oid, permission_id) - VALUES ($1, $2, $3) - ON CONFLICT (uid, oid) - DO UPDATE - SET permission_id = excluded.permission_id; - "#, - uid, - oid, - permission_id - ) - .execute(txn.deref_mut()) - .await - .context(format!( - "failed to insert collab member: user:{} oid:{}", - uid, oid - ))?; - - Ok(()) -} - -#[instrument(skip(txn), err)] -#[inline] -pub async fn insert_collab_member( - uid: i64, - oid: &str, - access_level: &AFAccessLevel, - txn: &mut Transaction<'_, sqlx::Postgres>, -) -> Result<(), AppError> { - upsert_collab_member_with_txn(uid, oid, access_level, txn).await?; - Ok(()) -} - -pub async fn delete_collab_member( - uid: i64, - oid: &str, - txn: &mut Transaction<'_, sqlx::Postgres>, -) -> Result<(), AppError> { - sqlx::query("DELETE FROM af_collab_member WHERE uid = $1 AND oid = $2") - .bind(uid) - .bind(oid) - .execute(txn.deref_mut()) - .await?; - Ok(()) -} - -#[inline] -pub async fn select_collab_members( - oid: &str, - pg_pool: &PgPool, -) -> Result, AppError> { - let members = sqlx::query( - r#" - SELECT af_collab_member.uid, - af_collab_member.oid, - af_permissions.id, - af_permissions.name, - af_permissions.access_level, - af_permissions.description - FROM af_collab_member - JOIN af_permissions ON af_collab_member.permission_id = af_permissions.id - WHERE af_collab_member.oid = $1 - ORDER BY af_collab_member.created_at ASC - "#, - ) - .bind(oid) - .try_map(collab_member_try_from_row) - .fetch_all(pg_pool) - .await?; - - Ok(members) -} - -#[inline] -pub async fn select_collab_member<'a, E: Executor<'a, Database = Postgres>>( - uid: &i64, - oid: &str, - executor: E, -) -> Result { - let row = sqlx::query( - r#" - SELECT af_collab_member.uid, af_collab_member.oid, af_permissions.id, af_permissions.name, af_permissions.access_level, af_permissions.description - FROM af_collab_member - JOIN af_permissions ON af_collab_member.permission_id = af_permissions.id - WHERE af_collab_member.uid = $1 AND af_collab_member.oid = $2 - "#, - ) - .bind(uid) - .bind(oid) - .fetch_one(executor) - .await?; - - let member = collab_member_try_from_row(row)?; - Ok(member) -} - -fn collab_member_try_from_row(row: PgRow) -> Result { - let access_level = AFAccessLevel::from(row.try_get::(4)?); - let permission = AFPermission { - id: row.try_get(2)?, - name: row.try_get(3)?, - access_level, - description: row.try_get(5)?, - }; - - Ok(AFCollabMember { - uid: row.try_get(0)?, - oid: row.try_get(1)?, - permission, - }) -} - -#[inline] -pub async fn is_collab_member_exists<'a, E: Executor<'a, Database = Postgres>>( - uid: i64, - oid: &str, - executor: E, -) -> Result { - let result = sqlx::query_scalar!( - r#" - SELECT EXISTS (SELECT 1 FROM af_collab_member WHERE oid = $1 AND uid = $2 LIMIT 1) - "#, - &oid, - &uid, - ) - .fetch_one(executor) - .await; - transform_record_not_found_error(result) -} - #[inline] fn transform_record_not_found_error( result: Result, sqlx::Error>, diff --git a/src/api/workspace.rs b/src/api/workspace.rs index a3b2dbdb1..9250c3fff 100644 --- a/src/api/workspace.rs +++ b/src/api/workspace.rs @@ -143,13 +143,6 @@ pub fn workspace_scope() -> Scope { web::resource("/v1/{workspace_id}/collab/{object_id}/web-update") .route(web::post().to(post_web_update_handler)), ) - .service( - web::resource("/{workspace_id}/collab/{object_id}/member") - .route(web::post().to(add_collab_member_handler)) - .route(web::get().to(get_collab_member_handler)) - .route(web::put().to(update_collab_member_handler)) - .route(web::delete().to(remove_collab_member_handler)), - ) .service( web::resource("/{workspace_id}/collab/{object_id}/embed-info") .route(web::get().to(get_collab_embed_info_handler)), @@ -288,10 +281,6 @@ pub fn workspace_scope() -> Scope { web::resource("/published-outline/{publish_namespace}") .route(web::get().to(get_workspace_publish_outline_handler)), ) - .service( - web::resource("/{workspace_id}/collab/{object_id}/member/list") - .route(web::get().to(get_collab_member_list_handler)), - ) .service( web::resource("/{workspace_id}/collab_list") .route(web::get().to(batch_get_collab_handler)) @@ -1577,91 +1566,6 @@ async fn delete_collab_handler( Ok(AppResponse::Ok().into()) } -#[instrument(level = "debug", skip(state, payload), err)] -async fn add_collab_member_handler( - payload: Json, - state: Data, -) -> Result>> { - let payload = payload.into_inner(); - if !state - .collab_cache - .is_exist(&payload.workspace_id, &payload.object_id) - .await? - { - return Err( - AppError::RecordNotFound(format!( - "Fail to insert collab member. The Collab with object_id {} does not exist", - payload.object_id - )) - .into(), - ); - } - - biz::collab::ops::create_collab_member( - &state.pg_pool, - &payload, - state.collab_access_control.clone(), - ) - .await?; - Ok(Json(AppResponse::Ok())) -} - -#[instrument(level = "debug", skip(state, payload), err)] -async fn update_collab_member_handler( - user_uuid: UserUuid, - payload: Json, - state: Data, -) -> Result>> { - let payload = payload.into_inner(); - - if !state - .collab_cache - .is_exist(&payload.workspace_id, &payload.object_id) - .await? - { - return Err( - AppError::RecordNotFound(format!( - "Fail to update collab member. The Collab with object_id {} does not exist", - payload.object_id - )) - .into(), - ); - } - biz::collab::ops::upsert_collab_member( - &state.pg_pool, - &user_uuid, - &payload, - state.collab_access_control.clone(), - ) - .await?; - Ok(Json(AppResponse::Ok())) -} -#[instrument(level = "debug", skip(state, payload), err)] -async fn get_collab_member_handler( - payload: Json, - state: Data, -) -> Result>> { - let payload = payload.into_inner(); - let member = biz::collab::ops::get_collab_member(&state.pg_pool, &payload).await?; - Ok(Json(AppResponse::Ok().with_data(member))) -} - -#[instrument(skip(state, payload), err)] -async fn remove_collab_member_handler( - payload: Json, - state: Data, -) -> Result>> { - let payload = payload.into_inner(); - biz::collab::ops::delete_collab_member( - &state.pg_pool, - &payload, - state.collab_access_control.clone(), - ) - .await?; - - Ok(Json(AppResponse::Ok())) -} - async fn put_workspace_default_published_view_handler( user_uuid: UserUuid, workspace_id: web::Path, @@ -2038,16 +1942,6 @@ async fn delete_published_collabs_handler( Ok(Json(AppResponse::Ok())) } -#[instrument(level = "debug", skip(state, payload), err)] -async fn get_collab_member_list_handler( - payload: Json, - state: Data, -) -> Result>> { - let members = - biz::collab::ops::get_collab_member_list(&state.pg_pool, &payload.into_inner()).await?; - Ok(Json(AppResponse::Ok().with_data(AFCollabMembers(members)))) -} - #[instrument(level = "info", skip_all, err)] async fn post_realtime_message_stream_handler( user_uuid: UserUuid, diff --git a/src/biz/collab/ops.rs b/src/biz/collab/ops.rs index 4e11ce84d..6ef739b02 100644 --- a/src/biz/collab/ops.rs +++ b/src/biz/collab/ops.rs @@ -36,9 +36,9 @@ use database::collab::{CollabStorage, GetCollabOrigin}; use database::publish::select_published_view_ids_for_workspace; use database::publish::select_published_view_ids_with_publish_info_for_workspace; use database::publish::select_workspace_id_for_publish_namespace; +use database_entity::dto::CollabParams; use database_entity::dto::QueryCollab; use database_entity::dto::QueryCollabResult; -use database_entity::dto::{CollabParams, WorkspaceCollabIdentify}; use shared_entity::dto::workspace_dto::AFDatabase; use shared_entity::dto::workspace_dto::AFDatabaseField; use shared_entity::dto::workspace_dto::AFDatabaseRow; @@ -51,7 +51,6 @@ use shared_entity::dto::workspace_dto::PublishedViewInfo; use shared_entity::dto::workspace_dto::RecentFolderView; use shared_entity::dto::workspace_dto::TrashFolderView; use sqlx::PgPool; -use std::ops::DerefMut; use yrs::Map; use crate::api::metrics::AppFlowyWebMetrics; @@ -60,16 +59,9 @@ use crate::biz::collab::folder_view::check_if_view_is_space; use crate::biz::collab::utils::get_database_row_doc_changes; use crate::biz::workspace::ops::broadcast_update_with_timeout; use crate::biz::workspace::page_view::update_workspace_folder_data; -use access_control::collab::CollabAccessControl; -use anyhow::Context; -use database_entity::dto::{ - AFCollabMember, InsertCollabMemberParams, QueryCollabMembers, UpdateCollabMemberParams, -}; use shared_entity::dto::workspace_dto::{FolderView, PublishedView}; use sqlx::types::Uuid; use std::collections::HashSet; -use tracing::{event, trace}; -use validator::Validate; use super::folder_view::collab_folder_to_folder_view; use super::folder_view::section_items_to_favorite_folder_view; @@ -93,133 +85,6 @@ use super::utils::DocChanges; use super::utils::DEFAULT_SPACE_ICON; use super::utils::DEFAULT_SPACE_ICON_COLOR; -/// Create a new collab member -/// If the collab member already exists, return [AppError::RecordAlreadyExists] -/// If the collab member does not exist, create a new one -pub async fn create_collab_member( - pg_pool: &PgPool, - params: &InsertCollabMemberParams, - collab_access_control: Arc, -) -> Result<(), AppError> { - params.validate()?; - - let mut transaction = pg_pool - .begin() - .await - .context("acquire transaction to insert collab member")?; - - if database::collab::is_collab_member_exists( - params.uid, - ¶ms.object_id, - transaction.deref_mut(), - ) - .await? - { - return Err(AppError::RecordAlreadyExists(format!( - "Collab member with uid {} and object_id {} already exists", - params.uid, params.object_id - ))); - } - - trace!("Inserting collab member: {:?}", params); - database::collab::insert_collab_member( - params.uid, - ¶ms.object_id, - ¶ms.access_level, - &mut transaction, - ) - .await?; - - collab_access_control - .update_access_level_policy(¶ms.uid, ¶ms.object_id, params.access_level) - .await?; - - transaction - .commit() - .await - .context("fail to commit the transaction to insert collab member")?; - Ok(()) -} - -pub async fn upsert_collab_member( - pg_pool: &PgPool, - _user_uuid: &Uuid, - params: &UpdateCollabMemberParams, - collab_access_control: Arc, -) -> Result<(), AppError> { - params.validate()?; - let mut transaction = pg_pool - .begin() - .await - .context("acquire transaction to upsert collab member")?; - - collab_access_control - .update_access_level_policy(¶ms.uid, ¶ms.object_id, params.access_level) - .await?; - - database::collab::insert_collab_member( - params.uid, - ¶ms.object_id, - ¶ms.access_level, - &mut transaction, - ) - .await?; - - transaction - .commit() - .await - .context("fail to commit the transaction to upsert collab member")?; - Ok(()) -} - -pub async fn get_collab_member( - pg_pool: &PgPool, - params: &WorkspaceCollabIdentify, -) -> Result { - params.validate()?; - let collab_member = - database::collab::select_collab_member(¶ms.uid, ¶ms.object_id, pg_pool).await?; - Ok(collab_member) -} - -pub async fn delete_collab_member( - pg_pool: &PgPool, - params: &WorkspaceCollabIdentify, - collab_access_control: Arc, -) -> Result<(), AppError> { - params.validate()?; - let mut transaction = pg_pool - .begin() - .await - .context("acquire transaction to remove collab member")?; - event!( - tracing::Level::DEBUG, - "Deleting member:{} from {}", - params.uid, - params.object_id - ); - database::collab::delete_collab_member(params.uid, ¶ms.object_id, &mut transaction).await?; - - collab_access_control - .remove_access_level(¶ms.uid, ¶ms.object_id) - .await?; - - transaction - .commit() - .await - .context("fail to commit the transaction to remove collab member")?; - Ok(()) -} - -pub async fn get_collab_member_list( - pg_pool: &PgPool, - params: &QueryCollabMembers, -) -> Result, AppError> { - params.validate()?; - let collab_member = database::collab::select_collab_members(¶ms.object_id, pg_pool).await?; - Ok(collab_member) -} - pub async fn get_user_favorite_folder_views( collab_storage: &CollabAccessControlStorage, pg_pool: &PgPool, diff --git a/src/biz/workspace/ops.rs b/src/biz/workspace/ops.rs index e3ad76f2f..33dbc2d32 100644 --- a/src/biz/workspace/ops.rs +++ b/src/biz/workspace/ops.rs @@ -19,15 +19,14 @@ use yrs::updates::encoder::Encode; use access_control::workspace::WorkspaceAccessControl; use app_error::AppError; use appflowy_collaborate::collab::storage::CollabAccessControlStorage; -use database::collab::upsert_collab_member_with_txn; use database::file::s3_client_impl::S3BucketStorage; use database::pg_row::AFWorkspaceMemberRow; use database::user::select_uid_from_email; use database::workspace::*; use database_entity::dto::{ - AFAccessLevel, AFRole, AFWorkspace, AFWorkspaceInvitation, AFWorkspaceInvitationStatus, - AFWorkspaceSettings, GlobalComment, Reaction, WorkspaceUsage, + AFRole, AFWorkspace, AFWorkspaceInvitation, AFWorkspaceInvitationStatus, AFWorkspaceSettings, + GlobalComment, Reaction, WorkspaceUsage, }; use gotrue::params::{GenerateLinkParams, GenerateLinkType}; @@ -532,16 +531,8 @@ pub async fn add_workspace_members_db_only( .context("Begin transaction to insert workspace members")?; for member in members.into_iter() { - let access_level = match &member.role { - AFRole::Owner => AFAccessLevel::FullAccess, - AFRole::Member => AFAccessLevel::ReadAndWrite, - AFRole::Guest => AFAccessLevel::ReadOnly, - }; - - let uid = select_uid_from_email(txn.deref_mut(), &member.email).await?; upsert_workspace_member_with_txn(&mut txn, workspace_id, &member.email, member.role.clone()) .await?; - upsert_collab_member_with_txn(uid, workspace_id.to_string(), &access_level, &mut txn).await?; } txn diff --git a/tests/collab/member_crud.rs b/tests/collab/member_crud.rs deleted file mode 100644 index 410c3a5d6..000000000 --- a/tests/collab/member_crud.rs +++ /dev/null @@ -1,155 +0,0 @@ -use crate::collab::util::test_encode_collab_v1; -use client_api_test::{generate_unique_registered_user_client, workspace_id_from_client}; - -use collab_entity::CollabType; -use database_entity::dto::{ - AFAccessLevel, CreateCollabParams, InsertCollabMemberParams, QueryCollabMembers, - UpdateCollabMemberParams, WorkspaceCollabIdentify, -}; -use uuid::Uuid; - -#[tokio::test] -async fn update_collab_member_permission_test() { - let (c, _user) = generate_unique_registered_user_client().await; - let workspace_id = workspace_id_from_client(&c).await; - let object_id = Uuid::new_v4().to_string(); - let encode_collab = test_encode_collab_v1(&object_id, "title", "hello world") - .encode_to_bytes() - .unwrap(); - let uid = c.get_profile().await.unwrap().uid; - - c.create_collab(CreateCollabParams { - object_id: object_id.clone(), - encoded_collab_v1: encode_collab.clone(), - collab_type: CollabType::Unknown, - workspace_id: workspace_id.clone(), - }) - .await - .unwrap(); - - c.update_collab_member(UpdateCollabMemberParams { - uid, - workspace_id: workspace_id.clone(), - object_id: object_id.clone(), - access_level: AFAccessLevel::ReadOnly, - }) - .await - .unwrap(); - - let member = c - .get_collab_member(WorkspaceCollabIdentify { - uid, - object_id, - workspace_id, - }) - .await - .unwrap(); - - assert_eq!(member.permission.access_level, AFAccessLevel::ReadOnly); -} - -#[tokio::test] -async fn add_collab_member_test() { - let (c_1, _user) = generate_unique_registered_user_client().await; - let workspace_id = workspace_id_from_client(&c_1).await; - let object_id = Uuid::new_v4().to_string(); - let encode_collab = test_encode_collab_v1(&object_id, "title", "hello world"); - c_1 - .create_collab(CreateCollabParams { - object_id: object_id.clone(), - encoded_collab_v1: encode_collab.encode_to_bytes().unwrap(), - collab_type: CollabType::Unknown, - workspace_id: workspace_id.clone(), - }) - .await - .unwrap(); - - // create new client - let (c_2, _user) = generate_unique_registered_user_client().await; - let uid_2 = c_2.get_profile().await.unwrap().uid; - - // add new member - c_1 - .add_collab_member(InsertCollabMemberParams { - uid: uid_2, - workspace_id: workspace_id.clone(), - object_id: object_id.clone(), - access_level: AFAccessLevel::ReadAndComment, - }) - .await - .unwrap(); - - // check the member is added and its permission is correct - let member = c_1 - .get_collab_member(WorkspaceCollabIdentify { - uid: uid_2, - object_id, - workspace_id, - }) - .await - .unwrap(); - assert_eq!( - member.permission.access_level, - AFAccessLevel::ReadAndComment - ); -} - -#[tokio::test] -async fn add_collab_member_then_remove_test() { - let (c_1, _user) = generate_unique_registered_user_client().await; - let workspace_id = workspace_id_from_client(&c_1).await; - let object_id = Uuid::new_v4().to_string(); - let encode_collab = test_encode_collab_v1(&object_id, "title", "hello world"); - c_1 - .create_collab(CreateCollabParams { - object_id: object_id.clone(), - encoded_collab_v1: encode_collab.encode_to_bytes().unwrap(), - collab_type: CollabType::Unknown, - workspace_id: workspace_id.clone(), - }) - .await - .unwrap(); - - // Create new client - let (c_2, _user) = generate_unique_registered_user_client().await; - let uid_2 = c_2.get_profile().await.unwrap().uid; - - // Add new member - c_1 - .add_collab_member(InsertCollabMemberParams { - uid: uid_2, - workspace_id: workspace_id.clone(), - object_id: object_id.clone(), - access_level: AFAccessLevel::ReadAndComment, - }) - .await - .unwrap(); - let members = c_1 - .get_collab_members(QueryCollabMembers { - workspace_id: workspace_id.clone(), - object_id: object_id.clone(), - }) - .await - .unwrap() - .0; - assert_eq!(members.len(), 1); - - // Delete the member - c_1 - .remove_collab_member(WorkspaceCollabIdentify { - uid: uid_2, - object_id: object_id.clone(), - workspace_id: workspace_id.clone(), - }) - .await - .unwrap(); - let members = c_1 - .get_collab_members(QueryCollabMembers { - workspace_id, - object_id, - }) - .await - .unwrap() - .0; - assert_eq!(members.len(), 0); -} diff --git a/tests/collab/mod.rs b/tests/collab/mod.rs index 2223bf81b..0aa64e8d9 100644 --- a/tests/collab/mod.rs +++ b/tests/collab/mod.rs @@ -2,7 +2,6 @@ mod awareness_test; mod collab_curd_test; mod collab_embedding_test; mod database_crud; -mod member_crud; mod missing_update_test; mod multi_devices_edit; mod permission_test;