2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-23 21:48:32 -05:00
|
|
|
class GroupUser < ActiveRecord::Base
|
|
|
|
belongs_to :group, counter_cache: "user_count"
|
|
|
|
belongs_to :user
|
2015-04-09 21:17:28 -05:00
|
|
|
|
|
|
|
after_save :update_title
|
2018-09-20 21:06:08 -05:00
|
|
|
after_destroy :grant_other_available_title
|
2015-04-09 21:17:28 -05:00
|
|
|
|
|
|
|
after_save :set_primary_group
|
2017-11-23 11:58:46 -06:00
|
|
|
after_destroy :remove_primary_group, :recalculate_trust_level
|
2015-04-09 21:17:28 -05:00
|
|
|
|
2017-04-20 14:47:25 -05:00
|
|
|
before_create :set_notification_level
|
2015-09-01 15:52:05 -05:00
|
|
|
after_save :grant_trust_level
|
2020-08-06 11:27:27 -05:00
|
|
|
after_save :set_category_notifications
|
|
|
|
after_save :set_tag_notifications
|
2015-09-01 15:52:05 -05:00
|
|
|
|
2016-07-06 14:56:40 -05:00
|
|
|
def self.notification_levels
|
|
|
|
NotificationLevels.all
|
|
|
|
end
|
|
|
|
|
2015-04-09 21:17:28 -05:00
|
|
|
protected
|
|
|
|
|
2017-04-20 14:47:25 -05:00
|
|
|
def set_notification_level
|
|
|
|
self.notification_level = group&.default_notification_level || 3
|
|
|
|
end
|
|
|
|
|
2015-04-09 21:17:28 -05:00
|
|
|
def set_primary_group
|
2018-11-13 18:28:41 -06:00
|
|
|
user.update!(primary_group: group) if group.primary_group
|
2015-04-09 21:17:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_primary_group
|
2018-06-19 01:13:14 -05:00
|
|
|
DB.exec("
|
2015-04-09 21:17:28 -05:00
|
|
|
UPDATE users
|
|
|
|
SET primary_group_id = NULL
|
|
|
|
WHERE id = :user_id AND primary_group_id = :id",
|
|
|
|
id: group.id, user_id: user_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-09-20 21:06:08 -05:00
|
|
|
def grant_other_available_title
|
|
|
|
if group.title.present? && group.title == user.title
|
2020-05-24 15:12:55 -05:00
|
|
|
user.update_attribute(:title, user.next_best_title)
|
2015-04-09 21:17:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_title
|
|
|
|
if group.title.present?
|
2018-06-19 01:13:14 -05:00
|
|
|
DB.exec("
|
2015-04-09 21:17:28 -05:00
|
|
|
UPDATE users SET title = :title
|
|
|
|
WHERE (title IS NULL OR title = '') AND id = :id",
|
|
|
|
id: user_id, title: group.title
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-09-01 15:52:05 -05:00
|
|
|
|
|
|
|
def grant_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2017-11-28 18:01:13 -06:00
|
|
|
|
2017-03-02 12:16:01 -06:00
|
|
|
TrustLevelGranter.grant(group.grant_trust_level, user)
|
2015-09-01 15:52:05 -05:00
|
|
|
end
|
2017-11-23 11:58:46 -06:00
|
|
|
|
|
|
|
def recalculate_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2020-07-27 11:40:10 -05:00
|
|
|
return if self.destroyed_by_association&.active_record == User # User is being destroyed, so don't try to recalculate
|
2017-11-23 11:58:46 -06:00
|
|
|
|
2018-04-10 01:19:08 -05:00
|
|
|
Promotion.recalculate(user)
|
2017-11-23 11:58:46 -06:00
|
|
|
end
|
2020-08-06 11:27:27 -05:00
|
|
|
|
|
|
|
def set_category_notifications
|
|
|
|
self.class.set_category_notifications(group, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_category_notifications(group, user)
|
|
|
|
group_levels = group.group_category_notification_defaults.each_with_object({}) do |r, h|
|
|
|
|
h[r.notification_level] ||= []
|
|
|
|
h[r.notification_level] << r.category_id
|
|
|
|
end
|
|
|
|
|
|
|
|
return if group_levels.empty?
|
|
|
|
|
|
|
|
user_levels = CategoryUser.where(user_id: user.id).each_with_object({}) do |r, h|
|
|
|
|
h[r.notification_level] ||= []
|
|
|
|
h[r.notification_level] << r.category_id
|
|
|
|
end
|
|
|
|
|
|
|
|
higher_level_category_ids = user_levels.values.flatten
|
|
|
|
|
2020-08-13 16:20:23 -05:00
|
|
|
[:muted, :regular, :tracking, :watching_first_post, :watching].each do |level|
|
2020-08-06 11:27:27 -05:00
|
|
|
level_num = NotificationLevels.all[level]
|
|
|
|
higher_level_category_ids -= (user_levels[level_num] || [])
|
|
|
|
if group_category_ids = group_levels[level_num]
|
|
|
|
CategoryUser.batch_set(
|
|
|
|
user,
|
|
|
|
level,
|
|
|
|
group_category_ids + (user_levels[level_num] || []) - higher_level_category_ids
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_tag_notifications
|
|
|
|
self.class.set_tag_notifications(group, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.set_tag_notifications(group, user)
|
|
|
|
group_levels = group.group_tag_notification_defaults.each_with_object({}) do |r, h|
|
|
|
|
h[r.notification_level] ||= []
|
|
|
|
h[r.notification_level] << r.tag_id
|
|
|
|
end
|
|
|
|
|
|
|
|
return if group_levels.empty?
|
|
|
|
|
|
|
|
user_levels = TagUser.where(user_id: user.id).each_with_object({}) do |r, h|
|
|
|
|
h[r.notification_level] ||= []
|
|
|
|
h[r.notification_level] << r.tag_id
|
|
|
|
end
|
|
|
|
|
|
|
|
higher_level_tag_ids = user_levels.values.flatten
|
|
|
|
|
2020-08-13 16:20:23 -05:00
|
|
|
[:muted, :regular, :tracking, :watching_first_post, :watching].each do |level|
|
2020-08-06 11:27:27 -05:00
|
|
|
level_num = NotificationLevels.all[level]
|
|
|
|
higher_level_tag_ids -= (user_levels[level_num] || [])
|
|
|
|
if group_tag_ids = group_levels[level_num]
|
|
|
|
TagUser.batch_set(
|
|
|
|
user,
|
|
|
|
level,
|
|
|
|
group_tag_ids + (user_levels[level_num] || []) - higher_level_tag_ids
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
end
|
|
|
|
|
2013-05-23 21:35:14 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_users
|
|
|
|
#
|
2016-01-11 00:30:56 -06:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# group_id :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# owner :boolean default(FALSE), not null
|
2016-01-27 04:56:25 -06:00
|
|
|
# notification_level :integer default(2), not null
|
2013-05-23 21:35:14 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_group_users_on_group_id_and_user_id (group_id,user_id) UNIQUE
|
2015-09-17 19:43:53 -05:00
|
|
|
# index_group_users_on_user_id_and_group_id (user_id,group_id) UNIQUE
|
2013-05-23 21:35:14 -05:00
|
|
|
#
|