discourse/plugins/chat/app/serializers/chat/thread_serializer.rb
Joffrey JAFFEUX 76953cc356
FEATURE: allows to force a thread (#25987)
Forcing a thread will work even in channel which don't have `threading_enabled` or in direct message channels.

For now this feature is only available through the `ChatSDK`:

```ruby
ChatSDK::Message.create(in_reply_to_id: 1, guardian: guardian, raw: "foo bar baz", channel_id: 2, force_thread: true)
```
2024-03-06 12:03:42 +01:00

82 lines
2.0 KiB
Ruby

# frozen_string_literal: true
module Chat
class ThreadSerializer < ApplicationSerializer
has_one :original_message, serializer: Chat::ThreadOriginalMessageSerializer, embed: :objects
attributes :id,
:title,
:status,
:channel_id,
:channel,
:meta,
:reply_count,
:current_user_membership,
:preview,
:last_message_id,
:force
def initialize(object, opts)
super(object, opts)
@opts = opts
# Avoids an N1 to re-load the thread in the serializer for original_message.
object&.original_message&.thread = object
@current_user_membership = opts[:membership]
end
def include_channel?
@options[:include_channel].presence || false
end
def channel
::Chat::ChannelSerializer.new(object.channel, scope: scope, root: false)
end
def include_original_message?
@opts[:include_thread_original_message].presence || true
end
def meta
{ message_bus_last_ids: { thread_message_bus_last_id: thread_message_bus_last_id } }
end
def reply_count
object.replies_count_cache || 0
end
def include_preview?
@opts[:include_thread_preview]
end
def preview
Chat::ThreadPreviewSerializer.new(
object,
scope: scope,
root: false,
participants: @opts[:participants],
).as_json
end
def include_current_user_membership?
@current_user_membership.present?
end
def current_user_membership
@current_user_membership.thread = object
Chat::BaseThreadMembershipSerializer.new(
@current_user_membership,
scope: scope,
root: false,
).as_json
end
private
def thread_message_bus_last_id
@opts[:thread_message_bus_last_id] ||
MessageBus.last_id(Chat::Publisher.thread_message_bus_channel(object.channel_id, object.id))
end
end
end