From 879f2a1e2e3a384323ff3b7600357f406cbc36a7 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Fri, 21 Oct 2016 19:26:35 -0700 Subject: [PATCH] message editing: Refactor editability computation to use editability_types. We compute the editability of messages in several places around the frontend; standardize the definitions and store in message_edit.get_editability. This commit should not change app behavior. --- static/js/message_edit.js | 56 +++++++++++++++---- static/js/popovers.js | 9 +-- static/js/ui.js | 8 +-- .../actions_popover_content.handlebars | 2 +- 4 files changed, 53 insertions(+), 22 deletions(-) diff --git a/static/js/message_edit.js b/static/js/message_edit.js index 9619450413..d513c098aa 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -2,6 +2,42 @@ 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 get_editability (message, edit_limit_seconds_buffer) { + edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0; + if (!message || !message.sent_by_me || message.local_id !== undefined || + !page_params.realm_allow_message_editing) { + return editability_types.NO; + } + if (page_params.realm_message_content_edit_limit_seconds === 0) { + 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) { + 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; // Returns true if the edit task should end. exports.save = function (row, from_topic_edited_only) { @@ -112,12 +148,7 @@ function edit_message (row, raw_content) { // If you change this number also change edit_limit_buffer in // zerver.views.messages.update_message_backend var seconds_left_buffer = 5; - - var now = new XDate(); - var seconds_left = page_params.realm_message_content_edit_limit_seconds + - now.diffSeconds(message.timestamp * 1000); - var can_edit_content = (page_params.realm_message_content_edit_limit_seconds === 0) || - (seconds_left + seconds_left_buffer > 0); + var editability = get_editability(message, seconds_left_buffer); var form = $(templates.render( 'message_edit_form', @@ -132,11 +163,11 @@ function edit_message (row, raw_content) { form.keydown(_.partial(handle_edit_keydown, false)); - if (!can_edit_content) { + if (editability === editability_types.TOPIC_ONLY) { row.find('textarea.message_edit_content').attr("disabled","disabled"); // Hint why you can edit the topic but not the message content row.find('.message_edit_countdown_timer').text(i18n.t("Topic editing only")); - } else { + } else if (editability === editability_types.FULL) { composebox_typeahead.initialize_compose_typeahead("#message_edit_content", {emoji: true}); } @@ -152,12 +183,15 @@ function edit_message (row, raw_content) { } // add timer - if (can_edit_content && + if (editability === editability_types.FULL && page_params.realm_message_content_edit_limit_seconds > 0) { // 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; + var now = new XDate(); + var seconds_left = page_params.realm_message_content_edit_limit_seconds + + now.diffSeconds(message.timestamp * 1000); seconds_left = Math.floor(Math.max(seconds_left, min_seconds_to_edit)); // I believe these need to be defined outside the countdown_timer, since @@ -182,7 +216,7 @@ function edit_message (row, raw_content) { message_topic_row.attr("disabled","disabled"); message_topic_propagate_row.hide(); } - // We don't go directly to "Topic editing only" state (with an active Save button), + // We don't go directly to a "TOPIC_ONLY" type state (with an active Save button), // 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. @@ -196,7 +230,7 @@ function edit_message (row, raw_content) { var edit_row = row.find(".message_edit"); if ((message.type === 'stream' && message.subject === compose.empty_topic_placeholder()) || - !can_edit_content) { + editability === editability_types.TOPIC_ONLY) { edit_row.find(".message_edit_topic").focus(); } else { edit_row.find(".message_edit_content").focus(); diff --git a/static/js/popovers.js b/static/js/popovers.js index 65424597d6..165c743cd5 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -57,7 +57,9 @@ exports.toggle_actions_popover = function (element, id) { var elt = $(element); if (elt.data('popover') === undefined) { var message = current_msg_list.get(id); - var can_edit = message.sent_by_me && message.local_id === undefined && page_params.realm_allow_message_editing; + var editability = message_edit.get_editability(message); + var is_editable = (editability === message_edit.editability_types.TOPIC_ONLY || + editability === message_edit.editability_types.FULL); var can_mute_topic = message.stream && message.subject && @@ -67,10 +69,9 @@ exports.toggle_actions_popover = function (element, id) { message.subject && muting.is_topic_muted(message.stream, message.subject); - var args = { - message: message, - can_edit_message: can_edit, + message: message, + is_editable: is_editable, can_mute_topic: can_mute_topic, can_unmute_topic: can_unmute_topic, conversation_time_uri: narrow.by_conversation_and_time_uri(message), diff --git a/static/js/ui.js b/static/js/ui.js index 4977d4c9c8..afad051f7a 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -118,12 +118,8 @@ function message_hover(message_row) { message = current_msg_list.get(rows.id(message_row)); message_unhover(); message_row.addClass('message_hovered'); - var now = new XDate(); - if (message && message.sent_by_me && !message.status_message && - page_params.realm_allow_message_editing && - (page_params.realm_message_content_edit_limit_seconds === 0 || - page_params.realm_message_content_edit_limit_seconds + now.diffSeconds(message.timestamp * 1000) > 0)) - { + if ((message_edit.get_editability(message) === message_edit.editability_types.FULL) && + !message.status_message) { message_row.find('.message_content').find('p:last').append(edit_content_button); } current_message_hover = message_row; diff --git a/static/templates/actions_popover_content.handlebars b/static/templates/actions_popover_content.handlebars index 7984fc0b6c..4638bd3583 100644 --- a/static/templates/actions_popover_content.handlebars +++ b/static/templates/actions_popover_content.handlebars @@ -1,6 +1,6 @@ {{! Contents of the "message actions" popup }}