feat: change doc name to publish name

This commit is contained in:
Zack Fu Zi Xiang 2024-06-21 11:18:44 +08:00
parent af9850817a
commit 0bf6d3bd60
No known key found for this signature in database
7 changed files with 48 additions and 44 deletions

View file

@ -80,14 +80,14 @@ impl Client {
pub async fn get_published_collab<T>(
&self,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<T, AppResponseError>
where
T: serde::de::DeserializeOwned,
{
let url = format!(
"{}/api/workspace/published/{}/{}",
self.base_url, publish_namespace, doc_name
self.base_url, publish_namespace, publish_name
);
let resp = self
@ -110,11 +110,11 @@ impl Client {
pub async fn get_published_collab_blob(
&self,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<Bytes, AppResponseError> {
let url = format!(
"{}/api/workspace/published/{}/{}/blob",
self.base_url, publish_namespace, doc_name
self.base_url, publish_namespace, publish_name
);
let bytes = self
.cloud_client

View file

@ -309,7 +309,7 @@ pub struct AFCollabMember {
#[derive(Serialize, Deserialize)]
pub struct PublishInfo {
pub namespace: Option<String>,
pub doc_name: String,
pub publish_name: String,
pub view_id: Uuid,
}
@ -766,7 +766,7 @@ pub struct CreateAnswerMessageParams {
#[derive(Serialize, Deserialize, Debug)]
pub struct PublishCollabMetadata<Metadata> {
pub view_id: uuid::Uuid,
pub doc_name: String,
pub publish_name: String,
pub metadata: Metadata,
}

View file

@ -896,9 +896,9 @@ pub async fn insert_or_replace_publish_collab_metas<'a, E: Executor<'a, Database
publish_item: &[PublishCollabItem<serde_json::Value, Vec<u8>>],
) -> Result<(), AppError> {
let view_ids: Vec<Uuid> = publish_item.iter().map(|item| item.meta.view_id).collect();
let doc_names: Vec<String> = publish_item
let publish_names: Vec<String> = publish_item
.iter()
.map(|item| item.meta.doc_name.clone())
.map(|item| item.meta.publish_name.clone())
.collect();
let metadatas: Vec<serde_json::Value> = publish_item
.iter()
@ -908,7 +908,7 @@ pub async fn insert_or_replace_publish_collab_metas<'a, E: Executor<'a, Database
let blobs: Vec<Vec<u8>> = publish_item.iter().map(|item| item.data.clone()).collect();
let res = sqlx::query!(
r#"
INSERT INTO af_published_collab (workspace_id, view_id, doc_name, published_by, metadata, blob)
INSERT INTO af_published_collab (workspace_id, view_id, publish_name, published_by, metadata, blob)
SELECT * FROM UNNEST(
(SELECT array_agg((SELECT $1::uuid)) FROM generate_series(1, $7))::uuid[],
$2::uuid[],
@ -922,7 +922,7 @@ pub async fn insert_or_replace_publish_collab_metas<'a, E: Executor<'a, Database
"#,
workspace_id,
&view_ids,
&doc_names,
&publish_names,
publisher_uuid,
&metadatas,
&blobs,
@ -945,17 +945,17 @@ pub async fn insert_or_replace_publish_collab_metas<'a, E: Executor<'a, Database
pub async fn select_publish_collab_meta<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<serde_json::Value, AppError> {
let res = sqlx::query!(
r#"
SELECT metadata
FROM af_published_collab
WHERE workspace_id = (SELECT workspace_id FROM af_workspace WHERE publish_namespace = $1)
AND doc_name = $2
AND publish_name = $2
"#,
publish_namespace,
doc_name,
publish_name,
)
.fetch_one(executor)
.await?;
@ -997,17 +997,17 @@ pub async fn delete_published_collabs<'a, E: Executor<'a, Database = Postgres>>(
pub async fn select_published_collab_blob<'a, E: Executor<'a, Database = Postgres>>(
executor: E,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<Vec<u8>, AppError> {
let res = sqlx::query_scalar!(
r#"
SELECT blob
FROM af_published_collab
WHERE workspace_id = (SELECT workspace_id FROM af_workspace WHERE publish_namespace = $1)
AND doc_name = $2
AND publish_name = $2
"#,
publish_namespace,
doc_name,
publish_name,
)
.fetch_one(executor)
.await?;
@ -1024,7 +1024,7 @@ pub async fn select_published_collab_info<'a, E: Executor<'a, Database = Postgre
r#"
SELECT
(SELECT publish_namespace FROM af_workspace aw WHERE aw.workspace_id = apc.workspace_id) AS namespace,
doc_name,
publish_name,
view_id
FROM af_published_collab apc
WHERE view_id = $1

View file

@ -0,0 +1 @@
ALTER TABLE af_published_collab RENAME COLUMN doc_name TO publish_name;

View file

@ -128,11 +128,11 @@ pub fn workspace_scope() -> Scope {
.route(web::delete().to(remove_collab_member_handler)),
)
.service(
web::resource("/published/{publish_namespace}/{doc_name}")
web::resource("/published/{publish_namespace}/{publish_name}")
.route(web::get().to(get_published_collab_handler))
)
.service(
web::resource("/published/{publish_namespace}/{doc_name}/blob")
web::resource("/published/{publish_namespace}/{publish_name}/blob")
.route(web::get().to(get_published_collab_blob_handler))
)
.service(
@ -970,9 +970,9 @@ async fn get_published_collab_handler(
path_param: web::Path<(String, String)>,
state: Data<AppState>,
) -> Result<Json<serde_json::Value>> {
let (workspace_namespace, doc_name) = path_param.into_inner();
let (workspace_namespace, publish_name) = path_param.into_inner();
let metadata =
biz::workspace::ops::get_published_collab(&state.pg_pool, &workspace_namespace, &doc_name)
biz::workspace::ops::get_published_collab(&state.pg_pool, &workspace_namespace, &publish_name)
.await?;
Ok(Json(metadata))
}
@ -981,10 +981,13 @@ async fn get_published_collab_blob_handler(
path_param: web::Path<(String, String)>,
state: Data<AppState>,
) -> Result<Vec<u8>> {
let (publish_namespace, doc_name) = path_param.into_inner();
let collab_data =
biz::workspace::ops::get_published_collab_blob(&state.pg_pool, &publish_namespace, &doc_name)
.await?;
let (publish_namespace, publish_name) = path_param.into_inner();
let collab_data = biz::workspace::ops::get_published_collab_blob(
&state.pg_pool,
&publish_namespace,
&publish_name,
)
.await?;
Ok(collab_data)
}

View file

@ -150,7 +150,7 @@ pub async fn publish_collabs(
publish_items: &[PublishCollabItem<serde_json::Value, Vec<u8>>],
) -> Result<(), AppError> {
for publish_item in publish_items {
check_collab_doc_name(publish_item.meta.doc_name.as_str())?;
check_collab_publish_name(publish_item.meta.publish_name.as_str())?;
}
insert_or_replace_publish_collab_metas(pg_pool, workspace_id, publisher_uuid, publish_items)
.await?;
@ -160,18 +160,18 @@ pub async fn publish_collabs(
pub async fn get_published_collab(
pg_pool: &PgPool,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<serde_json::Value, AppError> {
let metadata = select_publish_collab_meta(pg_pool, publish_namespace, doc_name).await?;
let metadata = select_publish_collab_meta(pg_pool, publish_namespace, publish_name).await?;
Ok(metadata)
}
pub async fn get_published_collab_blob(
pg_pool: &PgPool,
publish_namespace: &str,
doc_name: &str,
publish_name: &str,
) -> Result<Vec<u8>, AppError> {
select_published_collab_blob(pg_pool, publish_namespace, doc_name).await
select_published_collab_blob(pg_pool, publish_namespace, publish_name).await
}
pub async fn get_published_collab_info(
@ -589,16 +589,16 @@ async fn check_workspace_owner_or_publisher(
Ok(())
}
fn check_collab_doc_name(doc_name: &str) -> Result<(), AppError> {
fn check_collab_publish_name(publish_name: &str) -> Result<(), AppError> {
// Check len
if doc_name.len() > 20 {
if publish_name.len() > 20 {
return Err(AppError::InvalidRequest(
"Document name must be at most 20 characters long".to_string(),
));
}
// Only contain alphanumeric characters and hyphens
for c in doc_name.chars() {
for c in publish_name.chars() {
if !c.is_alphanumeric() && c != '-' {
return Err(AppError::InvalidRequest(
"Document name must only contain alphanumeric characters and hyphens".to_string(),

View file

@ -73,9 +73,9 @@ async fn test_publish_doc() {
.await
.unwrap();
let doc_name_1 = "doc1";
let publish_name_1 = "publish_name_1";
let view_id_1 = uuid::Uuid::new_v4();
let doc_name_2 = "doc2";
let publish_name_2 = "publish_name_2";
let view_id_2 = uuid::Uuid::new_v4();
c.publish_collabs::<MyCustomMetadata, &[u8]>(
&workspace_id,
@ -83,7 +83,7 @@ async fn test_publish_doc() {
PublishCollabItem {
meta: PublishCollabMetadata {
view_id: view_id_1,
doc_name: doc_name_1.to_string(),
publish_name: publish_name_1.to_string(),
metadata: MyCustomMetadata {
title: "my_title_1".to_string(),
},
@ -93,7 +93,7 @@ async fn test_publish_doc() {
PublishCollabItem {
meta: PublishCollabMetadata {
view_id: view_id_2,
doc_name: doc_name_2.to_string(),
publish_name: publish_name_2.to_string(),
metadata: MyCustomMetadata {
title: "my_title_2".to_string(),
},
@ -109,7 +109,7 @@ async fn test_publish_doc() {
// Non login user should be able to view the published collab
let guest_client = localhost_client();
let published_collab = guest_client
.get_published_collab::<MyCustomMetadata>(&my_namespace, doc_name_1)
.get_published_collab::<MyCustomMetadata>(&my_namespace, publish_name_1)
.await
.unwrap();
assert_eq!(published_collab.title, "my_title_1");
@ -119,11 +119,11 @@ async fn test_publish_doc() {
.await
.unwrap();
assert_eq!(publish_info.namespace, Some(my_namespace.clone()));
assert_eq!(publish_info.doc_name, doc_name_1);
assert_eq!(publish_info.publish_name, publish_name_1);
assert_eq!(publish_info.view_id, view_id_1);
let blob = guest_client
.get_published_collab_blob(&my_namespace, doc_name_1)
.get_published_collab_blob(&my_namespace, publish_name_1)
.await
.unwrap();
assert_eq!(blob, "yrs_encoded_data_1");
@ -137,7 +137,7 @@ async fn test_publish_doc() {
// Deleted collab should not be accessible
let guest_client = localhost_client();
let err = guest_client
.get_published_collab::<MyCustomMetadata>(&my_namespace, doc_name_1)
.get_published_collab::<MyCustomMetadata>(&my_namespace, publish_name_1)
.await
.err()
.unwrap();
@ -145,7 +145,7 @@ async fn test_publish_doc() {
let guest_client = localhost_client();
let err = guest_client
.get_published_collab_blob(&my_namespace, doc_name_1)
.get_published_collab_blob(&my_namespace, publish_name_1)
.await
.err()
.unwrap();
@ -177,9 +177,9 @@ async fn test_publish_load_test() {
.map(|i| PublishCollabItem {
meta: PublishCollabMetadata {
view_id: uuid::Uuid::new_v4(),
doc_name: format!("doc{}", i),
publish_name: format!("publish_name_{}", i),
metadata: MyCustomMetadata {
title: format!("title{}", i),
title: format!("title_{}", i),
},
},
data: vec![0; 100_000], // 100 KB