<commit_before>$(function () {
    // WebSocket connection management block.
    // Correctly decide between ws:// and wss://
    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
    var ws_path = ws_scheme + '://' + window.location.host + "/" + currentUser + "/";
    var webSocket = new channels.WebSocketBridge();
    webSocket.connect(ws_path);

    function scrollConversationScreen() {
        /* Set focus on the input box from the form, and rolls to show the
        the most recent message.
        */
        $("input[name='message']").focus();
        $('.conversation').scrollTop($('.conversation')[0].scrollHeight);
    }

    function addNewMessage(message_id) {
        /* This function calls the respective AJAX view, so it will be able to
        load the received message in a proper way.
         */
        $.ajax({
            url: '/messages/receive/',
            data: {'message_id': message_id},
            cache: false,
            success: function (data) {
                $(".send-message").before(data);
                scrollConversationScreen();
            }
        });
    }

    window.onbeforeunload = function () {
        payload = {
            'sender': currentUser,
            'activity_type': "set_status",
            'status': 'offline'
        }
        webSocket.send(payload);
    }

    // Helpful debugging
    webSocket.socket.onopen = function () {
        console.log("Connected to inbox stream");
        payload = {
            'sender': currentUser,
            'activity_type': "set_status",
            'status': 'online'
        }
        webSocket.send(payload);
    };

    webSocket.socket.onclose = function () {
        console.log("Disconnected from inbox stream");
    };

    // onmessage management.
    webSocket.listen(function(event) {
        if (event.activity_type === "message") {
            console.log("A new message from " + event.sender);
            if (event.sender === activeUser) {
                addNewMessage(event.message_id);
            } else {
                $("#new-message-" + event.sender).show();
            }
        }
    });

    $("#send").submit(function () {
        /*<commit_msg>Adding an small line to hide the inbox menu notification for new messages when a new message arrives in the moment the user is in the same private room with the message sender.<commit_after>$(function () {
    // WebSocket connection management block.
    // Correctly decide between ws:// and wss://
    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
    var ws_path = ws_scheme + '://' + window.location.host + "/" + currentUser + "/";
    var webSocket = new channels.WebSocketBridge();
    webSocket.connect(ws_path);

    function setUserOnlineOffline(username, status) {
        /* This function enables the client to switch the user connection
        status, allowing to show (when implemented the proper functionality)
        if an user is connected or not.
        */
        var elem = $("online-stat-" + username);
        if (elem) {
            if (status === 'online') {
                elem.attr("class", "btn btn-success btn-circle");
            } else {
                elem.attr("class", "btn btn-danger btn-circle");
            }
        }
    }

    function scrollConversationScreen() {
        /* Set focus on the input box from the form, and rolls to show the
        the most recent message.
        */
        $("input[name='message']").focus();
        $('.conversation').scrollTop($('.conversation')[0].scrollHeight);
    }

    function addNewMessage(message_id) {
        /* This function calls the respective AJAX view, so it will be able to
        load the received message in a proper way.
         */
        $.ajax({
            url: '/messages/receive/',
            data: {'message_id': message_id},
            cache: false,
            success: function (data) {
                $(".send-message").before(data);
                scrollConversationScreen();
            }
        });
    }

    window.onbeforeunload = function () {
        payload = {
            'sender': currentUser,
            'activity_type': "set_status",
            'status': 'offline'
        }
        webSocket.send(payload);
    }

    // Helpful debugging
    webSocket.socket.onopen = function () {
        console.log("Connected to inbox stream");
        payload = {
            'sender': currentUser,
            'activity_type': "set_status",
            'status': 'online'
        }
        webSocket.send(payload);
    };

    webSocket.socket.onclose = function () {
        console.log("Disconnected from inbox stream");
    };

    // onmessage management.
    webSocket.listen(function(event) {
        if (event.activity_type === "message") {
            console.log("A new message from " + event.sender);
            if (event.sender === activeUser) {
                addNewMessage(event.message_id);
                /*
                I put this line here because I wasn't able to find a better
                solution to hide the envelope icon showed by the notification
                javascript file when a new message arrives. I hope there is a
                more elegant way to work this out.
                */
                setTimeout(function(){$("#unread-count").hide()}, 1);
            } else {
                $("#new-message-" + event.sender).show();
            }
        } else if (event.activity_type === "set_status") {
            console.log('Status changed')
            //setUserOnlineOffline(event.sender, event.status)
        }
    });

    $("#send").submit(function () {
        /*
{'fraction_non_alphanumeric': 0.12748723366789927, 'fraction_numerical': 0.0005282620179609086, 'mean_word_length': 1.8614609571788414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '809860', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1459, 'n_words': 403}