DEV: Apply syntax_tree formatting to app/*

This commit is contained in:
David Taylor
2023-01-09 12:20:10 +00:00
parent a641ce4b62
commit 5a003715d3
696 changed files with 18447 additions and 15481 deletions

View File

@ -1,44 +1,38 @@
# frozen_string_literal: true
class GroupsController < ApplicationController
requires_login only: [
:set_notifications,
:mentionable,
:messageable,
:check_name,
:update,
:histories,
:request_membership,
:search,
:new,
:test_email_settings
]
requires_login only: %i[
set_notifications
mentionable
messageable
check_name
update
histories
request_membership
search
new
test_email_settings
]
skip_before_action :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
skip_before_action :preload_json, :check_xhr, only: %i[posts_feed mentions_feed]
skip_before_action :check_xhr, only: [:show]
after_action :add_noindex_header
TYPE_FILTERS = {
my: Proc.new { |groups, user|
raise Discourse::NotFound unless user
Group.member_of(groups, user)
},
owner: Proc.new { |groups, user|
raise Discourse::NotFound unless user
Group.owner_of(groups, user)
},
public: Proc.new { |groups|
groups.where(public_admission: true, automatic: false)
},
close: Proc.new { |groups|
groups.where(public_admission: false, automatic: false)
},
automatic: Proc.new { |groups|
groups.where(automatic: true)
},
non_automatic: Proc.new { |groups|
groups.where(automatic: false)
}
my:
Proc.new do |groups, user|
raise Discourse::NotFound unless user
Group.member_of(groups, user)
end,
owner:
Proc.new do |groups, user|
raise Discourse::NotFound unless user
Group.owner_of(groups, user)
end,
public: Proc.new { |groups| groups.where(public_admission: true, automatic: false) },
close: Proc.new { |groups| groups.where(public_admission: false, automatic: false) },
automatic: Proc.new { |groups| groups.where(automatic: true) },
non_automatic: Proc.new { |groups| groups.where(automatic: false) },
}
ADD_MEMBERS_LIMIT = 1000
@ -47,7 +41,7 @@ class GroupsController < ApplicationController
raise Discourse::InvalidAccess.new(:enable_group_directory)
end
order = %w{name user_count}.delete(params[:order])
order = %w[name user_count].delete(params[:order])
dir = params[:asc].to_s == "true" ? "ASC" : "DESC"
sort = order ? "#{order} #{dir}" : nil
groups = Group.visible_groups(current_user, sort)
@ -56,7 +50,7 @@ class GroupsController < ApplicationController
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)
type_filters = type_filters - [:my, :owner]
type_filters = type_filters - %i[my owner]
end
if (filter = params[:filter]).present?
@ -83,7 +77,7 @@ class GroupsController < ApplicationController
user_group_ids = group_users.pluck(:group_id)
owner_group_ids = group_users.where(owner: true).pluck(:group_id)
else
type_filters = type_filters - [:my, :owner]
type_filters = type_filters - %i[my owner]
end
type_filters.delete(:non_automatic)
@ -96,22 +90,19 @@ class GroupsController < ApplicationController
groups = groups.offset(page * page_size).limit(page_size)
render_json_dump(
groups: serialize_data(groups,
BasicGroupSerializer,
user_group_ids: user_group_ids || [],
owner_group_ids: owner_group_ids || []
),
groups:
serialize_data(
groups,
BasicGroupSerializer,
user_group_ids: user_group_ids || [],
owner_group_ids: owner_group_ids || [],
),
extras: {
type_filters: type_filters
type_filters: type_filters,
},
total_rows_groups: total,
load_more_groups: groups_path(
page: page + 1,
type: type,
order: order,
asc: params[:asc],
filter: filter
)
load_more_groups:
groups_path(page: page + 1, type: type, order: order, asc: params[:asc], filter: filter),
)
end
@ -122,21 +113,23 @@ class GroupsController < ApplicationController
format.html do
@title = group.full_name.present? ? group.full_name.capitalize : group.name
@full_title = "#{@title} - #{SiteSetting.title}"
@description_meta = group.bio_cooked.present? ? PrettyText.excerpt(group.bio_cooked, 300) : @title
@description_meta =
group.bio_cooked.present? ? PrettyText.excerpt(group.bio_cooked, 300) : @title
render :show
end
format.json do
groups = Group.visible_groups(current_user)
if !guardian.is_staff?
groups = groups.where("automatic IS FALSE OR groups.id = ?", Group::AUTO_GROUPS[:moderators])
groups =
groups.where("automatic IS FALSE OR groups.id = ?", Group::AUTO_GROUPS[:moderators])
end
render_json_dump(
group: serialize_data(group, GroupShowSerializer, root: nil),
extras: {
visible_group_names: groups.pluck(:name)
}
visible_group_names: groups.pluck(:name),
},
)
end
end
@ -161,7 +154,15 @@ class GroupsController < ApplicationController
if params[:update_existing_users].blank?
user_count = count_existing_users(group.group_users, notification_level, categories, tags)
return render status: 422, json: { user_count: user_count, errors: [I18n.t('invalid_params', message: :update_existing_users)] } if user_count > 0
if user_count > 0
return(
render status: 422,
json: {
user_count: user_count,
errors: [I18n.t("invalid_params", message: :update_existing_users)],
}
)
end
end
end
@ -169,7 +170,9 @@ class GroupsController < ApplicationController
GroupActionLogger.new(current_user, group).log_change_group_settings
group.record_email_setting_changes!(current_user)
group.expire_imap_mailbox_cache
update_existing_users(group.group_users, notification_level, categories, tags) if params[:update_existing_users] == "true"
if params[:update_existing_users] == "true"
update_existing_users(group.group_users, notification_level, categories, tags)
end
AdminDashboardData.clear_found_problem("group_#{group.id}_email_credentials")
# Redirect user to groups index page if they can no longer see the group
@ -185,10 +188,7 @@ class GroupsController < ApplicationController
group = find_group(:group_id)
guardian.ensure_can_see_group_members!(group)
posts = group.posts_for(
guardian,
params.permit(:before_post_id, :category_id)
).limit(20)
posts = group.posts_for(guardian, params.permit(:before_post_id, :category_id)).limit(20)
render_serialized posts.to_a, GroupPostSerializer
end
@ -196,37 +196,32 @@ class GroupsController < ApplicationController
group = find_group(:group_id)
guardian.ensure_can_see_group_members!(group)
@posts = group.posts_for(
guardian,
params.permit(:before_post_id, :category_id)
).limit(50)
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_posts", group_name: group.name)}"
@posts = group.posts_for(guardian, params.permit(:before_post_id, :category_id)).limit(50)
@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]
render "posts/latest", formats: [:rss]
end
def mentions
raise Discourse::NotFound unless SiteSetting.enable_mentions?
group = find_group(:group_id)
posts = group.mentioned_posts_for(
guardian,
params.permit(:before_post_id, :category_id)
).limit(20)
posts =
group.mentioned_posts_for(guardian, params.permit(:before_post_id, :category_id)).limit(20)
render_serialized posts.to_a, GroupPostSerializer
end
def mentions_feed
raise Discourse::NotFound unless SiteSetting.enable_mentions?
group = find_group(:group_id)
@posts = group.mentioned_posts_for(
guardian,
params.permit(:before_post_id, :category_id)
).limit(50)
@title = "#{SiteSetting.title} - #{I18n.t("rss_description.group_mentions", group_name: group.name)}"
@posts =
group.mentioned_posts_for(guardian, params.permit(:before_post_id, :category_id)).limit(50)
@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]
render "posts/latest", formats: [:rss]
end
def members
@ -240,10 +235,14 @@ class GroupsController < ApplicationController
raise Discourse::InvalidParameters.new(:limit) if limit < 0 || limit > 1000
raise Discourse::InvalidParameters.new(:offset) if offset < 0
dir = (params[:asc] && params[:asc].present?) ? 'ASC' : 'DESC'
dir = (params[:asc] && params[:asc].present?) ? "ASC" : "DESC"
if params[:desc]
Discourse.deprecate(":desc is deprecated please use :asc instead", output_in_test: true, drop_from: '2.9.0')
dir = (params[:desc] && params[:desc].present?) ? 'DESC' : 'ASC'
Discourse.deprecate(
":desc is deprecated please use :asc instead",
output_in_test: true,
drop_from: "2.9.0",
)
dir = (params[:desc] && params[:desc].present?) ? "DESC" : "ASC"
end
order = "NOT group_users.owner"
@ -254,7 +253,7 @@ class GroupsController < ApplicationController
total = users.count
if (filter = params[:filter]).present?
filter = filter.split(',') if filter.include?(',')
filter = filter.split(",") if filter.include?(",")
if current_user&.admin
users = users.filter_by_username_or_email(filter)
@ -263,26 +262,29 @@ class GroupsController < ApplicationController
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)
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
}
}
return(
render json: {
members: serialize_data(users, GroupRequesterSerializer),
meta: {
total: total,
limit: limit,
offset: offset,
},
}
)
end
if params[:order] && %w{last_posted_at last_seen_at}.include?(params[:order])
if params[:order] && %w[last_posted_at last_seen_at].include?(params[:order])
order = "#{params[:order]} #{dir} NULLS LAST"
elsif params[:order] == 'added_at'
elsif params[:order] == "added_at"
order = "group_users.created_at #{dir}"
end
@ -290,7 +292,7 @@ class GroupsController < ApplicationController
total = users.count
if (filter = params[:filter]).present?
filter = filter.split(',') if filter.include?(',')
filter = filter.split(",") if filter.include?(",")
if current_user&.admin
users = users.filter_by_username_or_email(filter)
@ -299,25 +301,26 @@ class GroupsController < ApplicationController
end
end
users = users
.includes(:primary_group)
.includes(:user_option)
.select('users.*, group_users.created_at as added_at')
.order(order)
.order(username_lower: dir)
users =
users
.includes(:primary_group)
.includes(:user_option)
.select("users.*, group_users.created_at as added_at")
.order(order)
.order(username_lower: dir)
members = users.limit(limit).offset(offset)
owners = users.where('group_users.owner')
owners = users.where("group_users.owner")
render json: {
members: serialize_data(members, GroupUserSerializer),
owners: serialize_data(owners, GroupUserSerializer),
meta: {
total: total,
limit: limit,
offset: offset
}
}
members: serialize_data(members, GroupUserSerializer),
owners: serialize_data(owners, GroupUserSerializer),
meta: {
total: total,
limit: limit,
offset: offset,
},
}
end
def add_members
@ -327,10 +330,12 @@ class GroupsController < ApplicationController
users = users_from_params.to_a
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
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
guardian.ensure_can_invite_to_forum!([group]) if emails.present?
@ -340,43 +345,43 @@ class GroupsController < ApplicationController
end
if users.length > ADD_MEMBERS_LIMIT
return render_json_error(
I18n.t("groups.errors.adding_too_many_users", count: ADD_MEMBERS_LIMIT)
return(
render_json_error(I18n.t("groups.errors.adding_too_many_users", count: ADD_MEMBERS_LIMIT))
)
end
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 &&
emails.blank?
render_json_error(I18n.t(
"groups.errors.member_already_exist",
username: usernames_already_in_group.sort.join(", "),
count: usernames_already_in_group.size
))
if usernames_already_in_group.present? && usernames_already_in_group.length == users.length &&
emails.blank?
render_json_error(
I18n.t(
"groups.errors.member_already_exist",
username: usernames_already_in_group.sort.join(", "),
count: usernames_already_in_group.size,
),
)
else
notify = params[:notify_users]&.to_s == "true"
uniq_users = users.uniq
uniq_users.each do |user|
add_user_to_group(group, user, notify)
end
uniq_users.each { |user| add_user_to_group(group, user, notify) }
emails.each do |email|
begin
Invite.generate(current_user, email: email, group_ids: [group.id])
rescue RateLimiter::LimitExceeded => e
return render_json_error(I18n.t(
"invite.rate_limit",
count: SiteSetting.max_invites_per_day,
time_left: e.time_left
))
return(
render_json_error(
I18n.t(
"invite.rate_limit",
count: SiteSetting.max_invites_per_day,
time_left: e.time_left,
),
)
)
end
end
render json: success_json.merge!(
usernames: uniq_users.map(&:username),
emails: emails
)
render json: success_json.merge!(usernames: uniq_users.map(&:username), emails: emails)
end
end
@ -412,12 +417,13 @@ class GroupsController < ApplicationController
end
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),
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
skip_validations: true,
).create!
end
@ -460,9 +466,9 @@ class GroupsController < ApplicationController
params[:user_emails] = params[:user_email] if params[:user_email].present?
users = users_from_params
raise Discourse::InvalidParameters.new(
'user_ids or usernames or user_emails must be present'
) if users.empty?
if users.empty?
raise Discourse::InvalidParameters.new("user_ids or usernames or user_emails must be present")
end
removed_users = []
skipped_users = []
@ -480,10 +486,7 @@ class GroupsController < ApplicationController
end
end
render json: success_json.merge!(
usernames: removed_users,
skipped_usernames: skipped_users
)
render json: success_json.merge!(usernames: removed_users, skipped_usernames: skipped_users)
end
def leave
@ -511,24 +514,35 @@ class GroupsController < ApplicationController
begin
GroupRequest.create!(group: group, user: current_user, reason: params[:reason])
rescue ActiveRecord::RecordNotUnique
return render json: failed_json.merge(error: I18n.t("groups.errors.already_requested_membership")), status: 409
return(
render json: failed_json.merge(error: I18n.t("groups.errors.already_requested_membership")),
status: 409
)
end
usernames = [current_user.username].concat(
group.users.where('group_users.owner')
group
.users
.where("group_users.owner")
.order("users.last_seen_at DESC")
.limit(MAX_NOTIFIED_OWNERS)
.pluck("users.username")
.pluck("users.username"),
)
post = PostCreator.new(current_user,
title: I18n.t('groups.request_membership_pm.title', group_name: group.name),
raw: params[:reason],
archetype: Archetype.private_message,
target_usernames: usernames.join(','),
topic_opts: { custom_fields: { requested_group_id: group.id } },
skip_validations: true
).create!
post =
PostCreator.new(
current_user,
title: I18n.t("groups.request_membership_pm.title", group_name: group.name),
raw: params[:reason],
archetype: Archetype.private_message,
target_usernames: usernames.join(","),
topic_opts: {
custom_fields: {
requested_group_id: group.id,
},
},
skip_validations: true,
).create!
render json: success_json.merge(relative_url: post.topic.relative_url)
end
@ -538,11 +552,10 @@ class GroupsController < ApplicationController
notification_level = params.require(:notification_level)
user_id = current_user.id
if guardian.is_staff?
user_id = params[:user_id] || user_id
end
user_id = params[:user_id] || user_id if guardian.is_staff?
GroupUser.where(group_id: group.id)
GroupUser
.where(group_id: group.id)
.where(user_id: user_id)
.update_all(notification_level: notification_level)
@ -556,29 +569,28 @@ class GroupsController < ApplicationController
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)
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
all_loaded: group_histories.count < page_size,
)
end
def search
groups = Group.visible_groups(current_user)
.where("groups.id <> ?", Group::AUTO_GROUPS[:everyone])
.includes(:flair_upload)
.order(:name)
groups =
Group
.visible_groups(current_user)
.where("groups.id <> ?", Group::AUTO_GROUPS[:everyone])
.includes(:flair_upload)
.order(:name)
if (term = params[:term]).present?
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
groups = groups.where(automatic: false) if params[:ignore_automatic].to_s == "true"
if Group.preloaded_custom_field_names.present?
Group.preload_custom_fields(groups, Group.preloaded_custom_field_names)
@ -589,8 +601,14 @@ class GroupsController < ApplicationController
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)
category_groups =
group.category_groups.select do |category_group|
guardian.can_see_category?(category_group.category)
end
render_serialized(
category_groups.sort_by { |category_group| category_group.category.name },
CategoryGroupSerializer,
)
end
def test_email_settings
@ -611,7 +629,7 @@ class GroupsController < ApplicationController
enable_tls = settings[:ssl] == "true"
email_host = params[:host]
if !["smtp", "imap"].include?(params[:protocol])
if !%w[smtp imap].include?(params[:protocol])
raise Discourse::InvalidParameters.new("Valid protocols to test are smtp and imap")
end
@ -622,20 +640,29 @@ class GroupsController < ApplicationController
enable_starttls_auto = false
settings.delete(:ssl)
final_settings = settings.merge(enable_tls: enable_tls, enable_starttls_auto: enable_starttls_auto)
.permit(:host, :port, :username, :password, :enable_tls, :enable_starttls_auto, :debug)
EmailSettingsValidator.validate_as_user(current_user, "smtp", **final_settings.to_h.symbolize_keys)
final_settings =
settings.merge(
enable_tls: enable_tls,
enable_starttls_auto: enable_starttls_auto,
).permit(:host, :port, :username, :password, :enable_tls, :enable_starttls_auto, :debug)
EmailSettingsValidator.validate_as_user(
current_user,
"smtp",
**final_settings.to_h.symbolize_keys,
)
when "imap"
final_settings = settings.merge(ssl: enable_tls)
.permit(:host, :port, :username, :password, :ssl, :debug)
EmailSettingsValidator.validate_as_user(current_user, "imap", **final_settings.to_h.symbolize_keys)
final_settings =
settings.merge(ssl: enable_tls).permit(:host, :port, :username, :password, :ssl, :debug)
EmailSettingsValidator.validate_as_user(
current_user,
"imap",
**final_settings.to_h.symbolize_keys,
)
end
render json: success_json
rescue *EmailSettingsExceptionHandler::EXPECTED_EXCEPTIONS, StandardError => err
render_json_error(
EmailSettingsExceptionHandler.friendly_exception_message(err, email_host)
)
render_json_error(EmailSettingsExceptionHandler.friendly_exception_message(err, email_host))
end
end
end
@ -653,7 +680,7 @@ class GroupsController < ApplicationController
end
def group_params(automatic: false)
attributes = %i{
attributes = %i[
bio_raw
default_notification_level
messageable_level
@ -662,7 +689,7 @@ class GroupsController < ApplicationController
flair_color
flair_icon
flair_upload_id
}
]
if automatic
attributes.push(:visibility_level)
@ -673,7 +700,7 @@ class GroupsController < ApplicationController
:full_name,
:public_exit,
:public_admission,
:membership_request_template
:membership_request_template,
)
end
@ -703,7 +730,7 @@ class GroupsController < ApplicationController
:grant_trust_level,
:automatic_membership_email_domains,
:publish_read_state,
:allow_unknown_sender_topic_replies
:allow_unknown_sender_topic_replies,
)
custom_fields = DiscoursePluginRegistry.editable_group_custom_fields
@ -711,7 +738,7 @@ class GroupsController < ApplicationController
end
if !automatic || current_user.admin
[:muted, :regular, :tracking, :watching, :watching_first_post].each do |level|
%i[muted regular tracking watching watching_first_post].each do |level|
attributes << { "#{level}_category_ids" => [] }
attributes << { "#{level}_tags" => [] }
end
@ -770,8 +797,10 @@ class GroupsController < ApplicationController
end
def user_default_notifications(group, params)
category_notifications = group.group_category_notification_defaults.pluck(:category_id, :notification_level).to_h
tag_notifications = group.group_tag_notification_defaults.pluck(:tag_id, :notification_level).to_h
category_notifications =
group.group_category_notification_defaults.pluck(:category_id, :notification_level).to_h
tag_notifications =
group.group_tag_notification_defaults.pluck(:tag_id, :notification_level).to_h
categories = {}
tags = {}
@ -782,10 +811,7 @@ class GroupsController < ApplicationController
category_id = category_id.to_i
old_value = category_notifications[category_id]
metadata = {
old_value: old_value,
new_value: value
}
metadata = { old_value: old_value, new_value: value }
if old_value.blank?
metadata[:action] = :create
@ -805,10 +831,7 @@ class GroupsController < ApplicationController
tag_ids.each do |tag_id|
old_value = tag_notifications[tag_id]
metadata = {
old_value: old_value,
new_value: value
}
metadata = { old_value: old_value, new_value: value }
if old_value.blank?
metadata[:action] = :create
@ -834,20 +857,18 @@ class GroupsController < ApplicationController
notification_level = nil
default_notification_level = params[:default_notification_level]&.to_i
if default_notification_level.present? && group.default_notification_level != default_notification_level
if default_notification_level.present? &&
group.default_notification_level != default_notification_level
notification_level = {
old_value: group.default_notification_level,
new_value: default_notification_level
new_value: default_notification_level,
}
end
[notification_level, categories, tags]
end
%i{
count
update
}.each do |action|
%i[count update].each do |action|
define_method("#{action}_existing_users") do |group_users, notification_level, categories, tags|
return 0 if notification_level.blank? && categories.blank? && tags.blank?
@ -865,7 +886,12 @@ class GroupsController < ApplicationController
categories.each do |category_id, data|
if data[:action] == :update || data[:action] == :delete
category_users = CategoryUser.where(category_id: category_id, notification_level: data[:old_value], user_id: group_users.select(:user_id))
category_users =
CategoryUser.where(
category_id: category_id,
notification_level: data[:old_value],
user_id: group_users.select(:user_id),
)
if action == :update
category_users.delete_all
@ -879,7 +905,12 @@ class GroupsController < ApplicationController
tags.each do |tag_id, data|
if data[:action] == :update || data[:action] == :delete
tag_users = TagUser.where(tag_id: tag_id, notification_level: data[:old_value], user_id: group_users.select(:user_id))
tag_users =
TagUser.where(
tag_id: tag_id,
notification_level: data[:old_value],
user_id: group_users.select(:user_id),
)
if action == :update
tag_users.delete_all
@ -892,47 +923,65 @@ class GroupsController < ApplicationController
end
if categories.present? || tags.present?
group_users.select(:id, :user_id).find_in_batches do |batch|
user_ids = batch.pluck(:user_id)
group_users
.select(:id, :user_id)
.find_in_batches do |batch|
user_ids = batch.pluck(:user_id)
categories.each do |category_id, data|
category_users = []
existing_users = CategoryUser.where(category_id: category_id, user_id: user_ids).where("notification_level IS NOT NULL")
skip_user_ids = existing_users.pluck(:user_id)
categories.each do |category_id, data|
category_users = []
existing_users =
CategoryUser.where(category_id: category_id, user_id: user_ids).where(
"notification_level IS NOT NULL",
)
skip_user_ids = existing_users.pluck(:user_id)
batch.each do |group_user|
next if skip_user_ids.include?(group_user.user_id)
category_users << { category_id: category_id, user_id: group_user.user_id, notification_level: data[:new_value] }
batch.each do |group_user|
next if skip_user_ids.include?(group_user.user_id)
category_users << {
category_id: category_id,
user_id: group_user.user_id,
notification_level: data[:new_value],
}
end
next if category_users.blank?
if action == :update
CategoryUser.insert_all!(category_users)
else
ids += category_users.pluck(:user_id)
end
end
next if category_users.blank?
tags.each do |tag_id, data|
tag_users = []
existing_users =
TagUser.where(tag_id: tag_id, user_id: user_ids).where(
"notification_level IS NOT NULL",
)
skip_user_ids = existing_users.pluck(:user_id)
if action == :update
CategoryUser.insert_all!(category_users)
else
ids += category_users.pluck(:user_id)
batch.each do |group_user|
next if skip_user_ids.include?(group_user.user_id)
tag_users << {
tag_id: tag_id,
user_id: group_user.user_id,
notification_level: data[:new_value],
created_at: Time.now,
updated_at: Time.now,
}
end
next if tag_users.blank?
if action == :update
TagUser.insert_all!(tag_users)
else
ids += tag_users.pluck(:user_id)
end
end
end
tags.each do |tag_id, data|
tag_users = []
existing_users = TagUser.where(tag_id: tag_id, user_id: user_ids).where("notification_level IS NOT NULL")
skip_user_ids = existing_users.pluck(:user_id)
batch.each do |group_user|
next if skip_user_ids.include?(group_user.user_id)
tag_users << { tag_id: tag_id, user_id: group_user.user_id, notification_level: data[:new_value], created_at: Time.now, updated_at: Time.now }
end
next if tag_users.blank?
if action == :update
TagUser.insert_all!(tag_users)
else
ids += tag_users.pluck(:user_id)
end
end
end
end
ids.uniq.count