discourse/plugins/chat/app/serializers/chat/thread_preview_serializer.rb
Martin Brennan 72bbc2fa8a
FIX: created_at datetime format inconsistencies in chat (#22611)
This commit attempts to fix an issue where we are ending
up with bad created_at date formats for last messages, which
is breaking the DM sort order and sometimes causing DM channels
to fall off the list, or show "Invalid date" on mobile.

I have not been able to consistently reproduce these issues
locally, however the serialzier for the channels index uses
MultiJSON.dump() and the Chat::Publisher uses .to_json, both of
which format created_at differently for messages.

The former is `2023-07-05T06:53:25.977Z` (iso8601).

The latter is `2023-07-14 03:59:22 UTC` (.to_s default).

Since we are doing comparison and sorting of these dates on the UI
we need consistent formatting for the JS Date parsers (and moment)
to deal with.

If the issue still occurs after this we can investigate further.
2023-07-14 16:05:01 +10:00

59 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Chat
class ThreadPreviewSerializer < ApplicationSerializer
attributes :last_reply_created_at,
:last_reply_excerpt,
:last_reply_id,
:participant_count,
:reply_count
has_many :participant_users, serializer: BasicUserSerializer, embed: :objects
has_one :last_reply_user, serializer: BasicUserSerializer, embed: :objects
def initialize(object, opts)
super(object, opts)
@participants = opts[:participants]
end
def reply_count
object.replies_count_cache || 0
end
def last_reply_created_at
object.last_message.created_at.iso8601
end
def last_reply_id
object.last_message.id
end
def last_reply_excerpt
object.last_message.excerpt(max_length: Chat::Thread::EXCERPT_LENGTH)
end
def last_reply_user
object.last_message.user
end
def include_participant_data?
@participants.present?
end
def include_participant_users?
include_participant_data?
end
def include_participant_count?
include_participant_data?
end
def participant_users
@participant_users ||= @participants[:users].map { |user| User.new(user) }
end
def participant_count
@participants[:total_count]
end
end
end