]> Repos - mime-chat/commitdiff
working protocols
authorAndrew Gundersen <gundersena@xavier.edu>
Tue, 29 Jun 2021 16:30:35 +0000 (11:30 -0500)
committerAndrew Gundersen <gundersena@xavier.edu>
Tue, 29 Jun 2021 16:30:35 +0000 (11:30 -0500)
src/account.ts
src/ipc/listeners.ts
src/main.ts
src/messages.ts
src/render/listeners.ts
src/render/shared/messages.ts
src/session.ts
src/types.ts

index ac53b23b61c87767685ac927e6171d183c242505..965502cb40bf9c8b4fcc4327460be0f2175de1a4 100644 (file)
@@ -1,10 +1,9 @@
 
 import { postAuth, postLogin, postLogout } from "@/api/account";
 import { endSession, launchSession } from "@/session";
-import { getToken,  setToken, setProfile, getProfile, clearStore } from "./store";
+import { getToken,  setToken, setProfile, clearStore } from "./store";
 import { parseAuthRes } from "./auth";
 import { ipcEmit } from "@/composables/useEmitter";
-import { messages } from "@/messages";
 
 export const accountAuth = async (): Promise<Error | AuthState> => {
 
@@ -84,13 +83,4 @@ export const accountLogout = async (): Promise<Error | void> => {
 
 }
 
-export const updateAppState = (): void => {
-
-    const profile = getProfile();
-
-    ipcEmit("set-profile", profile);
-
-    ipcEmit("init-messages", messages)
-
-}
 
index cb17f65496913d7c526822652e4351020d8a0a99..17af103218285d503b369e435d8809a8d50070cb 100644 (file)
@@ -2,7 +2,7 @@
 import { IpcListener } from "@/composables/useIpcMain"
 import { sendMessage } from '@/session';
 import { collect } from "@/audio";
-import { updateAppState } from "@/account";
+import { updateAppState } from "@/session";
 import { onNavBar } from "@/window";
 
 const CLIENT_MESSAGE_CHANNEL = "post-session-send"
index a360835a692426a0352df7ac43114372b958858c..6df9f6cee6623a661d344d24c9bf6ed0c3b6ccb0 100644 (file)
@@ -10,8 +10,8 @@
  */
 
 import initIpcMain from "@/ipc/index";
-import  { accountAuth, updateAppState } from "./account";
-import { launchSession } from "./session";
+import  { accountAuth } from "./account";
+import { launchSession, updateAppState } from "./session";
 import createWindow from "./window";
 
 let authState: AuthState | null;
index fa2ab22c98987df57d8cd022e39cbea88352f1ee..9a72b38a5eda762009466f1bc84f94eefcc5feed 100644 (file)
@@ -1,40 +1,67 @@
 import { ipcEmit } from "@/composables/useEmitter";
 
-export let messages: Message[] = [];
+export default class Messages {
 
-export const setMessages = (init: {messages: Message[]}) => {
-    console.log("setMessages", init);
-    messages = init.messages;
-    ipcEmit("init-messages", messages);
-}
+    messages: Message[];
 
-export const newMessage = (message: Message) => {
-    console.log("newMessage", message);
-    messages.push(message);
-    ipcEmit("add-message", message);
-}
+    constructor(messages: Message[]) {
+        console.log(messages);
+        this.messages = messages;
+        this.emit();
+    }
 
-export const annotateMessage = (annotation: Annotation) => {
-    console.log("annotateMessage", annotation);
+    emit() {
+        ipcEmit("init-messages", this.messages);
+    }
 
-    for (let i in messages) {
+    update(update: Update) {
 
-        if (messages[i].uid == annotation.uid) {
+        if (update.name === "add") {
+            this.messages.push(update.data as Message);
+            ipcEmit("add-message", update.data);
+        } 
 
-            if (annotation.data.hasOwnProperty("content")) {
-                const data = annotation.data as ContentData;
-                messages[i].content = data.content;
-            }
+        else if (update.name === "annotate") {
+            this._annotate(update.data as Annotation);
+            ipcEmit("update-message", update.data);
+        }
 
-            if (annotation.data.hasOwnProperty("context")) {
-                const data = annotation.data as ContextData;
-                messages[i].context = data.context;
-            }
+        else {
+            this._delete(update.data as string);
+            ipcEmit("delete-message", update.data);
+        }
+
+    }
+
+    _annotate(annotation: Annotation) {
+
+        for (let i in this.messages) {
 
-            ipcEmit("update-message", annotation);
-            break;
+            if (this.messages[i].uid == annotation.uid) {
+
+                if (annotation.name == "content") {
+                    this.messages[i].content = annotation.data as string[];
+                }
+
+                if (annotation.name == "context") {
+                    this.messages[i].context = annotation.data as string;
+                }
+
+                ipcEmit("update-message", annotation);
+
+            }
 
         }
+
     }
-}
 
+    _delete(uid: string) {
+        for (var i = 0; i < this.messages.length; i++) {
+            if (this.messages[i].uid === uid) {
+                this.messages.splice(i, 1);
+                break;
+            }
+        }
+    }
+    
+}
\ No newline at end of file
index 90b3cc45d8415c234e7a6990522b73a055a82823..e6ada7f92eb65384fd58af518475a89338f1d16f 100644 (file)
@@ -4,7 +4,7 @@ import { IpcRendererListener } from "./composables/useIpcRend"
  * Shared state imports
  * */
 import { setProfile } from "./shared/profile";
-import { setMessages, addMessage, updateMessage } from "./shared/messages";
+import { setMessages, addMessage, updateMessage, deleteMessage } from "./shared/messages";
 import { setConnectionStatus } from "./shared/connectionStatus";
 
 const SET_PROFILE_CHANNEL = "set-profile";
@@ -12,6 +12,7 @@ const SET_PROFILE_CHANNEL = "set-profile";
 const INIT_MESSAGES_CHANNEL = "init-messages";
 const ADD_MESSAGE_CHANNEL = "add-message";
 const UPDATE_MESSAGE_CHANNEL = "update-message";
+const DELETE_MESSAGE_CHANNEL = "delete-message";
 const CONNECTION_STATUS_CHANNEL = "set-connection-status";
 
 export const setProfileListener = new IpcRendererListener({
@@ -35,6 +36,11 @@ export const updateMessagesListener = new IpcRendererListener({
     listenerCallback: updateMessage
 });
 
+export const deleteMessagesListener = new IpcRendererListener({
+    channel: DELETE_MESSAGE_CHANNEL,
+    listenerCallback: deleteMessage
+});
+
 export const connectionStatusListener = new IpcRendererListener({
     channel: CONNECTION_STATUS_CHANNEL,
     listenerCallback: setConnectionStatus
index 79b8fe7851a2ce676cf4eb5c1f23fff2240d88ac..b24dca3517becf9f80592bc4d4d999ebc9e194a3 100644 (file)
@@ -5,55 +5,46 @@ import useScroll from "@/render/composables/useScroll";
 export const messages: Ref<Array<Message>> = ref([]);
 
 export const setMessages: IpcListenerCallback<Array<Message>> = (payload) => {
-    console.log("setMessages", payload);
     messages.value = payload as Array<Message>;
 }
 
 export const addMessage: IpcListenerCallback<Message> = (payload) => {
-    console.log("addMessage", payload);
     messages.value.push(payload as Message);
 }
 
 export const updateMessage: IpcListenerCallback<Annotation> = (payload) => {
-    console.log("updateMessage", payload);
     const annotation = payload as Annotation;
 
-    /* find the target message */
-    let targetMessage = messages.value.filter((m: Message) => {
-        return m.uid === annotation.uid;
-    })[0];
+    for (let i in messages.value) {
 
-    if (targetMessage) {
+        if (messages.value[i].uid == annotation.uid) {
+
+            if (annotation.name == "content") {
+                messages.value[i].content = annotation.data as string[];
+            }
+
+            if (annotation.name == "context") {
+                messages.value[i].context = annotation.data as string;
+            }
 
-        /* update the content (add a new bubble) */
-        if (annotation.data.hasOwnProperty("content")) {
-            const data = annotation.data as ContentData;
-            targetMessage.content = data.content;
         }
 
-        /* and/or update the context */
-        if (annotation.data.hasOwnProperty("context")) {
-            const data = annotation.data as ContextData;
-            targetMessage.context = data.context;
+    }
+
+}
+
+export const deleteMessage: IpcListenerCallback<string> = (payload) => {
+    const uid = payload as string;
+
+    for (var i = 0; i < messages.value.length; i++) {
+        if (messages.value[i].uid === uid) {
+            messages.value.splice(i, 1);
+            break;
         }
-       
     }
 
 }
 
-// messages.value = [
-// {
-//     content: ["Welcome Andrew"], 
-//     context: "Crimata", 
-//     modifier: "ai", 
-//     uid: "b5dd3d1b-cef0-4057-aa0f-c72e5c4cd67d"
-// }, 
-// {
-//     content: ["show contacts"], 
-//     context: "false", 
-//     modifier: "client", 
-//     uid: "ee2819a3-1125-4881-9942-ca5b3d0ad502"
-// }]
 
 // setTimeout(() => {
 
index 885d940efb190aa2584ae5e0e2e6d37d17c67f0d..6b40e43bf3657b2a6d00fed3dbad6fedfbb47476 100644 (file)
@@ -3,52 +3,34 @@
 
 
 // import useAudio from "@/audio";
-import { setMessages, newMessage, annotateMessage } from "./messages";
+import Messages from "./messages";
+import { getProfile } from "./store";
 import useWebsockets from "./composables/useWebsockets";
 import {config} from "@/config";
+import { ipcEmit } from "@/composables/useEmitter";
 
 /* start and stop audio functionality */
 // const { initAudio, closeAudio } = useAudio();
 
-const isInitMessage = (object: any): object is Message[] => {
-    return 'messages' in object;
-};
+let messages: Messages;
 
-const isNewMessage = (object: any): object is Message => {
-    return 'content' in object;
-};
-
-const isAnnotation = (object: any): object is Annotation => {
-    return 'uid' in object;
-};
-
-const deauthenticate = (): void => {
-    console.log('deauthenticating')
-};
-
-/**
- * Controls for interfacing with the platform.
- * Takes an onMessage callback which we define below.
- */
 
 const onMessageCallback = (payload: string) => {
 
     const message = JSON.parse(payload);
 
     if (message === "CLOSE_AUTH_FAIL") {
-        deauthenticate();
+        console.log("deauthenticating");
     }
 
-    else if (isInitMessage(message)) {
-        setMessages(message);
+    else if (message.header === "init") {
+        messages = new Messages(message.body);
     }
 
-    else if (isNewMessage(message)) {
-        newMessage(message);
-    }
-
-    else if (isAnnotation(message)) {
-        annotateMessage(message);
+    else {
+        if (messages) {
+            messages.update(message.body);
+        }
     }
 
 }
@@ -57,6 +39,17 @@ const connectionStatusCallback = (status: string) => {
     ipcEmit('set-connection-status', status);
 }
 
+
+export const updateAppState = (): void => {
+    const profile = getProfile();
+
+    ipcEmit("set-profile", profile);
+
+    if (messages)
+    messages.emit();
+
+}
+
 const statusOptions = {
    openMessage: "Connected",
    pongMessage: "Nominal",
index a0cbb70ddbbba4cbace157e2f953abb7d1da54da..bcadf73c871b2b4a293a4d78fbc381f36e478b9a 100644 (file)
@@ -1,4 +1,9 @@
 
+interface Update {
+    name: string;
+    data: Message[] | Message | Annotation | string;
+}
+
 interface Message {
     modifier: string;
     content: string[];
@@ -6,16 +11,9 @@ interface Message {
     uid: string;
 }
 
-interface ContentData {
-    content: string[];
-}
-
-interface ContextData {
-    context: string;
-}
-
 interface Annotation {
-    data: ContentData | ContextData;
+    name: string;
+    data: string | string[];
     uid: string;
 }