diff --git a/app/javascript/controllers/messages_controller.js b/app/javascript/controllers/messages_controller.js index 905e42686..643e7e205 100644 --- a/app/javascript/controllers/messages_controller.js +++ b/app/javascript/controllers/messages_controller.js @@ -66,10 +66,13 @@ export default class extends Controller { if (upToDate) { event.detail.render = async (streamElement) => { const didScroll = await this.#scrollManager.autoscroll(false, async () => { + const messageIds = this.#messageIdsIn(streamElement) + await render(streamElement) await nextEventLoopTick() this.#positionLastMessage() + if (this.#paginator.upToDate) this.#positionMessages(messageIds) this.#playSoundForLastMessage() this.#paginator.trimExcessMessages(true) }) @@ -183,6 +186,23 @@ export default class extends Controller { } } + #messageIdsIn(streamElement) { + return Array.from(streamElement.templateContent.querySelectorAll(".message"), (message) => message.id).filter(Boolean) + } + + // The refresh after a dropped connection brings what was missed and, again, what came in meanwhile, which Turbo moves + // to the end with the rest: a message that arrived live since would stay above them all. Only while the list is at + // its end, as one that's catching up pages on from its last message. + #positionMessages(ids) { + for (const id of ids) { + const message = document.getElementById(id) + + if (message?.parentElement === this.messagesTarget) { + this.#followingMessage(message)?.before(message) + } + } + } + #playSoundForLastMessage() { const soundTarget = this.#lastMessage.querySelector(".sound") diff --git a/app/javascript/controllers/refresh_room_controller.js b/app/javascript/controllers/refresh_room_controller.js index 3c177b1e6..84123392f 100644 --- a/app/javascript/controllers/refresh_room_controller.js +++ b/app/javascript/controllers/refresh_room_controller.js @@ -11,6 +11,7 @@ export default class extends Controller { static values = { loadedAt: Number, url: String, } #lastLoadedAt + #connected = false #offlineTimer = null #hiddenAt = null @@ -30,8 +31,12 @@ export default class extends Controller { this.channel?.unsubscribe() } + // Only a message that arrives while connected moves where the next refresh starts. One that arrives while + // disconnected, like the answer to a message sent then, can be newer than others missed meanwhile. messageTargetConnected(target) { - this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0) + if (this.#connected) { + this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0) + } } visibilityChanged() { @@ -53,6 +58,7 @@ export default class extends Controller { } #channelConnected() { + this.#connected = true this.#refresh("connection") clearTimeout(this.#offlineTimer) @@ -63,6 +69,8 @@ export default class extends Controller { this.#offlineTimer = setTimeout(() => { this.dispatch("offline", { target: window }) }, OFFLINE_AFTER_DISCONNECTED_TIMEOUT) + + this.#connected = false } #refresh(reason) { diff --git a/test/system/catching_up_after_a_dropped_connection_test.rb b/test/system/catching_up_after_a_dropped_connection_test.rb new file mode 100644 index 000000000..0c8320771 --- /dev/null +++ b/test/system/catching_up_after_a_dropped_connection_test.rb @@ -0,0 +1,96 @@ +require "application_system_test_case" + +class CatchingUpAfterADroppedConnectionTest < ApplicationSystemTestCase + setup do + sign_in "jz@37signals.com" + join_room rooms(:designers) + + using_session("Kevin") do + sign_in "kevin@37signals.com" + join_room rooms(:designers) + end + end + + test "a message sent while the connection is down doesn't hide the ones missed before it" do + drop_connection + using_session("Kevin") { send_message "Missed while away" } + assert_no_message_text "Missed while away" + + send_message "Sent while away" + restore_connection + + assert_message_text "Missed while away", wait: 10 + assert_equal [ "Missed while away", "Sent while away" ], texts_from_while_away + end + + test "messages missed or sent while the connection is down stay before one that arrives live as it comes back" do + drop_connection + using_session("Kevin") { send_message "Missed while away" } + send_message "Sent while away" + send_message "Sent again while away" + mark_message "Sent again while away" + + # As if it arrived live once the connection was back, between the refresh's query and its answer. + execute_script <<~JS, dom_id(rooms(:designers), :messages), 1.minute.from_now.to_fs(:epoch) + document.getElementById(arguments[0]).insertAdjacentHTML("beforeend", + `
`) + JS + restore_connection + + # The refresh brings the newest one again whatever happens, so its mark goes once the refresh is in. + assert_no_selector ".message[data-marked]", wait: 10 + assert_equal [ "Missed while away", "Sent while away", "Sent again while away", "Live while away" ], texts_from_while_away + end + + test "messages missed while the connection is down show once it's back, and the ones shown before stay as they are" do + using_session("Kevin") do + send_message "Seen live" + # The refresh asks from the newest message's millisecond, so that one comes again whatever happens. + send_message "Seen live, the newest" + end + mark_message "Seen live" + + drop_connection + using_session("Kevin") { send_message "Missed while away" } + assert_no_message_text "Missed while away" + + restore_connection + + assert_message_text "Missed while away", wait: 10 + assert_equal [ "Missed while away" ], texts_from_while_away + assert_selector ".message[data-marked]", text: "Seen live", count: 1 + end + + private + # The server drops the connection, as it does when the user signs out elsewhere, and Chrome keeps it from coming + # back until restore_connection, while the rest of the page still reaches the server. + def drop_connection + page.driver.browser.execute_cdp "Network.enable" + page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: [ "*/cable*" ] + users(:jz).reset_remote_connections + assert_no_selector "turbo-cable-stream-source[connected]", visible: false + end + + def restore_connection + page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: [] + # Action Cable tries again on its own schedule, several seconds apart. + assert_selector "turbo-cable-stream-source[connected]", count: 3, visible: false, wait: 30 + end + + # A refresh that brings the message again replaces its element, and the mark with it. + def mark_message(text) + assert_message_text text + execute_script <<~JS, text + Array.from(document.querySelectorAll(".message")).find((message) => message.textContent.includes(arguments[0])).dataset.marked = "true" + JS + assert_selector ".message[data-marked]", text: text + end + + def texts_from_while_away + all(".message .lexxy-content", text: "while away").map(&:text) + end + + def assert_no_message_text(text) + assert_no_selector ".message__body", text: text + end +end