2019-07-09 21:24:00 +02:00
|
|
|
var render_message_edit_form = require('../templates/message_edit_form.hbs');
|
|
|
|
var render_message_edit_history = require('../templates/message_edit_history.hbs');
|
|
|
|
var render_topic_edit_form = require('../templates/topic_edit_form.hbs');
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
var currently_editing_messages = {};
|
2019-01-24 20:51:10 +01:00
|
|
|
var currently_deleting_messages = [];
|
2013-05-15 00:22:16 +02:00
|
|
|
|
2016-10-22 04:26:35 +02:00
|
|
|
var editability_types = {
|
|
|
|
NO: 1,
|
|
|
|
NO_LONGER: 2,
|
|
|
|
// Note: TOPIC_ONLY does not include stream messages with no topic sent
|
|
|
|
// by someone else. You can edit the topic of such a message by editing
|
|
|
|
// the topic of the whole recipient_row it appears in, but you can't
|
|
|
|
// directly edit the topic of such a message.
|
|
|
|
// Similar story for messages whose topic you can change only because
|
|
|
|
// you are an admin.
|
|
|
|
TOPIC_ONLY: 3,
|
2017-01-12 00:17:43 +01:00
|
|
|
FULL: 4,
|
2016-10-22 04:26:35 +02:00
|
|
|
};
|
|
|
|
exports.editability_types = editability_types;
|
|
|
|
|
2018-04-25 23:15:04 +02:00
|
|
|
function is_topic_editable(message, edit_limit_seconds_buffer) {
|
|
|
|
var now = new XDate();
|
|
|
|
edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0;
|
|
|
|
|
2018-10-09 09:54:57 +02:00
|
|
|
if (!page_params.realm_allow_message_editing) {
|
|
|
|
// If message editing is disabled, so is topic editing.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Organization admins and message senders can edit message topics indefinitely.
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (message.sent_by_me) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!page_params.realm_allow_community_topic_editing) {
|
|
|
|
// If you're another non-admin user, you need community topic editing enabled.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you're using community topic editing, there's a deadline.
|
|
|
|
// TODO: Change hardcoded value (24 hrs) to be realm setting. Currently, it is
|
|
|
|
// DEFAULT_COMMUNITY_TOPIC_EDITING_LIMIT_SECONDS
|
|
|
|
return 86400 + edit_limit_seconds_buffer + now.diffSeconds(message.timestamp * 1000) > 0;
|
2018-04-25 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function get_editability(message, edit_limit_seconds_buffer) {
|
2016-10-22 04:26:35 +02:00
|
|
|
edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0;
|
2017-12-03 00:56:17 +01:00
|
|
|
if (!message) {
|
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2018-04-25 23:15:04 +02:00
|
|
|
if (!is_topic_editable(message, edit_limit_seconds_buffer)) {
|
2016-11-10 21:17:30 +01:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
|
|
|
if (message.failed_request) {
|
2017-07-18 14:44:29 +02:00
|
|
|
// TODO: For completely failed requests, we should be able
|
|
|
|
// to "edit" the message, but it won't really be like
|
|
|
|
// other message updates. This commit changed the result
|
|
|
|
// from FULL to NO, since the prior implementation was
|
|
|
|
// buggy.
|
|
|
|
return editability_types.NO;
|
2016-11-10 21:17:30 +01:00
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2016-12-29 23:50:24 +01:00
|
|
|
// Locally echoed messages are not editable, since the message hasn't
|
|
|
|
// finished being sent yet.
|
2017-07-17 16:52:57 +02:00
|
|
|
if (message.locally_echoed) {
|
2016-11-10 21:17:30 +01:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2016-11-10 21:17:30 +01:00
|
|
|
if (!page_params.realm_allow_message_editing) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2017-12-03 00:56:17 +01:00
|
|
|
if (page_params.realm_message_content_edit_limit_seconds === 0 && message.sent_by_me) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
var now = new XDate();
|
2018-06-06 18:50:09 +02:00
|
|
|
if (page_params.realm_message_content_edit_limit_seconds + edit_limit_seconds_buffer +
|
|
|
|
now.diffSeconds(message.timestamp * 1000) > 0 && message.sent_by_me) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.FULL;
|
|
|
|
}
|
2017-12-03 00:56:17 +01:00
|
|
|
|
2016-10-22 04:26:35 +02:00
|
|
|
// time's up!
|
|
|
|
if (message.type === 'stream') {
|
|
|
|
return editability_types.TOPIC_ONLY;
|
|
|
|
}
|
|
|
|
return editability_types.NO_LONGER;
|
|
|
|
}
|
|
|
|
exports.get_editability = get_editability;
|
2018-04-25 23:15:04 +02:00
|
|
|
exports.is_topic_editable = is_topic_editable;
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2017-11-26 09:12:10 +01:00
|
|
|
exports.get_deletability = function (message) {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.sent_by_me) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (message.locally_echoed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!page_params.realm_allow_message_deleting) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.realm_message_content_delete_limit_seconds === 0) {
|
|
|
|
// This means no time limit for message deletion.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.realm_allow_message_deleting) {
|
|
|
|
var now = new XDate();
|
|
|
|
if (page_params.realm_message_content_delete_limit_seconds +
|
|
|
|
now.diffSeconds(message.timestamp * 1000) > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-06-09 22:44:24 +02:00
|
|
|
// Returns true if the edit task should end.
|
2016-06-07 19:59:59 +02:00
|
|
|
exports.save = function (row, from_topic_edited_only) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var msg_list = current_msg_list;
|
2014-03-13 04:25:34 +01:00
|
|
|
var message_id;
|
2014-02-26 18:37:30 +01:00
|
|
|
|
|
|
|
if (row.hasClass('recipient_row')) {
|
2014-03-13 04:25:34 +01:00
|
|
|
message_id = rows.id_for_recipient_row(row);
|
2014-02-26 18:37:30 +01:00
|
|
|
} else {
|
2014-03-13 04:25:34 +01:00
|
|
|
message_id = rows.id(row);
|
2014-02-26 18:37:30 +01:00
|
|
|
}
|
2014-03-13 04:25:34 +01:00
|
|
|
var message = current_msg_list.get(message_id);
|
2013-08-20 17:00:55 +02:00
|
|
|
var changed = false;
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
var new_content = row.find(".message_edit_content").val();
|
|
|
|
var topic_changed = false;
|
|
|
|
var new_topic;
|
2018-12-23 16:01:30 +01:00
|
|
|
var old_topic = util.get_message_topic(message);
|
|
|
|
|
2013-08-20 17:00:55 +02:00
|
|
|
if (message.type === "stream") {
|
2016-10-25 23:19:43 +02:00
|
|
|
if (from_topic_edited_only) {
|
|
|
|
new_topic = row.find(".inline_topic_edit").val();
|
|
|
|
} else {
|
|
|
|
new_topic = row.find(".message_edit_topic").val();
|
|
|
|
}
|
2018-12-23 16:01:30 +01:00
|
|
|
topic_changed = new_topic !== old_topic && new_topic.trim() !== "";
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
// Editing a not-yet-acked message (because the original send attempt failed)
|
|
|
|
// just results in the in-memory message being changed
|
2017-07-17 16:52:57 +02:00
|
|
|
if (message.locally_echoed) {
|
2016-10-26 01:12:12 +02:00
|
|
|
if (new_content !== message.raw_content || topic_changed) {
|
|
|
|
echo.edit_locally(message, new_content, topic_changed ? new_topic : undefined);
|
2016-11-10 21:17:30 +01:00
|
|
|
row = current_msg_list.get_row(message_id);
|
2013-08-20 17:00:55 +02:00
|
|
|
}
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.end(row);
|
2016-10-26 01:12:12 +02:00
|
|
|
return;
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var request = {message_id: message.id};
|
|
|
|
if (topic_changed) {
|
2018-12-22 18:44:54 +01:00
|
|
|
util.set_message_topic(request, new_topic);
|
2014-01-02 19:39:22 +01:00
|
|
|
if (feature_flags.propagate_topic_edits) {
|
|
|
|
var selected_topic_propagation = row.find("select.message_edit_topic_propagate").val() || "change_later";
|
|
|
|
request.propagate_mode = selected_topic_propagation;
|
|
|
|
}
|
|
|
|
changed = true;
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2013-08-20 17:00:55 +02:00
|
|
|
|
2016-06-07 19:59:59 +02:00
|
|
|
if (new_content !== message.raw_content && !from_topic_edited_only) {
|
2013-05-15 00:22:16 +02:00
|
|
|
request.content = new_content;
|
2013-08-20 17:00:55 +02:00
|
|
|
changed = true;
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2013-08-20 17:00:55 +02:00
|
|
|
if (!changed) {
|
2013-05-22 18:15:59 +02:00
|
|
|
// If they didn't change anything, just cancel it.
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.end(row);
|
2016-10-26 01:12:12 +02:00
|
|
|
return;
|
2013-05-21 16:21:13 +02:00
|
|
|
}
|
2016-12-22 10:17:49 +01:00
|
|
|
channel.patch({
|
|
|
|
url: '/json/messages/' + message.id,
|
2013-05-22 18:15:59 +02:00
|
|
|
data: request,
|
2016-12-02 14:06:06 +01:00
|
|
|
success: function () {
|
2018-03-23 08:44:55 +01:00
|
|
|
var spinner = row.find(".topic_edit_spinner");
|
|
|
|
loading.destroy_indicator(spinner);
|
2013-07-11 23:42:44 +02:00
|
|
|
},
|
2016-12-02 14:06:06 +01:00
|
|
|
error: function (xhr) {
|
2016-10-26 01:15:07 +02:00
|
|
|
if (msg_list === current_msg_list) {
|
2017-03-01 05:36:51 +01:00
|
|
|
var message = channel.xhr_error_message(i18n.t("Error saving edit"), xhr);
|
2016-10-26 01:15:07 +02:00
|
|
|
row.find(".edit_error").text(message).show();
|
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-05-22 18:15:59 +02:00
|
|
|
});
|
2016-10-26 01:12:12 +02:00
|
|
|
// The message will automatically get replaced via message_list.update_message.
|
2013-05-15 00:22:16 +02:00
|
|
|
};
|
|
|
|
|
2018-03-05 18:28:37 +01:00
|
|
|
exports.update_message_topic_editing_pencil = function () {
|
|
|
|
if (page_params.realm_allow_message_editing) {
|
|
|
|
$(".on_hover_topic_edit, .always_visible_topic_edit").show();
|
|
|
|
} else {
|
|
|
|
$(".on_hover_topic_edit, .always_visible_topic_edit").hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-23 08:44:55 +01:00
|
|
|
exports.show_topic_edit_spinner = function (row) {
|
|
|
|
var spinner = row.find(".topic_edit_spinner");
|
|
|
|
loading.make_indicator(spinner);
|
|
|
|
$(spinner).removeAttr("style");
|
|
|
|
$(".topic_edit_save").hide();
|
|
|
|
$(".topic_edit_cancel").hide();
|
|
|
|
};
|
|
|
|
|
2016-06-07 19:59:59 +02:00
|
|
|
function handle_edit_keydown(from_topic_edited_only, e) {
|
2016-12-02 17:09:31 +01:00
|
|
|
var row;
|
|
|
|
var code = e.keyCode || e.which;
|
2013-08-02 19:25:37 +02:00
|
|
|
|
2019-01-04 16:28:32 +01:00
|
|
|
if ($(e.target).hasClass("message_edit_content") && code === 13) {
|
|
|
|
// Pressing enter to save edits is coupled with enter to send
|
|
|
|
if (composebox_typeahead.should_enter_send(e)) {
|
|
|
|
row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
2019-04-16 03:37:33 +02:00
|
|
|
var message_edit_save_button = row.find(".message_edit_save");
|
|
|
|
if (message_edit_save_button.attr('disabled') === "disabled") {
|
|
|
|
// In cases when the save button is disabled
|
|
|
|
// we need to disable save on pressing enter
|
|
|
|
// Prevent default to avoid new-line on pressing
|
|
|
|
// enter inside the textarea in this case
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-04 16:28:32 +01:00
|
|
|
} else {
|
|
|
|
composebox_typeahead.handle_enter($(e.target), e);
|
|
|
|
return;
|
|
|
|
}
|
2019-09-28 02:41:23 +02:00
|
|
|
} else if (code === 13 && ($(e.target).hasClass("message_edit_topic") ||
|
|
|
|
$(e.target).hasClass("message_edit_topic_propagate"))) {
|
2016-10-26 01:15:23 +02:00
|
|
|
row = $(e.target).closest(".message_row");
|
2016-10-25 23:19:43 +02:00
|
|
|
} else if (e.target.id === "inline_topic_edit" && code === 13) {
|
2016-10-26 01:15:23 +02:00
|
|
|
row = $(e.target).closest(".recipient_row");
|
2018-03-23 08:44:55 +01:00
|
|
|
exports.show_topic_edit_spinner(row);
|
2016-10-26 01:15:23 +02:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.save(row, from_topic_edited_only);
|
2013-08-02 19:25:37 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
function timer_text(seconds_left) {
|
|
|
|
var minutes = Math.floor(seconds_left / 60);
|
|
|
|
var seconds = seconds_left % 60;
|
|
|
|
if (minutes >= 1) {
|
2016-12-03 03:08:47 +01:00
|
|
|
return i18n.t("__minutes__ min to edit", {minutes: minutes.toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
} else if (seconds_left >= 10) {
|
2016-12-03 03:08:47 +01:00
|
|
|
return i18n.t("__seconds__ sec to edit", {seconds: (seconds - seconds % 5).toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
2016-12-03 03:08:47 +01:00
|
|
|
return i18n.t("__seconds__ sec to edit", {seconds: seconds.toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function edit_message(row, raw_content) {
|
2017-03-08 16:53:57 +01:00
|
|
|
row.find(".message_reactions").hide();
|
2017-03-15 15:26:39 +01:00
|
|
|
condense.hide_message_expander(row);
|
2019-06-25 08:26:53 +02:00
|
|
|
var content_top = row.find('.message_top_line')[0]
|
2013-07-22 20:19:35 +02:00
|
|
|
.getBoundingClientRect().top;
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
var message = current_msg_list.get(rows.id(row));
|
2013-08-02 19:25:37 +02:00
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
// We potentially got to this function by clicking a button that implied the
|
|
|
|
// user would be able to edit their message. Give a little bit of buffer in
|
|
|
|
// case the button has been around for a bit, e.g. we show the
|
|
|
|
// edit_content_button (hovering pencil icon) as long as the user would have
|
|
|
|
// been able to click it at the time the mouse entered the message_row. Also
|
|
|
|
// a buffer in case their computer is slow, or stalled for a second, etc
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
|
|
|
// zerver.views.messages.update_message_backend
|
|
|
|
var seconds_left_buffer = 5;
|
2016-10-22 04:26:35 +02:00
|
|
|
var editability = get_editability(message, seconds_left_buffer);
|
2019-10-25 09:45:13 +02:00
|
|
|
var is_editable = editability === exports.editability_types.TOPIC_ONLY ||
|
|
|
|
editability === exports.editability_types.FULL;
|
2019-05-04 20:24:36 +02:00
|
|
|
var max_file_upload_size = page_params.max_file_upload_size;
|
|
|
|
var file_upload_enabled = false;
|
|
|
|
|
|
|
|
if (max_file_upload_size > 0) {
|
|
|
|
file_upload_enabled = true;
|
|
|
|
}
|
2016-10-23 03:19:16 +02:00
|
|
|
|
2019-07-09 21:24:00 +02:00
|
|
|
var form = $(render_message_edit_form({
|
|
|
|
is_stream: message.type === 'stream',
|
|
|
|
message_id: message.id,
|
|
|
|
is_editable: is_editable,
|
2019-10-25 09:45:13 +02:00
|
|
|
is_content_editable: editability === exports.editability_types.FULL,
|
2019-07-09 21:24:00 +02:00
|
|
|
has_been_editable: editability !== editability_types.NO,
|
|
|
|
topic: util.get_message_topic(message),
|
|
|
|
content: raw_content,
|
|
|
|
file_upload_enabled: file_upload_enabled,
|
|
|
|
minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60),
|
|
|
|
}));
|
2016-10-23 03:19:16 +02:00
|
|
|
|
|
|
|
var edit_obj = {form: form, raw_content: raw_content};
|
2016-10-23 03:20:16 +02:00
|
|
|
currently_editing_messages[message.id] = edit_obj;
|
2016-10-23 03:19:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, edit_obj);
|
|
|
|
|
|
|
|
form.keydown(_.partial(handle_edit_keydown, false));
|
|
|
|
|
2017-11-28 22:36:58 +01:00
|
|
|
upload.feature_check($('#attach_files_' + rows.id(row)));
|
|
|
|
|
2016-11-02 23:46:34 +01:00
|
|
|
var message_edit_content = row.find('textarea.message_edit_content');
|
|
|
|
var message_edit_topic = row.find('input.message_edit_topic');
|
|
|
|
var message_edit_topic_propagate = row.find('select.message_edit_topic_propagate');
|
|
|
|
var message_edit_countdown_timer = row.find('.message_edit_countdown_timer');
|
2017-03-26 11:02:37 +02:00
|
|
|
var copy_message = row.find('.copy_message');
|
|
|
|
|
2016-10-22 02:38:56 +02:00
|
|
|
if (editability === editability_types.NO) {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_content.prop("readonly", "readonly");
|
|
|
|
message_edit_topic.prop("readonly", "readonly");
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 02:38:56 +02:00
|
|
|
} else if (editability === editability_types.NO_LONGER) {
|
|
|
|
// You can currently only reach this state in non-streams. If that
|
|
|
|
// changes (e.g. if we stop allowing topics to be modified forever
|
|
|
|
// in streams), then we'll need to disable
|
|
|
|
// row.find('input.message_edit_topic') as well.
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_content.prop("readonly", "readonly");
|
|
|
|
message_edit_countdown_timer.text(i18n.t("View source"));
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 02:38:56 +02:00
|
|
|
} else if (editability === editability_types.TOPIC_ONLY) {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_content.prop("readonly", "readonly");
|
2016-10-23 03:26:28 +02:00
|
|
|
// Hint why you can edit the topic but not the message content
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(i18n.t("Topic editing only"));
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 04:26:35 +02:00
|
|
|
} else if (editability === editability_types.FULL) {
|
2017-03-26 11:02:37 +02:00
|
|
|
copy_message.remove();
|
2017-04-26 05:14:33 +02:00
|
|
|
var edit_id = "#message_edit_content_" + rows.id(row);
|
|
|
|
var listeners = resize.watch_manual_resize(edit_id);
|
2017-05-11 18:28:33 +02:00
|
|
|
if (listeners) {
|
|
|
|
currently_editing_messages[rows.id(row)].listeners = listeners;
|
|
|
|
}
|
2017-10-13 03:05:31 +02:00
|
|
|
composebox_typeahead.initialize_compose_typeahead(edit_id);
|
2018-08-11 06:43:38 +02:00
|
|
|
compose.handle_keyup(null, $(edit_id).expectOne());
|
2018-07-18 05:59:55 +02:00
|
|
|
$(edit_id).on('keydown', function (event) {
|
|
|
|
compose.handle_keydown(event, $(this).expectOne());
|
|
|
|
});
|
2018-08-11 06:43:38 +02:00
|
|
|
$(edit_id).on('keyup', function (event) {
|
|
|
|
compose.handle_keyup(event, $(this).expectOne());
|
|
|
|
});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 03:31:39 +02:00
|
|
|
// Add tooltip
|
2016-10-22 02:38:56 +02:00
|
|
|
if (editability !== editability_types.NO &&
|
|
|
|
page_params.realm_message_content_edit_limit_seconds > 0) {
|
2016-07-08 02:25:55 +02:00
|
|
|
row.find('.message-edit-timer-control-group').show();
|
2016-10-23 03:33:41 +02:00
|
|
|
row.find('#message_edit_tooltip').tooltip({
|
2016-10-23 03:31:39 +02:00
|
|
|
animation: false,
|
|
|
|
placement: 'left',
|
|
|
|
template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div>' +
|
2017-01-12 00:17:43 +01:00
|
|
|
'<div class="tooltip-inner message-edit-tooltip-inner"></div></div>',
|
2016-10-23 03:31:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:35:42 +02:00
|
|
|
// add timer
|
2016-10-22 04:26:35 +02:00
|
|
|
if (editability === editability_types.FULL &&
|
2016-10-23 03:35:42 +02:00
|
|
|
page_params.realm_message_content_edit_limit_seconds > 0) {
|
2016-10-22 02:33:50 +02:00
|
|
|
// Give them at least 10 seconds.
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
|
|
|
// zerver.views.messages.update_message_backend
|
|
|
|
var min_seconds_to_edit = 10;
|
2016-10-22 04:26:35 +02:00
|
|
|
var now = new XDate();
|
|
|
|
var seconds_left = page_params.realm_message_content_edit_limit_seconds +
|
|
|
|
now.diffSeconds(message.timestamp * 1000);
|
2016-10-22 02:33:50 +02:00
|
|
|
seconds_left = Math.floor(Math.max(seconds_left, min_seconds_to_edit));
|
2016-10-23 03:35:42 +02:00
|
|
|
|
2016-11-02 23:46:34 +01:00
|
|
|
// I believe this needs to be defined outside the countdown_timer, since
|
2016-10-23 03:35:42 +02:00
|
|
|
// row just refers to something like the currently selected message, and
|
|
|
|
// can change out from under us
|
2016-11-02 23:46:34 +01:00
|
|
|
var message_edit_save = row.find('button.message_edit_save');
|
2016-10-23 03:35:42 +02:00
|
|
|
// Do this right away, rather than waiting for the timer to do its first update,
|
|
|
|
// since otherwise there is a noticeable lag
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(timer_text(seconds_left));
|
2019-10-26 00:26:37 +02:00
|
|
|
const countdown_timer = setInterval(function () {
|
2017-02-21 06:41:07 +01:00
|
|
|
seconds_left -= 1;
|
|
|
|
if (seconds_left <= 0) {
|
2016-10-23 03:35:42 +02:00
|
|
|
clearInterval(countdown_timer);
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_content.prop("readonly", "readonly");
|
2016-10-23 03:35:42 +02:00
|
|
|
if (message.type === 'stream') {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_topic.prop("readonly", "readonly");
|
|
|
|
message_edit_topic_propagate.hide();
|
2016-10-23 03:35:42 +02:00
|
|
|
}
|
2016-10-22 04:26:35 +02:00
|
|
|
// We don't go directly to a "TOPIC_ONLY" type state (with an active Save button),
|
2016-10-23 03:35:42 +02:00
|
|
|
// since it isn't clear what to do with the half-finished edit. It's nice to keep
|
|
|
|
// the half-finished edit around so that they can copy-paste it, but we don't want
|
|
|
|
// people to think "Save" will save the half-finished edit.
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_save.addClass("disabled");
|
|
|
|
message_edit_countdown_timer.text(i18n.t("Time's up!"));
|
2016-10-23 03:35:42 +02:00
|
|
|
} else {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(timer_text(seconds_left));
|
2016-10-23 03:35:42 +02:00
|
|
|
}
|
|
|
|
}, 1000);
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 02:38:56 +02:00
|
|
|
if (!is_editable) {
|
2016-11-03 01:53:13 +01:00
|
|
|
row.find(".message_edit_close").focus();
|
2018-12-23 16:01:30 +01:00
|
|
|
} else if (message.type === 'stream' && util.get_message_topic(message) === compose.empty_topic_placeholder()) {
|
2016-11-03 01:53:13 +01:00
|
|
|
message_edit_topic.val('');
|
|
|
|
message_edit_topic.focus();
|
2016-10-25 22:56:28 +02:00
|
|
|
} else if (editability === editability_types.TOPIC_ONLY) {
|
2019-04-13 22:12:40 +02:00
|
|
|
row.find(".message_edit_topic").focus();
|
2013-07-17 16:24:54 +02:00
|
|
|
} else {
|
2016-11-03 01:53:13 +01:00
|
|
|
message_edit_content.focus();
|
2016-11-03 02:03:02 +01:00
|
|
|
// Put cursor at end of input.
|
|
|
|
var contents = message_edit_content.val();
|
|
|
|
message_edit_content.val('');
|
|
|
|
message_edit_content.val(contents);
|
2013-07-17 16:24:54 +02:00
|
|
|
}
|
2013-07-22 20:19:35 +02:00
|
|
|
|
2019-06-25 07:53:57 +02:00
|
|
|
// Scroll to keep the top of the message content text in the same
|
|
|
|
// place visually, adjusting for border and padding.
|
2016-11-03 01:53:13 +01:00
|
|
|
var edit_top = message_edit_content[0].getBoundingClientRect().top;
|
2019-06-25 08:26:53 +02:00
|
|
|
var scroll_by = edit_top - content_top + 5 - 14;
|
|
|
|
|
2013-07-22 20:19:35 +02:00
|
|
|
edit_obj.scrolled_by = scroll_by;
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + scroll_by);
|
2013-09-04 20:30:33 +02:00
|
|
|
|
2017-07-17 16:52:57 +02:00
|
|
|
if (feature_flags.propagate_topic_edits && !message.locally_echoed) {
|
2018-12-23 16:01:30 +01:00
|
|
|
var original_topic = util.get_message_topic(message);
|
2017-10-06 21:36:39 +02:00
|
|
|
message_edit_topic.keyup(function () {
|
2016-11-03 01:53:13 +01:00
|
|
|
var new_topic = message_edit_topic.val();
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_topic_propagate.toggle(new_topic !== original_topic && new_topic !== "");
|
2013-09-13 18:12:29 +02:00
|
|
|
});
|
2013-09-04 20:30:33 +02:00
|
|
|
}
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
function start_edit_maintaining_scroll(row, content) {
|
|
|
|
edit_message(row, content);
|
|
|
|
var row_bottom = row.height() + row.offset().top;
|
|
|
|
var composebox_top = $("#compose").offset().top;
|
|
|
|
if (row_bottom > composebox_top) {
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + row_bottom - composebox_top);
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:51:05 +02:00
|
|
|
function start_edit_with_content(row, content, edit_box_open_callback) {
|
|
|
|
start_edit_maintaining_scroll(row, content);
|
|
|
|
if (edit_box_open_callback) {
|
|
|
|
edit_box_open_callback();
|
|
|
|
}
|
2018-03-05 09:10:05 +01:00
|
|
|
|
|
|
|
row.find('#message_edit_form').filedrop(
|
|
|
|
upload.options({
|
|
|
|
mode: 'edit',
|
|
|
|
row: rows.id(row),
|
|
|
|
})
|
|
|
|
);
|
2017-06-06 18:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
exports.start = function (row, edit_box_open_callback) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var message = current_msg_list.get(rows.id(row));
|
2017-08-28 01:10:55 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
blueslip.error("Couldn't find message ID for edit " + rows.id(row));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:51:05 +02:00
|
|
|
if (message.raw_content) {
|
|
|
|
start_edit_with_content(row, message.raw_content, edit_box_open_callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
var msg_list = current_msg_list;
|
2016-12-21 20:31:16 +01:00
|
|
|
channel.get({
|
|
|
|
url: '/json/messages/' + message.id,
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-05-15 00:22:16 +02:00
|
|
|
success: function (data) {
|
|
|
|
if (current_msg_list === msg_list) {
|
|
|
|
message.raw_content = data.raw_content;
|
2017-06-06 18:51:05 +02:00
|
|
|
start_edit_with_content(row, message.raw_content, edit_box_open_callback);
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-05-15 00:22:16 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
exports.start_topic_edit = function (recipient_row) {
|
2019-07-09 21:24:00 +02:00
|
|
|
var form = $(render_topic_edit_form());
|
2013-08-16 23:45:13 +02:00
|
|
|
current_msg_list.show_edit_topic(recipient_row, form);
|
2016-06-07 19:59:59 +02:00
|
|
|
form.keydown(_.partial(handle_edit_keydown, true));
|
2014-03-13 04:25:34 +01:00
|
|
|
var msg_id = rows.id_for_recipient_row(recipient_row);
|
|
|
|
var message = current_msg_list.get(msg_id);
|
2018-12-23 16:01:30 +01:00
|
|
|
var topic = util.get_message_topic(message);
|
2016-08-27 04:47:53 +02:00
|
|
|
if (topic === compose.empty_topic_placeholder()) {
|
2013-11-21 00:28:14 +01:00
|
|
|
topic = '';
|
|
|
|
}
|
2016-10-25 23:19:43 +02:00
|
|
|
form.find(".inline_topic_edit").val(topic).select().focus();
|
2013-08-16 23:45:13 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 18:54:07 +02:00
|
|
|
exports.is_editing = function (id) {
|
|
|
|
return currently_editing_messages[id] !== undefined;
|
|
|
|
};
|
|
|
|
|
2013-05-22 19:04:11 +02:00
|
|
|
exports.end = function (row) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var message = current_msg_list.get(rows.id(row));
|
2014-02-21 16:46:26 +01:00
|
|
|
if (message !== undefined &&
|
|
|
|
currently_editing_messages[message.id] !== undefined) {
|
2013-07-22 20:19:35 +02:00
|
|
|
var scroll_by = currently_editing_messages[message.id].scrolled_by;
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() - scroll_by);
|
2017-04-26 04:30:35 +02:00
|
|
|
|
|
|
|
// Clean up resize event listeners
|
|
|
|
var listeners = currently_editing_messages[message.id].listeners;
|
2017-04-26 05:14:33 +02:00
|
|
|
var edit_box = document.querySelector("#message_edit_content_" + message.id);
|
2017-05-02 00:07:10 +02:00
|
|
|
if (listeners !== undefined) {
|
|
|
|
// Event listeners to cleanup are only set in some edit types
|
|
|
|
edit_box.removeEventListener("mousedown", listeners[0]);
|
|
|
|
document.body.removeEventListener("mouseup", listeners[1]);
|
|
|
|
}
|
2017-04-26 04:30:35 +02:00
|
|
|
|
2013-05-28 22:13:49 +02:00
|
|
|
delete currently_editing_messages[message.id];
|
|
|
|
current_msg_list.hide_edit_message(row);
|
|
|
|
}
|
2016-10-26 01:12:12 +02:00
|
|
|
if (row !== undefined) {
|
|
|
|
current_msg_list.hide_edit_topic(row);
|
|
|
|
}
|
2017-03-15 15:26:39 +01:00
|
|
|
condense.show_message_expander(row);
|
2017-03-08 16:53:57 +01:00
|
|
|
row.find(".message_reactions").show();
|
2017-07-19 15:24:03 +02:00
|
|
|
|
|
|
|
// We have to blur out text fields, or else hotkeys.js
|
|
|
|
// thinks we are still editing.
|
|
|
|
row.find(".message_edit").blur();
|
2013-05-15 00:22:16 +02:00
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.maybe_show_edit = function (row, id) {
|
2015-11-10 19:01:34 +01:00
|
|
|
if (currently_editing_messages[id] !== undefined) {
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, currently_editing_messages[id]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
exports.edit_last_sent_message = function () {
|
2017-03-27 18:00:31 +02:00
|
|
|
var msg = current_msg_list.get_last_message_sent_by_me();
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
return;
|
2016-11-23 05:06:34 +01:00
|
|
|
}
|
2017-03-27 18:00:31 +02:00
|
|
|
|
|
|
|
if (!msg.id) {
|
|
|
|
blueslip.error('Message has invalid id in edit_last_sent_message.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg_editability_type = exports.get_editability(msg, 5);
|
|
|
|
if (msg_editability_type !== editability_types.FULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg_row = current_msg_list.get_row(msg.id);
|
|
|
|
if (!msg_row) {
|
|
|
|
// This should never happen, since we got the message above
|
|
|
|
// from current_msg_list.
|
|
|
|
blueslip.error('Could not find row for id ' + msg.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_msg_list.select_id(msg.id, {then_scroll: true, from_scroll: true});
|
|
|
|
|
|
|
|
// Finally do the real work!
|
2017-03-27 19:15:05 +02:00
|
|
|
compose_actions.cancel();
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.start(msg_row, function () {
|
2018-12-22 16:19:52 +01:00
|
|
|
$('#message_edit_content').focus();
|
2017-03-27 18:00:31 +02:00
|
|
|
});
|
2016-11-23 05:06:34 +01:00
|
|
|
};
|
|
|
|
|
2017-02-20 02:37:58 +01:00
|
|
|
exports.show_history = function (message) {
|
|
|
|
$('#message-history').html('');
|
|
|
|
$('#message-edit-history').modal("show");
|
|
|
|
channel.get({
|
|
|
|
url: "/json/messages/" + message.id + "/history",
|
|
|
|
data: {message_id: JSON.stringify(message.id)},
|
|
|
|
success: function (data) {
|
|
|
|
var content_edit_history = [];
|
2017-06-06 04:42:26 +02:00
|
|
|
var prev_timestamp;
|
2017-02-20 02:37:58 +01:00
|
|
|
_.each(data.message_history, function (msg, index) {
|
|
|
|
// Format timestamp nicely for display
|
2017-06-06 04:42:26 +02:00
|
|
|
var timestamp = timerender.get_full_time(msg.timestamp);
|
|
|
|
var item = {
|
|
|
|
timestamp: moment(timestamp).format("h:mm A"),
|
|
|
|
display_date: moment(timestamp).format("MMMM D, YYYY"),
|
|
|
|
};
|
2019-02-22 15:08:16 +01:00
|
|
|
if (msg.user_id) {
|
|
|
|
var person = people.get_person_from_user_id(msg.user_id);
|
|
|
|
item.edited_by = person.full_name;
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:37:58 +01:00
|
|
|
if (index === 0) {
|
|
|
|
item.posted_or_edited = "Posted by";
|
|
|
|
item.body_to_render = msg.rendered_content;
|
2017-06-06 04:42:26 +02:00
|
|
|
prev_timestamp = timestamp;
|
|
|
|
item.show_date_row = true;
|
2019-02-22 15:08:16 +01:00
|
|
|
} else if (msg.prev_topic && msg.prev_content) {
|
|
|
|
item.posted_or_edited = "Edited by";
|
|
|
|
item.body_to_render = msg.content_html_diff;
|
|
|
|
item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day');
|
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
|
|
|
} else if (msg.prev_topic) {
|
|
|
|
item.posted_or_edited = "Topic edited by";
|
|
|
|
item.topic_edited = true;
|
|
|
|
item.prev_topic = msg.prev_topic;
|
|
|
|
item.new_topic = msg.topic;
|
2017-02-20 02:37:58 +01:00
|
|
|
} else {
|
2019-02-22 15:08:16 +01:00
|
|
|
// just a content edit
|
2017-02-20 02:37:58 +01:00
|
|
|
item.posted_or_edited = "Edited by";
|
|
|
|
item.body_to_render = msg.content_html_diff;
|
2017-06-06 04:42:26 +02:00
|
|
|
item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day');
|
2017-02-20 02:37:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
content_edit_history.push(item);
|
|
|
|
});
|
|
|
|
|
2019-07-09 21:24:00 +02:00
|
|
|
$('#message-history').html(render_message_edit_history({
|
2017-02-20 02:37:58 +01:00
|
|
|
edited_messages: content_edit_history,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2017-03-18 21:17:41 +01:00
|
|
|
ui_report.error(i18n.t("Error fetching message edit history"), xhr,
|
2017-02-20 02:37:58 +01:00
|
|
|
$("#message-history-error"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-24 20:51:10 +01:00
|
|
|
function hide_delete_btn_show_spinner(deleting) {
|
|
|
|
if (deleting) {
|
|
|
|
$('do_delete_message_button').attr('disabled', 'disabled');
|
|
|
|
$('#delete_message_modal > div.modal-footer > button').hide();
|
|
|
|
var delete_spinner = $("#do_delete_message_spinner");
|
|
|
|
loading.make_indicator(delete_spinner, { abs_positioned: true });
|
|
|
|
} else {
|
|
|
|
loading.destroy_indicator($("#do_delete_message_spinner"));
|
|
|
|
$("#do_delete_message_button").prop('disabled', false);
|
|
|
|
$('#delete_message_modal > div.modal-footer > button').show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 21:14:26 +02:00
|
|
|
exports.delete_message = function (msg_id) {
|
|
|
|
$("#delete-message-error").html('');
|
|
|
|
$('#delete_message_modal').modal("show");
|
2019-01-24 20:51:10 +01:00
|
|
|
if (_.contains(currently_deleting_messages, msg_id)) {
|
|
|
|
hide_delete_btn_show_spinner(true);
|
|
|
|
} else {
|
|
|
|
hide_delete_btn_show_spinner(false);
|
|
|
|
}
|
2017-05-14 21:14:26 +02:00
|
|
|
$('#do_delete_message_button').off().on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-01-24 20:51:10 +01:00
|
|
|
currently_deleting_messages.push(msg_id);
|
|
|
|
hide_delete_btn_show_spinner(true);
|
2017-05-14 21:14:26 +02:00
|
|
|
channel.del({
|
|
|
|
url: "/json/messages/" + msg_id,
|
|
|
|
success: function () {
|
|
|
|
$('#delete_message_modal').modal("hide");
|
2019-01-24 20:51:10 +01:00
|
|
|
currently_deleting_messages = _.reject(currently_deleting_messages, function (id) {
|
|
|
|
return id === msg_id;
|
|
|
|
});
|
|
|
|
hide_delete_btn_show_spinner(false);
|
2017-05-14 21:14:26 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2019-01-24 20:51:10 +01:00
|
|
|
currently_deleting_messages = _.reject(currently_deleting_messages, function (id) {
|
|
|
|
return id === msg_id;
|
|
|
|
});
|
|
|
|
hide_delete_btn_show_spinner(false);
|
2018-04-28 04:11:16 +02:00
|
|
|
ui_report.error(i18n.t("Error deleting message"), xhr,
|
2018-05-06 21:43:17 +02:00
|
|
|
$("#delete-message-error"));
|
2017-05-14 21:14:26 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:40:54 +01:00
|
|
|
exports.delete_topic = function (stream_id, topic_name) {
|
|
|
|
channel.post({
|
|
|
|
url: "/json/streams/" + stream_id + "/delete_topic",
|
|
|
|
data: {
|
|
|
|
topic_name: topic_name,
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
$('#delete_topic_modal').modal('hide');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-06 18:01:04 +02:00
|
|
|
exports.handle_narrow_deactivated = function () {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(currently_editing_messages, function (elem, idx) {
|
2013-05-15 00:22:16 +02:00
|
|
|
if (current_msg_list.get(idx) !== undefined) {
|
2013-08-14 22:00:32 +02:00
|
|
|
var row = current_msg_list.get_row(idx);
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, elem);
|
|
|
|
}
|
|
|
|
});
|
2018-08-06 18:01:04 +02:00
|
|
|
};
|
2013-05-15 00:22:16 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.message_edit = exports;
|