mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 14:35:27 +00:00
We now isolate the code to transmit messages into transmit.js. It is stable code that most folks doing UI work in compose.js don't care about the details of, so it's just clutter there. Also, we may soon have other widgets than the compose box that send messages. This change mostly preserves test coverage, although in some cases we stub at a higher level for the compose path (this is a good thing). Extracting out transmit.js allows us to lock down 100% coverage on that file.
68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
var transmit = (function () {
|
|
|
|
var exports = {};
|
|
|
|
var socket;
|
|
if (page_params.use_websockets) {
|
|
socket = new Socket("/sockjs");
|
|
}
|
|
// For debugging. The socket will eventually move out of this file anyway.
|
|
exports._socket = socket;
|
|
|
|
function send_message_socket(request, success, error) {
|
|
request.socket_user_agent = navigator.userAgent;
|
|
socket.send(request, success, function (type, resp) {
|
|
var err_msg = "Error sending message";
|
|
if (type === 'response') {
|
|
err_msg += ": " + resp.msg;
|
|
}
|
|
error(err_msg);
|
|
});
|
|
}
|
|
|
|
function send_message_ajax(request, success, error) {
|
|
channel.post({
|
|
url: '/json/messages',
|
|
data: request,
|
|
success: success,
|
|
error: function (xhr, error_type) {
|
|
if (error_type !== 'timeout' && reload.is_pending()) {
|
|
// The error might be due to the server changing
|
|
reload.initiate({immediate: true,
|
|
save_pointer: true,
|
|
save_narrow: true,
|
|
save_compose: true,
|
|
send_after_reload: true});
|
|
return;
|
|
}
|
|
|
|
var response = channel.xhr_error_message("Error sending message", xhr);
|
|
error(response);
|
|
},
|
|
});
|
|
}
|
|
|
|
exports.send_message = function (request, on_success, error) {
|
|
function success(data) {
|
|
// Call back to our callers to do things like closing the compose
|
|
// box and turning off spinners and reifying locally echoed messages.
|
|
on_success(data);
|
|
|
|
// Once everything is done, get ready to report times to the server.
|
|
sent_messages.report_server_ack(request.local_id);
|
|
}
|
|
|
|
if (page_params.use_websockets) {
|
|
send_message_socket(request, success, error);
|
|
} else {
|
|
send_message_ajax(request, success, error);
|
|
}
|
|
};
|
|
|
|
return exports;
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
module.exports = transmit;
|
|
}
|