Skip to content

Commit

Permalink
chore: update create_space api
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Jan 2, 2025
1 parent baf53ad commit 330eca5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
24 changes: 18 additions & 6 deletions collab-folder/src/hierarchy_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::space_info::SpacePermission;
use crate::{
timestamp, IconType, RepeatedViewIdentifier, View, ViewIcon, ViewIdentifier, ViewLayout,
SPACE_CREATED_AT_KEY, SPACE_ICON_COLOR_KEY, SPACE_ICON_KEY, SPACE_IS_SPACE_KEY,
timestamp, IconType, RepeatedViewIdentifier, SpaceInfo, View, ViewIcon, ViewIdentifier,
ViewLayout, SPACE_CREATED_AT_KEY, SPACE_ICON_COLOR_KEY, SPACE_ICON_KEY, SPACE_IS_SPACE_KEY,
SPACE_PERMISSION_KEY,
};

Expand Down Expand Up @@ -248,13 +248,17 @@ impl ViewExtraBuilder {
self
}

pub fn with_space_icon_key(mut self, icon_key: &str) -> Self {
self.0[SPACE_ICON_KEY] = json!(icon_key);
pub fn with_space_icon(mut self, icon: Option<&str>) -> Self {
if let Some(icon) = icon {
self.0[SPACE_ICON_KEY] = json!(icon);
}
self
}

pub fn with_space_icon_color_key(mut self, icon_color_key: &str) -> Self {
self.0[SPACE_ICON_COLOR_KEY] = json!(icon_color_key);
pub fn with_space_icon_color(mut self, icon_color: Option<&str>) -> Self {
if let Some(icon_color) = icon_color {
self.0[SPACE_ICON_COLOR_KEY] = json!(icon_color);
}
self
}

Expand All @@ -263,6 +267,14 @@ impl ViewExtraBuilder {
self
}

pub fn with_space_info(mut self, space_info: SpaceInfo) -> Self {
self.0[SPACE_IS_SPACE_KEY] = json!(space_info.is_space);
self.0[SPACE_PERMISSION_KEY] = json!(space_info.space_permission as u8);
self.0[SPACE_ICON_KEY] = json!(space_info.space_icon);
self.0[SPACE_ICON_COLOR_KEY] = json!(space_info.space_icon_color);
self
}

pub fn build(self) -> serde_json::Value {
self.0
}
Expand Down
32 changes: 25 additions & 7 deletions collab-folder/src/space_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::timestamp;

pub const SPACE_IS_SPACE_KEY: &str = "is_space";
pub const SPACE_PERMISSION_KEY: &str = "space_permission";
pub const SPACE_ICON_KEY: &str = "space_icon";
Expand All @@ -22,21 +24,37 @@ pub struct SpaceInfo {
/// The permission of the space view.
///
/// If the space_permission is none, the space view will use the SpacePermission::PublicToAll.
pub space_permission: Option<SpacePermission>,
pub space_permission: SpacePermission,

/// The created time of the space view.
pub created_at: i64,

/// The space icon key.
/// The space icon.
///
/// If the space_icon_key is none, the space view will use the default icon.
pub space_icon_key: Option<String>,
/// If the space_icon is none, the space view will use the default icon.
pub space_icon: Option<String>,

/// The space icon color key.
/// The space icon color.
///
/// If the space_icon_color_key is none, the space view will use the default icon color.
/// If the space_icon_color is none, the space view will use the default icon color.
/// The value should be a valid hex color code: 0xFFA34AFD
pub space_icon_color_key: Option<String>,
pub space_icon_color: Option<String>,
}

impl Default for SpaceInfo {
/// Default space info is a public space
///
/// The permission is public to all
/// The created time is the current timestamp
fn default() -> Self {
Self {
is_space: true,
space_permission: SpacePermission::PublicToAll,
created_at: timestamp(),
space_icon: None,
space_icon_color: None,
}
}
}

#[derive(Debug, Clone, serde_repr::Serialize_repr, serde_repr::Deserialize_repr)]
Expand Down
8 changes: 4 additions & 4 deletions collab-folder/tests/folder_test/space_info_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fn create_public_space_test() {
let space_info = builder
.is_space(true)
.with_space_permission(SpacePermission::PublicToAll)
.with_space_icon_key("interface_essential/home-3")
.with_space_icon_color_key("0xFFA34AFD")
.with_space_icon(Some("interface_essential/home-3"))
.with_space_icon_color(Some("0xFFA34AFD"))
.build();
let space_info_json = serde_json::to_value(space_info).unwrap();
assert_json_diff::assert_json_eq!(
Expand All @@ -34,8 +34,8 @@ fn create_private_space_test() {
let space_info = builder
.is_space(true)
.with_space_permission(SpacePermission::Private)
.with_space_icon_key("interface_essential/lock")
.with_space_icon_color_key("0xFF4A4AFD")
.with_space_icon(Some("interface_essential/lock"))
.with_space_icon_color(Some("0xFF4A4AFD"))
.build();
let space_info_json = serde_json::to_value(space_info).unwrap();
assert_json_diff::assert_json_eq!(
Expand Down
2 changes: 1 addition & 1 deletion collab-importer/src/notion/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl ImportedInfo {
"Imported Space",
&view_id,
vec![],
SpacePermission::PublicToAll,
collab_folder::SpaceInfo::default(),
)?;
Ok(Self {
uid,
Expand Down
11 changes: 3 additions & 8 deletions collab-importer/src/space_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use collab::core::origin::CollabOrigin;
use collab::preclude::Collab;
use collab_document::document_data::default_document_collab_data;
use collab_folder::hierarchy_builder::{NestedChildViewBuilder, ParentChildViews};
use collab_folder::{SpacePermission, ViewLayout};
use collab_folder::{SpaceInfo, ViewLayout};

#[allow(dead_code)]
pub fn create_space_view(
Expand All @@ -13,7 +13,7 @@ pub fn create_space_view(
name: &str,
view_id: &str,
child_views: Vec<ParentChildViews>,
space_permission: SpacePermission,
space_info: SpaceInfo,
) -> Result<(ParentChildViews, Collab), ImporterError> {
let import_container_doc_state = default_document_collab_data(view_id)
.map_err(|err| ImporterError::Internal(err.into()))?
Expand All @@ -34,12 +34,7 @@ pub fn create_space_view(
.with_layout(ViewLayout::Document)
.with_name(name)
.with_children(child_views)
.with_extra(|extra| {
extra
.is_space(true)
.with_space_permission(space_permission)
.build()
})
.with_extra(|extra| extra.with_space_info(space_info).build())
.build();
Ok((view, collab))
}

0 comments on commit 330eca5

Please sign in to comment.