mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Extract sent_messages.js.
This is mostly straightforward moving of code out of compose.js.
The code that was moved currently supports sending time
reports for sent messages, but we intend to grow out the new
module to track more state about sent messages.
The following function names in this commit are new, but their
code was basically pulled over verbatim:
    process_success (was process_send_time)
    set_timer_for_restarting_event_loop
    clear
    initialize
All the code in the new module is covered by previous tests that
had been written for compose.js.  This commit only modifies
a few things to keep those tests.
The new module has 100% node coverage, so we updated `enforce_fully_covered`.
			
			
This commit is contained in:
		@@ -179,20 +179,6 @@ function send_message_ajax(request, success, error) {
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function report_send_time(send_time, receive_time, display_time, locally_echoed, rendered_changed) {
 | 
			
		||||
    var data = {time: send_time.toString(),
 | 
			
		||||
                received: receive_time.toString(),
 | 
			
		||||
                displayed: display_time.toString(),
 | 
			
		||||
                locally_echoed: locally_echoed};
 | 
			
		||||
    if (locally_echoed) {
 | 
			
		||||
        data.rendered_content_disparity = rendered_changed;
 | 
			
		||||
    }
 | 
			
		||||
    channel.post({
 | 
			
		||||
        url: '/json/report_send_time',
 | 
			
		||||
        data: data,
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var socket;
 | 
			
		||||
if (page_params.use_websockets) {
 | 
			
		||||
    socket = new Socket("/sockjs");
 | 
			
		||||
@@ -211,69 +197,6 @@ function send_message_socket(request, success, error) {
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exports.send_times_log = [];
 | 
			
		||||
exports.send_times_data = {};
 | 
			
		||||
function maybe_report_send_times(message_id) {
 | 
			
		||||
    var data = exports.send_times_data[message_id];
 | 
			
		||||
    if (data.send_finished === undefined || data.received === undefined ||
 | 
			
		||||
        data.displayed === undefined) {
 | 
			
		||||
        // We report the data once we have both the send and receive times
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    report_send_time(data.send_finished - data.start,
 | 
			
		||||
                     data.received - data.start,
 | 
			
		||||
                     data.displayed - data.start,
 | 
			
		||||
                     data.locally_echoed,
 | 
			
		||||
                     data.rendered_content_disparity || false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function mark_end_to_end_receive_time(message_id) {
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].received = new Date();
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function mark_end_to_end_display_time(message_id) {
 | 
			
		||||
    exports.send_times_data[message_id].displayed = new Date();
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exports.mark_rendered_content_disparity = function (message_id, changed) {
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].rendered_content_disparity = changed;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.report_as_received = function report_as_received(message) {
 | 
			
		||||
    if (message.sent_by_me) {
 | 
			
		||||
        mark_end_to_end_receive_time(message.id);
 | 
			
		||||
        setTimeout(function () {
 | 
			
		||||
            mark_end_to_end_display_time(message.id);
 | 
			
		||||
        }, 0);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
function process_send_time(message_id, start_time, locally_echoed) {
 | 
			
		||||
    var send_finished = new Date();
 | 
			
		||||
    var send_time = (send_finished - start_time);
 | 
			
		||||
    if (feature_flags.log_send_times) {
 | 
			
		||||
        blueslip.log("send time: " + send_time);
 | 
			
		||||
    }
 | 
			
		||||
    if (feature_flags.collect_send_times) {
 | 
			
		||||
        exports.send_times_log.push(send_time);
 | 
			
		||||
    }
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].start = start_time;
 | 
			
		||||
    exports.send_times_data[message_id].send_finished = send_finished;
 | 
			
		||||
    exports.send_times_data[message_id].locally_echoed  = locally_echoed;
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function clear_compose_box() {
 | 
			
		||||
    $("#new_message_content").val('').focus();
 | 
			
		||||
    drafts.delete_draft_after_send();
 | 
			
		||||
@@ -289,16 +212,13 @@ exports.send_message_success = function (local_id, message_id, start_time, local
 | 
			
		||||
        clear_compose_box();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    process_send_time(message_id, start_time, locally_echoed);
 | 
			
		||||
    sent_messages.process_success(message_id, start_time, locally_echoed);
 | 
			
		||||
 | 
			
		||||
    echo.reify_message_id(local_id, message_id);
 | 
			
		||||
 | 
			
		||||
    setTimeout(function () {
 | 
			
		||||
        if (exports.send_times_data[message_id].received === undefined) {
 | 
			
		||||
            blueslip.log("Restarting get_events due to delayed receipt of sent message " + message_id);
 | 
			
		||||
            server_events.restart_get_events();
 | 
			
		||||
        }
 | 
			
		||||
    }, 5000);
 | 
			
		||||
    /* This next line is kind of suspect, since we are processing success here. */
 | 
			
		||||
    sent_messages.set_timer_for_restarting_event_loop(message_id);
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.transmit_message = function (request, success, error) {
 | 
			
		||||
@@ -309,7 +229,8 @@ exports.transmit_message = function (request, success, error) {
 | 
			
		||||
    // its lifecycle of being posted/received/acked/sent/displayed.
 | 
			
		||||
    request.client_message_id = request.local_id;
 | 
			
		||||
 | 
			
		||||
    delete exports.send_times_data[request.id];
 | 
			
		||||
    sent_messages.clear(request.id);
 | 
			
		||||
 | 
			
		||||
    if (page_params.use_websockets) {
 | 
			
		||||
        send_message_socket(request, success, error);
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -906,15 +827,6 @@ exports.initialize = function () {
 | 
			
		||||
            compose_actions.start("stream", {});
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    $(document).on('message_id_changed', function (event) {
 | 
			
		||||
        if (exports.send_times_data[event.old_id] !== undefined) {
 | 
			
		||||
            var value = exports.send_times_data[event.old_id];
 | 
			
		||||
            delete exports.send_times_data[event.old_id];
 | 
			
		||||
            exports.send_times_data[event.new_id] =
 | 
			
		||||
                _.extend({}, exports.send_times_data[event.old_id], value);
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
return exports;
 | 
			
		||||
 
 | 
			
		||||
@@ -171,11 +171,11 @@ exports.process_from_server = function process_from_server(messages) {
 | 
			
		||||
            if (client_message.content !== message.content) {
 | 
			
		||||
                client_message.content = message.content;
 | 
			
		||||
                updated = true;
 | 
			
		||||
                compose.mark_rendered_content_disparity(message.id, true);
 | 
			
		||||
                sent_messages.mark_rendered_content_disparity(message.id, true);
 | 
			
		||||
            }
 | 
			
		||||
            msgs_to_rerender.push(client_message);
 | 
			
		||||
            locally_processed_ids.push(client_message.id);
 | 
			
		||||
            compose.report_as_received(client_message);
 | 
			
		||||
            sent_messages.report_as_received(client_message);
 | 
			
		||||
            delete waiting_for_ack[client_message.id];
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ exports.add_messages = function add_messages(messages, msg_list, opts) {
 | 
			
		||||
    if (msg_list === home_msg_list && opts.messages_are_new) {
 | 
			
		||||
        _.each(messages, function (message) {
 | 
			
		||||
            if (message.local_id === undefined) {
 | 
			
		||||
                compose.report_as_received(message);
 | 
			
		||||
                sent_messages.report_as_received(message);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										112
									
								
								static/js/sent_messages.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								static/js/sent_messages.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,112 @@
 | 
			
		||||
var sent_messages = (function () {
 | 
			
		||||
 | 
			
		||||
var exports = {};
 | 
			
		||||
 | 
			
		||||
exports.send_times_log = [];
 | 
			
		||||
exports.send_times_data = {};
 | 
			
		||||
 | 
			
		||||
function report_send_time(send_time, receive_time, display_time, locally_echoed, rendered_changed) {
 | 
			
		||||
    var data = {time: send_time.toString(),
 | 
			
		||||
                received: receive_time.toString(),
 | 
			
		||||
                displayed: display_time.toString(),
 | 
			
		||||
                locally_echoed: locally_echoed};
 | 
			
		||||
    if (locally_echoed) {
 | 
			
		||||
        data.rendered_content_disparity = rendered_changed;
 | 
			
		||||
    }
 | 
			
		||||
    channel.post({
 | 
			
		||||
        url: '/json/report_send_time',
 | 
			
		||||
        data: data,
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function maybe_report_send_times(message_id) {
 | 
			
		||||
    var data = exports.send_times_data[message_id];
 | 
			
		||||
    if (data.send_finished === undefined || data.received === undefined ||
 | 
			
		||||
        data.displayed === undefined) {
 | 
			
		||||
        // We report the data once we have both the send and receive times
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    report_send_time(data.send_finished - data.start,
 | 
			
		||||
                     data.received - data.start,
 | 
			
		||||
                     data.displayed - data.start,
 | 
			
		||||
                     data.locally_echoed,
 | 
			
		||||
                     data.rendered_content_disparity || false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function mark_end_to_end_receive_time(message_id) {
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].received = new Date();
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function mark_end_to_end_display_time(message_id) {
 | 
			
		||||
    exports.send_times_data[message_id].displayed = new Date();
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
exports.mark_rendered_content_disparity = function (message_id, changed) {
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].rendered_content_disparity = changed;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.report_as_received = function report_as_received(message) {
 | 
			
		||||
    if (message.sent_by_me) {
 | 
			
		||||
        mark_end_to_end_receive_time(message.id);
 | 
			
		||||
        setTimeout(function () {
 | 
			
		||||
            mark_end_to_end_display_time(message.id);
 | 
			
		||||
        }, 0);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.process_success = function (message_id, start_time, locally_echoed) {
 | 
			
		||||
    var send_finished = new Date();
 | 
			
		||||
    var send_time = (send_finished - start_time);
 | 
			
		||||
    if (feature_flags.log_send_times) {
 | 
			
		||||
        blueslip.log("send time: " + send_time);
 | 
			
		||||
    }
 | 
			
		||||
    if (feature_flags.collect_send_times) {
 | 
			
		||||
        exports.send_times_log.push(send_time);
 | 
			
		||||
    }
 | 
			
		||||
    if (exports.send_times_data[message_id] === undefined) {
 | 
			
		||||
        exports.send_times_data[message_id] = {};
 | 
			
		||||
    }
 | 
			
		||||
    exports.send_times_data[message_id].start = start_time;
 | 
			
		||||
    exports.send_times_data[message_id].send_finished = send_finished;
 | 
			
		||||
    exports.send_times_data[message_id].locally_echoed  = locally_echoed;
 | 
			
		||||
    maybe_report_send_times(message_id);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.set_timer_for_restarting_event_loop = function (message_id) {
 | 
			
		||||
    setTimeout(function () {
 | 
			
		||||
        if (exports.send_times_data[message_id].received === undefined) {
 | 
			
		||||
            blueslip.log("Restarting get_events due to delayed receipt of sent message " + message_id);
 | 
			
		||||
            server_events.restart_get_events();
 | 
			
		||||
        }
 | 
			
		||||
    }, 5000);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.clear = function (message_id) {
 | 
			
		||||
    delete exports.send_times_data[message_id];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.initialize = function () {
 | 
			
		||||
    $(document).on('message_id_changed', function (event) {
 | 
			
		||||
        if (exports.send_times_data[event.old_id] !== undefined) {
 | 
			
		||||
            var value = exports.send_times_data[event.old_id];
 | 
			
		||||
            delete exports.send_times_data[event.old_id];
 | 
			
		||||
            exports.send_times_data[event.new_id] =
 | 
			
		||||
                _.extend({}, exports.send_times_data[event.old_id], value);
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
return exports;
 | 
			
		||||
 | 
			
		||||
}());
 | 
			
		||||
if (typeof module !== 'undefined') {
 | 
			
		||||
    module.exports = sent_messages;
 | 
			
		||||
}
 | 
			
		||||
@@ -266,6 +266,7 @@ $(function () {
 | 
			
		||||
    pm_list.initialize();
 | 
			
		||||
    stream_list.initialize();
 | 
			
		||||
    drafts.initialize();
 | 
			
		||||
    sent_messages.initialize();
 | 
			
		||||
    compose.initialize();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user