From: Andrew Gundersen Date: Sun, 11 Jul 2021 16:54:39 +0000 (-0500) Subject: updated protocols X-Git-Tag: v2.0.1~10^2~5 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=531a32a5a588fd55225f878dd5106d2aefca281d;p=mime-chat updated protocols --- diff --git a/build/icon.icns b/build/icon.icns new file mode 100644 index 0000000..b173533 Binary files /dev/null and b/build/icon.icns differ diff --git a/src/account.ts b/src/account.ts index 965502c..6ea1ac4 100644 --- a/src/account.ts +++ b/src/account.ts @@ -1,66 +1,79 @@ +import { app } from "electron"; +import { parseAuthRes } from "@/auth"; import { postAuth, postLogin, postLogout } from "@/api/account"; -import { endSession, launchSession } from "@/session"; -import { getToken, setToken, setProfile, clearStore } from "./store"; -import { parseAuthRes } from "./auth"; -import { ipcEmit } from "@/composables/useEmitter"; +import { updateAppUI, launchSession, endSession } from "@/session"; +import { getToken, setToken, clearToken } from "./store"; +import { backgroundMitt, ipcEmit } from "@/composables/useEmitter"; -export const accountAuth = async (): Promise => { + +/* Either null or a crimataId */ +let account: string | null = null; + + +export const accountAuth = async (): Promise => { /* attempt to get a login token from the store */ const token = getToken(); - /* try to login with it, returns platform secret and new token on success */ - if (token) { - try { + try { - const res = await postAuth(token); + if (token) { + // attempt to login with token + const res = await postAuth(token); const parsed = parseAuthRes(res); - setToken(parsed.token) - setProfile(parsed.profile); + // save jwt token and profile + setToken(parsed.token); + account = parsed.crimataId + + // launch session + launchSession(parsed.token); - return { - profile: parsed.profile, - token: parsed.token - }; + } else throw(new Error('Failed to authenticate (no token).')); - } catch(e) { - console.log('[ACCOUNT]', e); - clearStore(); - throw(new Error('Failed to authenticate.')); + } catch (e) { + console.log(e); + clearToken(); + + } finally { + + // push state changes to the frontend + updateAppState(); - } - } else { - throw(new Error('Unable to authenticate.')); } + }; -export const accountLogin: IpcHandlerCallback = async (payload) => { - const account = payload as AccountCredentials; +export const accountLogin = async (payload: any): Promise => { + + const creds = payload as AccountCredentials; + try { // attempt login with email password - const res = await postLogin(account.email, account.password); + const res = await postLogin(creds.email, creds.password); const parsed = parseAuthRes(res); // save jwt token and profile setToken(parsed.token); - setProfile(parsed.profile); + account = parsed.crimataId; // launch session launchSession(parsed.token); - // return profile to renderer - return parsed.profile; + // push state changes to the frontend + updateAppState(); + + return; } catch(e) { - clearStore(); + clearToken(); throw e; } -} +} export const accountLogout = async (): Promise => { @@ -68,8 +81,12 @@ export const accountLogout = async (): Promise => { // post logout to backend await postLogout(); - // remove key and crimataId - clearStore(); + // remove key and account + clearToken(); + account = null; + + // push account state to browser + ipcEmit("set-account", account); // kill crimata platform session endSession(); @@ -83,4 +100,24 @@ export const accountLogout = async (): Promise => { } +// logout when auth fails on platform end +backgroundMitt.on("close-auth-fail", async (_payload: any) => { + + await accountLogout(); + +}); + +// emits state of account, version, and UI +export const updateAppState = () => { + + ipcEmit("set-account", account); + + ipcEmit("set-version", app.getVersion()); + + updateAppUI(); + +} + + + diff --git a/src/audio.ts b/src/audio.ts deleted file mode 100644 index 7ae2562..0000000 --- a/src/audio.ts +++ /dev/null @@ -1,187 +0,0 @@ -/* eslint @typescript-eslint/no-var-requires: "off" */ - -"use strict"; - -// where the audio goes -let buffer: ArrayBuffer[] = []; - -// place audio data in buffer -export const collect: IpcListenerCallback = (chunk) => { - if (chunk) buffer.push(chunk); -} - -// return audio and clear buffer -export const flush = async () => { - const bufferCopy = buffer; - buffer = []; - return bufferCopy; -} - -// import { backgroundMitt } from '@/modules/emitter'; -// const portAudio = require('naudiodon'); - -// // Audio in and out stream objects. -// let ai: typeof portAudio.AudioIO | boolean = false; -// let ao: typeof portAudio.AudioIO | boolean = false; - -// // Whether activly recording. -// let record = false; - -// const audioContainer = { -// input: '', -// } - -// const audioOptions = { -// channelCount: 1, -// sampleFormat: 16, -// sampleRate: 16000, -// deviceId: -1, -// closeOnError: false, -// } - -// export const toggleRecord = (): void => { record = !record }; - - -// export const fetchAudioInput = (): Promise => ( - -// new Promise((resolve, reject) => { - -// try { -// resolve(audioContainer.input); -// toggleRecord(); -// } catch (e) { -// reject(new Error('Failed to fetch the audio.')) -// } - -// }) -// ) - - -// // Main audio function run by run.ts module. -// export function initAudioIO(): void { -// console.log("AUDIO:Starting io streams.") - -// if (!ai) { - -// // Initialize and start input stream. -// ai = new portAudio.AudioIO({ inOptions: audioOptions }); -// ai.setEncoding("hex"); -// ai.start(); - -// // On each data chunk... -// ai.on('data', (chunk: string) => { - -// // If recording, we capture the data. -// if (record) { -// console.log('AUDIO:Recording...') -// audioContainer.input += chunk; -// } - -// // Else, we don't capture and also clear audioContainer. -// else { -// if (audioContainer.input.length) { -// audioContainer.input = ""; -// } -// } - -// }); -// } - -// if (!ao) { - -// // Initialize and start input stream. -// ao = new portAudio.AudioIO({ outOptions: audioOptions }); -// ao.start(); - -// } -// } - - -// // ---Audio playback-------------------------------------------- - -// // Split Buffer into an array of len-sized Buffers. -// function bufSplit(buf: Buffer, len: number): Array { -// const chunks = []; -// let i = 0; -// let L = len; - -// while(i < buf.byteLength) { -// chunks.push(buf.slice(i, L)); -// i = L; -// L += len; -// } - -// return chunks; -// } - -// // Audio playback. -// export function play(input: string): void { - -// // Format the audio. -// const audio = bufSplit( -// Buffer.from(input as string, 'hex'), -// 8192 -// ); - -// // Called on end of write. -// const callback = () => { - -// // We stop audio playback anim. -// backgroundMitt.emit('ipc-renderer', { -// endpoint: 'stop-playback-anim' -// }); - -// } - -// write(); - -// // Iterate through audio array and write buffers to portAudio writable. -// function write() { -// let chunk: Buffer; -// let ok = true; -// let i = 0; - -// do { -// chunk = audio[i]; -// if (i === audio.length - 1) { -// // write last chunk. -// ao.write(chunk, null, callback); -// } else { -// // check for backpreassure. -// ok = ao.write(chunk, null); -// } -// i++; -// } while (i < audio.length && ok); - -// if (i < audio.length) { -// // Had to stop early! -// // Write some more once it drains. -// ao.once('drain', write); -// } -// } -// } - -// // ------------------------------------------------------------- - -// // Get's called on window close. -// export async function stopStream() { -// console.log("AUDIO:Stopping audio stream.") -// if (ai) { -// try { -// await ai.quit() -// } catch(e){ -// console.log('AUDIO: Failed to shutdown audio input.'); -// throw e; -// } -// } -// if (ao) { -// try { -// await ao.quit() -// } catch(e){ -// console.log('AUDIO: Failed to shutdown audio output.'); -// throw e; -// } -// } -// } - - diff --git a/src/auth.ts b/src/auth.ts index 14ed36a..31c66e0 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,13 +1,5 @@ - export 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 - } -}; - - - - + const crimataId = authRes.data as string; + return {token, crimataId} +}; \ No newline at end of file diff --git a/src/composables/useMessageCanvas.ts b/src/composables/useMessageCanvas.ts deleted file mode 100644 index c85f7ed..0000000 --- a/src/composables/useMessageCanvas.ts +++ /dev/null @@ -1,36 +0,0 @@ - - - -export default class Canvas { - - messages: Message[]; - - /* seed canvas with messages on init */ - constructor(messages: Message[]) { - this.messages = messages; - ipcEmit("seed-view", this.messages); - } - - /* add a new message to the canvas */ - add(message: Message) { - this.messages.push(message); - ipcEmit("update-view", message); - } - - /* update an existing message */ - update(message: Message) { - - /* get the target message */ - let target_message = this.messages.filter((m: Message) => { - return m.uid = message.uid; - })[0]; - - /* replace the target message */ - if (target_message) { - target_message = message; - ipcEmit("update-view", message); - } - - } - -} diff --git a/src/composables/useWebsockets.ts b/src/composables/useWebsockets.ts index aed517c..ed0973e 100644 --- a/src/composables/useWebsockets.ts +++ b/src/composables/useWebsockets.ts @@ -76,7 +76,6 @@ export default function useWebSockets( setTimeout(() => { connect(socketUrl, secret), _reconnectTimeout }); - } }); diff --git a/src/ipc/handlers.ts b/src/ipc/handlers.ts index 5529662..1c812cd 100644 --- a/src/ipc/handlers.ts +++ b/src/ipc/handlers.ts @@ -1,7 +1,7 @@ "use strict"; -import { accountLogin, accountLogout, accountProfile } from "@/account"; +import { accountLogin, accountLogout } from "@/account"; import { IpcHandler } from "@/composables/useIpcMain"; import { flush } from "@/audio"; @@ -24,5 +24,3 @@ export const getAudioHandler = new IpcHandler({ channel: GET_AUDIO_CHANNEL, handlerCallback: flush }); - - diff --git a/src/ipc/listeners.ts b/src/ipc/listeners.ts index 17af103..0f63873 100644 --- a/src/ipc/listeners.ts +++ b/src/ipc/listeners.ts @@ -2,25 +2,19 @@ import { IpcListener } from "@/composables/useIpcMain" import { sendMessage } from '@/session'; import { collect } from "@/audio"; -import { updateAppState } from "@/session"; +import { updateAppState } from "@/account"; import { onNavBar } from "@/window"; +import { toggleRecord } from "@/audio"; const CLIENT_MESSAGE_CHANNEL = "post-session-send" -const GET_AUDIO_CHANNEL = "post-audio-collect"; const APP_MOUNT_CHANNEL = "post-app-mount"; const NAV_BAR_CHANNEL = "post-nav-bar"; -const WINDOW_BLUR_CHANNEL = "post-window-focus"; -export const messageListener = new IpcListener({ +export const messageListener = new IpcListener({ channel: CLIENT_MESSAGE_CHANNEL, listenerCallback: sendMessage }); -export const audioChunkListener = new IpcListener({ - channel: GET_AUDIO_CHANNEL, - listenerCallback: collect -}); - export const appMountListener = new IpcListener({ channel: APP_MOUNT_CHANNEL, listenerCallback: updateAppState @@ -31,16 +25,7 @@ export const navBarListener = new IpcListener({ listenerCallback: onNavBar }); - -const onWindowFocus: IpcListenerCallback<{ isFocused: boolean }> = (payload) => { - if (payload.isFocused) { - console.log('window is focused') - } else { - console.log('window is blurred') - } -} - -export const windowFocusListener = new IpcListener({ - channel: WINDOW_BLUR_CHANNEL, - listenerCallback: onWindowFocus -}); +export const recordingListener = new IpcListener({ + channel: RECORDING_CHANNEL, + listenerCallback: toggleRecord +}); \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 6df9f6c..99c1dfa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,10 +11,8 @@ import initIpcMain from "@/ipc/index"; import { accountAuth } from "./account"; -import { launchSession, updateAppState } from "./session"; import createWindow from "./window"; -let authState: AuthState | null; export default async function main() { @@ -24,16 +22,7 @@ export default async function main() { /* launch browser window */ await createWindow(); - try { - authState = await accountAuth() as AuthState; - } catch(e) { - console.log('AUTH:', e); - authState = null; - } finally { - if (authState) { - launchSession(authState.token as string); - } - updateAppState(); - } + /* try to authenticate with token */ + await accountAuth(); } diff --git a/src/profile.ts b/src/profile.ts new file mode 100644 index 0000000..0c5428d --- /dev/null +++ b/src/profile.ts @@ -0,0 +1,22 @@ +import { ipcEmit } from "@/composables/useEmitter"; + + +export default class ProfileHandler { + + profile: Profile; + + constructor(profile: Profile) { + this.profile = profile; + this.emit(); + } + + emit() { + ipcEmit("set-profile", this.profile); + } + + update(update: Update) { + this.profile = update.data as Profile; + this.emit() + } + +} \ No newline at end of file diff --git a/src/render/App.vue b/src/render/App.vue index ba56bac..3e5cf13 100644 --- a/src/render/App.vue +++ b/src/render/App.vue @@ -1,15 +1,10 @@ @@ -19,11 +18,8 @@ import { defineComponent } from "vue"; import { postNavBar } from "@/render/ipc"; - import ConnectionStatus from "./connectionStatus.vue"; - export default defineComponent({ name: "Header", - components: { ConnectionStatus }, setup() { const postNavBarExit = () => postNavBar('close'); const postNavBarMin = () => postNavBar('min'); diff --git a/src/render/components/inputItem.vue b/src/render/components/inputItem.vue index 5deb739..5b9c403 100644 --- a/src/render/components/inputItem.vue +++ b/src/render/components/inputItem.vue @@ -5,8 +5,9 @@ :class="{ playing: recording }" :style="{ top: `${elementY}px`, left: `${elementX}px` }" > -
{{ initials }}
+ +
{{ profile.photo }}
@@ -28,7 +29,7 @@ import { defineComponent } from "vue"; import draggify from "@/render/composables/useDraggify"; - +import { profile } from "@/render/shared/profile"; import useTextInputController from "@/render/components/controllers/inputItem.control.text"; @@ -38,8 +39,6 @@ import useTextInputController from export default defineComponent({ name: "InputItem", - props: ["initials"], - setup() { // Default values for position. @@ -55,6 +54,7 @@ export default defineComponent({ const recording = false; return { + profile, elementX, elementY, recording diff --git a/src/render/components/login.vue b/src/render/components/login.vue index 1de47ca..e786549 100644 --- a/src/render/components/login.vue +++ b/src/render/components/login.vue @@ -1,14 +1,8 @@