Skip to content

Commit

Permalink
feat: support disabling comments and disallow duplicate as template f…
Browse files Browse the repository at this point in the history
…or published page (#1167)
  • Loading branch information
khorshuheng authored Jan 17, 2025
1 parent 14c0547 commit 0f7a1f4
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 71 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

10 changes: 9 additions & 1 deletion libs/client-api-test/src/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,13 @@ impl TestClient {
}

/// data: [(view_id, meta_json, blob_hex)]
pub async fn publish_collabs(&self, workspace_id: &str, data: Vec<(Uuid, &str, &str)>) {
pub async fn publish_collabs(
&self,
workspace_id: &str,
data: Vec<(Uuid, &str, &str)>,
comments_enabled: bool,
duplicate_enabled: bool,
) {
let pub_items = data
.into_iter()
.map(|(view_id, meta_json, blob_hex)| {
Expand All @@ -1060,6 +1066,8 @@ impl TestClient {
metadata: meta,
},
data: blob,
comments_enabled,
duplicate_enabled,
}
})
.collect();
Expand Down
4 changes: 4 additions & 0 deletions libs/database-entity/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,12 +801,16 @@ pub struct PublishCollabKey {
pub struct PublishCollabItem<Meta, Data> {
pub meta: PublishCollabMetadata<Meta>,
pub data: Data,
pub comments_enabled: bool,
pub duplicate_enabled: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PatchPublishedCollab {
pub view_id: Uuid,
pub publish_name: Option<String>,
pub comments_enabled: Option<bool>,
pub duplicate_enabled: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
2 changes: 2 additions & 0 deletions libs/database/src/pg_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,6 @@ pub struct AFPublishViewWithPublishInfo {
pub publish_name: String,
pub publisher_email: String,
pub publish_timestamp: DateTime<Utc>,
pub comments_enabled: bool,
pub duplicate_enabled: bool,
}
72 changes: 47 additions & 25 deletions libs/database/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use app_error::AppError;
use database_entity::dto::{
PatchPublishedCollab, PublishCollabItem, PublishCollabKey, PublishInfo, WorkspaceNamespace,
};
use sqlx::{Executor, PgPool, Postgres};
use sqlx::{Executor, PgPool, Postgres, QueryBuilder};
use uuid::Uuid;

use crate::pg_row::AFPublishViewWithPublishInfo;
Expand Down Expand Up @@ -253,26 +253,32 @@ pub async fn insert_or_replace_publish_collabs(
let mut publish_names: Vec<String> = Vec::with_capacity(item_count);
let mut metadatas: Vec<serde_json::Value> = Vec::with_capacity(item_count);
let mut blobs: Vec<Vec<u8>> = Vec::with_capacity(item_count);
let mut comments_enabled_list: Vec<bool> = Vec::with_capacity(item_count);
let mut duplicate_enabled_list: Vec<bool> = Vec::with_capacity(item_count);
publish_items.into_iter().for_each(|item| {
view_ids.push(item.meta.view_id);
publish_names.push(item.meta.publish_name);
metadatas.push(item.meta.metadata);
blobs.push(item.data);
comments_enabled_list.push(item.comments_enabled);
duplicate_enabled_list.push(item.duplicate_enabled);
});

let mut txn = pg_pool.begin().await?;
delete_published_collabs(&mut txn, workspace_id, &publish_names).await?;

let res = sqlx::query!(
r#"
INSERT INTO af_published_collab (workspace_id, view_id, publish_name, published_by, metadata, blob)
INSERT INTO af_published_collab (workspace_id, view_id, publish_name, published_by, metadata, blob, comments_enabled, duplicate_enabled)
SELECT * FROM UNNEST(
(SELECT array_agg((SELECT $1::uuid)) FROM generate_series(1, $7))::uuid[],
(SELECT array_agg((SELECT $1::uuid)) FROM generate_series(1, $9))::uuid[],
$2::uuid[],
$3::text[],
(SELECT array_agg((SELECT uid FROM af_user WHERE uuid = $4)) FROM generate_series(1, $7))::bigint[],
(SELECT array_agg((SELECT uid FROM af_user WHERE uuid = $4)) FROM generate_series(1, $9))::bigint[],
$5::jsonb[],
$6::bytea[]
$6::bytea[],
$7::boolean[],
$8::boolean[]
)
ON CONFLICT (workspace_id, view_id) DO UPDATE
SET metadata = EXCLUDED.metadata,
Expand All @@ -286,6 +292,8 @@ pub async fn insert_or_replace_publish_collabs(
publisher_uuid,
&metadatas,
&blobs,
&comments_enabled_list,
&duplicate_enabled_list,
item_count as i32,
)
.execute(txn.as_mut())
Expand Down Expand Up @@ -372,33 +380,45 @@ pub async fn update_published_collabs(
.collect();
delete_published_collabs(txn, workspace_id, &publish_names).await?;
}

for patch in patches {
let new_publish_name = match &patch.publish_name {
Some(new_publish_name) => new_publish_name,
None => continue,
};

let res = sqlx::query!(
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
r#"
UPDATE af_published_collab
SET publish_name = $1
WHERE workspace_id = $2
AND view_id = $3
UPDATE af_published_collab SET
"#,
patch.publish_name,
workspace_id,
patch.view_id,
)
.execute(txn.as_mut())
.await?;
);
let mut first_set = true;
if let Some(comments_enabled) = patch.comments_enabled {
first_set = false;
query_builder.push(" comments_enabled = ");
query_builder.push_bind(comments_enabled);
}
if let Some(duplicate_enabled) = patch.duplicate_enabled {
if !first_set {
query_builder.push(",");
}
first_set = false;
query_builder.push(" duplicate_enabled = ");
query_builder.push_bind(duplicate_enabled);
}
if let Some(publish_name) = &patch.publish_name {
if !first_set {
query_builder.push(",");
}
query_builder.push(" publish_name = ");
query_builder.push_bind(publish_name);
}
query_builder.push(" WHERE workspace_id = ");
query_builder.push_bind(workspace_id);
query_builder.push(" AND view_id = ");
query_builder.push_bind(patch.view_id);
let query = query_builder.build();
let res = query.execute(txn.as_mut()).await?;

if res.rows_affected() != 1 {
tracing::error!(
"Failed to update published collab publish name, workspace_id: {}, view_id: {}, new_publish_name: {}, rows_affected: {}",
"Failed to update published collab publish name, workspace_id: {}, view_id: {}, rows_affected: {}",
workspace_id,
patch.view_id,
new_publish_name,
res.rows_affected()
);
}
Expand Down Expand Up @@ -695,7 +715,9 @@ pub async fn select_published_view_ids_with_publish_info_for_workspace<
apc.view_id,
apc.publish_name,
au.email AS publisher_email,
apc.created_at AS publish_timestamp
apc.created_at AS publish_timestamp,
apc.comments_enabled,
apc.duplicate_enabled
FROM af_published_collab apc
JOIN af_user au ON apc.published_by = au.uid
WHERE workspace_id = $1
Expand Down
4 changes: 4 additions & 0 deletions libs/shared-entity/src/dto/workspace_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ pub struct PublishInfoView {
pub struct PublishPageParams {
pub publish_name: Option<String>,
pub visible_database_view_ids: Option<Vec<String>>,
pub comments_enabled: Option<bool>,
pub duplicate_enabled: Option<bool>,
}

#[derive(Eq, PartialEq, Debug, Hash, Clone, Serialize_repr, Deserialize_repr)]
Expand Down Expand Up @@ -401,6 +403,8 @@ pub struct PublishedViewInfo {
pub publisher_email: String,
pub publish_name: String,
pub publish_timestamp: DateTime<Utc>,
pub comments_enabled: bool,
pub duplicate_enabled: bool,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions migrations/20250113091708_publish_options.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE af_published_collab
ADD COLUMN comments_enabled BOOLEAN NOT NULL DEFAULT TRUE,
ADD COLUMN duplicate_enabled BOOLEAN NOT NULL DEFAULT TRUE;
12 changes: 12 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ http {
proxy_pass $appflowy_cloud_backend;
proxy_request_buffering off;
client_max_body_size 256M;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' $cors_origin always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, Accept, Client-Version' always;
add_header 'Access-Control-Max-Age' 3600 always;
return 204;
}

add_header 'Access-Control-Allow-Origin' $cors_origin always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, PATCH, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, Accept, Client-Version' always;
add_header 'Access-Control-Max-Age' 3600 always;
}

# AppFlowy-Cloud
Expand Down
16 changes: 15 additions & 1 deletion src/api/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,8 @@ async fn publish_page_handler(
let PublishPageParams {
publish_name,
visible_database_view_ids,
comments_enabled,
duplicate_enabled,
} = payload.into_inner();
publish_page(
&state.pg_pool,
Expand All @@ -1311,6 +1313,8 @@ async fn publish_page_handler(
&view_id,
visible_database_view_ids,
publish_name,
comments_enabled.unwrap_or(true),
duplicate_enabled.unwrap_or(true),
)
.await?;
Ok(Json(AppResponse::Ok()))
Expand Down Expand Up @@ -1936,6 +1940,9 @@ async fn delete_published_collab_reaction_handler(
Ok(Json(AppResponse::Ok()))
}

// FIXME: This endpoint currently has a different behaviour from the publish page endpoint,
// as it doesn't accept parameters. We will need to deprecate this endpoint and use a new
// one that accepts parameters.
async fn post_publish_collabs_handler(
workspace_id: web::Path<Uuid>,
user_uuid: UserUuid,
Expand Down Expand Up @@ -1974,7 +1981,14 @@ async fn post_publish_collabs_handler(
data_buffer
};

accumulator.push(PublishCollabItem { meta, data });
// Set comments_enabled and duplicate_enabled to true by default, as this is the default
// behaviour for the older web version.
accumulator.push(PublishCollabItem {
meta,
data,
comments_enabled: true,
duplicate_enabled: true,
});
}

if accumulator.is_empty() {
Expand Down
2 changes: 2 additions & 0 deletions src/biz/collab/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ pub async fn get_published_view(
publisher_email: pv.publisher_email.clone(),
publish_name: pv.publish_name.clone(),
publish_timestamp: pv.publish_timestamp,
comments_enabled: pv.comments_enabled,
duplicate_enabled: pv.duplicate_enabled,
},
)
})
Expand Down
2 changes: 0 additions & 2 deletions src/biz/search/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use collab_folder::{Folder, View};
use database::collab::GetCollabOrigin;
use std::collections::HashSet;
use std::sync::Arc;
use tracing::info;

use database::index::{search_documents, SearchDocumentParams};
use shared_entity::dto::search_dto::{
Expand Down Expand Up @@ -126,7 +125,6 @@ pub async fn search_document(
0,
MAX_SEARCH_DEPTH,
);
info!("{:?}", searchable_view_ids);
let results = search_documents(
pg_pool,
SearchDocumentParams {
Expand Down
Loading

0 comments on commit 0f7a1f4

Please sign in to comment.