Skip to content

Commit

Permalink
feat: support publish interfaces for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
qinluhe committed Jun 26, 2024
1 parent fa7565d commit b6c22bc
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 2 deletions.
128 changes: 126 additions & 2 deletions libs/client-api-wasm/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use client_api::entity::AFUserProfile;
use client_api::error::{AppResponseError, ErrorCode};
use collab_entity::{CollabType, EncodedCollab};
use database_entity::dto::{
AFUserWorkspaceInfo, AFWorkspace, BatchQueryCollabResult, QueryCollab, QueryCollabParams,
QueryCollabResult,
AFUserWorkspaceInfo, AFWorkspace, BatchQueryCollabResult, PublishCollabMetadata, QueryCollab,
QueryCollabParams, QueryCollabResult,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::collections::HashMap;
use tsify::Tsify;
Expand Down Expand Up @@ -237,3 +238,126 @@ impl From<BatchQueryCollabResult> for BatchClientEncodeCollab {
BatchClientEncodeCollab(hash_map)
}
}

#[derive(Tsify, Serialize, Deserialize, Default, Debug)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct PublishViewMeta {
pub view_id: String,
pub publish_name: String,
pub metadata: PublishViewMetaData,
}

#[derive(Tsify, Serialize, Deserialize, Default, Debug)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct PublishViewMetaData {
pub view: PublishViewInfo,
pub child_views: Vec<PublishViewInfo>,
pub ancestor_views: Vec<PublishViewInfo>,
}

#[derive(Tsify, Serialize, Deserialize, Default, Debug)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct PublishViewInfo {
pub view_id: String,
pub name: String,
pub icon: Option<String>,
pub layout: i32,
pub extra: Option<String>,
pub created_by: Option<String>,
pub last_edited_by: Option<String>,
pub last_edited_time: String,
pub created_at: String,
pub child_views: Option<Vec<PublishViewInfo>>,
}

#[derive(Tsify, Serialize, Deserialize, Default, Debug)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct PublishViewPayload {
pub meta: PublishViewMeta,
/// The doc_state of the encoded collab.
pub data: Vec<u8>,
}
#[derive(Tsify, Serialize, Deserialize, Default, Debug)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct PublishInfo {
pub namespace: Option<String>,
pub publish_name: String,
}

from_struct_for_jsvalue!(PublishViewMeta);
from_struct_for_jsvalue!(PublishViewMetaData);
from_struct_for_jsvalue!(PublishViewPayload);
from_struct_for_jsvalue!(PublishInfo);

impl From<PublishCollabMetadata<serde_json::Value>> for PublishViewMeta {
fn from(value: PublishCollabMetadata<Value>) -> Self {
let view_id = value.view_id.to_string();
let publish_name = value.publish_name.to_string();
let metadata = PublishViewMetaData::from(value.metadata);
Self {
view_id,
publish_name,
metadata,
}
}
}

impl From<serde_json::Value> for PublishViewMetaData {
fn from(value: Value) -> Self {
let view = PublishViewInfo::from(value["view"].clone());
let child_views = parse_views(&value["child_views"]);
let ancestor_views = parse_views(&value["ancestor_views"]);
Self {
view,
child_views: child_views.unwrap_or_default(),
ancestor_views: ancestor_views.unwrap_or_default(),
}
}
}

fn match_serde_value_to_string(value: &Value) -> Option<String> {
match value {
Value::String(v) => Some(v.to_string()),
Value::Null => None,
v => Some(v.to_string()),
}
}

impl From<serde_json::Value> for PublishViewInfo {
fn from(value: Value) -> Self {
let view_id = match_serde_value_to_string(&value["view_id"]).unwrap_or_default();
let name = match_serde_value_to_string(&value["name"]).unwrap_or_default();
let icon = match_serde_value_to_string(&value["icon"]);
let layout = value["layout"]
.as_i64()
.map(|v| v as i32)
.unwrap_or_default();
let extra = match_serde_value_to_string(&value["extra"]);
let created_by = match_serde_value_to_string(&value["created_by"]);
let last_edited_by = match_serde_value_to_string(&value["last_edited_by"]);
let last_edited_time =
match_serde_value_to_string(&value["last_edited_time"]).unwrap_or_default();
let created_at = match_serde_value_to_string(&value["created_at"]).unwrap_or_default();
let child_views = parse_views(&value["child_views"]);
Self {
view_id,
name,
icon,
layout,
extra,
created_by,
last_edited_by,
last_edited_time,
created_at,
child_views,
}
}
}

fn parse_views(value: &Value) -> Option<Vec<PublishViewInfo>> {
value.as_array().map(|v| {
v.iter()
.map(|v| PublishViewInfo::from(v.clone()))
.collect::<Vec<_>>()
})
}
62 changes: 62 additions & 0 deletions libs/client-api-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::entities::*;
use client_api::notify::TokenState;
use client_api::{Client, ClientConfiguration};
use std::sync::Arc;
use uuid::Uuid;

use client_api::error::ErrorCode;
use database_entity::dto::QueryCollab;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -160,4 +162,64 @@ impl ClientAPI {
Err(err) => Err(ClientResponse::from(err)),
}
}

pub async fn get_publish_view_meta(
&self,
publish_namspace: String,
publish_name: String,
) -> Result<PublishViewMeta, ClientResponse> {
match self
.client
.get_published_collab::<serde_json::Value>(publish_namspace.as_str(), publish_name.as_str())
.await
{
Ok(data) => {
let metadata = PublishViewMetaData::from(data);
let view_id = metadata.view.view_id.clone();

Ok(PublishViewMeta {
metadata,
view_id,
publish_name,
})
},

Err(err) => Err(ClientResponse::from(err)),
}
}

pub async fn get_publish_view(
&self,
publish_namespace: String,
publish_name: String,
) -> Result<PublishViewPayload, ClientResponse> {
let data = self
.client
.get_published_collab_blob(publish_namespace.as_str(), publish_name.as_str())
.await
.map_err(ClientResponse::from)?;

let meta = self
.get_publish_view_meta(publish_namespace, publish_name)
.await?;

Ok(PublishViewPayload {
meta,
data: data.to_vec(),
})
}

pub async fn get_publish_info(&self, view_id: String) -> Result<PublishInfo, ClientResponse> {
let view_id = Uuid::parse_str(view_id.as_str()).map_err(|err| ClientResponse {
code: ErrorCode::UuidError,
message: format!("Failed to parse view_id: {}", err),
})?;
match self.client.get_published_collab_info(&view_id).await {
Ok(info) => Ok(PublishInfo {
namespace: info.namespace,
publish_name: info.publish_name,
}),
Err(err) => Err(ClientResponse::from(err)),
}
}
}

0 comments on commit b6c22bc

Please sign in to comment.