2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-30 16:10:36 -06:00
|
|
|
class GroupsController < ApplicationController
|
2018-01-31 22:17:59 -06:00
|
|
|
requires_login only: [
|
2016-11-29 02:25:02 -06:00
|
|
|
:set_notifications,
|
|
|
|
:mentionable,
|
2017-08-28 11:32:08 -05:00
|
|
|
:messageable,
|
2018-07-31 22:08:45 -05:00
|
|
|
:check_name,
|
2016-12-11 09:36:15 -06:00
|
|
|
:update,
|
2017-06-13 03:10:14 -05:00
|
|
|
:histories,
|
2017-07-21 01:12:24 -05:00
|
|
|
:request_membership,
|
2018-03-27 03:45:21 -05:00
|
|
|
:search,
|
|
|
|
:new
|
2016-11-29 02:25:02 -06:00
|
|
|
]
|
|
|
|
|
2017-08-30 23:06:56 -05:00
|
|
|
skip_before_action :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
|
2018-01-31 14:04:09 -06:00
|
|
|
skip_before_action :check_xhr, only: [:show]
|
2020-05-11 00:05:42 -05:00
|
|
|
after_action :add_noindex_header
|
2015-12-14 16:17:09 -06:00
|
|
|
|
2018-03-20 02:50:46 -05:00
|
|
|
TYPE_FILTERS = {
|
2018-03-21 03:32:08 -05:00
|
|
|
my: Proc.new { |groups, user|
|
|
|
|
raise Discourse::NotFound unless user
|
|
|
|
Group.member_of(groups, user)
|
2018-03-20 02:50:46 -05:00
|
|
|
},
|
2018-03-21 03:32:08 -05:00
|
|
|
owner: Proc.new { |groups, user|
|
|
|
|
raise Discourse::NotFound unless user
|
|
|
|
Group.owner_of(groups, user)
|
2018-03-20 02:50:46 -05:00
|
|
|
},
|
|
|
|
public: Proc.new { |groups|
|
|
|
|
groups.where(public_admission: true, automatic: false)
|
|
|
|
},
|
|
|
|
close: Proc.new { |groups|
|
2020-01-15 04:21:58 -06:00
|
|
|
groups.where(public_admission: false, automatic: false)
|
2018-03-20 02:50:46 -05:00
|
|
|
},
|
|
|
|
automatic: Proc.new { |groups|
|
|
|
|
groups.where(automatic: true)
|
2020-10-20 17:46:45 -05:00
|
|
|
},
|
|
|
|
non_automatic: Proc.new { |groups|
|
|
|
|
groups.where(automatic: false)
|
2018-03-20 02:50:46 -05:00
|
|
|
}
|
|
|
|
}
|
2020-08-24 17:55:21 -05:00
|
|
|
ADD_MEMBERS_LIMIT = 1000
|
2018-03-20 02:50:46 -05:00
|
|
|
|
2016-12-14 03:26:16 -06:00
|
|
|
def index
|
2018-04-09 20:22:01 -05:00
|
|
|
unless SiteSetting.enable_group_directory? || current_user&.staff?
|
2016-12-22 00:14:03 -06:00
|
|
|
raise Discourse::InvalidAccess.new(:enable_group_directory)
|
|
|
|
end
|
|
|
|
|
2018-03-19 03:14:50 -05:00
|
|
|
order = %w{name user_count}.delete(params[:order])
|
2020-01-15 04:21:58 -06:00
|
|
|
dir = params[:asc].to_s == "true" ? "ASC" : "DESC"
|
|
|
|
sort = order ? "#{order} #{dir}" : nil
|
|
|
|
groups = Group.visible_groups(current_user, sort)
|
2018-03-20 02:50:46 -05:00
|
|
|
type_filters = TYPE_FILTERS.keys
|
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
if (username = params[:username]).present?
|
|
|
|
raise Discourse::NotFound unless user = User.find_by_username(username)
|
|
|
|
groups = TYPE_FILTERS[:my].call(groups.members_visible_groups(current_user, sort), user)
|
2018-03-21 03:32:08 -05:00
|
|
|
type_filters = type_filters - [:my, :owner]
|
|
|
|
end
|
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
if (filter = params[:filter]).present?
|
|
|
|
groups = Group.search_groups(filter, groups: groups)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !guardian.is_staff?
|
2017-07-03 14:26:46 -05:00
|
|
|
# hide automatic groups from all non stuff to de-clutter page
|
2018-03-21 03:32:08 -05:00
|
|
|
groups = groups.where("automatic IS FALSE OR groups.id = #{Group::AUTO_GROUPS[:moderators]}")
|
2018-03-20 02:50:46 -05:00
|
|
|
type_filters.delete(:automatic)
|
2017-07-03 14:26:46 -05:00
|
|
|
end
|
|
|
|
|
2017-08-08 08:45:27 -05:00
|
|
|
if Group.preloaded_custom_field_names.present?
|
|
|
|
Group.preload_custom_fields(groups, Group.preloaded_custom_field_names)
|
|
|
|
end
|
|
|
|
|
2018-03-20 02:50:46 -05:00
|
|
|
if type = params[:type]&.to_sym
|
2020-01-15 04:21:58 -06:00
|
|
|
raise Discourse::InvalidParameters.new(:type) unless callback = TYPE_FILTERS[type]
|
2018-06-28 19:43:33 -05:00
|
|
|
groups = callback.call(groups, current_user)
|
2018-03-20 02:50:46 -05:00
|
|
|
end
|
|
|
|
|
2018-03-19 05:28:57 -05:00
|
|
|
if current_user
|
|
|
|
group_users = GroupUser.where(group: groups, user: current_user)
|
|
|
|
user_group_ids = group_users.pluck(:group_id)
|
|
|
|
owner_group_ids = group_users.where(owner: true).pluck(:group_id)
|
2018-03-21 03:32:08 -05:00
|
|
|
else
|
|
|
|
type_filters = type_filters - [:my, :owner]
|
2018-03-19 05:28:57 -05:00
|
|
|
end
|
2016-12-21 06:58:51 -06:00
|
|
|
|
2020-10-20 17:46:45 -05:00
|
|
|
type_filters.delete(:non_automatic)
|
|
|
|
|
2020-01-16 16:57:34 -06:00
|
|
|
# count the total before doing pagination
|
|
|
|
total = groups.count
|
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
page = params[:page].to_i
|
|
|
|
page_size = MobileDetection.mobile_device?(request.user_agent) ? 15 : 36
|
2018-03-20 20:25:42 -05:00
|
|
|
groups = groups.offset(page * page_size).limit(page_size)
|
|
|
|
|
2016-12-21 06:58:51 -06:00
|
|
|
render_json_dump(
|
2018-03-19 05:28:57 -05:00
|
|
|
groups: serialize_data(groups,
|
|
|
|
BasicGroupSerializer,
|
|
|
|
user_group_ids: user_group_ids || [],
|
|
|
|
owner_group_ids: owner_group_ids || []
|
|
|
|
),
|
2018-03-20 02:50:46 -05:00
|
|
|
extras: {
|
2018-03-21 03:32:08 -05:00
|
|
|
type_filters: type_filters
|
2018-03-20 02:50:46 -05:00
|
|
|
},
|
2020-01-16 16:57:34 -06:00
|
|
|
total_rows_groups: total,
|
2018-08-20 04:08:50 -05:00
|
|
|
load_more_groups: groups_path(
|
|
|
|
page: page + 1,
|
|
|
|
type: type,
|
|
|
|
order: order,
|
|
|
|
asc: params[:asc],
|
|
|
|
filter: filter
|
2020-01-16 16:57:34 -06:00
|
|
|
)
|
2016-12-21 06:58:51 -06:00
|
|
|
)
|
2016-12-14 03:26:16 -06:00
|
|
|
end
|
|
|
|
|
2014-01-30 16:10:36 -06:00
|
|
|
def show
|
2018-01-31 14:04:09 -06:00
|
|
|
respond_to do |format|
|
|
|
|
group = find_group(:id)
|
|
|
|
|
|
|
|
format.html do
|
|
|
|
@title = group.full_name.present? ? group.full_name.capitalize : group.name
|
|
|
|
@description_meta = group.bio_cooked.present? ? PrettyText.excerpt(group.bio_cooked, 300) : @title
|
|
|
|
render :show
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
2018-03-29 01:57:10 -05:00
|
|
|
groups = Group.visible_groups(current_user)
|
2020-09-10 13:36:40 -05:00
|
|
|
if !guardian.is_staff?
|
|
|
|
groups = groups.where("automatic IS FALSE OR groups.id = #{Group::AUTO_GROUPS[:moderators]}")
|
|
|
|
end
|
2018-03-29 01:57:10 -05:00
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
group: serialize_data(group, GroupShowSerializer, root: nil),
|
|
|
|
extras: {
|
|
|
|
visible_group_names: groups.pluck(:name)
|
|
|
|
}
|
|
|
|
)
|
2018-01-31 14:04:09 -06:00
|
|
|
end
|
|
|
|
end
|
2014-01-30 16:10:36 -06:00
|
|
|
end
|
|
|
|
|
2018-03-27 03:45:21 -05:00
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
2016-12-13 01:15:20 -06:00
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
2016-11-29 02:25:02 -06:00
|
|
|
def update
|
|
|
|
group = Group.find(params[:id])
|
2020-08-19 09:41:40 -05:00
|
|
|
guardian.ensure_can_edit!(group) unless guardian.can_admin_group?(group)
|
2016-11-29 02:25:02 -06:00
|
|
|
|
2018-04-05 00:53:00 -05:00
|
|
|
if group.update(group_params(automatic: group.automatic))
|
2020-08-19 09:41:40 -05:00
|
|
|
GroupActionLogger.new(current_user, group, skip_guardian: true).log_change_group_settings
|
|
|
|
|
|
|
|
if guardian.can_see?(group)
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
# They can no longer see the group after changing permissions
|
|
|
|
render json: { route_to: '/g' }
|
|
|
|
end
|
2016-11-29 02:25:02 -06:00
|
|
|
else
|
|
|
|
render_json_error(group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-07 09:44:03 -06:00
|
|
|
def posts
|
2014-02-18 15:43:02 -06:00
|
|
|
group = find_group(:group_id)
|
2019-08-14 08:30:04 -05:00
|
|
|
guardian.ensure_can_see_group_members!(group)
|
|
|
|
|
2017-10-31 15:47:47 -05:00
|
|
|
posts = group.posts_for(
|
|
|
|
guardian,
|
|
|
|
params.permit(:before_post_id, :category_id)
|
|
|
|
).limit(20)
|
2014-02-07 09:44:03 -06:00
|
|
|
render_serialized posts.to_a, GroupPostSerializer
|
|
|
|
end
|
|
|
|
|
2016-03-18 11:19:45 -05:00
|
|
|
def posts_feed
|
|
|
|
group = find_group(:group_id)
|
2019-08-14 08:30:04 -05:00
|
|
|
guardian.ensure_can_see_group_members!(group)
|
|
|
|
|
2017-10-31 15:47:47 -05:00
|
|
|
@posts = group.posts_for(
|
|
|
|
guardian,
|
|
|
|
params.permit(:before_post_id, :category_id)
|
|
|
|
).limit(50)
|
2016-03-18 11:19:45 -05:00
|
|
|
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_posts", group_name: group.name)}"
|
|
|
|
@link = Discourse.base_url
|
|
|
|
@description = I18n.t("rss_description.group_posts", group_name: group.name)
|
|
|
|
render 'posts/latest', formats: [:rss]
|
|
|
|
end
|
|
|
|
|
2015-11-30 23:52:43 -06:00
|
|
|
def mentions
|
2017-12-07 15:16:53 -06:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_mentions?
|
2015-11-30 23:52:43 -06:00
|
|
|
group = find_group(:group_id)
|
2017-10-31 15:47:47 -05:00
|
|
|
posts = group.mentioned_posts_for(
|
|
|
|
guardian,
|
|
|
|
params.permit(:before_post_id, :category_id)
|
|
|
|
).limit(20)
|
2015-11-30 23:52:43 -06:00
|
|
|
render_serialized posts.to_a, GroupPostSerializer
|
|
|
|
end
|
|
|
|
|
2016-03-18 11:19:45 -05:00
|
|
|
def mentions_feed
|
2017-12-07 15:16:53 -06:00
|
|
|
raise Discourse::NotFound unless SiteSetting.enable_mentions?
|
2016-03-18 11:19:45 -05:00
|
|
|
group = find_group(:group_id)
|
2017-10-31 15:47:47 -05:00
|
|
|
@posts = group.mentioned_posts_for(
|
|
|
|
guardian,
|
|
|
|
params.permit(:before_post_id, :category_id)
|
|
|
|
).limit(50)
|
2016-03-18 11:19:45 -05:00
|
|
|
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_mentions", group_name: group.name)}"
|
|
|
|
@link = Discourse.base_url
|
|
|
|
@description = I18n.t("rss_description.group_mentions", group_name: group.name)
|
|
|
|
render 'posts/latest', formats: [:rss]
|
|
|
|
end
|
|
|
|
|
2014-02-06 12:06:19 -06:00
|
|
|
def members
|
2014-02-18 15:43:02 -06:00
|
|
|
group = find_group(:group_id)
|
2014-11-24 14:12:48 -06:00
|
|
|
|
2019-08-14 08:30:04 -05:00
|
|
|
guardian.ensure_can_see_group_members!(group)
|
|
|
|
|
2019-11-18 06:59:28 -06:00
|
|
|
limit = (params[:limit] || 50).to_i
|
2015-01-05 11:51:45 -06:00
|
|
|
offset = params[:offset].to_i
|
2018-06-28 19:14:50 -05:00
|
|
|
|
2019-11-18 06:59:28 -06:00
|
|
|
raise Discourse::InvalidParameters.new(:limit) if limit < 0 || limit > 1000
|
|
|
|
raise Discourse::InvalidParameters.new(:offset) if offset < 0
|
2018-06-28 19:14:50 -05:00
|
|
|
|
2020-05-14 21:10:59 -05:00
|
|
|
dir = (params[:asc] && params[:asc].present?) ? 'ASC' : 'DESC'
|
|
|
|
if params[:desc]
|
|
|
|
Discourse.deprecate(":desc is deprecated please use :asc instead", output_in_test: true)
|
|
|
|
dir = (params[:desc] && params[:desc].present?) ? 'DESC' : 'ASC'
|
|
|
|
end
|
2016-12-22 00:55:24 -06:00
|
|
|
order = ""
|
2016-12-07 03:28:43 -06:00
|
|
|
|
2019-03-27 06:30:59 -05:00
|
|
|
if params[:requesters]
|
|
|
|
guardian.ensure_can_edit!(group)
|
|
|
|
|
|
|
|
users = group.requesters
|
|
|
|
total = users.count
|
|
|
|
|
|
|
|
if (filter = params[:filter]).present?
|
|
|
|
filter = filter.split(',') if filter.include?(',')
|
|
|
|
|
|
|
|
if current_user&.admin
|
|
|
|
users = users.filter_by_username_or_email(filter)
|
|
|
|
else
|
|
|
|
users = users.filter_by_username(filter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
users = users
|
|
|
|
.select("users.*, group_requests.reason, group_requests.created_at requested_at")
|
|
|
|
.order(params[:order] == 'requested_at' ? "group_requests.created_at #{dir}" : "")
|
|
|
|
.order(username_lower: dir)
|
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
|
|
|
|
return render json: {
|
|
|
|
members: serialize_data(users, GroupRequesterSerializer),
|
|
|
|
meta: {
|
|
|
|
total: total,
|
|
|
|
limit: limit,
|
|
|
|
offset: offset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-12-07 03:28:43 -06:00
|
|
|
if params[:order] && %w{last_posted_at last_seen_at}.include?(params[:order])
|
2016-12-22 00:55:24 -06:00
|
|
|
order = "#{params[:order]} #{dir} NULLS LAST"
|
2018-10-31 23:33:28 -05:00
|
|
|
elsif params[:order] == 'added_at'
|
|
|
|
order = "group_users.created_at #{dir}"
|
2016-12-07 03:28:43 -06:00
|
|
|
end
|
|
|
|
|
2017-10-05 21:35:40 -05:00
|
|
|
users = group.users.human_users
|
|
|
|
total = users.count
|
2018-03-22 00:42:46 -05:00
|
|
|
|
2018-03-26 01:30:37 -05:00
|
|
|
if (filter = params[:filter]).present?
|
|
|
|
filter = filter.split(',') if filter.include?(',')
|
|
|
|
|
|
|
|
if current_user&.admin
|
|
|
|
users = users.filter_by_username_or_email(filter)
|
|
|
|
else
|
|
|
|
users = users.filter_by_username(filter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-24 18:49:27 -06:00
|
|
|
users = users.joins(:user_option).select('users.*, user_options.timezone, group_users.created_at as added_at')
|
2018-10-31 23:33:28 -05:00
|
|
|
|
2017-10-05 21:35:40 -05:00
|
|
|
members = users
|
2016-12-07 03:28:43 -06:00
|
|
|
.order('NOT group_users.owner')
|
|
|
|
.order(order)
|
2017-07-27 20:20:09 -05:00
|
|
|
.order(username_lower: dir)
|
2016-12-07 03:28:43 -06:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2018-12-06 05:18:52 -06:00
|
|
|
.includes(:primary_group)
|
2016-12-07 03:28:43 -06:00
|
|
|
|
2017-10-05 21:35:40 -05:00
|
|
|
owners = users
|
2016-12-07 03:28:43 -06:00
|
|
|
.order(order)
|
2017-07-27 20:20:09 -05:00
|
|
|
.order(username_lower: dir)
|
2016-12-07 03:28:43 -06:00
|
|
|
.where('group_users.owner')
|
2018-12-06 05:18:52 -06:00
|
|
|
.includes(:primary_group)
|
2015-01-05 11:51:45 -06:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
members: serialize_data(members, GroupUserSerializer),
|
2015-11-09 07:52:04 -06:00
|
|
|
owners: serialize_data(owners, GroupUserSerializer),
|
2015-01-05 11:51:45 -06:00
|
|
|
meta: {
|
|
|
|
total: total,
|
|
|
|
limit: limit,
|
|
|
|
offset: offset
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 12:06:19 -06:00
|
|
|
end
|
|
|
|
|
2015-01-08 17:35:52 -06:00
|
|
|
def add_members
|
2015-11-09 07:52:04 -06:00
|
|
|
group = Group.find(params[:id])
|
2017-07-27 21:37:10 -05:00
|
|
|
group.public_admission ? ensure_logged_in : guardian.ensure_can_edit!(group)
|
2020-08-04 10:02:01 -05:00
|
|
|
users = users_from_params.to_a
|
2016-12-06 22:06:56 -06:00
|
|
|
|
2017-07-27 21:37:10 -05:00
|
|
|
if group.public_admission
|
2016-12-13 02:39:44 -06:00
|
|
|
if !guardian.can_log_group_changes?(group) && current_user != users.first
|
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
2016-12-06 22:06:56 -06:00
|
|
|
|
|
|
|
unless current_user.staff?
|
|
|
|
RateLimiter.new(current_user, "public_group_membership", 3, 1.minute).performed!
|
|
|
|
end
|
2015-11-09 07:52:04 -06:00
|
|
|
end
|
|
|
|
|
2020-08-04 10:02:01 -05:00
|
|
|
emails = []
|
|
|
|
if params[:emails]
|
|
|
|
params[:emails].split(",").each do |email|
|
|
|
|
existing_user = User.find_by_email(email)
|
|
|
|
existing_user.present? ? users.push(existing_user) : emails.push(email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if users.empty? && emails.empty?
|
2021-02-03 11:13:00 -06:00
|
|
|
raise Discourse::InvalidParameters.new(I18n.t("usernames_or_emails_required"))
|
|
|
|
end
|
|
|
|
|
|
|
|
if emails.any?
|
|
|
|
if SiteSetting.enable_sso?
|
|
|
|
raise Discourse::InvalidParameters.new(I18n.t("no_invites_with_sso"))
|
|
|
|
elsif !SiteSetting.enable_local_logins?
|
|
|
|
raise Discourse::InvalidParameters.new(I18n.t("no_invites_without_local_logins"))
|
|
|
|
end
|
2020-08-04 10:02:01 -05:00
|
|
|
end
|
2021-02-03 11:13:00 -06:00
|
|
|
|
2020-08-24 17:55:21 -05:00
|
|
|
if users.length > ADD_MEMBERS_LIMIT
|
|
|
|
return render_json_error(
|
2021-02-02 03:50:04 -06:00
|
|
|
I18n.t("groups.errors.adding_too_many_users", count: ADD_MEMBERS_LIMIT)
|
2020-08-24 17:55:21 -05:00
|
|
|
)
|
|
|
|
end
|
2021-02-03 11:13:00 -06:00
|
|
|
|
2020-08-20 22:38:09 -05:00
|
|
|
usernames_already_in_group = group.users.where(id: users.map(&:id)).pluck(:username)
|
|
|
|
if usernames_already_in_group.present? && usernames_already_in_group.length == users.length
|
2018-03-26 01:30:37 -05:00
|
|
|
render_json_error(I18n.t(
|
|
|
|
"groups.errors.member_already_exist",
|
2020-08-20 22:38:09 -05:00
|
|
|
username: usernames_already_in_group.sort.join(", "),
|
|
|
|
count: usernames_already_in_group.size
|
2018-03-26 01:30:37 -05:00
|
|
|
))
|
|
|
|
else
|
2020-08-04 10:02:01 -05:00
|
|
|
uniq_users = users.uniq
|
|
|
|
uniq_users.each do |user|
|
2019-05-27 14:41:57 -05:00
|
|
|
begin
|
|
|
|
group.add(user)
|
|
|
|
GroupActionLogger.new(current_user, group).log_add_user_to_group(user)
|
2020-08-04 10:02:01 -05:00
|
|
|
group.notify_added_to_group(user) if params[:notify_users]&.to_s == "true"
|
2019-05-27 14:41:57 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
# Under concurrency, we might attempt to insert two records quickly and hit a DB
|
|
|
|
# constraint. In this case we can safely ignore the error and act as if the user
|
|
|
|
# was added to the group.
|
|
|
|
end
|
2015-01-08 17:35:52 -06:00
|
|
|
end
|
|
|
|
|
2020-08-04 10:02:01 -05:00
|
|
|
emails.each do |email|
|
|
|
|
Invite.invite_by_email(email, current_user, nil, [group.id])
|
|
|
|
end
|
|
|
|
|
2018-03-26 01:30:37 -05:00
|
|
|
render json: success_json.merge!(
|
2020-08-04 10:02:01 -05:00
|
|
|
usernames: uniq_users.map(&:username),
|
|
|
|
emails: emails
|
2018-03-26 01:30:37 -05:00
|
|
|
)
|
2015-11-09 07:52:04 -06:00
|
|
|
end
|
2015-01-08 17:35:52 -06:00
|
|
|
end
|
|
|
|
|
2019-03-27 06:30:59 -05:00
|
|
|
def handle_membership_request
|
|
|
|
group = Group.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) if group.blank?
|
|
|
|
guardian.ensure_can_edit!(group)
|
|
|
|
|
2020-05-26 08:28:03 -05:00
|
|
|
user = User.find_by(id: params[:user_id])
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id) if user.blank?
|
2019-03-27 06:30:59 -05:00
|
|
|
|
2020-05-26 08:28:03 -05:00
|
|
|
ActiveRecord::Base.transaction do
|
2019-03-27 06:30:59 -05:00
|
|
|
if params[:accept]
|
2020-05-26 08:28:03 -05:00
|
|
|
group.add(user)
|
2019-03-27 06:30:59 -05:00
|
|
|
GroupActionLogger.new(current_user, group).log_add_user_to_group(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
GroupRequest.where(group_id: group.id, user_id: user.id).delete_all
|
|
|
|
end
|
|
|
|
|
2020-05-26 08:28:03 -05:00
|
|
|
if params[:accept]
|
|
|
|
PostCreator.new(current_user,
|
|
|
|
title: I18n.t('groups.request_accepted_pm.title', group_name: group.name),
|
|
|
|
raw: I18n.t('groups.request_accepted_pm.body', group_name: group.name),
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: user.username,
|
|
|
|
skip_validations: true
|
|
|
|
).create!
|
|
|
|
end
|
|
|
|
|
2019-03-27 06:30:59 -05:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2016-11-25 02:45:15 -06:00
|
|
|
def mentionable
|
2018-06-05 20:42:09 -05:00
|
|
|
group = find_group(:group_id, ensure_can_see: false)
|
2016-11-25 02:45:15 -06:00
|
|
|
|
|
|
|
if group
|
|
|
|
render json: { mentionable: Group.mentionable(current_user).where(id: group.id).present? }
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-28 11:32:08 -05:00
|
|
|
def messageable
|
2018-06-05 20:42:09 -05:00
|
|
|
group = find_group(:group_id, ensure_can_see: false)
|
2017-08-28 11:32:08 -05:00
|
|
|
|
|
|
|
if group
|
|
|
|
render json: { messageable: Group.messageable(current_user).where(id: group.id).present? }
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidAccess.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-31 22:08:45 -05:00
|
|
|
def check_name
|
|
|
|
group_name = params.require(:group_name)
|
|
|
|
checker = UsernameCheckerService.new(allow_reserved_username: true)
|
|
|
|
render json: checker.check_username(group_name, nil)
|
|
|
|
end
|
|
|
|
|
2015-01-08 17:35:52 -06:00
|
|
|
def remove_member
|
2018-04-06 04:11:00 -05:00
|
|
|
group = Group.find_by(id: params[:id])
|
|
|
|
raise Discourse::NotFound unless group
|
2017-07-27 21:37:10 -05:00
|
|
|
group.public_exit ? ensure_logged_in : guardian.ensure_can_edit!(group)
|
2016-12-06 22:06:56 -06:00
|
|
|
|
2018-10-11 16:27:41 -05:00
|
|
|
# Maintain backwards compatibility
|
|
|
|
params[:usernames] = params[:username] if params[:username].present?
|
|
|
|
params[:user_emails] = params[:user_email] if params[:user_email].present?
|
2015-11-09 07:52:04 -06:00
|
|
|
|
2018-10-11 16:27:41 -05:00
|
|
|
users = users_from_params
|
2020-08-04 10:02:01 -05:00
|
|
|
raise Discourse::InvalidParameters.new(
|
|
|
|
'user_ids or usernames or user_emails must be present'
|
|
|
|
) if users.empty?
|
2016-12-06 22:06:56 -06:00
|
|
|
|
2017-07-27 21:37:10 -05:00
|
|
|
if group.public_exit
|
2018-10-11 16:27:41 -05:00
|
|
|
if !guardian.can_log_group_changes?(group) && current_user != users.first
|
2016-12-13 02:39:44 -06:00
|
|
|
raise Discourse::InvalidAccess
|
|
|
|
end
|
2016-12-06 22:06:56 -06:00
|
|
|
|
|
|
|
unless current_user.staff?
|
|
|
|
RateLimiter.new(current_user, "public_group_membership", 3, 1.minute).performed!
|
|
|
|
end
|
2015-11-09 07:52:04 -06:00
|
|
|
end
|
|
|
|
|
2020-07-22 15:27:43 -05:00
|
|
|
removed_users = []
|
|
|
|
skipped_users = []
|
|
|
|
|
2018-10-11 16:27:41 -05:00
|
|
|
users.each do |user|
|
2020-03-10 16:25:00 -05:00
|
|
|
if group.remove(user)
|
2020-07-22 15:27:43 -05:00
|
|
|
removed_users << user.username
|
2020-03-10 16:25:00 -05:00
|
|
|
GroupActionLogger.new(current_user, group).log_remove_user_from_group(user)
|
|
|
|
else
|
2020-07-22 15:27:43 -05:00
|
|
|
if group.users.exclude? user
|
|
|
|
skipped_users << user.username
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidParameters
|
|
|
|
end
|
2020-03-10 16:25:00 -05:00
|
|
|
end
|
2015-01-08 17:35:52 -06:00
|
|
|
end
|
2018-10-11 16:27:41 -05:00
|
|
|
|
|
|
|
render json: success_json.merge!(
|
2020-07-22 15:27:43 -05:00
|
|
|
usernames: removed_users,
|
|
|
|
skipped_usernames: skipped_users
|
2018-10-11 16:27:41 -05:00
|
|
|
)
|
2017-06-13 03:10:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_membership
|
2020-08-24 18:29:18 -05:00
|
|
|
params.require(:reason)
|
2017-08-08 04:53:02 -05:00
|
|
|
|
2017-06-13 03:10:14 -05:00
|
|
|
group = find_group(:id)
|
2019-04-22 21:51:30 -05:00
|
|
|
|
|
|
|
begin
|
2020-08-24 18:29:18 -05:00
|
|
|
GroupRequest.create!(group: group, user: current_user, reason: params[:reason])
|
2019-04-22 21:51:30 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique => e
|
|
|
|
return render json: failed_json.merge(error: I18n.t("groups.errors.already_requested_membership")), status: 409
|
|
|
|
end
|
2017-06-14 22:36:09 -05:00
|
|
|
|
|
|
|
usernames = [current_user.username].concat(
|
|
|
|
group.users.where('group_users.owner')
|
|
|
|
.order("users.last_seen_at DESC")
|
|
|
|
.limit(5)
|
|
|
|
.pluck("users.username")
|
|
|
|
)
|
2017-06-13 03:10:14 -05:00
|
|
|
|
|
|
|
post = PostCreator.new(current_user,
|
2019-04-22 21:51:30 -05:00
|
|
|
title: I18n.t('groups.request_membership_pm.title', group_name: group.name),
|
2019-08-06 05:28:22 -05:00
|
|
|
raw: params[:reason],
|
2017-06-13 03:10:14 -05:00
|
|
|
archetype: Archetype.private_message,
|
2017-06-14 22:36:09 -05:00
|
|
|
target_usernames: usernames.join(','),
|
2020-03-24 04:12:52 -05:00
|
|
|
topic_opts: { custom_fields: { requested_group_id: group.id } },
|
2017-06-13 03:10:14 -05:00
|
|
|
skip_validations: true
|
|
|
|
).create!
|
|
|
|
|
|
|
|
render json: success_json.merge(relative_url: post.topic.relative_url)
|
2015-01-08 17:35:52 -06:00
|
|
|
end
|
|
|
|
|
2015-12-14 16:17:09 -06:00
|
|
|
def set_notifications
|
|
|
|
group = find_group(:id)
|
|
|
|
notification_level = params.require(:notification_level)
|
|
|
|
|
2017-04-20 14:47:25 -05:00
|
|
|
user_id = current_user.id
|
|
|
|
if guardian.is_staff?
|
|
|
|
user_id = params[:user_id] || user_id
|
|
|
|
end
|
|
|
|
|
2015-12-14 16:17:09 -06:00
|
|
|
GroupUser.where(group_id: group.id)
|
2017-07-27 20:20:09 -05:00
|
|
|
.where(user_id: user_id)
|
|
|
|
.update_all(notification_level: notification_level)
|
2015-12-14 16:17:09 -06:00
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2016-12-11 09:36:15 -06:00
|
|
|
def histories
|
|
|
|
group = find_group(:group_id)
|
2020-08-19 09:41:40 -05:00
|
|
|
guardian.ensure_can_edit!(group) unless guardian.can_admin_group?(group)
|
2016-12-11 09:36:15 -06:00
|
|
|
|
|
|
|
page_size = 25
|
|
|
|
offset = (params[:offset] && params[:offset].to_i) || 0
|
|
|
|
|
|
|
|
group_histories = GroupHistory.with_filters(group, params[:filters])
|
|
|
|
.limit(page_size)
|
|
|
|
.offset(offset * page_size)
|
|
|
|
|
|
|
|
render_json_dump(
|
|
|
|
logs: serialize_data(group_histories, BasicGroupHistorySerializer),
|
|
|
|
all_loaded: group_histories.count < page_size
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-07-21 01:12:24 -05:00
|
|
|
def search
|
|
|
|
groups = Group.visible_groups(current_user)
|
|
|
|
.where("groups.id <> ?", Group::AUTO_GROUPS[:everyone])
|
2020-10-05 03:22:55 -05:00
|
|
|
.includes(:flair_upload)
|
2017-07-21 01:12:24 -05:00
|
|
|
.order(:name)
|
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
if (term = params[:term]).present?
|
2017-07-21 01:12:24 -05:00
|
|
|
groups = groups.where("name ILIKE :term OR full_name ILIKE :term", term: "%#{term}%")
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:ignore_automatic].to_s == "true"
|
|
|
|
groups = groups.where(automatic: false)
|
|
|
|
end
|
|
|
|
|
2017-08-08 08:45:27 -05:00
|
|
|
if Group.preloaded_custom_field_names.present?
|
|
|
|
Group.preload_custom_fields(groups, Group.preloaded_custom_field_names)
|
|
|
|
end
|
|
|
|
|
2017-07-21 01:12:24 -05:00
|
|
|
render_serialized(groups, BasicGroupSerializer)
|
|
|
|
end
|
|
|
|
|
2020-08-10 09:49:05 -05:00
|
|
|
def permissions
|
|
|
|
group = find_group(:id)
|
|
|
|
category_groups = group.category_groups.select { |category_group| guardian.can_see_category?(category_group.category) }
|
|
|
|
render_serialized(category_groups.sort_by { |category_group| category_group.category.name }, CategoryGroupSerializer)
|
|
|
|
end
|
|
|
|
|
2014-02-18 15:43:02 -06:00
|
|
|
private
|
|
|
|
|
2018-04-05 00:53:00 -05:00
|
|
|
def group_params(automatic: false)
|
|
|
|
permitted_params =
|
|
|
|
if automatic
|
|
|
|
%i{
|
|
|
|
visibility_level
|
|
|
|
mentionable_level
|
|
|
|
messageable_level
|
|
|
|
default_notification_level
|
2019-04-17 21:44:30 -05:00
|
|
|
bio_raw
|
2018-04-05 00:53:00 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
default_params = %i{
|
|
|
|
mentionable_level
|
|
|
|
messageable_level
|
|
|
|
title
|
2020-05-25 00:38:47 -05:00
|
|
|
flair_icon
|
|
|
|
flair_upload_id
|
2018-04-05 00:53:00 -05:00
|
|
|
flair_bg_color
|
|
|
|
flair_color
|
|
|
|
bio_raw
|
|
|
|
public_admission
|
|
|
|
public_exit
|
|
|
|
allow_membership_requests
|
|
|
|
full_name
|
|
|
|
default_notification_level
|
|
|
|
membership_request_template
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:41:40 -05:00
|
|
|
if current_user.staff?
|
2018-04-05 22:44:58 -05:00
|
|
|
default_params.push(*[
|
|
|
|
:incoming_email,
|
2020-07-10 04:05:55 -05:00
|
|
|
:smtp_server,
|
|
|
|
:smtp_port,
|
|
|
|
:smtp_ssl,
|
|
|
|
:imap_server,
|
|
|
|
:imap_port,
|
|
|
|
:imap_ssl,
|
|
|
|
:imap_mailbox_name,
|
|
|
|
:email_username,
|
|
|
|
:email_password,
|
2018-04-05 22:44:58 -05:00
|
|
|
:primary_group,
|
|
|
|
:visibility_level,
|
2019-08-14 08:30:04 -05:00
|
|
|
:members_visibility_level,
|
2018-04-05 22:44:58 -05:00
|
|
|
:name,
|
|
|
|
:grant_trust_level,
|
|
|
|
:automatic_membership_email_domains,
|
2021-01-28 17:59:10 -06:00
|
|
|
:publish_read_state,
|
|
|
|
:allow_unknown_sender_topic_replies
|
2018-04-05 22:44:58 -05:00
|
|
|
])
|
2019-06-05 21:05:33 -05:00
|
|
|
|
2020-05-15 08:04:38 -05:00
|
|
|
custom_fields = DiscoursePluginRegistry.editable_group_custom_fields
|
2019-06-05 21:05:33 -05:00
|
|
|
default_params << { custom_fields: custom_fields } unless custom_fields.blank?
|
2018-04-05 00:53:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
default_params
|
|
|
|
end
|
2018-03-26 23:18:03 -05:00
|
|
|
|
2020-08-06 11:27:27 -05:00
|
|
|
if !automatic || current_user.admin
|
2020-08-13 16:20:23 -05:00
|
|
|
[:muted, :regular, :tracking, :watching, :watching_first_post].each do |level|
|
2020-08-06 11:27:27 -05:00
|
|
|
permitted_params << { "#{level}_category_ids" => [] }
|
|
|
|
permitted_params << { "#{level}_tags" => [] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-26 23:18:03 -05:00
|
|
|
params.require(:group).permit(*permitted_params)
|
2016-11-29 02:25:02 -06:00
|
|
|
end
|
|
|
|
|
2018-06-05 05:56:51 -05:00
|
|
|
def find_group(param_name, ensure_can_see: true)
|
2016-11-29 02:25:02 -06:00
|
|
|
name = params.require(param_name)
|
2020-01-15 04:21:58 -06:00
|
|
|
group = Group.find_by("LOWER(name) = ?", name.downcase)
|
2020-09-07 21:52:29 -05:00
|
|
|
raise Discourse::NotFound if ensure_can_see && !guardian.can_see_group?(group)
|
2016-11-29 02:25:02 -06:00
|
|
|
group
|
|
|
|
end
|
2018-10-11 16:27:41 -05:00
|
|
|
|
|
|
|
def users_from_params
|
|
|
|
if params[:usernames].present?
|
2018-10-18 14:17:24 -05:00
|
|
|
users = User.where(username_lower: params[:usernames].split(",").map(&:downcase))
|
2018-10-11 16:27:41 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:usernames) if users.blank?
|
2019-01-24 18:28:48 -06:00
|
|
|
elsif params[:user_id].present?
|
|
|
|
users = User.where(id: params[:user_id].to_i)
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id) if users.blank?
|
2018-10-11 16:27:41 -05:00
|
|
|
elsif params[:user_ids].present?
|
2019-01-24 18:28:48 -06:00
|
|
|
users = User.where(id: params[:user_ids].to_s.split(","))
|
2018-10-11 16:27:41 -05:00
|
|
|
raise Discourse::InvalidParameters.new(:user_ids) if users.blank?
|
|
|
|
elsif params[:user_emails].present?
|
|
|
|
users = User.with_email(params[:user_emails].split(","))
|
|
|
|
raise Discourse::InvalidParameters.new(:user_emails) if users.blank?
|
|
|
|
else
|
2020-08-04 10:02:01 -05:00
|
|
|
users = []
|
2018-10-11 16:27:41 -05:00
|
|
|
end
|
|
|
|
users
|
|
|
|
end
|
2014-01-30 16:10:36 -06:00
|
|
|
end
|