beginnings of refactor

This commit is contained in:
Andrew Gundersen 2021-06-08 21:21:11 -05:00
commit 3b5da6124f
63 changed files with 1697 additions and 2656 deletions

126
src/ipc/account.ts Normal file
View file

@ -0,0 +1,126 @@
"use strict";
import { submit, fetchProfile, logout } from "../api/account";
import { ipcMain, IpcMainInvokeEvent } from "electron";
import { store } from "@/composables/store";
const parseAuthRes = (authRes: any) => {
const token = authRes.headers['set-cookie'][0].split(";")[0].split("=")[1] as string;
const profile = authRes.data as Profile;
return {
token,
profile
}
};
/**
* Get user profile from store and try to login with it.
*/
const onTokenLogin = async (
_event: IpcMainInvokeEvent,
_payload: null
): Promise<Profile | Error> => (
new Promise(async (resolve, reject) => {
console.log('[IPC]: user-profile');
// get jwt token and crimataId from store
const token = store.get('key');
const crimataId = store.get('crimataId');
// authenticate and fetch profile
try {
// attempt login with email token
const res = await fetchProfile(crimataId, token);
const parsed = parseAuthRes(res);
// return profile to renderer
resolve(parsed.profile);
} catch(e) {
reject(new Error('Failed to fetch profile.'));
}
})
)
const onLogin = async (
_event: IpcMainInvokeEvent,
payload: string
): Promise<Profile | Error> => (
new Promise(async (resolve, reject) => {
console.log('[IPC]: user-login');
const account = JSON.parse(payload);
if ( account.password && account.email ) {
try {
// attempt login with email password
const res = await submit(account.email, account.password);
const parsed = parseAuthRes(res);
// save jwt token and profile
store.set('key', parsed.token);
store.set('crimataId', parsed.profile.crimataId);
// init session
// return profile to renderer
resolve(parsed.profile);
} catch(e) {
console.log('[API]', e);
reject(new Error('Failed to authenticate'));
}
}
})
)
const onLogout = async (
_event: IpcMainInvokeEvent,
_payload: null
): Promise<void> => (
new Promise(async (resolve, reject) => {
console.log('[IPC]: user-logout');
try {
// post logout to backend
await logout();
// remove key and crimataId
store.delete('key');
store.delete('crimataId');
// TODO: kill crimata platform session
// endSession();
resolve();
} catch(e) {
reject(new Error('Failed to logout. Please try again.'));
}
})
)
export default function useAccountListeners(): void {
ipcMain.removeHandler("user-profile");
ipcMain.handle("user-profile", onProfile);
ipcMain.removeHandler("user-login");
ipcMain.handle("user-login", onLogin);
ipcMain.removeHandler("user-logout");
ipcMain.handle("user-logout", onLogout);
}

34
src/ipc/audio.ts Normal file
View file

@ -0,0 +1,34 @@
"use strict";
import { ipcMain, IpcMainInvokeEvent, IpcMainEvent } from "electron";
import { collect, flush } from "@/composbales/audio";
// handle the new audio data
const onAudioChunk = (
_e: IpcMainEvent,
payload: ArrayBuffer
) => {
console.log('[IPC]: audio-buffer');
collect(payload);
}
// Returns recorded audio to frontend and sets record to false.
const onGetAudio = async (
_event: IpcMainInvokeEvent,
_payload: null
): Promise<Error | ArrayBuffer[]> => {
console.log('[IPC]: stop-recording');
return await flush()
};
export default function useAudioListeners(): void {
ipcMain.removeAllListeners("audio-chunk");
ipcMain.on("audio-chunk", onAudioChunk);
ipcMain.removeHandler("get-audio");
ipcMain.handle("get-audio", onGetAudio);
}

17
src/ipc/index.ts Normal file
View file

@ -0,0 +1,17 @@
"use strict";
import useAccountListeners from "./account";
import useSessionListeners from "./session";
// import useAudioListeners from "./audio";
export default function useIpc(): void {
useAccountListeners();
useSessionListeners();
// useAudioListeners();
}

20
src/ipc/session.ts Normal file
View file

@ -0,0 +1,20 @@
"use strict";
import { ipcMain, IpcMainEvent, IpcMainInvokeEvent } from "electron";
import { sendMessage } from '@/session';
// Handle messages from window/client.
function onSendMessage(_event: IpcMainEvent, payload: Message): void {
sendMessage(payload);
}
// Login attempt, returns success or not.
function onLogin(_event: IpcMainEvent, payload: LoginPayload) => {
authenticate(payload.email, payload.password);
}
export default function useSessionListeners(): void {
ipcMain.removeAllListeners("client-message");
ipcMain.on("client-message", onSendMessage);
}