From 0b2ab4f9a4f97b624af806907d07396f86be0ff1 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Wed, 11 Jan 2023 17:54:26 +0800 Subject: [PATCH] feat: create user if not exist --- .../src-tauri/examples/generate-jsonschema.rs | 6 +- client-app/src-tauri/src/commands.rs | 3 + client-app/src-tauri/src/commands/user.rs | 25 +++++++ client-app/src-tauri/types/src/lib.rs | 1 + client-app/src-tauri/types/src/user.rs | 9 +++ packages/data-center/src/datacenter.ts | 3 +- .../src/provider/tauri-ipc/index.ts | 22 +++++- .../src/provider/tauri-ipc/ipc/methods.ts | 7 ++ .../provider/tauri-ipc/ipc/types/user.json | 69 +++++++++++++++++++ .../src/provider/tauri-ipc/ipc/types/user.ts | 30 ++++++++ 10 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 client-app/src-tauri/src/commands/user.rs create mode 100644 client-app/src-tauri/types/src/user.rs create mode 100644 packages/data-center/src/provider/tauri-ipc/ipc/types/user.json create mode 100644 packages/data-center/src/provider/tauri-ipc/ipc/types/user.ts diff --git a/client-app/src-tauri/examples/generate-jsonschema.rs b/client-app/src-tauri/examples/generate-jsonschema.rs index 4ef8f1efb0..c8818faccd 100644 --- a/client-app/src-tauri/examples/generate-jsonschema.rs +++ b/client-app/src-tauri/examples/generate-jsonschema.rs @@ -1,4 +1,7 @@ -use ipc_types::{blob::IBlobParameters, document::IDocumentParameters, workspace::IWorkspaceParameters}; +use ipc_types::{ + blob::IBlobParameters, document::IDocumentParameters, user::IUserParameters, + workspace::IWorkspaceParameters, +}; /** * convert serde to jsonschema: https://imfeld.dev/writing/generating_typescript_types_from_rust * with way to optimize @@ -30,4 +33,5 @@ fn main() { generate::(Path::join(&data_center_ipc_type_folder, "document.json")); generate::(Path::join(&data_center_ipc_type_folder, "workspace.json")); generate::(Path::join(&data_center_ipc_type_folder, "blob.json")); + generate::(Path::join(&data_center_ipc_type_folder, "user.json")); } diff --git a/client-app/src-tauri/src/commands.rs b/client-app/src-tauri/src/commands.rs index 6a98817605..4b1262854b 100644 --- a/client-app/src-tauri/src/commands.rs +++ b/client-app/src-tauri/src/commands.rs @@ -1,10 +1,12 @@ pub mod blob; pub mod workspace; pub mod document; +pub mod user; use blob::*; use workspace::*; use document::*; +use user::*; pub fn invoke_handler() -> impl Fn(tauri::Invoke) + Send + Sync + 'static { tauri::generate_handler![ @@ -13,6 +15,7 @@ pub fn invoke_handler() -> impl Fn(tauri::Invoke) + Send + Sync + 'static { update_workspace, get_workspaces, get_workspace, + create_user, get_doc, put_blob, get_blob diff --git a/client-app/src-tauri/src/commands/user.rs b/client-app/src-tauri/src/commands/user.rs new file mode 100644 index 0000000000..360fa35419 --- /dev/null +++ b/client-app/src-tauri/src/commands/user.rs @@ -0,0 +1,25 @@ +use jwst_storage::{CreateUser, User}; + +use crate::state::AppState; + +#[tauri::command] +/// create yDoc for a workspace +pub async fn create_user<'s>( + state: tauri::State<'s, AppState>, + parameters: CreateUser, +) -> Result { + match &state + .0 + .lock() + .await + .metadata_db + .create_user(parameters.clone()) + .await + { + Ok(new_user_option) => match new_user_option { + Some(new_user) => Ok(new_user.clone()), + None => Err("User creation failed".to_string()), + }, + Err(error_message) => Err(error_message.to_string()), + } +} diff --git a/client-app/src-tauri/types/src/lib.rs b/client-app/src-tauri/types/src/lib.rs index def441e017..d6d8f19cc5 100644 --- a/client-app/src-tauri/types/src/lib.rs +++ b/client-app/src-tauri/types/src/lib.rs @@ -6,3 +6,4 @@ extern crate jwst_storage; pub mod blob; pub mod document; pub mod workspace; +pub mod user; diff --git a/client-app/src-tauri/types/src/user.rs b/client-app/src-tauri/types/src/user.rs new file mode 100644 index 0000000000..9d3a1daf5a --- /dev/null +++ b/client-app/src-tauri/types/src/user.rs @@ -0,0 +1,9 @@ +use jwst_storage::{CreateUser, User}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub enum IUserParameters { + CreateUser(CreateUser), + User(User), +} diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 1eb4315474..7f40f123c9 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -47,8 +47,9 @@ export class DataCenter { ) { const { TauriIPCProvider } = await import('./provider/tauri-ipc'); dc.registerProvider(new TauriIPCProvider(getInitParams())); + } else { + dc.registerProvider(new LocalProvider(getInitParams())); } - dc.registerProvider(new LocalProvider(getInitParams())); dc.registerProvider(new AffineProvider(getInitParams())); return dc; diff --git a/packages/data-center/src/provider/tauri-ipc/index.ts b/packages/data-center/src/provider/tauri-ipc/index.ts index bb48fffac2..88248440a3 100644 --- a/packages/data-center/src/provider/tauri-ipc/index.ts +++ b/packages/data-center/src/provider/tauri-ipc/index.ts @@ -10,6 +10,7 @@ import { WorkspaceMeta, WorkspaceInfo } from '../../types'; import { IPCBlobProvider } from './blocksuite-provider/blob.js'; import { WorkspaceDetail } from '../affine/apis/workspace.js'; import { setDefaultAvatar } from '../utils.js'; +import { User } from './ipc/types/user.js'; export class TauriIPCProvider extends LocalProvider { static id = 'tauri-ipc'; @@ -18,6 +19,7 @@ export class TauriIPCProvider extends LocalProvider { constructor(params: ProviderConstructorParams) { super(params); // TODO: let blocksuite's blob provider get blob receive workspace id. Currently, all blobs are placed together + this.loadWorkspaces(); } async init() { @@ -111,7 +113,7 @@ export class TauriIPCProvider extends LocalProvider { owner: undefined, isLocal: true, memberCount: 1, - provider: 'affine', + provider: this.id, }; if (!blocksuiteWorkspace.meta.avatar) { @@ -124,15 +126,29 @@ export class TauriIPCProvider extends LocalProvider { override async loadWorkspaces() { // TODO: get user id here + // try create a default user, otherwise getWorkspaces will failed due to user not exists + let createdUserID = 0; + try { + const user = await ipcMethods.createUser({ + email: 'xxx@xx.xx', + name: 'xxx', + password: 'xxx', + avatar_url: '', + }); + createdUserID = user.id; + } catch (error) { + // maybe user existed, which can be omited + console.error(error); + } const { workspaces: workspacesList } = await ipcMethods.getWorkspaces({ - user_id: 0, + user_id: createdUserID, }); const workspaces: WorkspaceInfo[] = workspacesList.map(w => { return { ...w, memberCount: 0, name: '', - provider: 'affine', + provider: this.id, }; }); const workspaceInstances = workspaces.map(({ id }) => { diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts b/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts index b637bfffa2..7eb1678533 100644 --- a/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts +++ b/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts @@ -11,8 +11,10 @@ import { GetWorkspaceResult, GetWorkspaces, GetWorkspacesResult, + User, } from './types/workspace'; import { GetBlob, PutBlob } from './types/blob'; +import { CreateUser } from './types/user'; export const updateYDocument = async (parameters: YDocumentUpdate) => await invoke('update_y_document', { @@ -48,3 +50,8 @@ export const getBlob = async (parameters: GetBlob) => await invoke('get_blob', { parameters, }); + +export const createUser = async (parameters: CreateUser) => + await invoke('create_user', { + parameters, + }); diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/user.json b/packages/data-center/src/provider/tauri-ipc/ipc/types/user.json new file mode 100644 index 0000000000..ae91d63e0f --- /dev/null +++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/user.json @@ -0,0 +1,69 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "IUserParameters", + "oneOf": [ + { + "type": "object", + "required": ["CreateUser"], + "properties": { + "CreateUser": { + "$ref": "#/definitions/CreateUser" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": ["User"], + "properties": { + "User": { + "$ref": "#/definitions/User" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "CreateUser": { + "type": "object", + "required": ["email", "name", "password"], + "properties": { + "avatar_url": { + "type": ["string", "null"] + }, + "email": { + "type": "string" + }, + "name": { + "type": "string" + }, + "password": { + "type": "string" + } + } + }, + "User": { + "type": "object", + "required": ["created_at", "email", "id", "name"], + "properties": { + "avatar_url": { + "type": ["string", "null"] + }, + "created_at": { + "type": "integer", + "format": "int64" + }, + "email": { + "type": "string" + }, + "id": { + "type": "integer", + "format": "int32" + }, + "name": { + "type": "string" + } + } + } + } +} diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/user.ts b/packages/data-center/src/provider/tauri-ipc/ipc/types/user.ts new file mode 100644 index 0000000000..f925eaa35d --- /dev/null +++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/user.ts @@ -0,0 +1,30 @@ +/* tslint:disable */ +/** + * This file was automatically generated by json-schema-to-typescript. + * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, + * and run json-schema-to-typescript to regenerate this file. + */ + +export type IUserParameters = + | { + CreateUser: CreateUser; + } + | { + User: User; + }; + +export interface CreateUser { + avatar_url?: string | null; + email: string; + name: string; + password: string; + [k: string]: unknown; +} +export interface User { + avatar_url?: string | null; + created_at: number; + email: string; + id: number; + name: string; + [k: string]: unknown; +}