fix: user id is now string, we need to get default on local

This commit is contained in:
linonetwo 2023-02-09 19:51:56 +08:00
parent fccf7e2f12
commit 483d1d67c6
11 changed files with 83 additions and 27 deletions

View File

@ -16,6 +16,7 @@ pub fn invoke_handler() -> impl Fn(tauri::Invoke) + Send + Sync + 'static {
get_workspaces, get_workspaces,
get_workspace, get_workspace,
create_user, create_user,
get_user,
create_doc, create_doc,
get_doc, get_doc,
put_blob, put_blob,

View File

@ -1,5 +1,5 @@
use cloud_database::{CreateUser, User}; use cloud_database::{CreateUser, User};
use ipc_types::document::CreateDocumentParameter; use ipc_types::{document::CreateDocumentParameter, user::GetUserParameters};
use crate::state::AppState; use crate::state::AppState;
@ -18,7 +18,7 @@ pub async fn create_user<'s>(
.metadata_db .metadata_db
.create_user(parameters.clone()) .create_user(parameters.clone())
.await; .await;
match new_user_result{ match new_user_result {
Ok(new_user_option) => match new_user_option { Ok(new_user_option) => match new_user_option {
Some((new_user, new_workspace)) => { Some((new_user, new_workspace)) => {
// a new private workspace is created, we have to create a yDoc for it // a new private workspace is created, we have to create a yDoc for it
@ -38,3 +38,16 @@ pub async fn create_user<'s>(
Err(error_message) => Err(error_message.to_string()), Err(error_message) => Err(error_message.to_string()),
} }
} }
#[tauri::command]
/// get the only one user in local sqlite
pub async fn get_user<'s>(
state: tauri::State<'s, AppState>,
parameters: GetUserParameters,
) -> Result<User, String> {
let db = &state.0.lock().await.metadata_db;
match db.get_user_by_email(&parameters.email).await.ok().unwrap() {
Some(user) => Ok(user),
None => Err("User not found".to_string()),
}
}

View File

@ -2,8 +2,14 @@ use cloud_database::{CreateUser, User};
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GetUserParameters {
pub email: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub enum IUserParameters { pub enum IUserParameters {
CreateUser(CreateUser), CreateUser(CreateUser),
User(User), User(User),
GetUserParameters(GetUserParameters),
} }

View File

@ -14,8 +14,7 @@ pub struct CreateWorkspace {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GetWorkspaces { pub struct GetWorkspaces {
// TODO: make all id string, on Octobase side, and rewrite all related tests pub user_id: String,
pub user_id: i32,
} }
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]

View File

@ -61,7 +61,7 @@ export const getBlob = async (parameters: GetBlob): Promise<number[]> =>
export const createUser = async (parameters: CreateUser): Promise<User> => export const createUser = async (parameters: CreateUser): Promise<User> =>
await { await {
created_at: 0, created_at: 0,
id: 1, id: '1',
email: 'xxx@xxx.xxx', email: 'xxx@xxx.xxx',
name: 'xxx', name: 'xxx',
}; };

View File

@ -21,10 +21,11 @@ import { applyUpdate } from '../../utils/index.js';
*/ */
export class TauriIPCProvider extends LocalProvider { export class TauriIPCProvider extends LocalProvider {
static id = 'tauri-ipc'; static id = 'tauri-ipc';
static defaultUserEmail = 'xxx@xx.xx';
/** /**
* // TODO: We only have one user in this version of app client. But may support switch user later. * // TODO: We only have one user in this version of app client. But may support switch user later.
*/ */
#defaultUserID = 1; #userID?: string;
#ipc: IPCMethodsType | undefined; #ipc: IPCMethodsType | undefined;
constructor(params: ProviderConstructorParams) { constructor(params: ProviderConstructorParams) {
@ -37,18 +38,25 @@ export class TauriIPCProvider extends LocalProvider {
} else { } else {
this.#ipc = await import('./ipc/methods.js'); this.#ipc = await import('./ipc/methods.js');
} }
// we create a default user if we don't have one.
try { try {
const user = await this.#ipc?.createUser({ const user = await this.#ipc?.getUser({
email: 'xxx@xx.xx', email: TauriIPCProvider.defaultUserEmail,
name: 'xxx',
password: 'xxx',
avatar_url: '',
}); });
this.#defaultUserID = user.id; this.#userID = user.id;
} catch (error) { } catch (error) {
// maybe user existed, which can be omited // maybe user not existed, we create a default user if we don't have one.
console.error(error); try {
const user = await this.#ipc?.createUser({
email: TauriIPCProvider.defaultUserEmail,
name: 'xxx',
password: 'xxx',
avatar_url: '',
});
this.#userID = user.id;
} catch (error) {
// maybe user existed, which can be omited
console.error(error);
}
} }
} }
@ -116,7 +124,7 @@ export class TauriIPCProvider extends LocalProvider {
const { id } = await this.#ipc.createWorkspace({ const { id } = await this.#ipc.createWorkspace({
name: meta.name, name: meta.name,
// TODO: get userID here // TODO: get userID here
user_id: this.#defaultUserID, user_id: this.#userID,
}); });
const workspaceUnit = await createWorkspaceUnit({ const workspaceUnit = await createWorkspaceUnit({
@ -144,7 +152,7 @@ export class TauriIPCProvider extends LocalProvider {
override async loadWorkspaces(): Promise<WorkspaceUnit[]> { override async loadWorkspaces(): Promise<WorkspaceUnit[]> {
assert(this.#ipc); assert(this.#ipc);
const { workspaces } = await this.#ipc.getWorkspaces({ const { workspaces } = await this.#ipc.getWorkspaces({
user_id: this.#defaultUserID, user_id: this.#userID,
}); });
const workspaceUnits = await Promise.all( const workspaceUnits = await Promise.all(
workspaces.map((meta: WorkspaceWithPermission) => { workspaces.map((meta: WorkspaceWithPermission) => {

View File

@ -14,7 +14,7 @@ import {
User, User,
} from './types/workspace'; } from './types/workspace';
import { GetBlob, PutBlob } from './types/blob'; import { GetBlob, PutBlob } from './types/blob';
import { CreateUser } from './types/user'; import { CreateUser, GetUserParameters } from './types/user';
export interface IPCMethodsType { export interface IPCMethodsType {
updateYDocument: typeof updateYDocument; updateYDocument: typeof updateYDocument;
@ -25,6 +25,7 @@ export interface IPCMethodsType {
putBlob: typeof putBlob; putBlob: typeof putBlob;
getBlob: typeof getBlob; getBlob: typeof getBlob;
createUser: typeof createUser; createUser: typeof createUser;
getUser: typeof getUser;
} }
export const updateYDocument = async (parameters: YDocumentUpdate) => export const updateYDocument = async (parameters: YDocumentUpdate) =>
@ -70,3 +71,8 @@ export const createUser = async (parameters: CreateUser) =>
await invoke<User>('create_user', { await invoke<User>('create_user', {
parameters, parameters,
}); });
export const getUser = async (parameters: GetUserParameters) =>
await invoke<User>('get_user', {
parameters,
});

View File

@ -21,6 +21,16 @@
} }
}, },
"additionalProperties": false "additionalProperties": false
},
{
"type": "object",
"required": ["GetUserParameters"],
"properties": {
"GetUserParameters": {
"$ref": "#/definitions/GetUserParameters"
}
},
"additionalProperties": false
} }
], ],
"definitions": { "definitions": {
@ -42,6 +52,15 @@
} }
} }
}, },
"GetUserParameters": {
"type": "object",
"required": ["email"],
"properties": {
"email": {
"type": "string"
}
}
},
"User": { "User": {
"type": "object", "type": "object",
"required": ["created_at", "email", "id", "name"], "required": ["created_at", "email", "id", "name"],
@ -57,8 +76,7 @@
"type": "string" "type": "string"
}, },
"id": { "id": {
"type": "integer", "type": "string"
"format": "int32"
}, },
"name": { "name": {
"type": "string" "type": "string"

View File

@ -10,6 +10,9 @@ export type IUserParameters =
} }
| { | {
User: User; User: User;
}
| {
GetUserParameters: GetUserParameters;
}; };
export interface CreateUser { export interface CreateUser {
@ -23,7 +26,11 @@ export interface User {
avatar_url?: string | null; avatar_url?: string | null;
created_at: number; created_at: number;
email: string; email: string;
id: number; id: string;
name: string; name: string;
[k: string]: unknown; [k: string]: unknown;
} }
export interface GetUserParameters {
email: string;
[k: string]: unknown;
}

View File

@ -123,8 +123,7 @@
"required": ["user_id"], "required": ["user_id"],
"properties": { "properties": {
"user_id": { "user_id": {
"type": "integer", "type": "string"
"format": "int32"
} }
} }
}, },
@ -172,8 +171,7 @@
"type": "string" "type": "string"
}, },
"id": { "id": {
"type": "integer", "type": "string"
"format": "int32"
}, },
"name": { "name": {
"type": "string" "type": "string"

View File

@ -42,7 +42,7 @@ export interface GetWorkspace {
[k: string]: unknown; [k: string]: unknown;
} }
export interface GetWorkspaces { export interface GetWorkspaces {
user_id: number; user_id: string;
[k: string]: unknown; [k: string]: unknown;
} }
export interface GetWorkspaceResult { export interface GetWorkspaceResult {
@ -62,7 +62,7 @@ export interface User {
avatar_url?: string | null; avatar_url?: string | null;
created_at: number; created_at: number;
email: string; email: string;
id: number; id: string;
name: string; name: string;
[k: string]: unknown; [k: string]: unknown;
} }