Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? }
Expand Down
24 changes: 22 additions & 2 deletions app/javascript/controllers/messages_controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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"
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 }

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
56 changes: 5 additions & 51 deletions app/views/messages/_actions.html.erb
Original file line number Diff line number Diff line change
@@ -1,62 +1,16 @@
<%# Be sure to check/update messages/_template.html.erb when changing this file %>

<%= cache_message_actions(message) do %>
<div class="message__actions" data-controller="soft-keyboard">
<%= 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 %>
<summary class="btn message__action-btn message__options-btn">
<%= image_tag "menu-dots-horizontal.svg", size: 20, class: "colorize--black", aria: { hidden: "true" } %>
<span class="for-screen-reader">Message options</span>
</summary>

<div class="message__actions-menu border shadow" data-popup-target="menu">
<div class="quick-boosts">
<% boost_path = message_boosts_path(message) %>
<% boost_frame = dom_id(message, :boosting) %>
<% EmojiHelper::REACTIONS.each do |character, title| %>
<form data-turbo-frame="<%= boost_frame %>" data-action="popup#close" action="<%= boost_path %>" accept-charset="UTF-8" method="post">
<%= hidden_field_tag "boost[content]", character %>
<button name="button" type="submit" title="<%= title %>" class="btn message__action-btn" data-emoji="<%= character %>">
<figure class="margin-none boost-character"><%= character %></figure>
<span class="for-screen-reader"><%= title %></span>
</button>
</form>
<% 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" } %>
<span class="for-screen-reader">New boost</span>
<% end %>
</div>

<div class="flex flex-wrap border-top margin-block-start-half pad-block-start-half message__actions-grid">
<% 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 %>
</div>
</div>
<%# 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 %>
Comment on lines +12 to +13

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The menu does depend on the room page's controller and template, and those two views have neither. Neither of them puts the button within reach, though, on main or here:

  • Search results hide a message's actions altogether: .searches .message__actions { display: none !important; visibility: hidden !important; } in sidebar.css.
  • messages/show is the source of a message's edit frame: the redirect after an update and the editor's close link both load it inside the frame. Opened on its own, the message stays visibility: hidden, because a message only becomes visible once the messages or the search results controller formats it (.message--formatted), and that page has neither.

So no click gets to an empty <details> there. 10795f5 adds a system test that holds both pages to it: the message is there and its options button can't be reached. It fails if the search rule is removed, or if a message is made visible without being formatted, which is when those pages would need the template too.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

<% end %>
<% end %>
</div>
<% end %>
50 changes: 50 additions & 0 deletions app/views/messages/_actions_menu.html.erb
Original file line number Diff line number Diff line change
@@ -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. %>

<div class="message__actions-menu border shadow" data-popup-target="menu">
<div class="quick-boosts">
<% boost_path = message_boosts_path(message_id) %>
<% boost_frame = "boosting_message_#{message_id}" %>
<% EmojiHelper::REACTIONS.each do |character, title| %>
<form data-turbo-frame="<%= boost_frame %>" data-action="popup#close" action="<%= boost_path %>" accept-charset="UTF-8" method="post">
<%= hidden_field_tag "boost[content]", character %>
<button name="button" type="submit" title="<%= title %>" class="btn message__action-btn" data-emoji="<%= character %>">
<figure class="margin-none boost-character"><%= character %></figure>
<span class="for-screen-reader"><%= title %></span>
</button>
</form>
<% 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" } %>
<span class="for-screen-reader">New boost</span>
<% end %>
</div>

<div class="flex flex-wrap border-top margin-block-start-half pad-block-start-half message__actions-grid">
<% 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 %>
</div>
</div>
4 changes: 4 additions & 0 deletions app/views/messages/_actions_menu_template.html.erb
Original file line number Diff line number Diff line change
@@ -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. %>
<script type="text/template" data-messages-target="actionsMenuTemplate">
<%= render "messages/actions_menu", message_id: "$messageId$", room: room, attachment: nil %>
</script>
2 changes: 1 addition & 1 deletion app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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" } %>
</span>
</h3>
<%= render "messages/actions", message: message, url: room_at_message_url(message.room, message) %>
<%= render "messages/actions", message: message %>
</div>
<%= render "messages/presentation", message: message %>
<%= render "messages/boosts/boosts", message: message %>
Expand Down
1 change: 1 addition & 0 deletions app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
8 changes: 3 additions & 5 deletions test/controllers/fragment_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/controllers/messages/boosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
32 changes: 29 additions & 3 deletions test/controllers/messages_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" ],
Expand All @@ -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}"
Expand Down
26 changes: 26 additions & 0 deletions test/controllers/rooms_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Loading