From 49215cf42dc47ad3642cf6151f82d37c9fc9ad58 Mon Sep 17 00:00:00 2001 From: Radu Cazacu Date: Fri, 19 Jan 2024 01:46:53 +0200 Subject: [PATCH] Remove uuids --- common/index.tsx | 26 +++++++++++ common/types/api.ts | 21 +++++++++ common/types/c2s.ts | 26 +++++++++++ common/types/hooks.ts | 1 + common/types/user.ts | 13 ++++++ hooks/c2s/useC2SConnect.ts | 2 + hooks/const.ts | 4 ++ hooks/hooks.ts | 6 +++ hooks/redux/slices/streamHandler.slice.ts | 55 +++++++++++++++++++++++ hooks/redux/slices/uuid.slice.ts | 18 ++++++++ hooks/redux/store.ts | 40 +++++++++++++++++ hooks/types/hooks.ts | 7 +++ hooks/utils/invoke-api.ts | 13 ++++++ src-tauri/src/commands/connect.rs | 51 +++++++++------------ src-tauri/src/commands/disconnect.rs | 41 +++++++---------- src-tauri/src/commands/get_session.rs | 36 +++++++-------- src-tauri/src/commands/message.rs | 47 +++++++++---------- src-tauri/src/commands/register.rs | 49 +++++++++----------- src-tauri/src/lib.rs | 1 - 19 files changed, 328 insertions(+), 129 deletions(-) create mode 100644 common/index.tsx create mode 100644 common/types/api.ts create mode 100644 common/types/c2s.ts create mode 100644 common/types/hooks.ts create mode 100644 common/types/user.ts create mode 100644 hooks/c2s/useC2SConnect.ts create mode 100644 hooks/const.ts create mode 100644 hooks/hooks.ts create mode 100644 hooks/redux/slices/streamHandler.slice.ts create mode 100644 hooks/redux/slices/uuid.slice.ts create mode 100644 hooks/redux/store.ts create mode 100644 hooks/types/hooks.ts create mode 100644 hooks/utils/invoke-api.ts diff --git a/common/index.tsx b/common/index.tsx new file mode 100644 index 0000000..0c14ddf --- /dev/null +++ b/common/index.tsx @@ -0,0 +1,26 @@ +import { createContext, ReactNode, useContext, useMemo } from 'react'; +import { ApiProviderContext } from './types/api'; +import { ApiHooks } from 'hooks/types/hooks'; + +interface ApiProviderProps { + children: ReactNode | ReactNode[]; + hooks: ApiHooks; +} + +export const ApiContext = createContext>({}); + +export const ApiProvider = ({ children, hooks }: ApiProviderProps) => { + const coreConfig = useMemo(() => { + return { + hooks, + }; + }, [hooks]); + + return ( + {children} + ); +}; + +export const useApiProvider = () => { + return useContext(ApiContext) as ApiProviderContext; +}; diff --git a/common/types/api.ts b/common/types/api.ts new file mode 100644 index 0000000..f51520d --- /dev/null +++ b/common/types/api.ts @@ -0,0 +1,21 @@ +import { ApiHooks } from 'hooks/types/hooks'; + +export interface ApiInvokerOptions { + type: ApiInvokeTypes; +} + +export type Variables = { [key: string]: string }; +export type ApiInvokeTypes = + | 'message' + | 'register' + | 'connect' + | 'disconnect' + | 'getSession' + | 'getAccInfo'; + +export interface ApiProviderContext { + hooks: ApiHooks; +} +export type ApiInvokerResults = { + data: T; +}; diff --git a/common/types/c2s.ts b/common/types/c2s.ts new file mode 100644 index 0000000..075d9ff --- /dev/null +++ b/common/types/c2s.ts @@ -0,0 +1,26 @@ +export type ServiceTCPConnectionAccepted = { + ServiceConnectionAccepted: { + id: string; + }; +}; + +export type ServiceRegisterAccepted = { + ServiceRegisterAccepted: { + id: string; + request_id: string; + }; +}; + +export type ServiceConnectionAccepted = { + ServiceConnectionAccepted: { + id: string; + request_id: string; + }; +}; + +export type ServiceDisconnect = { + ServiceDisconnectAccepted: { + uuid: string; + request_id: string; + }; +}; diff --git a/common/types/hooks.ts b/common/types/hooks.ts new file mode 100644 index 0000000..ebf29c7 --- /dev/null +++ b/common/types/hooks.ts @@ -0,0 +1 @@ +export type Hook = (input: T) => any; diff --git a/common/types/user.ts b/common/types/user.ts new file mode 100644 index 0000000..2c5f3eb --- /dev/null +++ b/common/types/user.ts @@ -0,0 +1,13 @@ +export interface User { + email: string; + username: string; + online?: boolean; +} + +export interface UsersState { + users: User[]; + onlineUsersByUsername: string[]; + loading: boolean; + error: string | null; + typingUsers: string[]; +} diff --git a/hooks/c2s/useC2SConnect.ts b/hooks/c2s/useC2SConnect.ts new file mode 100644 index 0000000..b09eaad --- /dev/null +++ b/hooks/c2s/useC2SConnect.ts @@ -0,0 +1,2 @@ +import { invoke } from '@tauri-apps/api/core'; +export const handler = (input: any) => {}; diff --git a/hooks/const.ts b/hooks/const.ts new file mode 100644 index 0000000..529d30e --- /dev/null +++ b/hooks/const.ts @@ -0,0 +1,4 @@ +export const API_URL = + process.env.NEXT_PUBLIC_FRAMEWORK === 'citadel_local' + ? process.env.CITADEL_PUBLIC_LOCAL_DOMAIN + : process.env.CITADEL_PUBLIC_DOMAIN; diff --git a/hooks/hooks.ts b/hooks/hooks.ts new file mode 100644 index 0000000..c02e883 --- /dev/null +++ b/hooks/hooks.ts @@ -0,0 +1,6 @@ +import { handler as useConnect_c2s } from './c2s/useC2SConnect'; +export const appHooks = { + c2s: { + useConnect: useConnect_c2s, + }, +}; diff --git a/hooks/redux/slices/streamHandler.slice.ts b/hooks/redux/slices/streamHandler.slice.ts new file mode 100644 index 0000000..beab534 --- /dev/null +++ b/hooks/redux/slices/streamHandler.slice.ts @@ -0,0 +1,55 @@ +import { createSlice } from '@reduxjs/toolkit'; + +type Sessions = { + current_used_session_server: string; + current_sessions: { + [key: string]: { [key: string]: string }; + }; +}; +export type ContextType = 'Register' | 'GetSession'; + +const initialState: { + context: { + [key: string]: ContextType; + }; + sessions: Sessions; +} = { + context: {}, + sessions: { + current_used_session_server: '', + current_sessions: {}, + }, +}; + +const streamExecSlice = createSlice({ + name: 'stram_handler', + initialState, + reducers: { + addToContext: (state, action) => { + const req_id = action.payload.req_id; + + const context_type: ContextType = + action.payload.context_type ?? state.context[req_id]; + const payload: { [key: string]: string | number } = + action.payload.payload; + + let updatedObject: { [key: string]: string | number } = {}; + + for (const key in payload) { + if (key != 'request_id') { + updatedObject[key] = payload[key]; + } + } + + state.context[req_id] = context_type; + }, + + setCurrentServer: (state, action) => { + state.sessions.current_used_session_server = action.payload; + }, + }, +}); + +const { reducer, actions } = streamExecSlice; +export const { addToContext, setCurrentServer } = actions; +export default reducer; diff --git a/hooks/redux/slices/uuid.slice.ts b/hooks/redux/slices/uuid.slice.ts new file mode 100644 index 0000000..1dd1770 --- /dev/null +++ b/hooks/redux/slices/uuid.slice.ts @@ -0,0 +1,18 @@ +import { createSlice } from '@reduxjs/toolkit'; + +const initialState = { + uuid: '', +}; +const showSwitchToBusinessOverlaySlice = createSlice({ + name: 'uuid', + initialState, + reducers: { + setUuid: (state, action) => { + state.uuid = action.payload; + }, + }, +}); + +const { reducer, actions } = showSwitchToBusinessOverlaySlice; +export const { setUuid } = actions; +export default reducer; diff --git a/hooks/redux/store.ts b/hooks/redux/store.ts new file mode 100644 index 0000000..05f2c21 --- /dev/null +++ b/hooks/redux/store.ts @@ -0,0 +1,40 @@ +import { configureStore } from '@reduxjs/toolkit'; +import uuid from './slices/uuid.slice'; +import executor from './slices/streamHandler.slice'; +import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; +import { + ServiceRegisterAccepted, + ServiceTCPConnectionAccepted, +} from '@common/types/c2s'; +const stringMiddleware = + () => + (next: any) => + (action: string | { type: string; payload?: unknown }) => { + if (typeof action === 'string') { + return next({ type: action }); + } + return next(action); + }; + +const store = configureStore({ + reducer: { uuid, context: executor }, + devTools: process.env.NODE_ENV !== 'production', + middleware: (getDefaultMiddleware) => + getDefaultMiddleware().concat(stringMiddleware), +}); + +export default store; + +export type RootState = ReturnType; +export type AppDispatch = typeof store.dispatch; + +export const useAppDispatch: () => AppDispatch = useDispatch; +export const useAppSelector: TypedUseSelectorHook = useSelector; + +type Data = ServiceRegisterAccepted | ServiceTCPConnectionAccepted; + +export type ContextType = + | 'RegisterAndConnect' + | 'GetSession' + | 'ADD_TO_CONTEXT' + | 'HANDLE_PACKET'; diff --git a/hooks/types/hooks.ts b/hooks/types/hooks.ts new file mode 100644 index 0000000..e509999 --- /dev/null +++ b/hooks/types/hooks.ts @@ -0,0 +1,7 @@ +import { Hook } from '@common/types/hooks'; + +export interface ApiHooks { + c2s: { + useConnect: Hook; + }; +} diff --git a/hooks/utils/invoke-api.ts b/hooks/utils/invoke-api.ts new file mode 100644 index 0000000..c51ad15 --- /dev/null +++ b/hooks/utils/invoke-api.ts @@ -0,0 +1,13 @@ +import { ApiInvokeTypes, Variables } from '@common/types/api'; +import { invoke } from '@tauri-apps/api/core'; + +const invokeApi = async (type: ApiInvokeTypes, variables: Variables) => { + try { + const data = await invoke(type, variables); + return { data }; + } catch (error: any) { + throw new Error(error); + } +}; + +export default invokeApi; diff --git a/src-tauri/src/commands/connect.rs b/src-tauri/src/commands/connect.rs index 34617ac..f353d94 100644 --- a/src-tauri/src/commands/connect.rs +++ b/src-tauri/src/commands/connect.rs @@ -1,44 +1,37 @@ use citadel_workspace_types::InternalServiceRequest::Connect; use futures::SinkExt; use tauri::State; -use uuid::Uuid; use crate::structs::ConnectionState; #[tauri::command] pub async fn connect( - uuid: String, username: String, password: String, request_id: String, state: State<'_, ConnectionState>, ) -> Result { - match Uuid::parse_str(&uuid) { - Ok(uuid) => { - let payload = Connect { - username, - password: password.into_bytes().into(), - connect_mode: Default::default(), - udp_mode: Default::default(), - keep_alive_timeout: Default::default(), - session_security_settings: Default::default(), - request_id: request_id.parse().unwrap(), - }; - if state - .sink - .lock() - .await - .as_mut() - .unwrap() - .send(bincode2::serialize(&payload).unwrap().into()) - .await - .is_ok() - { - Ok(request_id.to_string()) - } else { - Err("Unable to connect".to_string()) - } - } - Err(_) => return Err("Invalid UUID".to_string()), + let payload = Connect { + username, + password: password.into_bytes().into(), + connect_mode: Default::default(), + udp_mode: Default::default(), + keep_alive_timeout: Default::default(), + session_security_settings: Default::default(), + request_id: request_id.parse().unwrap(), + }; + if state + .sink + .lock() + .await + .as_mut() + .unwrap() + .send(bincode2::serialize(&payload).unwrap().into()) + .await + .is_ok() + { + Ok(request_id.to_string()) + } else { + Err("Unable to connect".to_string()) } } diff --git a/src-tauri/src/commands/disconnect.rs b/src-tauri/src/commands/disconnect.rs index 9254a6f..1b73d66 100644 --- a/src-tauri/src/commands/disconnect.rs +++ b/src-tauri/src/commands/disconnect.rs @@ -2,37 +2,30 @@ use crate::structs::ConnectionState; use citadel_workspace_types::InternalServiceRequest::Disconnect; use futures::SinkExt; use tauri::State; -use uuid::Uuid; #[tauri::command] pub async fn disconnect( - uuid: String, cid: u64, request_id: String, state: State<'_, ConnectionState>, ) -> Result { - match Uuid::parse_str(&uuid) { - Ok(uuid) => { - let payload = Disconnect { - cid, - request_id: request_id.parse().unwrap(), - }; + let payload = Disconnect { + cid, + request_id: request_id.parse().unwrap(), + }; - if state - .sink - .lock() - .await - .as_mut() - .unwrap() - .send(bincode2::serialize(&payload).unwrap().into()) - .await - .is_ok() - { - Ok(request_id.to_string()) - } else { - Err("Unable to disconnect".to_string()) - } - } - Err(_) => return Err("Invalid UUID".to_string()), + if state + .sink + .lock() + .await + .as_mut() + .unwrap() + .send(bincode2::serialize(&payload).unwrap().into()) + .await + .is_ok() + { + Ok(request_id.to_string()) + } else { + Err("Unable to disconnect".to_string()) } } diff --git a/src-tauri/src/commands/get_session.rs b/src-tauri/src/commands/get_session.rs index 89ac2c3..5fb6e06 100644 --- a/src-tauri/src/commands/get_session.rs +++ b/src-tauri/src/commands/get_session.rs @@ -6,29 +6,23 @@ use uuid::Uuid; #[tauri::command] pub async fn get_session( - uuid: String, state: State<'_, ConnectionState>, _window: tauri::Window, ) -> Result { - match Uuid::parse_str(&uuid) { - Ok(uuid) => { - let request_id = Uuid::new_v4(); - let payload = InternalServiceRequest::GetSessions { request_id }; - if state - .sink - .lock() - .await - .as_mut() - .unwrap() - .send(bincode2::serialize(&payload).unwrap().into()) - .await - .is_ok() - { - Ok(request_id.to_string()) - } else { - Err("Unable to get_session".to_string()) - } - } - Err(_) => return Err("Invalid UUID".to_string()), + let request_id = Uuid::new_v4(); + let payload = InternalServiceRequest::GetSessions { request_id }; + if state + .sink + .lock() + .await + .as_mut() + .unwrap() + .send(bincode2::serialize(&payload).unwrap().into()) + .await + .is_ok() + { + Ok(request_id.to_string()) + } else { + Err("Unable to get_session".to_string()) } } diff --git a/src-tauri/src/commands/message.rs b/src-tauri/src/commands/message.rs index d855a74..2c4a3d7 100644 --- a/src-tauri/src/commands/message.rs +++ b/src-tauri/src/commands/message.rs @@ -1,43 +1,36 @@ use citadel_workspace_types::InternalServiceRequest::Message; use futures::SinkExt; use tauri::State; -use uuid::Uuid; use crate::structs::ConnectionState; #[tauri::command] pub async fn message( - uuid: String, message: String, cid: u64, peer_cid: Option, request_id: String, state: State<'_, ConnectionState>, ) -> Result { - match Uuid::parse_str(&uuid) { - Ok(uuid) => { - let payload = Message { - message: message.into_bytes(), - cid, - peer_cid, - security_level: Default::default(), - request_id: request_id.parse().unwrap(), - }; - if state - .sink - .lock() - .await - .as_mut() - .unwrap() - .send(bincode2::serialize(&payload).unwrap().into()) - .await - .is_ok() - { - Ok(request_id.to_string()) - } else { - return Err("Unable to message".to_string()); - } - } - Err(_) => return Err("Invalid UUID".to_string()), + let payload = Message { + message: message.into_bytes(), + cid, + peer_cid, + security_level: Default::default(), + request_id: request_id.parse().unwrap(), + }; + if state + .sink + .lock() + .await + .as_mut() + .unwrap() + .send(bincode2::serialize(&payload).unwrap().into()) + .await + .is_ok() + { + Ok(request_id.to_string()) + } else { + return Err("Unable to message".to_string()); } } diff --git a/src-tauri/src/commands/register.rs b/src-tauri/src/commands/register.rs index 1372b58..51349fb 100644 --- a/src-tauri/src/commands/register.rs +++ b/src-tauri/src/commands/register.rs @@ -18,32 +18,27 @@ pub async fn register( ) -> Result { let server_addr = SocketAddr::from_str(&server_addr).map_err(|_| "Invalid server address")?; let request_id = Uuid::new_v4(); - match Uuid::parse_str(&uuid) { - Ok(uuid) => { - let payload = InternalServiceRequest::Register { - request_id, - server_addr, - full_name, - username: username.clone(), - proposed_password: proposed_password.into_bytes().into(), - connect_after_register: true, - session_security_settings: Default::default(), - }; - if state - .sink - .lock() - .await - .as_mut() - .unwrap() - .send(bincode2::serialize(&payload).unwrap().into()) - .await - .is_ok() - { - Ok(request_id.to_string()) - } else { - Err("Unable to register".to_string()) - } - } - Err(_) => return Err("Invalid UUID".to_string()), + let payload = InternalServiceRequest::Register { + request_id, + server_addr, + full_name, + username: username.clone(), + proposed_password: proposed_password.into_bytes().into(), + connect_after_register: true, + session_security_settings: Default::default(), + }; + if state + .sink + .lock() + .await + .as_mut() + .unwrap() + .send(bincode2::serialize(&payload).unwrap().into()) + .await + .is_ok() + { + Ok(request_id.to_string()) + } else { + Err("Unable to register".to_string()) } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 0b0466e..3f7ad85 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -17,7 +17,6 @@ use std::time::Duration; use tauri::{Manager, State}; use tokio::net::TcpStream; use tokio::time::timeout; -use uuid::Uuid; fn send_response( packet_name: &str,