mirror of
https://github.com/discourse/discourse.git
synced 2025-06-02 01:58:05 +08:00
DEV: Move discourse-chat
to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
This commit is contained in:
@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Chat::Api::CategoryChatablesController < ApplicationController
|
||||
def permissions
|
||||
category = Category.find(params[:id])
|
||||
|
||||
if category.read_restricted?
|
||||
permissions =
|
||||
Group
|
||||
.joins(:category_groups)
|
||||
.where(category_groups: { category_id: category.id })
|
||||
.joins("LEFT OUTER JOIN group_users ON groups.id = group_users.group_id")
|
||||
.group("groups.id", "groups.name")
|
||||
.pluck("groups.name", "COUNT(group_users.user_id)")
|
||||
|
||||
group_names = permissions.map { |p| "@#{p[0]}" }
|
||||
members_count = permissions.sum { |p| p[1].to_i }
|
||||
|
||||
permissions_result = {
|
||||
allowed_groups: group_names,
|
||||
members_count: members_count,
|
||||
private: true,
|
||||
}
|
||||
else
|
||||
everyone_group = Group.find(Group::AUTO_GROUPS[:everyone])
|
||||
|
||||
permissions_result = { allowed_groups: ["@#{everyone_group.name}"], private: false }
|
||||
end
|
||||
|
||||
render json: permissions_result
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Chat::Api::ChatChannelMembershipsController < Chat::Api::ChatChannelsController
|
||||
def index
|
||||
channel = find_chat_channel
|
||||
|
||||
offset = (params[:offset] || 0).to_i
|
||||
limit = (params[:limit] || 50).to_i.clamp(1, 50)
|
||||
|
||||
memberships =
|
||||
ChatChannelMembershipsQuery.call(
|
||||
channel,
|
||||
offset: offset,
|
||||
limit: limit,
|
||||
username: params[:username],
|
||||
)
|
||||
|
||||
render_serialized(memberships, UserChatChannelMembershipSerializer, root: false)
|
||||
end
|
||||
end
|
@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
MEMBERSHIP_EDITABLE_PARAMS = %i[muted desktop_notification_level mobile_notification_level]
|
||||
|
||||
class Chat::Api::ChatChannelNotificationsSettingsController < Chat::Api::ChatChannelsController
|
||||
def update
|
||||
settings_params = params.permit(MEMBERSHIP_EDITABLE_PARAMS)
|
||||
membership = find_membership
|
||||
membership.update!(settings_params.to_h)
|
||||
render_serialized(membership, UserChatChannelMembershipSerializer, root: false)
|
||||
end
|
||||
end
|
92
plugins/chat/app/controllers/api/chat_channels_controller.rb
Normal file
92
plugins/chat/app/controllers/api/chat_channels_controller.rb
Normal file
@ -0,0 +1,92 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
CHAT_CHANNEL_EDITABLE_PARAMS = %i[name description]
|
||||
CATEGORY_CHAT_CHANNEL_EDITABLE_PARAMS = %i[auto_join_users]
|
||||
|
||||
class Chat::Api::ChatChannelsController < Chat::Api
|
||||
def index
|
||||
options = { status: params[:status] ? ChatChannel.statuses[params[:status]] : nil }.merge(
|
||||
params.permit(:filter, :limit, :offset),
|
||||
).symbolize_keys!
|
||||
|
||||
memberships = Chat::ChatChannelMembershipManager.all_for_user(current_user)
|
||||
channels = Chat::ChatChannelFetcher.secured_public_channels(guardian, memberships, options)
|
||||
|
||||
serialized_channels =
|
||||
channels.map do |channel|
|
||||
ChatChannelSerializer.new(
|
||||
channel,
|
||||
scope: Guardian.new(current_user),
|
||||
membership: memberships.find { |membership| membership.chat_channel_id == channel.id },
|
||||
)
|
||||
end
|
||||
render json: serialized_channels, root: false
|
||||
end
|
||||
|
||||
def update
|
||||
guardian.ensure_can_edit_chat_channel!
|
||||
|
||||
chat_channel = find_chat_channel
|
||||
|
||||
if chat_channel.direct_message_channel?
|
||||
raise Discourse::InvalidParameters.new(
|
||||
I18n.t("chat.errors.cant_update_direct_message_channel"),
|
||||
)
|
||||
end
|
||||
|
||||
params_to_edit = editable_params(params, chat_channel)
|
||||
params_to_edit.each { |k, v| params_to_edit[k] = nil if params_to_edit[k].blank? }
|
||||
|
||||
if ActiveRecord::Type::Boolean.new.deserialize(params_to_edit[:auto_join_users])
|
||||
auto_join_limiter(chat_channel).performed!
|
||||
end
|
||||
|
||||
chat_channel.update!(params_to_edit)
|
||||
|
||||
ChatPublisher.publish_chat_channel_edit(chat_channel, current_user)
|
||||
|
||||
if chat_channel.category_channel? && chat_channel.auto_join_users
|
||||
Chat::ChatChannelMembershipManager.new(chat_channel).enforce_automatic_channel_memberships
|
||||
end
|
||||
|
||||
render_serialized(
|
||||
chat_channel,
|
||||
ChatChannelSerializer,
|
||||
root: false,
|
||||
membership: chat_channel.membership_for(current_user),
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_chat_channel
|
||||
chat_channel = ChatChannel.find(params.require(:chat_channel_id))
|
||||
guardian.ensure_can_see_chat_channel!(chat_channel)
|
||||
chat_channel
|
||||
end
|
||||
|
||||
def find_membership
|
||||
chat_channel = find_chat_channel
|
||||
membership = Chat::ChatChannelMembershipManager.new(chat_channel).find_for_user(current_user)
|
||||
raise Discourse::NotFound if membership.blank?
|
||||
membership
|
||||
end
|
||||
|
||||
def auto_join_limiter(chat_channel)
|
||||
RateLimiter.new(
|
||||
current_user,
|
||||
"auto_join_users_channel_#{chat_channel.id}",
|
||||
1,
|
||||
3.minutes,
|
||||
apply_limit_to_staff: true,
|
||||
)
|
||||
end
|
||||
|
||||
def editable_params(params, chat_channel)
|
||||
permitted_params = CHAT_CHANNEL_EDITABLE_PARAMS
|
||||
|
||||
permitted_params += CATEGORY_CHAT_CHANNEL_EDITABLE_PARAMS if chat_channel.category_channel?
|
||||
|
||||
params.permit(*permitted_params)
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user