Skip to content

A platform for community discussion. Free, open, simple.

FIX: client based chat message timestamp (#34592)

Joffrey JAFFEUXa96ace858e1c
diff --git a/plugins/chat/app/services/chat/create_message.rb b/plugins/chat/app/services/chat/create_message.rb
index 449e814d1a2..c2629d2c5c2 100644
--- a/plugins/chat/app/services/chat/create_message.rb
+++ b/plugins/chat/app/services/chat/create_message.rb
@@ -46,6 +46,7 @@ module Chat
attribute :upload_ids, :array
attribute :thread_id, :string
attribute :blocks, :array
+ attribute :client_created_at, :string
validates :chat_channel_id, presence: true
validates :message, presence: true, if: -> { upload_ids.blank? && blocks.blank? }
@@ -154,7 +155,7 @@ module Chat
end
def instantiate_message(channel:, guardian:, params:, uploads:, thread:, reply:, options:)
- channel.chat_messages.new(
+ message_attrs = {
user: guardian.user,
last_editor: guardian.user,
in_reply_to: reply,
@@ -165,7 +166,20 @@ module Chat
cooked_version: ::Chat::Message::BAKED_VERSION,
streaming: options.streaming,
blocks: params.blocks,
- )
+ }
+
+ if params.client_created_at.present?
+ begin
+ client_time = Time.zone.parse(params.client_created_at)
+ # Only accept client timestamps that are reasonable (within 1 minute of server time)
+ if client_time && (Time.zone.now - client_time).abs <= 60.seconds
+ message_attrs[:created_at] = client_time
+ end
+ rescue ArgumentError
+ end
+ end
+
+ channel.chat_messages.new(message_attrs)
end
def save_message(message_instance:)
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
index 20c6dadabe3..70f0daa3d5a 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-channel.gjs
@@ -565,6 +565,7 @@ export default class ChatChannel extends Component {
in_reply_to_id: message.inReplyTo?.id,
staged_id: message.id,
upload_ids: message.uploads.map((upload) => upload.id),
+ client_created_at: message.createdAt.toISOString(),
...extractCurrentTopicInfo(this),
});
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
index 063fdc851f3..97a7cf33516 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.gjs
@@ -454,6 +454,7 @@ export default class ChatThread extends Component {
staged_id: message.id,
upload_ids: message.uploads.map((upload) => upload.id),
thread_id: message.thread.id,
+ client_created_at: message.createdAt.toISOString(),
...extractCurrentTopicInfo(this),
}
);
diff --git a/plugins/chat/spec/services/chat/create_message_spec.rb b/plugins/chat/spec/services/chat/create_message_spec.rb
index b14571b04c4..152edb58afc 100644
--- a/plugins/chat/spec/services/chat/create_message_spec.rb
+++ b/plugins/chat/spec/services/chat/create_message_spec.rb
@@ -24,6 +24,13 @@ RSpec.describe Chat::CreateMessage do
it { is_expected.not_to validate_presence_of :message }
end
+
+ describe "client_created_at parameter" do
+ it "accepts client_created_at parameter" do
+ contract = described_class.new(client_created_at: 1.minute.ago.iso8601)
+ expect(contract.attributes.keys).to include("client_created_at")
+ end
+ end
end
describe ".call" do
@@ -506,6 +513,65 @@ RSpec.describe Chat::CreateMessage do
)
result
end
+
+ context "when client_created_at is provided" do
+ let(:client_timestamp) { 30.seconds.ago }
+
+ before do
+ params[:client_created_at] = client_timestamp.iso8601
+ params[:upload_ids] = [] # Remove uploads to avoid ID conflicts
+ end
+
+ it "uses the client timestamp for created_at" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(1.second).of(client_timestamp)
+ end
+
+ context "when client timestamp is too old" do
+ let(:client_timestamp) { 2.minutes.ago }
+
+ it "falls back to server timestamp" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(5.seconds).of(Time.zone.now)
+ end
+ end
+
+ context "when client timestamp is in the future" do
+ let(:client_timestamp) { 2.minutes.from_now }
+
+ it "falls back to server timestamp" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(5.seconds).of(Time.zone.now)
+ end
+ end
+
+ context "when client timestamp is invalid format" do
+ before { params[:client_created_at] = "invalid-timestamp" }
+
+ it "falls back to server timestamp" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(5.seconds).of(Time.zone.now)
+ end
+ end
+
+ context "when client timestamp is empty" do
+ before { params[:client_created_at] = "" }
+
+ it "falls back to server timestamp" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(5.seconds).of(Time.zone.now)
+ end
+ end
+ end
+
+ context "when client_created_at is not provided" do
+ before { params[:upload_ids] = [] } # Remove uploads to avoid ID conflicts
+
+ it "uses server timestamp" do
+ expect(result).to run_successfully
+ expect(message.created_at).to be_within(5.seconds).of(Time.zone.now)
+ end
+ end
end
end
end
diff --git a/plugins/chat/spec/system/chat_channel_spec.rb b/plugins/chat/spec/system/chat_channel_spec.rb
index 03410d6d34e..fed30f5d874 100644
--- a/plugins/chat/spec/system/chat_channel_spec.rb
+++ b/plugins/chat/spec/system/chat_channel_spec.rb
@@ -426,4 +426,46 @@ RSpec.describe "Chat channel", type: :system do
expect(page).to have_title("#🐕 Dogs - Chat - Discourse")
end
+
+ context "when messages are sent with client timestamps" do
+ it "orders messages by client timestamp rather than server timestamp" do
+ first_message =
+ Chat::CreateMessage.call(
+ params: {
+ chat_channel_id: channel_1.id,
+ message: "I was created first but should appear second",
+ client_created_at: 30.seconds.ago.iso8601,
+ },
+ guardian: current_user.guardian,
+ ).message_instance
+
+ second_message =
+ Chat::CreateMessage.call(
+ params: {
+ chat_channel_id: channel_1.id,
+ message: "I was created second but should appear first",
+ client_created_at: 45.seconds.ago.iso8601,
+ },
+ guardian: current_user.guardian,
+ ).message_instance
+
+ chat_page.visit_channel(channel_1)
+
+ expect(page).to have_selector(
+ ".chat-message-container[data-id='#{first_message.id}'] .chat-message-text",
+ text: "I was created first but should appear second",
+ )
+ expect(page).to have_selector(
+ ".chat-message-container[data-id='#{second_message.id}'] .chat-message-text",
+ text: "I was created second but should appear first",
+ )
+
+ messages = page.all(".chat-message-container[data-id]")
+ message_ids = messages.map { |msg| msg["data-id"].to_i }
+ second_message_index = message_ids.index(second_message.id)
+ first_message_index = message_ids.index(first_message.id)
+
+ expect(second_message_index).to be < first_message_index
+ end
+ end
end
diff --git a/plugins/chat/spec/system/single_thread_spec.rb b/plugins/chat/spec/system/single_thread_spec.rb
index d967cdd8e9e..8f98eefa5df 100644
--- a/plugins/chat/spec/system/single_thread_spec.rb
+++ b/plugins/chat/spec/system/single_thread_spec.rb
@@ -266,5 +266,49 @@ describe "Single thread in side panel", type: :system do
expect(page).to have_selector(".chat-thread .chat-message-separator__text", text: "Today")
end
end
+
+ context "when thread messages are sent with client timestamps" do
+ it "orders thread messages by client timestamp rather than server timestamp" do
+ first_message =
+ Chat::CreateMessage.call(
+ params: {
+ chat_channel_id: channel.id,
+ thread_id: thread.id,
+ message: "Thread message created first but should appear second",
+ client_created_at: 30.seconds.ago.iso8601,
+ },
+ guardian: current_user.guardian,
+ ).message_instance
+
+ second_message =
+ Chat::CreateMessage.call(
+ params: {
+ chat_channel_id: channel.id,
+ thread_id: thread.id,
+ message: "Thread message created second but should appear first",
+ client_created_at: 45.seconds.ago.iso8601,
+ },
+ guardian: current_user.guardian,
+ ).message_instance
+
+ chat_page.visit_thread(thread)
+
+ expect(page).to have_selector(
+ ".chat-thread .chat-message-container[data-id='#{first_message.id}'] .chat-message-text",
+ text: "Thread message created first but should appear second",
+ )
+ expect(page).to have_selector(
+ ".chat-thread .chat-message-container[data-id='#{second_message.id}'] .chat-message-text",
+ text: "Thread message created second but should appear first",
+ )
+
+ messages = page.all(".chat-thread .chat-message-container[data-id]")
+ message_ids = messages.map { |msg| msg["data-id"].to_i }
+ second_message_index = message_ids.index(second_message.id)
+ first_message_index = message_ids.index(first_message.id)
+
+ expect(second_message_index).to be < first_message_index
+ end
+ end
end
end