src/utils/api/stream.js
/**
     * Handle incoming socket data
     * @param e - socket event
     */
Handle incoming socket data
@param e - socket event
Handle incoming socket data @param e - socket event
handleMessage(e) {
        if (e.data.indexOf("ping") != -1) { 
            store.commit('last_ping', Date.now() / 1000 >> 0);
            return;
        }
        const json = JSON.parse(e.data);
        if (typeof json.message == "undefined")
            return;
        const operation = json.message.operation;
        if (typeof json.message.content.data != "undefined")
            json.message.content.data = joypixels.toImage(
                json.message.content.data
            );
        if (operation == "added_message") {
            let message = json.message.content;
            message.message_from = message.from;
            message = Crypto.decryptMessage(message);
            this.notify(message);
            SessionCache.cacheMessage(message);
            SessionCache.updateConversation(message);
            store.state.msgbus.$emit('newMessage', message);
        } else if (operation == "removed_message") {
            let message = json.message.content;
            SessionCache.deleteMessage(message.id);
            store.state.msgbus.$emit('deletedMessage', message.id);
        } else if (operation == "read_conversation") {
            const id = json.message.content.id;
            SessionCache.readConversation(id, 'index_public_unarchived');
            SessionCache.readConversation(id, 'index_archived');
            store.state.msgbus.$emit('conversationRead', id);
        } else if (operation == "updated_conversation") {
            const conversation = Crypto.decryptConversation(json.message.content);
            conversation.conversation_id = conversation.id; 
            if (conversation.snippet) {
                SessionCache.updateConversationSnippet(conversation.id, conversation.snippet, 'index_public_unarchived');
                SessionCache.updateConversationSnippet(conversation.id, conversation.snippet, 'index_archived');
                SessionCache.updateConversationSnippet(conversation.id, conversation.snippet, 'index_private');
            }
            SessionCache.readConversation(conversation.id, 'index_public_unarchived', conversation.read);
            SessionCache.readConversation(conversation.id, 'index_archived', conversation.read);
            store.state.msgbus.$emit('newMessage', conversation);
        } else if (operation == "update_message_type") {
            const id = json.message.content.id;
            const message_type = json.message.content.message_type;
            SessionCache.updateMessageType(id, message_type);
            store.state.msgbus.$emit('updateMessageType-' + id, { message_type });
        } else if (operation == "added_conversation") {
            const id = json.message.content.id;
            SessionCache.invalidateConversations('index_public_unarchived');
            store.state.msgbus.$emit('addedConversation', { id });
        } else if (operation == "removed_conversation") {
            const id = json.message.content.id;
            SessionCache.removeConversation(id, 'index_public_unarchived');
            SessionCache.removeConversation(id, 'index_archived');
            SessionCache.removeConversation(id, 'index_private');
            store.state.msgbus.$emit('removedConversation', { id });
        } else if (operation == "archive_conversation") {
            const id = json.message.content.id;
            if (json.message.content.archive) {
                SessionCache.removeConversation(id, 'index_public_unarchived');
                SessionCache.invalidateConversations('index_archived');
            } else {
                SessionCache.removeConversation(id, 'index_archived');
                SessionCache.invalidateConversations('index_public_unarchived');
            }
            store.state.msgbus.$emit('removedConversation', { id });
        }
    }
handleMessage ( e ) { if ( e . data . indexOf ( "ping" ) != - 1 ) { store . commit ( 'last_ping' , Date . now ( ) / 1000 >> 0 ) ; return ; } const json = JSON . parse ( e . data ) ; if ( typeof json . message == "undefined" ) return ; const operation = json . message . operation ; if ( typeof json . message . content . data != "undefined" ) json . message . content . data = joypixels . toImage ( json . message . content . data ) ; if ( operation == "added_message" ) { let message = json . message . content ; message . message_from = message . from ; message = Crypto . decryptMessage ( message ) ; this . notify ( message ) ; SessionCache . cacheMessage ( message ) ; SessionCache . updateConversation ( message ) ; store . state . msgbus . $emit ( 'newMessage' , message ) ; } else if ( operation == "removed_message" ) { let message = json . message . content ; SessionCache . deleteMessage ( message . id ) ; store . state . msgbus . $emit ( 'deletedMessage' , message . id ) ; } else if ( operation == "read_conversation" ) { const id = json . message . content . id ; SessionCache . readConversation ( id , 'index_public_unarchived' ) ; SessionCache . readConversation ( id , 'index_archived' ) ; store . state . msgbus . $emit ( 'conversationRead' , id ) ; } else if ( operation == "updated_conversation" ) { const conversation = Crypto . decryptConversation ( json . message . content ) ; conversation . conversation_id = conversation . id ; if ( conversation . snippet ) { SessionCache . updateConversationSnippet ( conversation . id , conversation . snippet , 'index_public_unarchived' ) ; SessionCache . updateConversationSnippet ( conversation . id , conversation . snippet , 'index_archived' ) ; SessionCache . updateConversationSnippet ( conversation . id , conversation . snippet , 'index_private' ) ; } SessionCache . readConversation ( conversation . id , 'index_public_unarchived' , conversation . read ) ; SessionCache . readConversation ( conversation . id , 'index_archived' , conversation . read ) ; store . state . msgbus . $emit ( 'newMessage' , conversation ) ; } else if ( operation == "update_message_type" ) { const id = json . message . content . id ; const message_type = json . message . content . message_type ; SessionCache . updateMessageType ( id , message_type ) ; store . state . msgbus . $emit ( 'updateMessageType-' + id , { message_type } ) ; } else if ( operation == "added_conversation" ) { const id = json . message . content . id ; SessionCache . invalidateConversations ( 'index_public_unarchived' ) ; store . state . msgbus . $emit ( 'addedConversation' , { id } ) ; } else if ( operation == "removed_conversation" ) { const id = json . message . content . id ; SessionCache . removeConversation ( id , 'index_public_unarchived' ) ; SessionCache . removeConversation ( id , 'index_archived' ) ; SessionCache . removeConversation ( id , 'index_private' ) ; store . state . msgbus . $emit ( 'removedConversation' , { id } ) ; } else if ( operation == "archive_conversation" ) { const id = json . message . content . id ; if ( json . message . content . archive ) { SessionCache . removeConversation ( id , 'index_public_unarchived' ) ; SessionCache . invalidateConversations ( 'index_archived' ) ; } else { SessionCache . removeConversation ( id , 'index_archived' ) ; SessionCache . invalidateConversations ( 'index_public_unarchived' ) ; } store . state . msgbus . $emit ( 'removedConversation' , { id } ) ; } }
Handle incoming socket data
 @param e - socket event
Handle incoming socket data @param e - socket event
// Is keep alive event // Store last ping to maintain data connection // Parse out JSON // Ignore bad messages // Parse any emojis // Normalize ID