<commit_before>var message_events = (function () {

var exports = {};

function maybe_add_narrowed_messages(messages, msg_list, messages_are_new) {
    var ids = [];
    _.each(messages, function (elem) {
        ids.push(elem.id);
    });

    channel.get({
        url: '/json/messages/matches_narrow',
        data: {msg_ids: JSON.stringify(ids),
               narrow: JSON.stringify(narrow_state.public_operators())},
        timeout: 5000,
        success: function (data) {
            if (msg_list !== current_msg_list) {
                // We unnarrowed in the mean time
                return;
            }

            var new_messages = [];
            var elsewhere_messages = [];
            _.each(messages, function (elem) {
                if (data.messages.hasOwnProperty(elem.id)) {
                    util.set_match_data(elem, data.messages[elem.id]);
                    new_messages.push(elem);
                } else {
                    elsewhere_messages.push(elem);
                }
            });

            // This second call to add_message_metadata in the
            // insert_new_messages code path helps in very rare race
            // conditions, where e.g. the current user's name was
            // edited in between when they sent the message and when
            // we hear back from the server and can echo the new
            // message.  Arguably, it's counterproductive complexity.
            new_messages = _.map(new_messages, message_store.add_message_metadata);

            message_util.add_messages(
                new_messages,
                msg_list,
                {messages_are_new: messages_are_new}
            );
            unread_ops.process_visible();
            notifications.notify_messages_outside_current_search(elsewhere_messages);
        },
        error: function () {
            // We might want to be more clever here
            setTimeout(function () {
                if (msg_list === current_msg_list) {
                    // Don't actually try again if we unnarrowed
                    // while waiting
                    maybe_add_narrowed_messages(messages, msg_list, messages_are_new);
                }
            }, 5000);
        }});
}


exports.insert_new_messages = function insert_new_messages(messages, locally_echoed) {
    messages = _.map(messages, message_store.add_message_metadata);

    unread.process_loaded_messages(messages);

    // message_list.all is a data-only list that we use to populate
    // other lists, so we always update this
    message_util.add_messages(messages, message_list.all, {messages_are_new: true});

    if (narrow_state.active()) {
        // We do this NOW even though the home view is not active,
        // because we want the home view to load fast later.
        message_util.add_messages(messages, home_msg_list, {messages_are_new: true});

        if (narrow_state.filter().can_apply_locally()) {
            message_util.add_messages(messages, message_list.narrowed, {messages_are_new: true});
        } else {
            // if we cannot apply locally, we have to wait for this callback to happen to notify
            maybe_add_narrowed_messages(messages, message_list.narrowed, true);
        }
    } else {
        // we're in the home view, so update its list<commit_msg>minor: Simplify maybe_add_narrowed_messages.<commit_after>var message_events = (function () {

var exports = {};

function maybe_add_narrowed_messages(messages, msg_list) {
    var ids = [];
    _.each(messages, function (elem) {
        ids.push(elem.id);
    });

    channel.get({
        url: '/json/messages/matches_narrow',
        data: {msg_ids: JSON.stringify(ids),
               narrow: JSON.stringify(narrow_state.public_operators())},
        timeout: 5000,
        success: function (data) {
            if (msg_list !== current_msg_list) {
                // We unnarrowed in the mean time
                return;
            }

            var new_messages = [];
            var elsewhere_messages = [];
            _.each(messages, function (elem) {
                if (data.messages.hasOwnProperty(elem.id)) {
                    util.set_match_data(elem, data.messages[elem.id]);
                    new_messages.push(elem);
                } else {
                    elsewhere_messages.push(elem);
                }
            });

            // This second call to add_message_metadata in the
            // insert_new_messages code path helps in very rare race
            // conditions, where e.g. the current user's name was
            // edited in between when they sent the message and when
            // we hear back from the server and can echo the new
            // message.  Arguably, it's counterproductive complexity.
            new_messages = _.map(new_messages, message_store.add_message_metadata);

            message_util.add_messages(
                new_messages,
                msg_list,
                {messages_are_new: true}
            );
            unread_ops.process_visible();
            notifications.notify_messages_outside_current_search(elsewhere_messages);
        },
        error: function () {
            // We might want to be more clever here
            setTimeout(function () {
                if (msg_list === current_msg_list) {
                    // Don't actually try again if we unnarrowed
                    // while waiting
                    maybe_add_narrowed_messages(messages, msg_list);
                }
            }, 5000);
        }});
}


exports.insert_new_messages = function insert_new_messages(messages, locally_echoed) {
    messages = _.map(messages, message_store.add_message_metadata);

    unread.process_loaded_messages(messages);

    // message_list.all is a data-only list that we use to populate
    // other lists, so we always update this
    message_util.add_messages(messages, message_list.all, {messages_are_new: true});

    if (narrow_state.active()) {
        // We do this NOW even though the home view is not active,
        // because we want the home view to load fast later.
        message_util.add_messages(messages, home_msg_list, {messages_are_new: true});

        if (narrow_state.filter().can_apply_locally()) {
            message_util.add_messages(messages, message_list.narrowed, {messages_are_new: true});
        } else {
            // if we cannot apply locally, we have to wait for this callback to happen to notify
            maybe_add_narrowed_messages(messages, message_list.narrowed);
        }
    } else {
        // we're in the home view, so update its list
{'fraction_non_alphanumeric': 0.08923448588253058, 'fraction_numerical': 0.0024158236448739243, 'mean_word_length': 1.963758389261745, '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': '6858891', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1726, 'n_words': 462}