src/main/ChatController.js
/**
 * This method checks if a new message or invitation has arrived to user's inbox.
 * The necessary data is stored and the UI is updated.
 */
This method checks if a new message or invitation has arrived to user's inbox.
The necessary data is stored and the UI is updated.
This method checks if a new message or invitation has arrived to user ' s inbox . The necessary data is stored and the UI is updated .
async function checkForNotifications() {
    const updates = await baseService.checkUserInboxForUpdates(await baseService.getInboxUrl(userWebId));
    updates.forEach(async (fileurl) => {
        let newMessageFound = false;
        let message = await messageService.getNewMessage(fileurl, userWebId);
        if (message) {
            newMessageFound = true;
            var nameThroughUrl;
            var authorUrl;
            console.log(message);
            if (!message.author.includes("Group")) {
                nameThroughUrl = message.author.split("/").pop();
                authorUrl = message.messageUrl.split("priv")[0] + "profile/card#me";
            } else {
                nameThroughUrl = message.author.split("/")[1];
                authorUrl = message.inboxUrl.split("inbox")[0] + "profile/" + message.author.split("/")[0] + "/" + message.author.split("/")[1];
                console.log(authorUrl);
            }
            if (nameThroughUrl === $("#interlocutorw-name").text()) {
                interlocutorMessages.push(message);
                await showAndStoreMessages();
            } else if (contactsWithChat.indexOf(authorUrl) != -1) {
                var index = contactsWithChat.indexOf(authorUrl);
                $("#chatwindow" + index).remove();
                var parsedmessage = message.messagetext.replace(/\:(.*?)\:/g, "<img src='main/resources/static/img/$1.gif' alt='$1'></img>");
                var html = $("<div class='contact new-message-contact' id='chatwindow" + index + "'><img src='" + semanticChats[index].photo + "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + semanticChats[index].interlocutorName + "</h1><p class='font-preview' id='lastMsg" +
                    index + "'>" + parsedmessage +
                    "</p></div></div><div class='contact-time'><p>" +
                    semanticChats[index].getHourOfMessage(semanticChats[index].numberOfMessages - 1) +
                    "</p><div class='new-message' id='nm" + index + "'><p>" + "1" + "</p></div></div></div>");
                $(".contact-list").prepend(html);
                document.getElementById("chatwindow" + index).addEventListener("click", loadMessagesToWindow, false);
                interlocutorMessages.push(message);
            } else if (openChat) {
                interlocutorMessages.push(message);
            }
        }
        if (!newMessageFound) {
            const convoToJoin = await joinService.getJoinRequest(fileurl, userWebId);
            if (convoToJoin) {
                $("#showinvs").show();
                chatsToJoin.push(await joinService.processChatToJoin(convoToJoin, fileurl, userWebId, userDataUrl));
                alert("New invitations. They shall be dismissed if not accepted on this session.");
            }
        }
    });
    if (chatsToJoin.length === 0) {
        $("#showinvs").hide();
    }
}
async function checkForNotifications ( ) { const updates = await baseService . checkUserInboxForUpdates ( await baseService . getInboxUrl ( userWebId ) ) ; updates . forEach ( async ( fileurl ) => { let newMessageFound = false ; let message = await messageService . getNewMessage ( fileurl , userWebId ) ; if ( message ) { newMessageFound = true ; var nameThroughUrl ; var authorUrl ; console . log ( message ) ; if ( ! message . author . includes ( "Group" ) ) { nameThroughUrl = message . author . split ( "/" ) . pop ( ) ; authorUrl = message . messageUrl . split ( "priv" ) [ 0 ] + "profile/card#me" ; } else { nameThroughUrl = message . author . split ( "/" ) [ 1 ] ; authorUrl = message . inboxUrl . split ( "inbox" ) [ 0 ] + "profile/" + message . author . split ( "/" ) [ 0 ] + "/" + message . author . split ( "/" ) [ 1 ] ; console . log ( authorUrl ) ; } if ( nameThroughUrl === $ ( "#interlocutorw-name" ) . text ( ) ) { interlocutorMessages . push ( message ) ; await showAndStoreMessages ( ) ; } else if ( contactsWithChat . indexOf ( authorUrl ) != - 1 ) { var index = contactsWithChat . indexOf ( authorUrl ) ; $ ( "#chatwindow" + index ) . remove ( ) ; var parsedmessage = message . messagetext . replace ( / \:(.*?)\: / g , "<img src='main/resources/static/img/$1.gif' alt='$1'></img>" ) ; var html = $ ( "<div class='contact new-message-contact' id='chatwindow" + index + "'><img src='" + semanticChats [ index ] . photo + "' alt='!' onerror='this.onerror=null;this.src='http://www.philosophica.info/voces/aquino/Aquino.jpg';'><div class='contact-preview'><div class='contact-text'><h1 class='font-name'>" + semanticChats [ index ] . interlocutorName + "</h1><p class='font-preview' id='lastMsg" + index + "'>" + parsedmessage + "</p></div></div><div class='contact-time'><p>" + semanticChats [ index ] . getHourOfMessage ( semanticChats [ index ] . numberOfMessages - 1 ) + "</p><div class='new-message' id='nm" + index + "'><p>" + "1" + "</p></div></div></div>" ) ; $ ( ".contact-list" ) . prepend ( html ) ; document . getElementById ( "chatwindow" + index ) . addEventListener ( "click" , loadMessagesToWindow , false ) ; interlocutorMessages . push ( message ) ; } else if ( openChat ) { interlocutorMessages . push ( message ) ; } } if ( ! newMessageFound ) { const convoToJoin = await joinService . getJoinRequest ( fileurl , userWebId ) ; if ( convoToJoin ) { $ ( "#showinvs" ) . show ( ) ; chatsToJoin . push ( await joinService . processChatToJoin ( convoToJoin , fileurl , userWebId , userDataUrl ) ) ; alert ( "New invitations. They shall be dismissed if not accepted on this session." ) ; } } } ) ; if ( chatsToJoin . length === 0 ) { $ ( "#showinvs" ) . hide ( ) ; } }
This method checks if a new message or invitation has arrived to user's inbox.
This method checks if a new message or invitation has arrived to user ' s inbox .
/* Case I : Chat related is showing */ /* Case II: Chat related is not showing, but is opened. */ /* Notification arises */ /* Case III: no such chat exists yet. Invitation to be accepted. */ /* If it is not a message, it is an Invitation. Processing... */