discourse/plugins/chat/app/serializers/chat/thread_preview_serializer.rb
Joffrey JAFFEUX ab832cc865
FEATURE: introduces group channels (#24288)
Group channels will allow users to create channels with a name and invite people. It's possible to add people even after creation of the channel. Removing users is not yet possible but will be added in the near future.

Technically a group channel is `direct_message_channel` with a group attribute set to true on its direct message (chatable). This model might evolve in the future but offers much flexibility for now without having to rely on a complex migration.

The commit essentially consists of:
- a migration to set existing direct message channels with more than 2 users to a group
- a new message creator which allows to search, add members, and create groups
- a new `AddUsersToChannel` service
- a modified `SearchChatable` service
2023-11-10 11:29:28 +01: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