From 99fc95f33a05387cc5017d41fce780bbc711188a Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Mon, 17 Jun 2024 22:55:04 +0800 Subject: [PATCH] chore: refine api --- libs/client-api/src/http_publish.rs | 73 +++++++++++++++++++++++------ src/biz/workspace/ops.rs | 7 +-- tests/workspace/publish.rs | 54 +++++++++++++-------- 3 files changed, 96 insertions(+), 38 deletions(-) diff --git a/libs/client-api/src/http_publish.rs b/libs/client-api/src/http_publish.rs index d97d75aa..846fd020 100644 --- a/libs/client-api/src/http_publish.rs +++ b/libs/client-api/src/http_publish.rs @@ -1,25 +1,12 @@ use bytes::Bytes; use database_entity::dto::UpdatePublishNamespace; -use reqwest::Method; +use reqwest::{Body, Method}; use shared_entity::response::{AppResponse, AppResponseError}; use crate::Client; +// Publisher API impl Client { - pub async fn get_workspace_publish_namespace( - &self, - workspace_id: &str, - ) -> Result { - let url = format!( - "{}/api/workspace/{}/publish-namespace", - self.base_url, workspace_id - ); - let resp = self.cloud_client.get(&url).send().await?; - AppResponse::::from_response(resp) - .await? - .into_data() - } - pub async fn set_workspace_publish_namespace( &self, workspace_id: &str, @@ -66,6 +53,62 @@ impl Client { AppResponse::<()>::from_response(resp).await?.into_error() } + pub async fn put_published_collab_blob( + &self, + workspace_id: &str, + doc_name: &str, + data: T, + ) -> Result<(), AppResponseError> + where + T: Into, + { + let url = format!( + "{}/api/workspace/{}/publish/{}/blob", + self.base_url, workspace_id, doc_name + ); + let resp = self + .http_client_with_auth(Method::PUT, &url) + .await? + .body(data) + .send() + .await?; + AppResponse::<()>::from_response(resp).await?.into_error() + } + + pub async fn delete_published_collab( + &self, + workspace_id: &str, + doc_name: &str, + ) -> Result<(), AppResponseError> { + let url = format!( + "{}/api/workspace/{}/publish/{}", + self.base_url, workspace_id, doc_name + ); + let resp = self + .http_client_with_auth(Method::DELETE, &url) + .await? + .send() + .await?; + AppResponse::<()>::from_response(resp).await?.into_error() + } +} + +// Guest API (no login required) +impl Client { + pub async fn get_workspace_publish_namespace( + &self, + workspace_id: &str, + ) -> Result { + let url = format!( + "{}/api/workspace/{}/publish-namespace", + self.base_url, workspace_id + ); + let resp = self.cloud_client.get(&url).send().await?; + AppResponse::::from_response(resp) + .await? + .into_data() + } + pub async fn get_published_collab( &self, publish_namespace: &str, diff --git a/src/biz/workspace/ops.rs b/src/biz/workspace/ops.rs index 953711f8..4f53333b 100644 --- a/src/biz/workspace/ops.rs +++ b/src/biz/workspace/ops.rs @@ -157,6 +157,8 @@ pub async fn publish_collab( publisher_uuid: &Uuid, metadata: &serde_json::Value, ) -> Result<(), AppError> { + check_workspace_owner_or_publisher(pg_pool, publisher_uuid, workspace_id, doc_name).await?; + check_collab_doc_name(doc_name).await?; insert_or_replace_publish_collab_meta(pg_pool, workspace_id, doc_name, publisher_uuid, metadata) .await?; Ok(()) @@ -166,11 +168,10 @@ pub async fn put_published_collab_blob( pg_pool: &PgPool, workspace_id: &Uuid, doc_name: &str, - user_uuid: &Uuid, + publisher_uuid: &Uuid, collab_data: &[u8], ) -> Result<(), AppError> { - check_workspace_owner_or_publisher(pg_pool, user_uuid, workspace_id, doc_name).await?; - check_collab_doc_name(doc_name).await?; + check_workspace_owner_or_publisher(pg_pool, publisher_uuid, workspace_id, doc_name).await?; insert_or_replace_published_collab_blob(pg_pool, workspace_id, doc_name, collab_data).await?; Ok(()) } diff --git a/tests/workspace/publish.rs b/tests/workspace/publish.rs index 3d57f177..077c098e 100644 --- a/tests/workspace/publish.rs +++ b/tests/workspace/publish.rs @@ -1,22 +1,21 @@ use client_api_test::{generate_unique_registered_user_client, localhost_client}; -#[tokio::test] -async fn test_get_publish_namespace_not_set() { - let (c, _user) = generate_unique_registered_user_client().await; - let workspace_id = get_first_workspace_string(&c).await; - let err = c - .get_workspace_publish_namespace(&workspace_id.to_string()) - .await - .err() - .unwrap(); - - assert_eq!(format!("{:?}", err.code), "PublishNamespaceNotSet"); -} - #[tokio::test] async fn test_set_publish_namespace_set() { let (c, _user) = generate_unique_registered_user_client().await; let workspace_id = get_first_workspace_string(&c).await; + + { + // cannot get namespace if not set + let err = c + .get_workspace_publish_namespace(&workspace_id.to_string()) + .await + .err() + .unwrap(); + + assert_eq!(format!("{:?}", err.code), "PublishNamespaceNotSet"); + } + let namespace = uuid::Uuid::new_v4().to_string(); c.set_workspace_publish_namespace(&workspace_id.to_string(), &namespace) .await @@ -79,9 +78,10 @@ async fn test_publish_doc() { .await .unwrap(); + let my_doc_name = "my-doc"; c.publish_collab( &workspace_id, - "my_doc", + my_doc_name, Metadata { title: "my_title".to_string(), }, @@ -91,18 +91,32 @@ async fn test_publish_doc() { { // Non login user should be able to view the published collab metadata - let non_login = localhost_client(); - let published_collab = non_login - .get_published_collab::(&my_namespace, "my_doc") + let guest_client = localhost_client(); + let published_collab = guest_client + .get_published_collab::(&my_namespace, my_doc_name) .await .unwrap(); assert_eq!(published_collab.title, "my_title"); - let collab_data = non_login - .get_published_collab_blob(&my_namespace, "my_doc") + let collab_data = guest_client + .get_published_collab_blob(&my_namespace, my_doc_name) .await .unwrap(); - assert!(collab_data.is_empty()); + assert!(collab_data.is_empty()); // empty data because publisher need to set it + } + + c.put_published_collab_blob(&workspace_id, my_doc_name, "some_collab_data") + .await + .unwrap(); + + { + // Non login user should be able to view the published collab data + let guest_client = localhost_client(); + let collab_data = guest_client + .get_published_collab_blob(&my_namespace, my_doc_name) + .await + .unwrap(); + assert!(collab_data == "some_collab_data"); } }