diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb index fcc4dcf92..ac6e166be 100644 --- a/app/helpers/messages_helper.rb +++ b/app/helpers/messages_helper.rb @@ -72,17 +72,6 @@ def message_presentation(message) "" end - # These controls contain URLs and static markup, but no viewer or token state. - # Keep them across database commits; attachment controls still render afresh. - def cache_message_actions(message, &block) - return capture(&block) unless controller.perform_caching && !message.attachment? - - key = fragment_name_with_digest([ - "message-actions-v1", request.base_url, request.script_name, I18n.locale, message.id, message.room_id - ], nil) - FragmentCache.store.fetch(key) { capture(&block) } - end - def message_fragment_cache_key(message) return message unless controller.perform_caching && %i[room creator rich_text_body boosts attachment_attachment].all? { |name| message.association(name).loaded? } diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index 905e42686..937ea68a2 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -1,4 +1,5 @@ import { Controller } from "@hotwired/stimulus" +import { parseHTMLFragment } from "helpers/dom_helpers" import { nextEventLoopTick } from "helpers/timing_helpers" import ClientMessage from "models/client_message" import MessageFormatter, { ThreadStyle } from "models/message_formatter" @@ -6,7 +7,7 @@ import MessagePaginator from "models/message_paginator" import ScrollManager from "models/scroll_manager" export default class extends Controller { - static targets = [ "latest", "message", "body", "messages", "template" ] + static targets = [ "latest", "message", "body", "messages", "template", "actionsMenuTemplate" ] static classes = [ "firstOfDay", "formatted", "me", "mentioned", "threaded" ] static values = { pageUrl: String } @@ -93,10 +94,17 @@ export default class extends Controller { const editorEmpty = document.querySelector("#composer lexxy-editor").isBlank if (editorEmpty && this.#paginator.upToDate) { - this.#myLastMessage?.querySelector(".message__edit-btn")?.click() + const message = this.#myLastMessage + + this.#ensureActionsMenu(message?.querySelector(".message__actions details")) + message?.querySelector(".message__edit-btn")?.click() } } + addActionsMenu({ currentTarget }) { + this.#ensureActionsMenu(currentTarget) + } + // Outlet actions @@ -145,6 +153,18 @@ export default class extends Controller { } } + // A message arrives with the button of its menu but, unless it has a file, without + // the menu: that is the same for every message but for the message's id, so the room + // carries it once and a message gets its copy the first time the menu is wanted. + #ensureActionsMenu(details) { + if (details && !details.querySelector(".message__actions-menu")) { + const { messageId } = details.closest(".message").dataset + const menu = this.actionsMenuTemplateTarget.innerHTML.replaceAll("$messageId$", messageId) + + details.append(parseHTMLFragment(menu)) + } + } + async #ensureUpToDate() { if (!this.#paginator.upToDate) { await this.#paginator.resetToLastPage() diff --git a/app/views/messages/_actions.html.erb b/app/views/messages/_actions.html.erb index 8948783f3..8df86717e 100644 --- a/app/views/messages/_actions.html.erb +++ b/app/views/messages/_actions.html.erb @@ -1,62 +1,16 @@ <%# Be sure to check/update messages/_template.html.erb when changing this file %> -<%= cache_message_actions(message) do %>
<%= tag.details class: "position-relative", - data: { controller: "popup", action: "keydown.esc->popup#close toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> + data: { controller: "popup", action: "keydown.esc->popup#close toggle->messages#addActionsMenu toggle->popup#toggle click@document->popup#closeOnClickOutside", popup_orientation_top_class: "popup-orientation-top" } do %> <%= image_tag "menu-dots-horizontal.svg", size: 20, class: "colorize--black", aria: { hidden: "true" } %> Message options -
-
- <% boost_path = message_boosts_path(message) %> - <% boost_frame = dom_id(message, :boosting) %> - <% EmojiHelper::REACTIONS.each do |character, title| %> -
- <%= hidden_field_tag "boost[content]", character %> - -
- <% end %> - - <%= link_to new_message_boost_path(message), - class: "btn message__action-btn message__boost-btn", - data: { turbo_frame: dom_id(message, :new_boost), action: "soft-keyboard#open popup#close" } do %> - <%= image_tag "boost.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - New boost - <% end %> -
- -
- <% if message.attachment? %> - <%= link_to rails_blob_path(message.attachment, disposition: "attachment", only_path: true), class: "btn message__action-btn center full-width hide-in-ios-pwa", title: "Download", aria: { label: "Download" } do %> - <%= image_tag "download.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - <% end %> - - <%= tag.button class: "btn message__action-btn center full-width", - data: { controller: "web-share", action: "web-share#share", web_share_files_value: rails_blob_path(message.attachment, only_path: true), web_share_title_value: message.attachment.filename.to_s }, title: "Share", aria: { label: "Share" } do %> - <%= image_tag "share.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - <% end %> - <% else %> - <%= tag.button class: "btn message__action-btn center full-width", data: { action: "reply#reply" }, title: "Reply", aria: { label: "Reply" } do %> - <%= image_tag "reply.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - <% end %> - <% end %> - - <%= tag.button class: "btn message__action-btn center full-width", title: "Copy link", aria: { label: "Copy link" }, data: { controller: "copy-to-clipboard", action: "copy-to-clipboard#copy", copy_to_clipboard_success_class: "btn--success", copy_to_clipboard_content_value: url } do %> - <%= image_tag "link.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - <% end %> - - <%= link_to edit_room_message_path(message.room, message), class: "btn message__action-btn center full-width message__edit-btn", - data: { turbo_frame: dom_id(message, :edit) }, title: "Edit", aria: { label: "Edit"} do %> - <%= image_tag "pencil.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> - <% end %> -
-
+ <%# The menu is the same for every message but for the message's id, so a message travels without it and the room adds it when the menu is first opened. Only a message with a file brings its own, which points at the file. %> + <% if message.attachment? %> + <%= render "messages/actions_menu", message_id: message.id, room: message.room, attachment: message.attachment %> + <% end %> <% end %>
-<% end %> diff --git a/app/views/messages/_actions_menu.html.erb b/app/views/messages/_actions_menu.html.erb new file mode 100644 index 000000000..48edea5a6 --- /dev/null +++ b/app/views/messages/_actions_menu.html.erb @@ -0,0 +1,50 @@ +<%# The menu behind a message's options button. Rendered for a message with a file, and once per room in messages/_actions_menu_template.html.erb for all the others, where $messageId$ stands in for message_id. %> + +
+
+ <% boost_path = message_boosts_path(message_id) %> + <% boost_frame = "boosting_message_#{message_id}" %> + <% EmojiHelper::REACTIONS.each do |character, title| %> +
+ <%= hidden_field_tag "boost[content]", character %> + +
+ <% end %> + + <%= link_to new_message_boost_path(message_id), + class: "btn message__action-btn message__boost-btn", + data: { turbo_frame: "new_boost_message_#{message_id}", action: "soft-keyboard#open popup#close" } do %> + <%= image_tag "boost.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + New boost + <% end %> +
+ +
+ <% if attachment %> + <%= link_to rails_blob_path(attachment, disposition: "attachment", only_path: true), class: "btn message__action-btn center full-width hide-in-ios-pwa", title: "Download", aria: { label: "Download" } do %> + <%= image_tag "download.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + <% end %> + + <%= tag.button class: "btn message__action-btn center full-width", + data: { controller: "web-share", action: "web-share#share", web_share_files_value: rails_blob_path(attachment, only_path: true), web_share_title_value: attachment.filename.to_s }, title: "Share", aria: { label: "Share" } do %> + <%= image_tag "share.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + <% end %> + <% else %> + <%= tag.button class: "btn message__action-btn center full-width", data: { action: "reply#reply" }, title: "Reply", aria: { label: "Reply" } do %> + <%= image_tag "reply.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + <% end %> + <% end %> + + <%= tag.button class: "btn message__action-btn center full-width", title: "Copy link", aria: { label: "Copy link" }, data: { controller: "copy-to-clipboard", action: "copy-to-clipboard#copy", copy_to_clipboard_success_class: "btn--success", copy_to_clipboard_content_value: room_at_message_url(room, message_id) } do %> + <%= image_tag "link.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + <% end %> + + <%= link_to edit_room_message_path(room, message_id), class: "btn message__action-btn center full-width message__edit-btn", + data: { turbo_frame: "edit_message_#{message_id}" }, title: "Edit", aria: { label: "Edit"} do %> + <%= image_tag "pencil.svg", class: "colorize--black", size: 20, aria: { hidden: "true" } %> + <% end %> +
+
diff --git a/app/views/messages/_actions_menu_template.html.erb b/app/views/messages/_actions_menu_template.html.erb new file mode 100644 index 000000000..df7f3234e --- /dev/null +++ b/app/views/messages/_actions_menu_template.html.erb @@ -0,0 +1,4 @@ +<%# The one menu the room's messages share. The messages controller copies it into a message, with the message's id for $messageId$, when the message's menu is first opened. %> + diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb index 307e31f16..e34b735a5 100644 --- a/app/views/messages/_message.html.erb +++ b/app/views/messages/_message.html.erb @@ -23,7 +23,7 @@ <%= link_to room_display_name(message.room, for_user: nil), room_at_message_path(message.room, message), target: "_top", data: { reply_target: "link" } %> - <%= render "messages/actions", message: message, url: room_at_message_url(message.room, message) %> + <%= render "messages/actions", message: message %> <%= render "messages/presentation", message: message %> <%= render "messages/boosts/boosts", message: message %> diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index 234aac0c5..398a596cd 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -11,6 +11,7 @@ <%= message_area_tag(@room) do %> <%= render "messages/template" %> + <%= render "messages/actions_menu_template", room: @room %> <%= messages_tag(@room) do %> <%= render "rooms/show/invitation", room: @room %> diff --git a/test/controllers/fragment_cache_test.rb b/test/controllers/fragment_cache_test.rb index 484bbf2a0..0c2e5e5fc 100644 --- a/test/controllers/fragment_cache_test.rb +++ b/test/controllers/fragment_cache_test.rb @@ -63,15 +63,13 @@ class FragmentRenderingTest < ActionDispatch::IntegrationTest assert_includes response.body, "foreign conditional body" end - test "message controls survive creator edits while the author is rendered fresh" do + test "the author is rendered fresh after a creator edit" do get room_messages_url(@room) assert_response :success - foreign_write("UPDATE users SET name = ? WHERE id = ?", "Fresh creator with cached controls", @message.creator_id) - ActionView::Base.any_instance.expects(:form_with).never + foreign_write("UPDATE users SET name = ? WHERE id = ?", "Fresh creator", @message.creator_id) get room_messages_url(@room) assert_response :success - assert_includes response.body, "Fresh creator with cached controls" - assert_select "form[action=?]", message_boosts_path(@message) + assert_includes response.body, "Fresh creator" end test "a commit in another room retains content-validated message collection hits" do diff --git a/test/controllers/messages/boosts_controller_test.rb b/test/controllers/messages/boosts_controller_test.rb index 85998921e..55ca9c3d8 100644 --- a/test/controllers/messages/boosts_controller_test.rb +++ b/test/controllers/messages/boosts_controller_test.rb @@ -33,7 +33,8 @@ class Messages::BoostsControllerTest < ActionDispatch::IntegrationTest test "quick boost controls submit their displayed reaction to the message" do get room_url(@message.room) - forms = css_select("##{dom_id(@message)} .quick-boosts form") + assert_select "##{dom_id(@message)} .quick-boosts", count: 0 + forms = message_menu_for(@message).css(".quick-boosts form") assert_equal EmojiHelper::REACTIONS.size, forms.size forms.each do |form| assert_equal "post", form["method"] diff --git a/test/controllers/messages_controller_test.rb b/test/controllers/messages_controller_test.rb index 09a319f61..ab7ed41c7 100644 --- a/test/controllers/messages_controller_test.rb +++ b/test/controllers/messages_controller_test.rb @@ -57,11 +57,30 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest assert_rendered_turbo_stream_broadcast @room, :messages, action: "append", target: [ @room, :messages ] do assert_select ".message__body", text: /New one/ - assert_copy_link_button room_at_message_url(@room, Message.last, host: "once.campfire.test") + assert_select ".message__options-btn", count: 1 + assert_select ".message__actions-menu", count: 0 end end - test "message copy links preserve the request origin in broadcasts and regular reads" do + test "creating a message with a file broadcasts it with its own menu" do + post room_messages_url(@room, format: :turbo_stream), params: { message: { + attachment: fixture_file_upload("moon.jpg", "image/jpeg"), client_message_id: 999 } } + + assert_response :success + message = Message.last + assert_rendered_turbo_stream_broadcast @room, :messages, action: "append", target: [ @room, :messages ] do + assert_select ".message__actions-menu[data-popup-target='menu']", count: 1 + assert_select ".quick-boosts form[action='#{message_boosts_path(message)}'][data-turbo-frame='#{dom_id(message, :boosting)}']", count: EmojiHelper::REACTIONS.size + assert_select "a.message__boost-btn[href='#{new_message_boost_path(message)}'][data-turbo-frame='#{dom_id(message, :new_boost)}']" + assert_select "a[title='Download'][href='#{rails_blob_path(message.attachment, disposition: "attachment", only_path: true)}']" + assert_select "button[title='Share'][data-web-share-files-value='#{rails_blob_path(message.attachment, only_path: true)}'][data-web-share-title-value='moon.jpg']" + assert_select "[title='Reply']", count: 0 + assert_copy_link_button room_at_message_url(@room, message, host: "once.campfire.test") + assert_select "a.message__edit-btn[href='#{edit_room_message_path(@room, message)}'][data-turbo-frame='#{dom_id(message, :edit)}']" + end + end + + test "message copy links preserve the request origin in the room's menu, broadcasts and regular reads" do [ [ "once.campfire.test:38683", false, "http://once.campfire.test:38683" ], [ "once.campfire.test:8443", true, "https://once.campfire.test:8443" ], @@ -71,8 +90,15 @@ class MessagesControllerTest < ActionDispatch::IntegrationTest host! host https! secure + get room_path(@room) + assert_response :success + text_message = @messages.last + copy_link = message_menu_for(text_message).at_css(".btn[title='Copy link']") + assert_equal "#{origin}/rooms/#{@room.id}/@#{text_message.id}", copy_link["data-copy-to-clipboard-content-value"] + + # A message with a file is the one that still carries its menu post room_messages_path(@room, format: :turbo_stream), params: { message: { - body: "Request origin #{index}", client_message_id: "request-origin-#{index}" } } + attachment: fixture_file_upload("moon.jpg", "image/jpeg"), client_message_id: "request-origin-#{index}" } } assert_response :success message = Message.last expected_url = "#{origin}/rooms/#{@room.id}/@#{message.id}" diff --git a/test/controllers/rooms_controller_test.rb b/test/controllers/rooms_controller_test.rb index e8ed51fa1..6b97c8bc5 100644 --- a/test/controllers/rooms_controller_test.rb +++ b/test/controllers/rooms_controller_test.rb @@ -15,6 +15,32 @@ class RoomsControllerTest < ActionDispatch::IntegrationTest assert_response :success end + test "show carries the message menu once and each message only its button" do + room = rooms(:watercooler) + message = room.messages.ordered.last + get room_url(room) + + assert_response :success + assert_select ".message .message__options-btn", count: room.messages.count + assert_select ".message .message__actions-menu", count: 0 + + menu = message_menu_for(message) + assert_equal 1, menu.css(".message__actions-menu[data-popup-target='menu']").size + assert_not_includes menu.to_html, "$messageId$" + + new_boost = menu.at_css("a.message__boost-btn") + assert_equal new_message_boost_path(message), new_boost["href"] + assert_equal dom_id(message, :new_boost), new_boost["data-turbo-frame"] + + edit = menu.at_css("a.message__edit-btn") + assert_equal edit_room_message_path(room, message), edit["href"] + assert_equal dom_id(message, :edit), edit["data-turbo-frame"] + + assert menu.at_css("button[title='Reply'][data-action='reply#reply']") + assert_nil menu.at_css("[title='Download']") + assert_nil menu.at_css("[title='Share']") + end + test "show renders notification help for each platform" do { "Firefox on Android" => "Mozilla/5.0 (Android 14; Mobile; rv:131.0) Gecko/131.0 Firefox/131.0", diff --git a/test/system/message_menu_test.rb b/test/system/message_menu_test.rb new file mode 100644 index 000000000..24928f041 --- /dev/null +++ b/test/system/message_menu_test.rb @@ -0,0 +1,64 @@ +require "application_system_test_case" + +class MessageMenuTest < ApplicationSystemTestCase + setup do + sign_in "kevin@37signals.com" + end + + test "quick boosting a message from its menu" do + join_room rooms(:designers) + + within_message messages(:third) do + reveal_message_actions + click_on "Thumbs up" + + assert_selector ".boost", text: "👍" + end + end + + test "a message gets its menu when it is first opened, and only then" do + message = messages(:third) + join_room message.room + + within_message message do + assert_no_selector ".message__actions-menu", visible: :all + + reveal_message_actions + assert_selector ".message__actions-menu[style*='--max-width']" + + find(".message__options-btn").click + assert_no_selector ".message__boost-btn" + reveal_message_actions + + assert_selector ".message__actions-menu", count: 1 + assert_selector "[aria-label='Copy link'][data-copy-to-clipboard-content-value$='#{room_at_message_path(message.room, message)}']" + end + end + + # Only the room page carries the menu the messages copy. Search results hide a message's actions, and a + # message opened on its own is the source of its edit frame, never formatted into view. + test "a message's menu is out of reach on the pages that don't carry the room's" do + message = rooms(:designers).messages.create! body: "A needle in the haystack", client_message_id: "needle", creator: users(:jz) + + visit searches_url(q: "needle") + assert_selector "#search-results .message", text: "A needle in the haystack" + assert_selector "#search-results .message__actions", visible: :hidden + + visit room_message_url(message.room, message) + assert_selector "##{dom_id(message)} .message__actions", visible: :hidden + end + + test "a message with a file opens the menu it came with" do + message = rooms(:designers).messages.create_with_attachment! creator: users(:jz), client_message_id: "moon", + attachment: { io: file_fixture("moon.jpg").open, filename: "moon.jpg", content_type: "image/jpeg" } + join_room message.room + + within_message message do + reveal_message_actions + + assert_selector ".message__actions-menu", count: 1 + assert_selector "[aria-label='Download']" + assert_no_selector "[aria-label='Reply']" + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 30b87bed3..6327d2710 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,7 +16,7 @@ class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - include SessionTestHelper, MentionTestHelper, TurboTestHelper, DnsTestHelper + include SessionTestHelper, MentionTestHelper, TurboTestHelper, DnsTestHelper, MessageMenuTestHelper setup do ActionCable.server.pubsub.clear diff --git a/test/test_helpers/message_menu_test_helper.rb b/test/test_helpers/message_menu_test_helper.rb new file mode 100644 index 000000000..c922b7b1c --- /dev/null +++ b/test/test_helpers/message_menu_test_helper.rb @@ -0,0 +1,8 @@ +module MessageMenuTestHelper + # A room page carries the menu of its messages once, with $messageId$ where the message's id + # goes. This is the menu as the browser builds it for the message when its menu is first opened. + def message_menu_for(message) + template = assert_select("script[type='text/template'][data-messages-target='actionsMenuTemplate']", count: 1).first + Nokogiri::HTML5.fragment(template.text.gsub("$messageId$", message.id.to_s)) + end +end