From 8865a692be06e3b158d072675df7f7c7dbfff76c Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Sun, 25 Oct 2026 02:07:05 +0200 Subject: [PATCH] Remove messages deleted while a room page was disconnected A page that reconnects asks for what changed since its last load, and gets the messages created and updated since then. One deleted meanwhile, by its author, an administrator or a ban, no longer exists, so the page kept showing it until it was reloaded. The refresh now sends the ids of the messages the page shows, and removes those that are no longer in the room. Fixes #404. Co-Authored-By: Claude Opus 5.5 --- app/controllers/rooms/refreshes_controller.rb | 7 +- .../controllers/refresh_room_controller.js | 8 ++- .../rooms/refreshes/show.turbo_stream.erb | 4 ++ .../rooms/refreshes_controller_test.rb | 24 +++++++ .../messages_removed_while_away_test.rb | 65 +++++++++++++++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 test/system/messages_removed_while_away_test.rb diff --git a/app/controllers/rooms/refreshes_controller.rb b/app/controllers/rooms/refreshes_controller.rb index 2c2171ee9..ec2fa3eab 100644 --- a/app/controllers/rooms/refreshes_controller.rb +++ b/app/controllers/rooms/refreshes_controller.rb @@ -1,9 +1,10 @@ class Rooms::RefreshesController < ApplicationController include RoomScoped - before_action :set_last_updated_at + before_action :set_last_updated_at, :set_shown_message_ids def show + @deleted_message_ids = @shown_message_ids - @room.messages.where(id: @shown_message_ids).ids @new_messages = @room.messages.with_presentation.page_created_since(@last_updated_at) @updated_messages = @room.messages.without(@new_messages).with_presentation.page_updated_since(@last_updated_at) end @@ -12,4 +13,8 @@ def show def set_last_updated_at @last_updated_at = Time.at(0, params[:since].to_i, :millisecond) end + + def set_shown_message_ids + @shown_message_ids = params[:shown].to_s.split(",").map(&:to_i) + end end diff --git a/app/javascript/controllers/refresh_room_controller.js b/app/javascript/controllers/refresh_room_controller.js index 3c177b1e6..bc4ffbcfc 100644 --- a/app/javascript/controllers/refresh_room_controller.js +++ b/app/javascript/controllers/refresh_room_controller.js @@ -66,7 +66,13 @@ export default class extends Controller { } #refresh(reason) { - get(this.urlValue, { query: { since: this.#lastLoadedAt, reason: reason }, responseKind: "turbo-stream" }) + get(this.urlValue, { query: { since: this.#lastLoadedAt, shown: this.#shownMessageIds, reason: reason }, responseKind: "turbo-stream" }) + } + + // Messages deleted meanwhile are gone from the room, so the refresh can't send them: it's told which ones the page + // shows, and removes those that are gone. + get #shownMessageIds() { + return this.messageTargets.map((message) => message.dataset.messageId).join(",") } #hiddenForTooLong() { diff --git a/app/views/rooms/refreshes/show.turbo_stream.erb b/app/views/rooms/refreshes/show.turbo_stream.erb index d2d485f1e..b3dc8a7d0 100644 --- a/app/views/rooms/refreshes/show.turbo_stream.erb +++ b/app/views/rooms/refreshes/show.turbo_stream.erb @@ -5,3 +5,7 @@ <% @updated_messages.each do |message| %> <%= turbo_stream.replace dom_id(message), partial: "messages/message", locals: { message: message } %> <% end %> + +<% @deleted_message_ids.each do |id| %> + <%= turbo_stream.remove Message.new(id: id) %> +<% end %> diff --git a/test/controllers/rooms/refreshes_controller_test.rb b/test/controllers/rooms/refreshes_controller_test.rb index 392d03dfa..ddd2ffa58 100644 --- a/test/controllers/rooms/refreshes_controller_test.rb +++ b/test/controllers/rooms/refreshes_controller_test.rb @@ -5,6 +5,30 @@ class Rooms::RefreshesControllerTest < ActionDispatch::IntegrationTest sign_in :david end + test "refresh removes the messages shown that are gone" do + kept = rooms(:hq).messages.create!(creator: users(:jason), body: "Kept", client_message_id: "kept") + deleted = rooms(:hq).messages.create!(creator: users(:jason), body: "Deleted", client_message_id: "deleted") + elsewhere = rooms(:pets).messages.create!(creator: users(:jason), body: "Elsewhere", client_message_id: "elsewhere") + deleted.destroy + + get room_refresh_url(rooms(:hq), format: :turbo_stream), + params: { since: Time.current.to_fs(:epoch), shown: [ kept.id, deleted.id, elsewhere.id ].join(",") } + + assert_response :success + assert_select "turbo-stream[action='remove']", count: 2 + assert_select "turbo-stream[action='remove'][target='#{dom_id(deleted)}']" + assert_select "turbo-stream[action='remove'][target='#{dom_id(elsewhere)}']" + end + + test "refresh removes nothing when the page doesn't say what it shows" do + rooms(:hq).messages.create!(creator: users(:jason), body: "Deleted", client_message_id: "deleted").destroy + + get room_refresh_url(rooms(:hq), format: :turbo_stream), params: { since: Time.current.to_fs(:epoch) } + + assert_response :success + assert_select "turbo-stream[action='remove']", count: 0 + end + test "refresh includes new messages since the last known" do travel_to 1.day.ago do @old_message = rooms(:hq).messages.create!(creator: users(:jason), body: "Old message", client_message_id: "old") diff --git a/test/system/messages_removed_while_away_test.rb b/test/system/messages_removed_while_away_test.rb new file mode 100644 index 000000000..3b1480a14 --- /dev/null +++ b/test/system/messages_removed_while_away_test.rb @@ -0,0 +1,65 @@ +require "application_system_test_case" + +class MessagesRemovedWhileAwayTest < ApplicationSystemTestCase + setup do + @room = rooms(:designers) + @kept = @room.messages.create!(body: "Still here", creator: users(:david)) + @edited = @room.messages.create!(body: "To be edited", creator: users(:david)) + + sign_in "jz@37signals.com" + end + + test "a message deleted while the page is disconnected is gone once it reconnects" do + deleted = @room.messages.create!(body: "To be deleted", creator: users(:kevin)) + join_room @room + assert_message_text "To be deleted" + + while_disconnected do + # What MessagesController#destroy does. + deleted.destroy + deleted.broadcast_remove + end + + assert_no_message_text "To be deleted" + assert_message_text "Still here" + end + + test "the messages of someone banned while the page is disconnected are gone once it reconnects" do + @room.messages.create!(body: "Spam one", creator: users(:kevin)) + @room.messages.create!(body: "Spam two", creator: users(:kevin)) + join_room @room + assert_message_text "Spam two" + + while_disconnected do + # What RemoveBannedContentJob does after a ban. + users(:kevin).remove_banned_content + end + + assert_no_message_text "Spam one" + assert_no_message_text "Spam two" + assert_message_text "Still here" + end + + private + # Takes the page's cable connection down and keeps it down, as a sleeping laptop or a dropped network would, then + # brings it back. A message edited meanwhile comes back with the refresh after reconnecting, so once it shows the + # refresh has been answered. + def while_disconnected + page.driver.browser.execute_cdp("Network.enable") + page.driver.browser.execute_cdp("Network.setBlockedURLs", urls: [ "*/cable*" ]) + ActionCable.server.remote_connections.where(current_user: users(:jz)).disconnect(reconnect: true) + assert_no_selector "turbo-cable-stream-source[connected]", visible: false, wait: 10 + + yield + @edited.update!(body: "Edited while away") + + page.driver.browser.execute_cdp("Network.setBlockedURLs", urls: []) + assert_message_text "Edited while away", wait: 60 + ensure + page.driver.browser.execute_cdp("Network.setBlockedURLs", urls: []) + end + + def assert_no_message_text(text) + assert_no_selector ".message__body", text: text + end +end