mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-06 00:41:39 +08:00
feat: ipc get_workspace
This commit is contained in:
+1
-1
Submodule client-app/src-OctoBase updated: eb1826dbef...b22b072a1b
@@ -12,6 +12,7 @@ pub fn invoke_handler() -> impl Fn(tauri::Invoke) + Send + Sync + 'static {
|
|||||||
create_workspace,
|
create_workspace,
|
||||||
update_workspace,
|
update_workspace,
|
||||||
get_workspaces,
|
get_workspaces,
|
||||||
|
get_workspace,
|
||||||
get_doc,
|
get_doc,
|
||||||
put_blob,
|
put_blob,
|
||||||
get_blob
|
get_blob
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use ipc_types::workspace::{
|
use ipc_types::workspace::{
|
||||||
CreateWorkspace, CreateWorkspaceResult, GetWorkspacesResult, UpdateWorkspace,
|
CreateWorkspace, CreateWorkspaceResult, GetWorkspace, GetWorkspaceResult, GetWorkspaces,
|
||||||
|
GetWorkspacesResult, UpdateWorkspace,
|
||||||
};
|
};
|
||||||
use jwst::{DocStorage, Workspace as OctoBaseWorkspace};
|
use jwst::{DocStorage, Workspace as OctoBaseWorkspace};
|
||||||
use lib0::any::Any;
|
use lib0::any::Any;
|
||||||
@@ -10,7 +11,7 @@ use crate::state::AppState;
|
|||||||
/// create yDoc for a workspace
|
/// create yDoc for a workspace
|
||||||
pub async fn get_workspaces<'s>(
|
pub async fn get_workspaces<'s>(
|
||||||
state: tauri::State<'s, AppState>,
|
state: tauri::State<'s, AppState>,
|
||||||
parameters: CreateWorkspace,
|
parameters: GetWorkspaces,
|
||||||
) -> Result<GetWorkspacesResult, String> {
|
) -> Result<GetWorkspacesResult, String> {
|
||||||
match &state
|
match &state
|
||||||
.0
|
.0
|
||||||
@@ -27,6 +28,30 @@ pub async fn get_workspaces<'s>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
/// create yDoc for a workspace
|
||||||
|
pub async fn get_workspace<'s>(
|
||||||
|
state: tauri::State<'s, AppState>,
|
||||||
|
parameters: GetWorkspace,
|
||||||
|
) -> Result<GetWorkspaceResult, String> {
|
||||||
|
match &state
|
||||||
|
.0
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.metadata_db
|
||||||
|
.get_workspace_by_id(parameters.id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(user_workspace_option) => match user_workspace_option {
|
||||||
|
Some(user_workspace) => Ok(GetWorkspaceResult {
|
||||||
|
workspace: user_workspace.clone(),
|
||||||
|
}),
|
||||||
|
None => Err("Get workspace has no result".to_string())
|
||||||
|
},
|
||||||
|
Err(error_message) => Err(error_message.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
/// create yDoc for a workspace
|
/// create yDoc for a workspace
|
||||||
pub async fn create_workspace<'s>(
|
pub async fn create_workspace<'s>(
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use jwst_storage::model::WorkspaceWithPermission;
|
use jwst_storage::{model::WorkspaceWithPermission, WorkspaceDetail};
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -18,26 +18,37 @@ pub struct GetWorkspaces {
|
|||||||
pub user_id: i32,
|
pub user_id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
|
pub struct GetWorkspace {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct CreateWorkspaceResult {
|
pub struct CreateWorkspaceResult {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct GetWorkspacesResult {
|
pub struct GetWorkspacesResult {
|
||||||
pub workspaces: Vec<WorkspaceWithPermission>,
|
pub workspaces: Vec<WorkspaceWithPermission>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
|
pub struct GetWorkspaceResult {
|
||||||
|
pub workspace: WorkspaceDetail,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
pub struct UpdateWorkspace {
|
pub struct UpdateWorkspace {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub public: bool,
|
pub public: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, JsonSchema)]
|
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
|
||||||
pub enum IWorkspaceParameters {
|
pub enum IWorkspaceParameters {
|
||||||
CreateWorkspace(CreateWorkspace),
|
CreateWorkspace(CreateWorkspace),
|
||||||
|
GetWorkspace(GetWorkspace),
|
||||||
GetWorkspaces(GetWorkspaces),
|
GetWorkspaces(GetWorkspaces),
|
||||||
GetWorkspacesResult(GetWorkspacesResult),
|
GetWorkspacesResult(GetWorkspacesResult),
|
||||||
UpdateWorkspace(UpdateWorkspace),
|
UpdateWorkspace(UpdateWorkspace),
|
||||||
|
|||||||
@@ -132,10 +132,79 @@ export class TauriIPCProvider extends LocalProvider {
|
|||||||
return nw;
|
return nw;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getWorkspaces(
|
override async loadWorkspaces() {
|
||||||
config: Readonly<ConfigStore<boolean>>
|
// TODO: get user id here
|
||||||
): Promise<Map<string, boolean> | undefined> {
|
const workspacesList = await this.#ipc.getWorkspaces({ user_id: 0 });
|
||||||
const entries = await config.entries();
|
const workspaces: WS[] = workspacesList.workspaces.map(w => {
|
||||||
return new Map(entries);
|
return {
|
||||||
|
...w,
|
||||||
|
memberCount: 0,
|
||||||
|
name: '',
|
||||||
|
provider: 'affine',
|
||||||
|
};
|
||||||
|
});
|
||||||
|
const workspaceInstances = workspaces.map(({ id }) => {
|
||||||
|
const workspace =
|
||||||
|
this._workspacesCache.get(id) ||
|
||||||
|
new Workspace({
|
||||||
|
room: id,
|
||||||
|
}).register(BlockSchema);
|
||||||
|
this._workspacesCache.set(id, workspace);
|
||||||
|
if (workspace) {
|
||||||
|
return new Promise<Workspace>(resolve => {
|
||||||
|
downloadWorkspace(id).then(data => {
|
||||||
|
applyUpdate(workspace.doc, new Uint8Array(data));
|
||||||
|
resolve(workspace);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return Promise.resolve(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(await Promise.all(workspaceInstances)).forEach((workspace, i) => {
|
||||||
|
if (workspace) {
|
||||||
|
workspaces[i] = {
|
||||||
|
...workspaces[i],
|
||||||
|
name: workspace.doc.meta.name,
|
||||||
|
avatar: workspace.doc.meta.avatar,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const getDetailList = workspacesList.map(w => {
|
||||||
|
const { id } = w;
|
||||||
|
return new Promise<{ id: string; detail: WorkspaceDetail | null }>(
|
||||||
|
resolve => {
|
||||||
|
getWorkspaceDetail({ id }).then(data => {
|
||||||
|
resolve({ id, detail: data || null });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
const ownerList = await Promise.all(getDetailList);
|
||||||
|
(await Promise.all(ownerList)).forEach(detail => {
|
||||||
|
if (detail) {
|
||||||
|
const { id, detail: workspaceDetail } = detail;
|
||||||
|
if (workspaceDetail) {
|
||||||
|
const { owner, member_count } = workspaceDetail;
|
||||||
|
const currentWorkspace = workspaces.find(w => w.id === id);
|
||||||
|
if (currentWorkspace) {
|
||||||
|
currentWorkspace.owner = {
|
||||||
|
id: owner.id,
|
||||||
|
name: owner.name,
|
||||||
|
avatar: owner.avatar_url,
|
||||||
|
email: owner.email,
|
||||||
|
};
|
||||||
|
currentWorkspace.memberCount = member_count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
workspaces.forEach(workspace => {
|
||||||
|
this._workspaces.add(workspace);
|
||||||
|
});
|
||||||
|
|
||||||
|
return workspaces;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,16 @@
|
|||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": ["GetWorkspace"],
|
||||||
|
"properties": {
|
||||||
|
"GetWorkspace": {
|
||||||
|
"$ref": "#/definitions/GetWorkspace"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": ["GetWorkspaces"],
|
"required": ["GetWorkspaces"],
|
||||||
@@ -80,6 +90,15 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"GetWorkspace": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id"],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"GetWorkspaces": {
|
"GetWorkspaces": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": ["user_id"],
|
"required": ["user_id"],
|
||||||
@@ -103,8 +122,20 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"PermissionType": {
|
"PermissionType": {
|
||||||
"type": "string",
|
"anyOf": [
|
||||||
"enum": ["Read", "Write", "Admin", "Owner"]
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"UpdateWorkspace": {
|
"UpdateWorkspace": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
@@ -120,8 +151,14 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"WorkspaceType": {
|
"WorkspaceType": {
|
||||||
"type": "string",
|
"anyOf": [
|
||||||
"enum": ["Private", "Normal"]
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"WorkspaceWithPermission": {
|
"WorkspaceWithPermission": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ export type IWorkspaceParameters =
|
|||||||
| {
|
| {
|
||||||
CreateWorkspace: CreateWorkspace;
|
CreateWorkspace: CreateWorkspace;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
GetWorkspace: GetWorkspace;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
GetWorkspaces: GetWorkspaces;
|
GetWorkspaces: GetWorkspaces;
|
||||||
}
|
}
|
||||||
@@ -21,8 +24,8 @@ export type IWorkspaceParameters =
|
|||||||
| {
|
| {
|
||||||
CreateWorkspaceResult: CreateWorkspaceResult;
|
CreateWorkspaceResult: CreateWorkspaceResult;
|
||||||
};
|
};
|
||||||
export type PermissionType = 'Read' | 'Write' | 'Admin' | 'Owner';
|
export type PermissionType = null | null | null | null;
|
||||||
export type WorkspaceType = 'Private' | 'Normal';
|
export type WorkspaceType = null | null;
|
||||||
|
|
||||||
export interface CreateWorkspace {
|
export interface CreateWorkspace {
|
||||||
/**
|
/**
|
||||||
@@ -32,6 +35,10 @@ export interface CreateWorkspace {
|
|||||||
user_id: number;
|
user_id: number;
|
||||||
[k: string]: unknown;
|
[k: string]: unknown;
|
||||||
}
|
}
|
||||||
|
export interface GetWorkspace {
|
||||||
|
id: string;
|
||||||
|
[k: string]: unknown;
|
||||||
|
}
|
||||||
export interface GetWorkspaces {
|
export interface GetWorkspaces {
|
||||||
user_id: number;
|
user_id: number;
|
||||||
[k: string]: unknown;
|
[k: string]: unknown;
|
||||||
|
|||||||
Reference in New Issue
Block a user