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_workspace,
create_user,
get_user,
create_doc,
get_doc,
put_blob,

View File

@ -1,5 +1,5 @@
use cloud_database::{CreateUser, User};
use ipc_types::document::CreateDocumentParameter;
use ipc_types::{document::CreateDocumentParameter, user::GetUserParameters};
use crate::state::AppState;
@ -18,7 +18,7 @@ pub async fn create_user<'s>(
.metadata_db
.create_user(parameters.clone())
.await;
match new_user_result{
match new_user_result {
Ok(new_user_option) => match new_user_option {
Some((new_user, new_workspace)) => {
// 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()),
}
}
#[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 serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GetUserParameters {
pub email: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub enum IUserParameters {
CreateUser(CreateUser),
User(User),
GetUserParameters(GetUserParameters),
}

View File

@ -14,8 +14,7 @@ pub struct CreateWorkspace {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct GetWorkspaces {
// TODO: make all id string, on Octobase side, and rewrite all related tests
pub user_id: i32,
pub user_id: String,
}
#[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> =>
await {
created_at: 0,
id: 1,
id: '1',
email: 'xxx@xxx.xxx',
name: 'xxx',
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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