2018-02-20 13:08:50 +01:00
|
|
|
var transmit = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var socket;
|
2018-06-14 18:16:02 +02:00
|
|
|
exports.initialize = function () {
|
2018-03-31 02:38:10 +02:00
|
|
|
// We initialize the socket inside a function so that this code
|
|
|
|
// runs after `csrf_token` is initialized in setup.js.
|
|
|
|
if (page_params.use_websockets) {
|
|
|
|
socket = new Socket("/sockjs");
|
|
|
|
}
|
|
|
|
// For debugging. The socket will eventually move out of this file anyway.
|
|
|
|
exports._socket = socket;
|
2018-06-14 18:16:02 +02:00
|
|
|
};
|
2018-02-20 13:08:50 +01:00
|
|
|
|
|
|
|
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) {
|
2018-08-04 15:40:25 +02:00
|
|
|
if (error_type !== 'timeout' && reload_state.is_pending()) {
|
2018-02-20 13:08:50 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-23 16:18:27 +01:00
|
|
|
exports.reply_message = function (opts) {
|
|
|
|
// This code does an application-triggered reply to a message (as
|
|
|
|
// opposed to the user themselves doing it). Its only use case
|
|
|
|
// for now is experimental widget-aware bots, so treat this as
|
|
|
|
// somewhat beta code. To understand the use case, think of a
|
|
|
|
// bot that wants to give users 3 or 4 canned replies to some
|
|
|
|
// choice, but it wants to front-end each of these options
|
|
|
|
// with a one-click button. This function is part of that architecture.
|
|
|
|
var message = opts.message;
|
|
|
|
var content = opts.content;
|
|
|
|
|
|
|
|
function success() {
|
|
|
|
// TODO: If server response comes back before the message event,
|
|
|
|
// we could show it earlier, although that creates some
|
|
|
|
// complexity. For now do nothing. (Note that send_message
|
|
|
|
// already handles things like reporting times to the server.)
|
|
|
|
}
|
|
|
|
|
|
|
|
function error() {
|
|
|
|
// TODO: In our current use case, which is widgets, to meaningfully
|
|
|
|
// handle errors, we would want the widget to provide some
|
|
|
|
// kind of callback to us so it can do some appropriate UI.
|
|
|
|
// For now do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
var locally_echoed = false;
|
|
|
|
var local_id = sent_messages.get_new_local_id();
|
|
|
|
|
|
|
|
var reply = {
|
|
|
|
sender_id: page_params.user_id,
|
|
|
|
queue_id: page_params.queue_id,
|
|
|
|
local_id: local_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
sent_messages.start_tracking_message({
|
|
|
|
local_id: local_id,
|
|
|
|
locally_echoed: locally_echoed,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (message.type === 'stream') {
|
|
|
|
var stream = message.stream;
|
|
|
|
|
2018-10-13 02:31:49 +02:00
|
|
|
var mention = people.get_mention_syntax(message.sender_full_name, message.sender_id);
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
content = mention + ' ' + content;
|
|
|
|
|
|
|
|
reply.type = 'stream';
|
|
|
|
reply.to = stream;
|
|
|
|
reply.content = content;
|
2018-12-23 16:01:30 +01:00
|
|
|
util.set_message_topic(reply, util.get_message_topic(message));
|
2018-02-23 16:18:27 +01:00
|
|
|
|
|
|
|
transmit.send_message(reply, success, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'private') {
|
|
|
|
var pm_recipient = people.pm_reply_to(message);
|
|
|
|
|
|
|
|
reply.type = 'private';
|
|
|
|
reply.to = JSON.stringify(pm_recipient.split(','));
|
|
|
|
reply.content = content;
|
|
|
|
|
|
|
|
transmit.send_message(reply, success, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
blueslip.error('unknown message type: ' + message.type);
|
|
|
|
};
|
|
|
|
|
2018-02-20 13:08:50 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = transmit;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.transmit = transmit;
|