A platform for community discussion. Free, open, simple.
FIX: auto fill was not happening on first load (#21809)
Joffrey JAFFEUX55ef2d069862
diff --git a/plugins/chat/app/queries/chat/messages_query.rb b/plugins/chat/app/queries/chat/messages_query.rbindex a5746037b68..aaf9ec11f79 100644--- a/plugins/chat/app/queries/chat/messages_query.rb+++ b/plugins/chat/app/queries/chat/messages_query.rb@@ -125,6 +125,8 @@ module Chatmessages,target_message_id = nil)+ page_size = [page_size || MAX_PAGE_SIZE, MAX_PAGE_SIZE].min+if target_message_id.present?condition = direction == PAST ? "<" : ">"messages = messages.where("id #{condition} ?", target_message_id.to_i)diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.js b/plugins/chat/assets/javascripts/discourse/components/chat-channel.jsindex 4fe4574f98a..967221fd89f 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.js+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.js@@ -94,7 +94,7 @@ export default class ChatLivePane extends Component {@actiondidResizePane() {- this.fillPaneAttempt();+ this.debounceFillPaneAttempt();this.computeDatesSeparators();this.forceRendering();}@@ -228,7 +228,7 @@ export default class ChatLivePane extends Component {this.loadedOnce = true;this.requestedTargetMessageId = null;this.loadingMorePast = false;- this.fillPaneAttempt();+ this.debounceFillPaneAttempt();this.updateLastReadMessage();this.subscribeToUpdates(this.args.channel);});@@ -276,12 +276,6 @@ export default class ChatLivePane extends Component {return;}- // prevents an edge case where user clicks bottom arrow- // just after scrolling to top- if (loadingPast && this.#isAtBottom()) {- return;- }-const [messages, meta] = this.afterFetchCallback(this.args.channel,result@@ -318,11 +312,23 @@ export default class ChatLivePane extends Component {.catch(this._handleErrors).finally(() => {this[loadingMoreKey] = false;- this.fillPaneAttempt();+ this.debounceFillPaneAttempt();});}- @debounce(500)+ debounceFillPaneAttempt() {+ if (!this.loadedOnce) {+ return;+ }++ this._debouncedFillPaneAttemptHandler = discourseDebounce(+ this,+ this.fillPaneAttempt,+ 500+ );+ }++ @bindfillPaneAttempt() {if (this._selfDeleted) {return;@@ -333,11 +339,12 @@ export default class ChatLivePane extends Component {return;}- if (!this.args.channel?.messagesManager?.canLoadMorePast) {+ if (!this.args.channel?.canLoadMorePast) {return;}- const firstMessage = this.args.channel?.messages?.[0];+ const firstMessage = this.args.channel?.messages?.firstObject;+if (!firstMessage?.visible) {return;}@@ -1055,6 +1062,7 @@ export default class ChatLivePane extends Component {}#cancelHandlers() {+ cancel(this._debouncedFillPaneAttemptHandler);cancel(this._onScrollEndedHandler);cancel(this._laterComputeHandler);cancel(this._debounceFetchMessagesHandler);diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-api.js b/plugins/chat/assets/javascripts/discourse/services/chat-api.jsindex 426c28357d7..86ac936a9aa 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-api.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-api.js@@ -24,14 +24,13 @@ export default class ChatApi extends Service {*/channel(channelId, data = {}) {const args = {};+ args.page_size = data.pageSize;if (data.targetMessageId) {args.target_message_id = data.targetMessageId;} else if (data.fetchFromLastRead) {args.fetch_from_last_read = true;} else {- args.page_size = data.pageSize;-if (data.direction) {args.direction = data.direction;}diff --git a/plugins/chat/spec/queries/chat/messages_query_spec.rb b/plugins/chat/spec/queries/chat/messages_query_spec.rbindex 57755f998f9..ee1a31328e0 100644--- a/plugins/chat/spec/queries/chat/messages_query_spec.rb+++ b/plugins/chat/spec/queries/chat/messages_query_spec.rb@@ -71,6 +71,11 @@ RSpec.describe Chat::MessagesQuery doendend+ it "limits results of paginated query when page_size is not set" do+ options[:target_message_id] = nil+ stub_const(described_class, "MAX_PAGE_SIZE", 1) { expect(subject[:messages].length).to eq(1) }+ end+describe "when some messages are in threads" dofab!(:thread) { Fabricate(:chat_thread, channel: channel) }diff --git a/plugins/chat/spec/system/chat_channel_spec.rb b/plugins/chat/spec/system/chat_channel_spec.rbindex 27c4ced9cb8..26d2354f328 100644--- a/plugins/chat/spec/system/chat_channel_spec.rb+++ b/plugins/chat/spec/system/chat_channel_spec.rb@@ -7,15 +7,41 @@ RSpec.describe "Chat channel", type: :system, js: true dolet(:chat) { PageObjects::Pages::Chat.new }let(:channel_page) { PageObjects::Pages::ChatChannel.new }+ let(:sidebar_page) { PageObjects::Pages::Sidebar.new }- before { chat_system_bootstrap }+ before do+ chat_system_bootstrap+ channel_1.add(current_user)+ sign_in(current_user)+ end- context "when sending a message" do+ context "when first batch of messages doesnt fill page" dobefore do- channel_1.add(current_user)- sign_in(current_user)+ 50.times do+ Fabricate(+ :chat_message,+ message: Faker::Lorem.characters(number: SiteSetting.chat_minimum_message_length),+ user: current_user,+ chat_channel: channel_1,+ )+ endend+ it "autofills for more messages" do+ chat.prefers_full_page+ visit("/")+ # cheap trick to ensure the messages don't fill the initial page+ page.execute_script(+ "document.head.insertAdjacentHTML('beforeend', `<style>.chat-message-text{font-size:3px;}</style>`)",+ )+ sidebar_page.open_channel(channel_1)++ expect(channel_page).to have_no_loading_skeleton+ expect(channel_page.messages).to have_x_messages(51)+ end+ end++ context "when sending a message" docontext "with lots of messages" dobefore { 50.times { Fabricate(:chat_message, chat_channel: channel_1) } }@@ -71,11 +97,7 @@ RSpec.describe "Chat channel", type: :system, js: true doendcontext "when clicking the arrow button" do- before do- channel_1.add(current_user)- 50.times { Fabricate(:chat_message, chat_channel: channel_1) }- sign_in(current_user)- end+ before { 50.times { Fabricate(:chat_message, chat_channel: channel_1) } }it "jumps to the bottom of the channel" dounloaded_message = Fabricate(:chat_message, chat_channel: channel_1)@@ -92,11 +114,6 @@ RSpec.describe "Chat channel", type: :system, js: true doendcontext "when returning to a channel where last read is not last message" do- before do- channel_1.add(current_user)- sign_in(current_user)- end-it "jumps to the bottom of the channel" dochannel_1.membership_for(current_user).update!(last_read_message: message_1)messages = 50.times.map { Fabricate(:chat_message, chat_channel: channel_1) }@@ -112,9 +129,7 @@ RSpec.describe "Chat channel", type: :system, js: true dobefore dochannel_1.add(other_user)- channel_1.add(current_user)50.times { Fabricate(:chat_message, chat_channel: channel_1) }- sign_in(current_user)endit "doesn’t scroll the pane" do@@ -142,11 +157,7 @@ RSpec.describe "Chat channel", type: :system, js: true do)end- before do- channel_1.add(other_user)- channel_1.add(current_user)- sign_in(current_user)- end+ before { channel_1.add(other_user) }it "highlights the mentions" dochat.visit_channel(channel_1)@@ -185,8 +196,6 @@ RSpec.describe "Chat channel", type: :system, js: true dobefore doFabricate(:chat_message, in_reply_to: message_1, user: other_user, chat_channel: channel_1)channel_1.add(other_user)- channel_1.add(current_user)- sign_in(current_user)endit "doesn’t show the reply-to line" do@@ -203,8 +212,6 @@ RSpec.describe "Chat channel", type: :system, js: true doFabricate(:chat_message, user: other_user, chat_channel: channel_1)Fabricate(:chat_message, in_reply_to: message_1, user: other_user, chat_channel: channel_1)channel_1.add(other_user)- channel_1.add(current_user)- sign_in(current_user)endit "shows the reply-to line" do@@ -229,8 +236,6 @@ RSpec.describe "Chat channel", type: :system, js: true doFabricate(:chat_message, user: other_user, chat_channel: channel_1)Fabricate(:chat_message, in_reply_to: message_2, user: current_user, chat_channel: channel_1)channel_1.add(other_user)- channel_1.add(current_user)- sign_in(current_user)endit "renders text in the reply-to" do@@ -241,12 +246,7 @@ RSpec.describe "Chat channel", type: :system, js: true doendcontext "when messages are separated by a day" do- before do- Fabricate(:chat_message, chat_channel: channel_1, created_at: 2.days.ago)-- channel_1.add(current_user)- sign_in(current_user)- end+ before { Fabricate(:chat_message, chat_channel: channel_1, created_at: 2.days.ago) }it "shows a date separator" dochat.visit_channel(channel_1)@@ -264,11 +264,6 @@ RSpec.describe "Chat channel", type: :system, js: true do\`\`\`MESSAGE- before do- channel_1.add(current_user)- sign_in(current_user)- end-it "adds the correct lang" dochat.visit_channel(channel_1)@@ -277,11 +272,7 @@ RSpec.describe "Chat channel", type: :system, js: true doendcontext "when scrolling" do- before do- channel_1.add(current_user)- 50.times { Fabricate(:chat_message, chat_channel: channel_1) }- sign_in(current_user)- end+ before { 50.times { Fabricate(:chat_message, chat_channel: channel_1) } }it "resets the active message" dochat.visit_channel(channel_1)diff --git a/plugins/chat/spec/system/page_objects/chat/components/messages.rb b/plugins/chat/spec/system/page_objects/chat/components/messages.rbindex 191a38b8cec..54c0082a465 100644--- a/plugins/chat/spec/system/page_objects/chat/components/messages.rb+++ b/plugins/chat/spec/system/page_objects/chat/components/messages.rb@@ -12,6 +12,10 @@ module PageObjects@context = contextend+ def component+ find(context)+ end+def has_message?(**args)PageObjects::Components::Chat::Message.new(".chat-channel").exists?(**args)end@@ -19,6 +23,10 @@ module PageObjectsdef has_no_message?(**args)!has_message?(**args)end++ def has_x_messages?(count)+ find(context).has_css?(SELECTOR, count: count, visible: :all)+ endendendend