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
7 changes: 6 additions & 1 deletion app/controllers/rooms/refreshes_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion app/javascript/controllers/refresh_room_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions app/views/rooms/refreshes/show.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
24 changes: 24 additions & 0 deletions test/controllers/rooms/refreshes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
65 changes: 65 additions & 0 deletions test/system/messages_removed_while_away_test.rb
Original file line number Diff line number Diff line change
@@ -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
Loading