var message_edit = (function () { var exports = {}; var currently_editing_messages = {}; 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, FULL: 4, }; exports.editability_types = editability_types; function is_topic_editable(message, edit_limit_seconds_buffer) { var now = new XDate(); edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0; // TODO: Change hardcoded value (24 hrs) to be realm setting return message.sent_by_me || (page_params.realm_allow_community_topic_editing && (86400 + edit_limit_seconds_buffer + now.diffSeconds(message.timestamp * 1000) > 0)); } function get_editability(message, edit_limit_seconds_buffer) { edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0; if (!message) { return editability_types.NO; } if (!is_topic_editable(message, edit_limit_seconds_buffer)) { return editability_types.NO; } if (message.failed_request) { // 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; } // Locally echoed messages are not editable, since the message hasn't // finished being sent yet. if (message.locally_echoed) { return editability_types.NO; } if (!page_params.realm_allow_message_editing) { return editability_types.NO; } if (page_params.realm_message_content_edit_limit_seconds === 0 && message.sent_by_me) { return editability_types.FULL; } var now = new XDate(); if ((page_params.realm_message_content_edit_limit_seconds + edit_limit_seconds_buffer + now.diffSeconds(message.timestamp * 1000) > 0) && message.sent_by_me) { return editability_types.FULL; } // time's up! if (message.type === 'stream') { return editability_types.TOPIC_ONLY; } return editability_types.NO_LONGER; } exports.get_editability = get_editability; exports.is_topic_editable = is_topic_editable; 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; }; // Returns true if the edit task should end. exports.save = function (row, from_topic_edited_only) { var msg_list = current_msg_list; var message_id; if (row.hasClass('recipient_row')) { message_id = rows.id_for_recipient_row(row); } else { message_id = rows.id(row); } var message = current_msg_list.get(message_id); var changed = false; var new_content = row.find(".message_edit_content").val(); var topic_changed = false; var new_topic; if (message.type === "stream") { if (from_topic_edited_only) { new_topic = row.find(".inline_topic_edit").val(); } else { new_topic = row.find(".message_edit_topic").val(); } topic_changed = (new_topic !== message.subject && new_topic.trim() !== ""); } // Editing a not-yet-acked message (because the original send attempt failed) // just results in the in-memory message being changed if (message.locally_echoed) { if (new_content !== message.raw_content || topic_changed) { echo.edit_locally(message, new_content, topic_changed ? new_topic : undefined); row = current_msg_list.get_row(message_id); } message_edit.end(row); return; } var request = {message_id: message.id}; if (topic_changed) { request.subject = new_topic; 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; } if (new_content !== message.raw_content && !from_topic_edited_only) { request.content = new_content; changed = true; } if (!changed) { // If they didn't change anything, just cancel it. message_edit.end(row); return; } channel.patch({ url: '/json/messages/' + message.id, data: request, success: function () { var spinner = row.find(".topic_edit_spinner"); loading.destroy_indicator(spinner); }, error: function (xhr) { if (msg_list === current_msg_list) { var message = channel.xhr_error_message(i18n.t("Error saving edit"), xhr); row.find(".edit_error").text(message).show(); } }, }); // The message will automatically get replaced via message_list.update_message. }; 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(); } }; 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(); }; function handle_edit_keydown(from_topic_edited_only, e) { var row; var code = e.keyCode || e.which; if ($(e.target).hasClass("message_edit_content") && code === 13 && (e.metaKey || e.ctrlKey)) { row = $(".message_edit_content").filter(":focus").closest(".message_row"); } else if (e.target.id === "message_edit_topic" && code === 13) { row = $(e.target).closest(".message_row"); } else if (e.target.id === "inline_topic_edit" && code === 13) { row = $(e.target).closest(".recipient_row"); exports.show_topic_edit_spinner(row); } else { return; } e.stopPropagation(); e.preventDefault(); message_edit.save(row, from_topic_edited_only); } function timer_text(seconds_left) { var minutes = Math.floor(seconds_left / 60); var seconds = seconds_left % 60; if (minutes >= 1) { return i18n.t("__minutes__ min to edit", {minutes: minutes.toString()}); } else if (seconds_left >= 10) { return i18n.t("__seconds__ sec to edit", {seconds: (seconds - seconds % 5).toString()}); } return i18n.t("__seconds__ sec to edit", {seconds: seconds.toString()}); } function edit_message(row, raw_content) { row.find(".message_reactions").hide(); condense.hide_message_expander(row); var content_top = row.find('.message_content')[0] .getBoundingClientRect().top; var message = current_msg_list.get(rows.id(row)); // 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; var editability = get_editability(message, seconds_left_buffer); var is_editable = (editability === message_edit.editability_types.TOPIC_ONLY || editability === message_edit.editability_types.FULL); var form = $(templates.render( 'message_edit_form', {is_stream: (message.type === 'stream'), message_id: message.id, is_editable: is_editable, has_been_editable: (editability !== editability_types.NO), topic: message.subject, content: raw_content, minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60)})); var edit_obj = {form: form, raw_content: raw_content}; currently_editing_messages[message.id] = edit_obj; current_msg_list.show_edit_message(row, edit_obj); form.keydown(_.partial(handle_edit_keydown, false)); upload.feature_check($('#attach_files_' + rows.id(row))); 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'); var copy_message = row.find('.copy_message'); if (editability === editability_types.NO) { message_edit_content.prop("readonly", "readonly"); message_edit_topic.prop("readonly", "readonly"); new ClipboardJS(copy_message[0]); } 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. message_edit_content.prop("readonly", "readonly"); message_edit_countdown_timer.text(i18n.t("View source")); new ClipboardJS(copy_message[0]); } else if (editability === editability_types.TOPIC_ONLY) { message_edit_content.prop("readonly", "readonly"); // Hint why you can edit the topic but not the message content message_edit_countdown_timer.text(i18n.t("Topic editing only")); new ClipboardJS(copy_message[0]); } else if (editability === editability_types.FULL) { copy_message.remove(); var edit_id = "#message_edit_content_" + rows.id(row); var listeners = resize.watch_manual_resize(edit_id); if (listeners) { currently_editing_messages[rows.id(row)].listeners = listeners; } composebox_typeahead.initialize_compose_typeahead(edit_id); } // Add tooltip if (editability !== editability_types.NO && page_params.realm_message_content_edit_limit_seconds > 0) { row.find('.message-edit-timer-control-group').show(); row.find('#message_edit_tooltip').tooltip({ animation: false, placement: 'left', template: '