discourse/plugins/chat/app/queries/chat/channel_memberships_query.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

52 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Chat
class ChannelMembershipsQuery
def self.call(channel:, limit: 50, offset: 0, username: nil, count_only: false)
query =
Chat::UserChatChannelMembership
.joins(:user)
.includes(:user)
.where(user: User.human_users.activated.not_suspended.not_staged)
.where(chat_channel: channel)
query = query.where(following: true) if channel.category_channel?
return query.count if count_only
if channel.category_channel? && channel.read_restricted? && channel.allowed_group_ids
query =
query.where(
"user_id IN (SELECT user_id FROM group_users WHERE group_id IN (?))",
channel.allowed_group_ids,
)
end
if username.present?
if SiteSetting.prioritize_username_in_ux || !SiteSetting.enable_names
query = query.where("users.username_lower ILIKE ?", "%#{username}%")
else
query =
query.where(
"LOWER(users.name) ILIKE ? OR users.username_lower ILIKE ?",
"%#{username}%",
"%#{username}%",
)
end
end
if SiteSetting.prioritize_username_in_ux || !SiteSetting.enable_names
query = query.order("users.username_lower ASC")
else
query = query.order("users.name ASC, users.username_lower ASC")
end
query.offset(offset).limit(limit)
end
def self.count(channel)
call(channel: channel, count_only: true)
end
end
end