2012-10-18 20:29:16 +02:00
|
|
|
var compose = (function () {
|
2012-10-03 22:53:15 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
var exports = {};
|
2017-04-14 16:26:00 +02:00
|
|
|
|
2016-12-02 15:16:33 +01:00
|
|
|
/* Track the state of the @all warning. The user must acknowledge that they are spamming the entire
|
|
|
|
stream before the warning will go away. If they try to send before explicitly dismissing the
|
|
|
|
warning, they will get an error message too.
|
|
|
|
|
|
|
|
undefined: no @all/@everyone in message;
|
|
|
|
false: user typed @all/@everyone;
|
|
|
|
true: user clicked YES */
|
2016-11-09 19:34:07 +01:00
|
|
|
|
2016-06-04 04:31:35 +02:00
|
|
|
var user_acknowledged_all_everyone;
|
2017-12-18 16:09:41 +01:00
|
|
|
var user_acknowledged_announce;
|
2016-06-04 04:31:35 +02:00
|
|
|
|
2016-11-09 19:34:07 +01:00
|
|
|
exports.all_everyone_warn_threshold = 15;
|
2017-12-18 16:09:41 +01:00
|
|
|
exports.announce_warn_threshold = 60;
|
2016-11-09 19:34:07 +01:00
|
|
|
|
2017-11-28 06:26:39 +01:00
|
|
|
exports.uploads_domain = document.location.protocol + '//' + document.location.host;
|
|
|
|
exports.uploads_path = '/user_uploads';
|
|
|
|
exports.uploads_re = new RegExp("\\]\\(" + exports.uploads_domain + "(" + exports.uploads_path + "[^\\)]+)\\)", 'g');
|
|
|
|
exports.clone_file_input = undefined;
|
|
|
|
|
2013-10-23 16:46:18 +02:00
|
|
|
function make_uploads_relative(content) {
|
|
|
|
// Rewrite uploads in markdown links back to domain-relative form
|
2017-11-28 06:26:39 +01:00
|
|
|
return content.replace(exports.uploads_re, "]($1)");
|
2013-10-23 16:46:18 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 04:31:35 +02:00
|
|
|
function show_all_everyone_warnings() {
|
2017-06-29 22:54:14 +02:00
|
|
|
var stream_count = stream_data.get_subscriber_count(compose_state.stream_name()) || 0;
|
2016-06-04 04:31:35 +02:00
|
|
|
|
|
|
|
var all_everyone_template = templates.render("compose_all_everyone", {count: stream_count});
|
|
|
|
var error_area_all_everyone = $("#compose-all-everyone");
|
|
|
|
|
|
|
|
// only show one error for any number of @all or @everyone mentions
|
|
|
|
if (!error_area_all_everyone.is(':visible')) {
|
|
|
|
error_area_all_everyone.append(all_everyone_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_area_all_everyone.show();
|
|
|
|
user_acknowledged_all_everyone = false;
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.clear_all_everyone_warnings = function () {
|
2016-06-04 04:31:35 +02:00
|
|
|
$("#compose-all-everyone").hide();
|
|
|
|
$("#compose-all-everyone").empty();
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").hide();
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
2016-06-04 04:31:35 +02:00
|
|
|
|
2018-01-04 22:28:25 +01:00
|
|
|
function show_sending_indicator(whats_happening) {
|
|
|
|
if (whats_happening === undefined) {
|
|
|
|
whats_happening = 'Sending...';
|
|
|
|
}
|
|
|
|
$("#sending-indicator").html(i18n.t(whats_happening));
|
|
|
|
$("#sending-indicator").show();
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:09:41 +01:00
|
|
|
function show_announce_warnings() {
|
|
|
|
var stream_count = stream_data.get_subscriber_count(compose_state.stream_name()) || 0;
|
|
|
|
|
|
|
|
var announce_template = templates.render("compose_announce", {count: stream_count});
|
|
|
|
var error_area_announce = $("#compose-announce");
|
|
|
|
|
|
|
|
if (!error_area_announce.is(':visible')) {
|
|
|
|
error_area_announce.append(announce_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_area_announce.show();
|
|
|
|
user_acknowledged_announce = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.clear_announce_warnings = function () {
|
|
|
|
$("#compose-announce").hide();
|
|
|
|
$("#compose-announce").empty();
|
|
|
|
$("#compose-send-status").hide();
|
|
|
|
};
|
|
|
|
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.clear_invites = function () {
|
2013-09-16 21:13:11 +02:00
|
|
|
$("#compose_invite_users").hide();
|
|
|
|
$("#compose_invite_users").empty();
|
2017-04-14 15:09:13 +02:00
|
|
|
};
|
|
|
|
|
2017-10-14 15:18:21 +02:00
|
|
|
exports.clear_private_stream_alert = function () {
|
|
|
|
$("#compose_private_stream_alert").hide();
|
|
|
|
$("#compose_private_stream_alert").empty();
|
|
|
|
};
|
|
|
|
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.reset_user_acknowledged_all_everyone_flag = function () {
|
2016-06-04 04:31:35 +02:00
|
|
|
user_acknowledged_all_everyone = undefined;
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
2013-07-12 23:26:18 +02:00
|
|
|
|
2017-12-18 16:09:41 +01:00
|
|
|
exports.reset_user_acknowledged_announce_flag = function () {
|
|
|
|
user_acknowledged_announce = undefined;
|
|
|
|
};
|
|
|
|
|
2017-04-14 16:27:27 +02:00
|
|
|
exports.clear_preview_area = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").show();
|
2016-08-29 22:37:27 +02:00
|
|
|
$("#undo_markdown_preview").hide();
|
|
|
|
$("#preview_message_area").hide();
|
2016-09-26 22:03:42 +02:00
|
|
|
$("#preview_content").empty();
|
2016-08-29 22:37:27 +02:00
|
|
|
$("#markdown_preview").show();
|
2017-04-14 16:27:27 +02:00
|
|
|
};
|
2016-08-29 22:37:27 +02:00
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function update_fade() {
|
2017-04-15 01:15:59 +02:00
|
|
|
if (!compose_state.composing()) {
|
2013-08-01 17:47:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-03-12 21:46:54 +01:00
|
|
|
|
2017-04-15 01:15:59 +02:00
|
|
|
var msg_type = compose_state.get_message_type();
|
2013-08-11 23:54:50 +02:00
|
|
|
compose_fade.set_focused_recipient(msg_type);
|
2013-08-11 21:21:47 +02:00
|
|
|
compose_fade.update_faded_messages();
|
2013-03-12 21:46:54 +01:00
|
|
|
}
|
|
|
|
|
2017-04-14 16:33:00 +02:00
|
|
|
exports.abort_xhr = function () {
|
2017-06-30 00:57:46 +02:00
|
|
|
$("#compose-send-button").prop("disabled", false);
|
2013-03-27 18:49:28 +01:00
|
|
|
var xhr = $("#compose").data("filedrop_xhr");
|
|
|
|
if (xhr !== undefined) {
|
|
|
|
xhr.abort();
|
|
|
|
$("#compose").removeData("filedrop_xhr");
|
|
|
|
}
|
2017-04-14 16:33:00 +02:00
|
|
|
};
|
2013-03-27 18:49:28 +01:00
|
|
|
|
2016-08-27 04:47:53 +02:00
|
|
|
exports.empty_topic_placeholder = function () {
|
2016-10-30 06:27:53 +01:00
|
|
|
return i18n.t("(no topic)");
|
2013-05-20 22:52:03 +02:00
|
|
|
};
|
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
function create_message_object() {
|
2013-05-20 22:52:03 +02:00
|
|
|
// Subjects are optional, and we provide a placeholder if one isn't given.
|
2017-04-15 01:15:59 +02:00
|
|
|
var subject = compose_state.subject();
|
2013-05-20 22:52:03 +02:00
|
|
|
if (subject === "") {
|
2016-08-27 04:47:53 +02:00
|
|
|
subject = compose.empty_topic_placeholder();
|
2013-05-20 22:52:03 +02:00
|
|
|
}
|
2013-10-23 16:46:18 +02:00
|
|
|
|
2017-04-15 01:15:59 +02:00
|
|
|
var content = make_uploads_relative(compose_state.message_content());
|
2013-10-23 16:46:18 +02:00
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
// Changes here must also be kept in sync with echo.try_deliver_locally
|
2017-02-17 22:15:38 +01:00
|
|
|
var message = {
|
2017-04-24 20:35:26 +02:00
|
|
|
type: compose_state.get_message_type(),
|
2017-02-17 22:15:38 +01:00
|
|
|
content: content,
|
|
|
|
sender_id: page_params.user_id,
|
2017-04-24 21:40:16 +02:00
|
|
|
queue_id: page_params.queue_id,
|
2017-02-23 04:08:05 +01:00
|
|
|
stream: '',
|
|
|
|
subject: '',
|
2017-02-17 22:15:38 +01:00
|
|
|
};
|
2013-04-17 19:34:20 +02:00
|
|
|
|
|
|
|
if (message.type === "private") {
|
|
|
|
// TODO: this should be collapsed with the code in composebox_typeahead.js
|
2017-03-18 18:48:43 +01:00
|
|
|
var recipient = compose_state.recipient();
|
2017-02-24 23:51:23 +01:00
|
|
|
var emails = util.extract_pm_recipients(recipient);
|
|
|
|
message.to = emails;
|
2017-02-24 16:20:25 +01:00
|
|
|
message.reply_to = recipient;
|
|
|
|
message.private_message_recipient = recipient;
|
2017-02-24 23:51:23 +01:00
|
|
|
message.to_user_ids = people.email_list_to_user_ids_string(emails);
|
2013-04-17 19:34:20 +02:00
|
|
|
} else {
|
2017-04-15 01:15:59 +02:00
|
|
|
var stream_name = compose_state.stream_name();
|
2017-02-23 04:08:05 +01:00
|
|
|
message.to = stream_name;
|
|
|
|
message.stream = stream_name;
|
|
|
|
var sub = stream_data.get_sub(stream_name);
|
|
|
|
if (sub) {
|
|
|
|
message.stream_id = sub.stream_id;
|
|
|
|
}
|
|
|
|
message.subject = subject;
|
2013-04-17 19:34:20 +02:00
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
2017-03-29 08:54:26 +02:00
|
|
|
// Export for testing
|
|
|
|
exports.create_message_object = create_message_object;
|
2013-04-05 16:44:46 +02:00
|
|
|
|
2013-02-01 17:04:23 +01:00
|
|
|
function compose_error(error_text, bad_input) {
|
2017-11-26 19:58:36 +01:00
|
|
|
$('#compose-send-status').removeClass(common.status_classes)
|
2013-02-01 17:04:23 +01:00
|
|
|
.addClass('alert-error')
|
|
|
|
.stop(true).fadeTo(0, 1);
|
2017-11-26 20:03:46 +01:00
|
|
|
$('#compose-error-msg').html(error_text);
|
2017-06-30 00:57:46 +02:00
|
|
|
$("#compose-send-button").prop('disabled', false);
|
2013-03-06 17:40:51 +01:00
|
|
|
$("#sending-indicator").hide();
|
2013-11-21 00:48:03 +01:00
|
|
|
if (bad_input !== undefined) {
|
|
|
|
bad_input.focus().select();
|
|
|
|
}
|
2013-02-01 17:04:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-11 11:39:49 +01:00
|
|
|
function nonexistent_stream_reply_error() {
|
|
|
|
$("#nonexistent_stream_reply_error").show();
|
|
|
|
$("#compose-reply-error-msg").html("There are no messages to reply to yet.");
|
|
|
|
setTimeout(function () {
|
|
|
|
$("#nonexistent_stream_reply_error").hide();
|
|
|
|
}, 5000);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.nonexistent_stream_reply_error = nonexistent_stream_reply_error;
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
function send_message_ajax(request, success, error) {
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2015-12-01 00:18:33 +01:00
|
|
|
url: '/json/messages',
|
2012-11-07 20:26:24 +01:00
|
|
|
data: request,
|
2013-09-11 20:42:12 +02:00
|
|
|
success: success,
|
2012-10-29 18:06:53 +01:00
|
|
|
error: function (xhr, error_type) {
|
2012-12-11 21:44:04 +01:00
|
|
|
if (error_type !== 'timeout' && reload.is_pending()) {
|
2012-10-29 18:06:53 +01:00
|
|
|
// The error might be due to the server changing
|
2015-11-30 17:47:19 +01:00
|
|
|
reload.initiate({immediate: true,
|
|
|
|
save_pointer: true,
|
|
|
|
save_narrow: true,
|
|
|
|
save_compose: true,
|
|
|
|
send_after_reload: true});
|
2012-10-29 18:06:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2014-03-13 15:32:44 +01:00
|
|
|
var response = channel.xhr_error_message("Error sending message", xhr);
|
2013-12-19 17:03:08 +01:00
|
|
|
error(response);
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2012-11-07 20:26:24 +01:00
|
|
|
});
|
2013-09-11 20:42:12 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:45:12 +01:00
|
|
|
var socket;
|
2016-12-28 18:24:12 +01:00
|
|
|
if (page_params.use_websockets) {
|
2013-11-06 22:45:12 +01:00
|
|
|
socket = new Socket("/sockjs");
|
|
|
|
}
|
2013-10-24 23:08:31 +02:00
|
|
|
// For debugging. The socket will eventually move out of this file anyway.
|
|
|
|
exports._socket = socket;
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
function send_message_socket(request, success, error) {
|
2017-02-17 23:59:25 +01:00
|
|
|
request.socket_user_agent = navigator.userAgent;
|
2013-09-11 20:42:12 +02:00
|
|
|
socket.send(request, success, function (type, resp) {
|
|
|
|
var err_msg = "Error sending message";
|
|
|
|
if (type === 'response') {
|
|
|
|
err_msg += ": " + resp.msg;
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
error(err_msg);
|
2013-09-11 20:42:12 +02:00
|
|
|
});
|
|
|
|
}
|
2012-10-29 18:06:53 +01:00
|
|
|
|
2013-12-03 20:59:23 +01:00
|
|
|
function clear_compose_box() {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").val('').focus();
|
2017-02-22 02:34:05 +01:00
|
|
|
drafts.delete_draft_after_send();
|
2017-04-23 08:51:26 +02:00
|
|
|
compose_ui.autosize_textarea();
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").hide(0);
|
2017-06-30 00:57:46 +02:00
|
|
|
$("#compose-send-button").prop('disabled', false);
|
2013-12-03 20:59:23 +01:00
|
|
|
$("#sending-indicator").hide();
|
2014-03-13 19:03:31 +01:00
|
|
|
resize.resize_bottom_whitespace();
|
2013-12-03 20:59:23 +01:00
|
|
|
}
|
|
|
|
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
exports.send_message_success = function (local_id, message_id, locally_echoed) {
|
2017-05-09 19:34:42 +02:00
|
|
|
if (!locally_echoed) {
|
2013-12-19 17:03:08 +01:00
|
|
|
clear_compose_box();
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:30:23 +02:00
|
|
|
echo.reify_message_id(local_id, message_id);
|
|
|
|
};
|
2017-07-13 15:01:02 +02:00
|
|
|
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
exports.transmit_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);
|
|
|
|
}
|
|
|
|
|
2016-12-28 18:24:12 +01:00
|
|
|
if (page_params.use_websockets) {
|
2013-12-19 17:03:08 +01:00
|
|
|
send_message_socket(request, success, error);
|
|
|
|
} else {
|
|
|
|
send_message_ajax(request, success, error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-06 19:50:52 +02:00
|
|
|
exports.send_message = function send_message(request) {
|
2013-10-23 17:39:05 +02:00
|
|
|
if (request === undefined) {
|
|
|
|
request = create_message_object();
|
|
|
|
}
|
2013-09-11 20:42:12 +02:00
|
|
|
|
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(request.to);
|
|
|
|
} else {
|
|
|
|
request.to = JSON.stringify([request.to]);
|
|
|
|
}
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
var local_id;
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
var locally_echoed;
|
2017-05-09 19:34:42 +02:00
|
|
|
|
|
|
|
local_id = echo.try_deliver_locally(request);
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
if (local_id) {
|
|
|
|
// We are rendering this message locally with an id
|
|
|
|
// like 92l99.01 that corresponds to a reasonable
|
|
|
|
// approximation of the id we'll get from the server
|
|
|
|
// in terms of sorting messages.
|
|
|
|
locally_echoed = true;
|
|
|
|
} else {
|
|
|
|
// We are not rendering this message locally, but we
|
|
|
|
// track the message's life cycle with an id like
|
|
|
|
// loc-1, loc-2, loc-3,etc.
|
|
|
|
locally_echoed = false;
|
|
|
|
local_id = sent_messages.get_new_local_id();
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2017-07-14 19:30:23 +02:00
|
|
|
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
request.local_id = local_id;
|
|
|
|
|
|
|
|
sent_messages.start_tracking_message({
|
|
|
|
local_id: local_id,
|
|
|
|
locally_echoed: locally_echoed,
|
|
|
|
});
|
2013-12-19 17:03:08 +01:00
|
|
|
|
2017-07-17 16:52:57 +02:00
|
|
|
request.locally_echoed = locally_echoed;
|
|
|
|
|
2013-11-06 18:57:38 +01:00
|
|
|
function success(data) {
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
exports.send_message_success(local_id, data.id, locally_echoed);
|
2013-12-19 17:03:08 +01:00
|
|
|
}
|
2013-11-04 23:58:51 +01:00
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
function error(response) {
|
|
|
|
// If we're not local echo'ing messages, or if this message was not
|
|
|
|
// locally echoed, show error in compose box
|
sending messages: Extract sent_messages.js.
This commit extract send_messages.js to clean up code related
to the following things:
* sending data to /json/report_send_time
* restarting the event loop if events don't arrive on time
The code related to /json/report changes the following ways:
* We track the state almost completely in the new
send_messages.js module, with other modules just
making one-line calls.
* We no longer send "displayed" times to the servers, since
we were kind of lying about them anyway.
* We now explicitly track the state of each single sent
message in its own object.
* We now look up data related to the messages by local_id,
instead of message_id. The problem with message_id was
that is was mutable. Now we use local_id, and we extend
the local_id concept to messages that don't get rendered
client side. We no longer need to react to the
'message_id_changed' event to change our hash key.
* The code used to live in many places:
* various big chunks were scattered among compose.js,
and those were all moved or reduced to one-line
calls into the new module
* echo.js continues to make basically one-line calls,
but it no longer calls compose.report_as_received(),
nor does it set the "start" time.
* message_util.js used to report received events, but
only when they finally got drawn in the home view;
this code is gone now
The code related to restarting the event loop if events don't arrive
changes as follows:
* The timer now gets set up from within
send_messages.message_state.report_server_ack,
where we can easily inspect the current state of the
possibly-still-in-flight message.
* The code to confirm that an event was received happens now
in server_events.js, rather than later, so that we don't
falsely blame the event loop for a downstream bug. (Plus
it's easier to just do it one place.)
This change removes a fair amount of code from our node tests. Some
of the removal is good stuff related to us completing killing off
unnecessary code. Other removals are more expediency-driven, and
we should make another sweep at ramping up our coverage on compose.js,
with possibly a little more mocking of the new `send_messages` code
layer, since it's now abstracted better.
There is also some minor cleanup to echo.resend_message() in this
commit.
See #5968 for a detailed breakdown of the changes.
2017-07-30 12:56:46 +02:00
|
|
|
if (!locally_echoed) {
|
2017-11-26 20:37:44 +01:00
|
|
|
compose_error(response, $('#compose-textarea'));
|
2013-12-19 17:03:08 +01:00
|
|
|
return;
|
2013-12-03 20:59:23 +01:00
|
|
|
}
|
2013-10-31 15:56:30 +01:00
|
|
|
|
2014-01-02 17:34:16 +01:00
|
|
|
echo.message_send_error(local_id, response);
|
2013-09-11 20:42:12 +02:00
|
|
|
}
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
exports.transmit_message(request, success, error);
|
2014-01-30 20:29:00 +01:00
|
|
|
server_events.assert_get_events_running("Restarting get_events because it was not running during send");
|
2013-12-03 20:59:23 +01:00
|
|
|
|
2017-05-09 19:34:42 +02:00
|
|
|
if (locally_echoed) {
|
2013-12-03 20:59:23 +01:00
|
|
|
clear_compose_box();
|
|
|
|
}
|
2017-07-06 19:50:52 +02:00
|
|
|
};
|
2012-10-29 18:06:53 +01:00
|
|
|
|
2018-01-12 23:13:56 +01:00
|
|
|
exports.deferred_message_types = {
|
|
|
|
scheduled: {
|
|
|
|
delivery_type: 'send_later',
|
|
|
|
test: /^\/schedule/,
|
|
|
|
slash_command: '/schedule',
|
|
|
|
},
|
|
|
|
reminders: {
|
|
|
|
delivery_type: 'remind',
|
|
|
|
test: /^\/remind/,
|
|
|
|
slash_command: '/remind',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function is_deferred_delivery(message_content) {
|
|
|
|
var reminders_test = exports.deferred_message_types.reminders.test;
|
|
|
|
var scheduled_test = exports.deferred_message_types.scheduled.test;
|
|
|
|
return (reminders_test.test(message_content) ||
|
|
|
|
scheduled_test.test(message_content));
|
2018-01-04 21:31:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function patch_request_for_scheduling(request) {
|
|
|
|
var new_request = request;
|
|
|
|
var raw_message = request.content.split('\n');
|
|
|
|
var command_line = raw_message[0];
|
|
|
|
var message = raw_message.slice(1).join('\n');
|
|
|
|
|
2018-01-12 23:13:56 +01:00
|
|
|
var deferred_message_type = _.filter(exports.deferred_message_types, function (props) {
|
|
|
|
return command_line.match(props.test) !== null;
|
|
|
|
})[0];
|
|
|
|
var command = command_line.match(deferred_message_type.test)[0];
|
|
|
|
|
|
|
|
var deliver_at = command_line.slice(command.length + 1);
|
|
|
|
|
|
|
|
if (message.trim() === '' || deliver_at.trim() === '' ||
|
|
|
|
command_line.slice(command.length, command.length + 1) !== ' ') {
|
|
|
|
|
2018-01-04 21:31:23 +01:00
|
|
|
$("#compose-textarea").attr('disabled', false);
|
2018-01-12 23:13:56 +01:00
|
|
|
if (command_line.slice(command.length, command.length + 1) !== ' ') {
|
2018-01-04 21:31:23 +01:00
|
|
|
compose_error(i18n.t('Invalid slash command. Check if you are missing a space after the command.'), $('#compose-textarea'));
|
|
|
|
} else if (deliver_at.trim() === '') {
|
|
|
|
compose_error(i18n.t('Please specify time for your reminder.'), $('#compose-textarea'));
|
|
|
|
} else {
|
|
|
|
compose_error(i18n.t('Your reminder note is empty!'), $('#compose-textarea'));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_request.content = message;
|
|
|
|
new_request.deliver_at = deliver_at;
|
2018-01-12 23:13:56 +01:00
|
|
|
new_request.delivery_type = deferred_message_type.delivery_type;
|
2018-01-04 21:31:23 +01:00
|
|
|
new_request.tz_guess = moment.tz.guess();
|
|
|
|
return new_request;
|
|
|
|
}
|
|
|
|
|
2018-01-16 00:45:17 +01:00
|
|
|
exports.schedule_message = function schedule_message(request, success, error) {
|
2018-01-04 21:31:23 +01:00
|
|
|
if (request === undefined) {
|
|
|
|
request = create_message_object();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(request.to);
|
|
|
|
} else {
|
|
|
|
request.to = JSON.stringify([request.to]);
|
|
|
|
}
|
|
|
|
|
2018-01-16 00:45:17 +01:00
|
|
|
/* success and error callbacks are kind of a package deal here. When scheduling
|
|
|
|
a message either by means of slash command or from message feed, if we need to do
|
|
|
|
something special on success then we will also need to know if our request errored
|
|
|
|
and do something appropriate. Therefore we just check if success callback is not
|
|
|
|
defined and just assume request to be coming from compose box. This is correct
|
|
|
|
because we won't ever actually have success operate in different context than error. */
|
|
|
|
if (success === undefined) {
|
|
|
|
success = function (data) {
|
|
|
|
notifications.notify_above_composebox('Scheduled your Message to be delivered at: ' + data.deliver_at);
|
|
|
|
$("#compose-textarea").attr('disabled', false);
|
|
|
|
clear_compose_box();
|
|
|
|
};
|
|
|
|
error = function (response) {
|
|
|
|
$("#compose-textarea").attr('disabled', false);
|
|
|
|
compose_error(response, $('#compose-textarea'));
|
|
|
|
};
|
|
|
|
/* We are adding a disable on compose under this block since it actually
|
|
|
|
has its place with the branch of code which does stuff when slash command
|
|
|
|
is incoming from compose_box */
|
|
|
|
$("#compose-textarea").attr('disabled', true);
|
2018-01-04 21:31:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
request = patch_request_for_scheduling(request);
|
|
|
|
|
|
|
|
if (request === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.transmit_message(request, success, error);
|
|
|
|
};
|
|
|
|
|
2017-02-19 07:43:02 +01:00
|
|
|
exports.enter_with_preview_open = function () {
|
2017-04-14 16:27:27 +02:00
|
|
|
exports.clear_preview_area();
|
2017-02-19 07:43:02 +01:00
|
|
|
if (page_params.enter_sends) {
|
2017-07-07 19:31:32 +02:00
|
|
|
// If enter_sends is enabled, we attempt to send the message
|
|
|
|
exports.finish();
|
2017-02-19 07:43:02 +01:00
|
|
|
} else {
|
|
|
|
// Otherwise, we return to the compose box and focus it
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").focus();
|
2017-02-19 07:43:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
exports.finish = function () {
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.clear_invites();
|
2017-10-14 15:18:21 +02:00
|
|
|
exports.clear_private_stream_alert();
|
2018-01-04 21:31:23 +01:00
|
|
|
notifications.clear_compose_notifications();
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2012-10-29 22:37:34 +01:00
|
|
|
if (! compose.validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-04 21:31:23 +01:00
|
|
|
|
|
|
|
var message_content = compose_state.message_content();
|
2018-01-12 23:13:56 +01:00
|
|
|
if (is_deferred_delivery(message_content)) {
|
2018-01-04 21:31:23 +01:00
|
|
|
exports.schedule_message();
|
|
|
|
} else {
|
|
|
|
exports.send_message();
|
|
|
|
}
|
2017-04-14 16:27:27 +02:00
|
|
|
exports.clear_preview_area();
|
2012-11-07 19:59:35 +01:00
|
|
|
// TODO: Do we want to fire the event even if the send failed due
|
|
|
|
// to a server-side error?
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('compose_finished.zulip'));
|
2012-10-29 22:37:34 +01:00
|
|
|
return true;
|
2012-10-18 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
2017-02-10 20:03:23 +01:00
|
|
|
exports.update_email = function (user_id, new_email) {
|
2017-04-15 01:15:59 +02:00
|
|
|
var reply_to = compose_state.recipient();
|
2017-02-10 20:03:23 +01:00
|
|
|
|
|
|
|
if (!reply_to) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
reply_to = people.update_email_in_reply_to(reply_to, user_id, new_email);
|
|
|
|
|
2017-04-15 01:15:59 +02:00
|
|
|
compose_state.recipient(reply_to);
|
2017-02-10 20:03:23 +01:00
|
|
|
};
|
2013-10-10 16:43:49 +02:00
|
|
|
|
2017-03-22 14:28:16 +01:00
|
|
|
exports.get_invalid_recipient_emails = function () {
|
|
|
|
var private_recipients = util.extract_pm_recipients(compose_state.recipient());
|
2017-10-26 18:45:08 +02:00
|
|
|
var invalid_recipients = _.reject(private_recipients, people.is_valid_email_for_compose);
|
2017-03-22 14:28:16 +01:00
|
|
|
|
|
|
|
return invalid_recipients;
|
|
|
|
};
|
|
|
|
|
2017-06-28 13:17:10 +02:00
|
|
|
function check_unsubscribed_stream_for_send(stream_name, autosubscribe) {
|
2017-02-12 05:42:47 +01:00
|
|
|
var stream_obj = stream_data.get_sub(stream_name);
|
|
|
|
var result;
|
|
|
|
if (!stream_obj) {
|
|
|
|
return "does-not-exist";
|
2017-04-28 23:47:35 +02:00
|
|
|
}
|
2017-02-12 05:42:47 +01:00
|
|
|
if (!autosubscribe) {
|
|
|
|
return "not-subscribed";
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the rare circumstance of the autosubscribe option, we
|
|
|
|
// *Synchronously* try to subscribe to the stream before sending
|
|
|
|
// the message. This is deprecated and we hope to remove it; see
|
|
|
|
// #4650.
|
2017-04-28 23:47:35 +02:00
|
|
|
channel.post({
|
|
|
|
url: "/json/subscriptions/exists",
|
2017-02-12 05:42:47 +01:00
|
|
|
data: {stream: stream_name, autosubscribe: true},
|
2017-04-28 23:47:35 +02:00
|
|
|
async: false,
|
|
|
|
success: function (data) {
|
|
|
|
if (data.subscribed) {
|
|
|
|
result = "subscribed";
|
|
|
|
} else {
|
|
|
|
result = "not-subscribed";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
if (xhr.status === 404) {
|
|
|
|
result = "does-not-exist";
|
|
|
|
} else {
|
|
|
|
result = "error";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return result;
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
|
2017-01-05 12:05:51 +01:00
|
|
|
function validate_stream_message_mentions(stream_name) {
|
2017-06-29 15:54:31 +02:00
|
|
|
var stream_count = stream_data.get_subscriber_count(stream_name) || 0;
|
2016-11-09 19:34:07 +01:00
|
|
|
|
2016-06-04 04:31:35 +02:00
|
|
|
// check if @all or @everyone is in the message
|
2017-04-15 01:15:59 +02:00
|
|
|
if (util.is_all_or_everyone_mentioned(compose_state.message_content()) &&
|
2016-11-09 19:34:07 +01:00
|
|
|
stream_count > compose.all_everyone_warn_threshold) {
|
2016-06-04 04:31:35 +02:00
|
|
|
if (user_acknowledged_all_everyone === undefined ||
|
|
|
|
user_acknowledged_all_everyone === false) {
|
|
|
|
// user has not seen a warning message yet if undefined
|
|
|
|
show_all_everyone_warnings();
|
2016-11-09 19:34:07 +01:00
|
|
|
|
2017-06-30 00:57:46 +02:00
|
|
|
$("#compose-send-button").prop('disabled', false);
|
2016-11-09 19:34:07 +01:00
|
|
|
$("#sending-indicator").hide();
|
2016-06-04 04:31:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the message no longer contains @all or @everyone
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.clear_all_everyone_warnings();
|
2016-06-04 04:31:35 +02:00
|
|
|
}
|
|
|
|
// at this point, the user has either acknowledged the warning or removed @all / @everyone
|
|
|
|
user_acknowledged_all_everyone = undefined;
|
|
|
|
|
2017-01-05 12:05:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:09:41 +01:00
|
|
|
function validate_stream_message_announce(stream_name) {
|
|
|
|
var stream_count = stream_data.get_subscriber_count(stream_name) || 0;
|
|
|
|
|
|
|
|
if (stream_name === "announce" &&
|
|
|
|
stream_count > compose.announce_warn_threshold) {
|
|
|
|
if (user_acknowledged_announce === undefined ||
|
|
|
|
user_acknowledged_announce === false) {
|
|
|
|
// user has not seen a warning message yet if undefined
|
|
|
|
show_announce_warnings();
|
|
|
|
|
|
|
|
$("#compose-send-button").prop('disabled', false);
|
|
|
|
$("#sending-indicator").hide();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exports.clear_announce_warnings();
|
|
|
|
}
|
|
|
|
// at this point, the user has acknowledged the warning
|
|
|
|
user_acknowledged_announce = undefined;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-28 12:40:53 +02:00
|
|
|
exports.validate_stream_message_address_info = function (stream_name) {
|
2017-06-28 13:14:09 +02:00
|
|
|
if (stream_data.is_subscribed(stream_name)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:04:23 +01:00
|
|
|
var response;
|
|
|
|
|
2017-06-28 13:17:10 +02:00
|
|
|
switch (check_unsubscribed_stream_for_send(stream_name,
|
|
|
|
page_params.narrow_stream !== undefined)) {
|
2017-06-28 13:14:09 +02:00
|
|
|
case "does-not-exist":
|
|
|
|
response = "<p>The stream <b>" +
|
|
|
|
Handlebars.Utils.escapeExpression(stream_name) + "</b> does not exist.</p>" +
|
|
|
|
"<p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>";
|
|
|
|
compose_error(response, $('#stream'));
|
|
|
|
return false;
|
|
|
|
case "error":
|
|
|
|
compose_error(i18n.t("Error checking subscription"), $("#stream"));
|
|
|
|
return false;
|
|
|
|
case "not-subscribed":
|
|
|
|
response = "<p>You're not subscribed to the stream <b>" +
|
|
|
|
Handlebars.Utils.escapeExpression(stream_name) + "</b>.</p>" +
|
|
|
|
"<p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>";
|
|
|
|
compose_error(response, $('#stream'));
|
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
2017-06-28 18:34:39 +02:00
|
|
|
return true;
|
2017-06-28 12:40:53 +02:00
|
|
|
};
|
2017-01-05 12:05:51 +01:00
|
|
|
|
|
|
|
function validate_stream_message() {
|
2017-04-15 01:15:59 +02:00
|
|
|
var stream_name = compose_state.stream_name();
|
2017-01-05 12:05:51 +01:00
|
|
|
if (stream_name === "") {
|
|
|
|
compose_error(i18n.t("Please specify a stream"), $("#stream"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-20 07:50:34 +02:00
|
|
|
if (page_params.realm_mandatory_topics) {
|
2017-04-15 01:15:59 +02:00
|
|
|
var topic = compose_state.subject();
|
2017-01-05 12:05:51 +01:00
|
|
|
if (topic === "") {
|
|
|
|
compose_error(i18n.t("Please specify a topic"), $("#subject"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 16:09:41 +01:00
|
|
|
// If both `@all` is mentioned and it's in `#announce`, just validate
|
|
|
|
// for `@all`. Users shouldn't have to hit "yes" more than once.
|
|
|
|
if (util.is_all_or_everyone_mentioned(compose_state.message_content()) &&
|
|
|
|
stream_name === "announce") {
|
|
|
|
if (!exports.validate_stream_message_address_info(stream_name) ||
|
|
|
|
!validate_stream_message_mentions(stream_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If either criteria isn't met, just do the normal validation.
|
|
|
|
} else {
|
|
|
|
if (!exports.validate_stream_message_address_info(stream_name) ||
|
|
|
|
!validate_stream_message_mentions(stream_name) ||
|
|
|
|
!validate_stream_message_announce(stream_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-05 12:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-22 14:28:16 +01:00
|
|
|
|
2016-12-02 15:16:33 +01:00
|
|
|
// The function checks whether the recipients are users of the realm or cross realm users (bots
|
|
|
|
// for now)
|
2012-11-08 00:38:21 +01:00
|
|
|
function validate_private_message() {
|
2017-04-15 01:15:59 +02:00
|
|
|
if (compose_state.recipient() === "") {
|
2016-06-13 08:40:35 +02:00
|
|
|
compose_error(i18n.t("Please specify at least one recipient"), $("#private_message_recipient"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2017-04-20 08:03:44 +02:00
|
|
|
} else if (page_params.realm_is_zephyr_mirror_realm) {
|
2016-08-11 00:35:52 +02:00
|
|
|
// For Zephyr mirroring realms, the frontend doesn't know which users exist
|
|
|
|
return true;
|
2016-12-02 21:34:35 +01:00
|
|
|
}
|
2017-03-22 14:28:16 +01:00
|
|
|
|
|
|
|
var invalid_recipients = exports.get_invalid_recipient_emails();
|
|
|
|
|
2016-12-02 21:34:35 +01:00
|
|
|
var context = {};
|
|
|
|
if (invalid_recipients.length === 1) {
|
2016-12-03 03:08:47 +01:00
|
|
|
context = {recipient: invalid_recipients.join()};
|
2017-03-27 23:25:43 +02:00
|
|
|
compose_error(i18n.t("The recipient __recipient__ is not valid", context), $("#private_message_recipient"));
|
2016-12-02 21:34:35 +01:00
|
|
|
return false;
|
|
|
|
} else if (invalid_recipients.length > 1) {
|
2016-12-03 03:08:47 +01:00
|
|
|
context = {recipients: invalid_recipients.join()};
|
2017-03-27 23:25:43 +02:00
|
|
|
compose_error(i18n.t("The recipients __recipients__ are not valid", context), $("#private_message_recipient"));
|
2016-12-02 21:34:35 +01:00
|
|
|
return false;
|
2016-06-10 19:46:53 +02:00
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return true;
|
2012-10-03 22:39:05 +02:00
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.validate = function () {
|
2012-10-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").attr('disabled', 'disabled').blur();
|
2018-01-04 21:31:23 +01:00
|
|
|
var message_content = compose_state.message_content();
|
2018-01-12 23:13:56 +01:00
|
|
|
if (is_deferred_delivery(message_content)) {
|
2018-01-04 21:31:23 +01:00
|
|
|
show_sending_indicator('Scheduling...');
|
|
|
|
} else {
|
|
|
|
show_sending_indicator();
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2018-01-04 21:31:23 +01:00
|
|
|
if (/^\s*$/.test(message_content)) {
|
2017-11-26 20:37:44 +01:00
|
|
|
compose_error(i18n.t("You have nothing to send!"), $("#compose-textarea"));
|
2012-10-31 22:18:48 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-21 00:48:03 +01:00
|
|
|
if ($("#zephyr-mirror-error").is(":visible")) {
|
2016-06-13 08:40:35 +02:00
|
|
|
compose_error(i18n.t("You need to be running Zephyr mirroring in order to send messages!"));
|
2013-11-21 00:48:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-24 20:35:26 +02:00
|
|
|
if (compose_state.get_message_type() === 'private') {
|
2012-11-08 00:38:21 +01:00
|
|
|
return validate_private_message();
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return validate_stream_message();
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
|
|
|
|
2017-12-06 14:46:23 +01:00
|
|
|
exports.handle_keydown = function (event) {
|
|
|
|
var code = event.keyCode || event.which;
|
|
|
|
var textarea = $("#compose-textarea");
|
|
|
|
var range = textarea.range();
|
|
|
|
var bKey = 66;
|
|
|
|
var iKey = 73;
|
|
|
|
var lKey = 76;
|
|
|
|
|
|
|
|
if ((code === bKey || code === iKey || code === lKey) && (event.ctrlKey || event.metaKey)) {
|
|
|
|
function add_markdown(markdown) {
|
|
|
|
var textarea = $("#compose-textarea");
|
|
|
|
var range = textarea.range();
|
|
|
|
if (!document.execCommand('insertText', false, markdown)) {
|
|
|
|
textarea.range(range.start, range.end).range(markdown);
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code === bKey) {
|
|
|
|
// ctrl + b: Convert selected text to bold text
|
|
|
|
add_markdown("**" + range.text + "**");
|
|
|
|
if (!range.length) {
|
|
|
|
textarea.caret(textarea.caret() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (code === iKey) {
|
|
|
|
// ctrl + i: Convert selected text to italic text
|
|
|
|
add_markdown("*" + range.text + "*");
|
|
|
|
if (!range.length) {
|
|
|
|
textarea.caret(textarea.caret() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (code === lKey) {
|
|
|
|
// ctrl + l: Insert a link to selected text
|
|
|
|
add_markdown("[" + range.text + "](url)");
|
|
|
|
var position = textarea.caret();
|
|
|
|
var txt = document.getElementById("compose-textarea");
|
|
|
|
|
|
|
|
// Include selected text in between [] parantheses and insert '(url)'
|
|
|
|
// where "url" should be automatically selected.
|
|
|
|
// Position of cursor depends on whether browser supports exec
|
|
|
|
// command or not. So set cursor position accrodingly.
|
|
|
|
if (document.queryCommandEnabled('insertText')) {
|
|
|
|
txt.selectionStart = position - 4;
|
|
|
|
txt.selectionEnd = position - 1;
|
|
|
|
} else {
|
|
|
|
txt.selectionStart = position + range.length + 3;
|
|
|
|
txt.selectionEnd = position + range.length + 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compose_ui.autosize_textarea();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-26 21:29:08 +02:00
|
|
|
exports.initialize = function () {
|
2017-07-07 00:59:09 +02:00
|
|
|
$('#stream,#subject,#private_message_recipient').on('keyup', update_fade);
|
|
|
|
$('#stream,#subject,#private_message_recipient').on('change', update_fade);
|
2017-12-06 14:46:23 +01:00
|
|
|
$('#compose-textarea').on('keydown', exports.handle_keydown);
|
2017-06-26 21:29:08 +02:00
|
|
|
|
|
|
|
$("#compose form").on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
compose.finish();
|
|
|
|
});
|
|
|
|
|
2017-11-26 20:37:44 +01:00
|
|
|
resize.watch_manual_resize("#compose-textarea");
|
2013-05-03 22:16:52 +02:00
|
|
|
|
2013-08-11 19:57:44 +02:00
|
|
|
// Run a feature test and decide whether to display
|
|
|
|
// the "Attach files" button
|
|
|
|
if (window.XMLHttpRequest && (new XMLHttpRequest()).upload) {
|
|
|
|
$("#compose #attach_files").removeClass("notdisplayed");
|
|
|
|
}
|
2013-08-26 19:00:38 +02:00
|
|
|
|
|
|
|
// Lazy load the Dropbox script, since it can slow our page load
|
|
|
|
// otherwise, and isn't enabled for all users. Also, this Dropbox
|
|
|
|
// script isn't under an open source license, so we can't (for legal
|
|
|
|
// reasons) minify it with our own code.
|
|
|
|
if (feature_flags.dropbox_integration) {
|
|
|
|
LazyLoad.js('https://www.dropbox.com/static/api/1/dropins.js', function () {
|
|
|
|
// Successful load. We should now have window.Dropbox.
|
|
|
|
if (! _.has(window, 'Dropbox')) {
|
|
|
|
blueslip.error('Dropbox script reports loading but window.Dropbox undefined');
|
|
|
|
} else if (Dropbox.isBrowserSupported()) {
|
|
|
|
Dropbox.init({appKey: window.dropboxAppKey});
|
|
|
|
$("#compose #attach_dropbox_files").removeClass("notdisplayed");
|
|
|
|
}
|
|
|
|
});
|
2013-08-11 19:57:44 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 21:13:11 +02:00
|
|
|
// Show a warning if a user @-mentions someone who will not receive this message
|
|
|
|
$(document).on('usermention_completed.zulip', function (event, data) {
|
2017-04-15 01:15:59 +02:00
|
|
|
if (compose_state.get_message_type() !== 'stream') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-27 01:45:29 +02:00
|
|
|
// Disable for Zephyr mirroring realms, since we never have subscriber lists there
|
2017-04-20 08:03:44 +02:00
|
|
|
if (page_params.realm_is_zephyr_mirror_realm) {
|
2013-09-16 21:13:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data !== undefined && data.mentioned !== undefined) {
|
|
|
|
var email = data.mentioned.email;
|
2016-06-04 04:31:35 +02:00
|
|
|
|
|
|
|
// warn if @all or @everyone is mentioned
|
|
|
|
if (data.mentioned.full_name === 'all' || data.mentioned.full_name === 'everyone') {
|
|
|
|
return; // don't check if @all or @everyone is subscribed to a stream
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:51:26 +02:00
|
|
|
if (compose_fade.would_receive_message(email) === false) {
|
2013-09-16 21:13:11 +02:00
|
|
|
var error_area = $("#compose_invite_users");
|
2017-07-08 23:29:59 +02:00
|
|
|
var existing_invites_area = $('#compose_invite_users .compose_invite_user');
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2017-07-08 23:29:59 +02:00
|
|
|
var existing_invites = _.map($(existing_invites_area), function (user_row) {
|
2013-09-16 21:13:11 +02:00
|
|
|
return $(user_row).data('useremail');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (existing_invites.indexOf(email) === -1) {
|
2017-07-09 14:13:42 +02:00
|
|
|
var context = {email: email, name: data.mentioned.full_name};
|
|
|
|
var new_row = templates.render("compose-invite-users", context);
|
2013-09-16 21:13:11 +02:00
|
|
|
error_area.append(new_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
error_area.show();
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 04:31:35 +02:00
|
|
|
|
2017-11-08 09:05:13 +01:00
|
|
|
// User group mentions will fall through here. In the future,
|
|
|
|
// we may want to add some sort of similar warning for cases
|
|
|
|
// where nobody in the group is subscribed, but that decision
|
|
|
|
// can wait on user feedback.
|
2016-06-04 04:31:35 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#compose-all-everyone").on('click', '.compose-all-everyone-confirm', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
$(event.target).parents('.compose-all-everyone').remove();
|
|
|
|
user_acknowledged_all_everyone = true;
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.clear_all_everyone_warnings();
|
2016-11-09 19:34:07 +01:00
|
|
|
compose.finish();
|
2013-09-16 21:13:11 +02:00
|
|
|
});
|
|
|
|
|
2017-12-18 16:09:41 +01:00
|
|
|
$("#compose-announce").on('click', '.compose-announce-confirm', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
$(event.target).parents('.compose-announce').remove();
|
|
|
|
user_acknowledged_announce = true;
|
|
|
|
exports.clear_announce_warnings();
|
|
|
|
compose.finish();
|
|
|
|
});
|
|
|
|
|
2013-09-16 21:13:11 +02:00
|
|
|
$("#compose_invite_users").on('click', '.compose_invite_link', function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var invite_row = $(event.target).parents('.compose_invite_user');
|
|
|
|
|
|
|
|
var email = $(invite_row).data('useremail');
|
2017-03-04 21:42:17 +01:00
|
|
|
if (email === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2017-03-04 23:02:59 +01:00
|
|
|
function success() {
|
2017-03-04 21:42:17 +01:00
|
|
|
var all_invites = $("#compose_invite_users");
|
|
|
|
invite_row.remove();
|
2013-09-16 21:13:11 +02:00
|
|
|
|
2017-03-04 21:42:17 +01:00
|
|
|
if (all_invites.children().length === 0) {
|
|
|
|
all_invites.hide();
|
|
|
|
}
|
2017-03-04 23:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function failure() {
|
2017-03-04 21:42:17 +01:00
|
|
|
var error_msg = invite_row.find('.compose_invite_user_error');
|
|
|
|
error_msg.show();
|
|
|
|
|
|
|
|
$(event.target).attr('disabled', true);
|
2017-03-04 23:02:59 +01:00
|
|
|
}
|
|
|
|
|
2017-04-15 01:15:59 +02:00
|
|
|
var stream_name = compose_state.stream_name();
|
2017-03-04 23:02:59 +01:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
|
|
|
if (!sub) {
|
|
|
|
// This should only happen if a stream rename occurs
|
|
|
|
// before the user clicks. We could prevent this by
|
|
|
|
// putting a stream id in the link.
|
|
|
|
blueslip.warn('Stream no longer exists: ' + stream_name);
|
|
|
|
failure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 04:11:25 +02:00
|
|
|
stream_edit.invite_user_to_stream(email, sub, success, failure);
|
2013-09-16 21:13:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#compose_invite_users").on('click', '.compose_invite_close', function (event) {
|
|
|
|
var invite_row = $(event.target).parents('.compose_invite_user');
|
|
|
|
var all_invites = $("#compose_invite_users");
|
|
|
|
|
|
|
|
invite_row.remove();
|
|
|
|
|
|
|
|
if (all_invites.children().length === 0) {
|
|
|
|
all_invites.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-14 15:18:21 +02:00
|
|
|
// Show a warning if a private stream is linked
|
|
|
|
$(document).on('streamname_completed.zulip', function (event, data) {
|
2017-11-27 18:13:57 +01:00
|
|
|
// For PMs, we don't warn about links to private streams, since
|
|
|
|
// you are often specifically encouraging somebody to subscribe
|
|
|
|
// to the stream over PMs.
|
2017-10-14 15:18:21 +02:00
|
|
|
if (compose_state.get_message_type() !== 'stream') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-27 18:13:57 +01:00
|
|
|
if (data === undefined || data.stream === undefined) {
|
|
|
|
blueslip.error('Invalid options passed into handler.');
|
|
|
|
return;
|
|
|
|
}
|
2017-10-14 15:18:21 +02:00
|
|
|
|
2017-11-27 18:13:57 +01:00
|
|
|
// data.stream refers to the stream we're linking to in
|
|
|
|
// typeahead. If it's not invite-only, then it's public, and
|
|
|
|
// there is no need to warn about it, since all users can already
|
|
|
|
// see all the public streams.
|
|
|
|
if (!data.stream.invite_only) {
|
|
|
|
return;
|
2017-10-14 15:18:21 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 18:13:57 +01:00
|
|
|
var stream_name = data.stream.name;
|
|
|
|
|
|
|
|
var warning_area = $("#compose_private_stream_alert");
|
|
|
|
var context = { stream_name: stream_name };
|
|
|
|
var new_row = templates.render("compose_private_stream_alert", context);
|
|
|
|
|
|
|
|
warning_area.append(new_row);
|
|
|
|
warning_area.show();
|
2017-10-14 15:18:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#compose_private_stream_alert").on('click', '.compose_private_stream_alert_close', function (event) {
|
|
|
|
var stream_alert_row = $(event.target).parents('.compose_private_stream_alert');
|
|
|
|
var stream_alert = $("#compose_private_stream_alert");
|
|
|
|
|
|
|
|
stream_alert_row.remove();
|
|
|
|
|
|
|
|
if (stream_alert.children().length === 0) {
|
|
|
|
stream_alert.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-11 19:57:44 +02:00
|
|
|
// Click event binding for "Attach files" button
|
|
|
|
// Triggers a click on a hidden file input field
|
|
|
|
|
|
|
|
$("#compose").on("click", "#attach_files", function (e) {
|
|
|
|
e.preventDefault();
|
2017-11-28 06:26:39 +01:00
|
|
|
if (exports.clone_file_input === undefined) {
|
|
|
|
exports.clone_file_input = $('#file_input').clone(true);
|
2017-06-04 14:10:14 +02:00
|
|
|
}
|
2013-08-11 19:57:44 +02:00
|
|
|
$("#compose #file_input").trigger("click");
|
2017-10-06 21:36:39 +02:00
|
|
|
});
|
2013-08-11 19:57:44 +02:00
|
|
|
|
2017-08-27 18:46:27 +02:00
|
|
|
function show_preview(rendered_content) {
|
|
|
|
var preview_html;
|
|
|
|
if (rendered_content.indexOf("<p>/me ") === 0) {
|
|
|
|
// Handle previews of /me messages
|
|
|
|
preview_html = "<strong>" + page_params.full_name + "</strong> " + rendered_content.slice(4 + 3, -4);
|
|
|
|
} else {
|
|
|
|
preview_html = rendered_content;
|
|
|
|
}
|
|
|
|
$("#preview_content").html(preview_html);
|
2018-01-02 13:02:13 +01:00
|
|
|
if (page_params.emojiset === "text") {
|
2017-10-23 13:54:20 +02:00
|
|
|
$("#preview_content").find(".emoji").replaceWith(function () {
|
|
|
|
var text = $(this).attr("title");
|
|
|
|
return ":" + text + ":";
|
|
|
|
});
|
|
|
|
}
|
2017-08-27 18:46:27 +02:00
|
|
|
}
|
2016-08-29 22:37:27 +02:00
|
|
|
|
2017-08-11 01:51:48 +02:00
|
|
|
$('#compose').on('click', '#video_link', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var video_call_id = util.random_int(100000000000000, 999999999999999);
|
|
|
|
var video_call_link = 'https://meet.jit.si/' + video_call_id;
|
|
|
|
var video_call_link_text = '[' + _('Click to join video call') + '](' + video_call_link + ')';
|
2017-11-09 16:57:58 +01:00
|
|
|
compose_ui.insert_syntax_and_focus(video_call_link_text);
|
2017-08-11 01:51:48 +02:00
|
|
|
});
|
|
|
|
|
2016-08-29 22:37:27 +02:00
|
|
|
$("#compose").on("click", "#markdown_preview", function (e) {
|
|
|
|
e.preventDefault();
|
2017-11-26 20:37:44 +01:00
|
|
|
var content = $("#compose-textarea").val();
|
|
|
|
$("#compose-textarea").hide();
|
2016-08-29 22:37:27 +02:00
|
|
|
$("#markdown_preview").hide();
|
|
|
|
$("#undo_markdown_preview").show();
|
|
|
|
$("#preview_message_area").show();
|
|
|
|
|
2017-08-27 18:27:50 +02:00
|
|
|
if (content.length === 0) {
|
2017-08-27 18:46:27 +02:00
|
|
|
show_preview(i18n.t("Nothing to preview"));
|
2016-08-29 22:37:27 +02:00
|
|
|
} else {
|
2017-08-27 18:27:50 +02:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2016-09-26 22:03:42 +02:00
|
|
|
var spinner = $("#markdown_preview_spinner").expectOne();
|
|
|
|
loading.make_indicator(spinner);
|
2016-09-24 12:16:42 +02:00
|
|
|
} else {
|
|
|
|
// For messages that don't appear to contain
|
|
|
|
// bugdown-specific syntax not present in our
|
|
|
|
// marked.js frontend processor, we render using the
|
|
|
|
// frontend markdown processor message (but still
|
|
|
|
// render server-side to ensure the preview is
|
2017-07-29 02:51:33 +02:00
|
|
|
// accurate; if the `markdown.contains_backend_only_syntax` logic is
|
2016-09-24 12:16:42 +02:00
|
|
|
// incorrect wrong, users will see a brief flicker).
|
2017-08-27 18:26:02 +02:00
|
|
|
var message_obj = {
|
2017-08-27 18:27:50 +02:00
|
|
|
raw_content: content,
|
2017-08-27 18:26:02 +02:00
|
|
|
};
|
2017-08-27 18:46:27 +02:00
|
|
|
markdown.apply_markdown(message_obj);
|
2016-09-24 12:16:42 +02:00
|
|
|
}
|
2017-01-13 01:10:19 +01:00
|
|
|
channel.post({
|
2016-09-24 12:16:42 +02:00
|
|
|
url: '/json/messages/render',
|
|
|
|
idempotent: true,
|
2017-08-27 18:27:50 +02:00
|
|
|
data: {content: content},
|
2016-09-24 12:16:42 +02:00
|
|
|
success: function (response_data) {
|
2017-08-27 18:27:50 +02:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2016-09-26 22:03:42 +02:00
|
|
|
loading.destroy_indicator($("#markdown_preview_spinner"));
|
|
|
|
}
|
2017-08-27 18:46:27 +02:00
|
|
|
show_preview(response_data.rendered);
|
2016-09-24 12:16:42 +02:00
|
|
|
},
|
|
|
|
error: function () {
|
2017-08-27 18:27:50 +02:00
|
|
|
if (markdown.contains_backend_only_syntax(content)) {
|
2016-09-26 22:03:42 +02:00
|
|
|
loading.destroy_indicator($("#markdown_preview_spinner"));
|
|
|
|
}
|
2017-08-27 18:46:27 +02:00
|
|
|
show_preview(i18n.t("Failed to generate preview"));
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2016-09-24 12:16:42 +02:00
|
|
|
});
|
2016-08-29 22:37:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#compose").on("click", "#undo_markdown_preview", function (e) {
|
|
|
|
e.preventDefault();
|
2017-04-14 16:27:27 +02:00
|
|
|
exports.clear_preview_area();
|
2016-08-29 22:37:27 +02:00
|
|
|
});
|
|
|
|
|
2013-08-11 19:57:44 +02:00
|
|
|
$("#compose").on("click", "#attach_dropbox_files", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var options = {
|
|
|
|
// Required. Called when a user selects an item in the Chooser.
|
|
|
|
success: function (files) {
|
2017-11-26 20:37:44 +01:00
|
|
|
var textbox = $("#compose-textarea");
|
2013-08-11 19:57:44 +02:00
|
|
|
var links = _.map(files, function (file) { return '[' + file.name + '](' + file.link +')'; })
|
|
|
|
.join(' ') + ' ';
|
|
|
|
textbox.val(textbox.val() + links);
|
|
|
|
},
|
|
|
|
// Optional. A value of false (default) limits selection to a single file, while
|
|
|
|
// true enables multiple file selection.
|
2013-08-14 16:31:12 +02:00
|
|
|
multiselect: true,
|
2017-01-12 00:17:43 +01:00
|
|
|
iframe: true,
|
2013-08-11 19:57:44 +02:00
|
|
|
};
|
|
|
|
Dropbox.choose(options);
|
|
|
|
});
|
|
|
|
|
2017-11-23 15:00:05 +01:00
|
|
|
upload.initialize();
|
2014-01-14 17:21:41 +01:00
|
|
|
|
|
|
|
if (page_params.narrow !== undefined) {
|
2014-01-17 00:05:35 +01:00
|
|
|
if (page_params.narrow_topic !== undefined) {
|
2017-03-18 17:41:47 +01:00
|
|
|
compose_actions.start("stream", {subject: page_params.narrow_topic});
|
2014-01-17 00:05:35 +01:00
|
|
|
} else {
|
2017-03-18 17:41:47 +01:00
|
|
|
compose_actions.start("stream", {});
|
2014-01-17 00:05:35 +01:00
|
|
|
}
|
2014-01-14 17:21:41 +01:00
|
|
|
}
|
2017-06-26 21:29:08 +02:00
|
|
|
};
|
2012-10-24 04:02:39 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-29 12:23:07 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = compose;
|
|
|
|
}
|