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> => {
}
-export const updateAppState = (): void => {
-
- const profile = getProfile();
-
- ipcEmit("set-profile", profile);
-
- ipcEmit("init-messages", messages)
-
-}
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"
*/
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;
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
* 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";
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({
listenerCallback: updateMessage
});
+export const deleteMessagesListener = new IpcRendererListener({
+ channel: DELETE_MESSAGE_CHANNEL,
+ listenerCallback: deleteMessage
+});
+
export const connectionStatusListener = new IpcRendererListener({
channel: CONNECTION_STATUS_CHANNEL,
listenerCallback: setConnectionStatus
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(() => {
// 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);
+ }
}
}
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",
+interface Update {
+ name: string;
+ data: Message[] | Message | Annotation | string;
+}
+
interface Message {
modifier: string;
content: string[];
uid: string;
}
-interface ContentData {
- content: string[];
-}
-
-interface ContextData {
- context: string;
-}
-
interface Annotation {
- data: ContentData | ContextData;
+ name: string;
+ data: string | string[];
uid: string;
}