2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-23 22:48:32 -04:00
|
|
|
class GroupUser < ActiveRecord::Base
|
2022-11-30 09:52:08 -05:00
|
|
|
belongs_to :group
|
2013-05-23 22:48:32 -04:00
|
|
|
belongs_to :user
|
2015-04-09 22:17:28 -04:00
|
|
|
|
|
|
|
after_save :update_title
|
2018-09-20 22:06:08 -04:00
|
|
|
after_destroy :grant_other_available_title
|
2015-04-09 22:17:28 -04:00
|
|
|
|
|
|
|
after_save :set_primary_group
|
2022-08-12 08:45:09 -04:00
|
|
|
after_destroy :remove_primary_and_flair_group, :recalculate_trust_level
|
2015-04-09 22:17:28 -04:00
|
|
|
|
2017-04-20 15:47:25 -04:00
|
|
|
before_create :set_notification_level
|
2015-09-01 16:52:05 -04:00
|
|
|
after_save :grant_trust_level
|
2020-08-06 12:27:27 -04:00
|
|
|
after_save :set_category_notifications
|
|
|
|
after_save :set_tag_notifications
|
2015-09-01 16:52:05 -04:00
|
|
|
|
2022-11-30 09:52:08 -05:00
|
|
|
after_commit :increase_group_user_count, on: [:create]
|
|
|
|
after_commit :decrease_group_user_count, on: [:destroy]
|
|
|
|
|
2016-07-06 15:56:40 -04:00
|
|
|
def self.notification_levels
|
|
|
|
NotificationLevels.all
|
|
|
|
end
|
|
|
|
|
2020-09-03 02:02:15 -04:00
|
|
|
def self.ensure_consistency!(last_seen = 1.hour.ago)
|
|
|
|
update_first_unread_pm(last_seen)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update_first_unread_pm(last_seen, limit: 10_000)
|
2022-06-29 20:18:12 -04:00
|
|
|
DB.exec(
|
|
|
|
<<~SQL,
|
2020-09-03 02:02:15 -04:00
|
|
|
UPDATE group_users gu
|
|
|
|
SET first_unread_pm_at = Y.min_date
|
|
|
|
FROM (
|
|
|
|
SELECT
|
|
|
|
X.group_id,
|
|
|
|
X.user_id,
|
|
|
|
X.min_date
|
|
|
|
FROM (
|
|
|
|
SELECT
|
2020-09-10 05:13:11 -04:00
|
|
|
gu.group_id,
|
|
|
|
gu.user_id,
|
|
|
|
COALESCE(Z.min_date, :now) min_date
|
|
|
|
FROM group_users gu
|
|
|
|
LEFT JOIN (
|
|
|
|
SELECT
|
|
|
|
gu2.group_id,
|
|
|
|
gu2.user_id,
|
|
|
|
MIN(t.updated_at) min_date
|
|
|
|
FROM group_users gu2
|
|
|
|
INNER JOIN topic_allowed_groups tag ON tag.group_id = gu2.group_id
|
|
|
|
INNER JOIN topics t ON t.id = tag.topic_id
|
|
|
|
INNER JOIN users u ON u.id = gu2.user_id
|
|
|
|
LEFT JOIN topic_users tu ON t.id = tu.topic_id AND tu.user_id = gu2.user_id
|
|
|
|
WHERE t.deleted_at IS NULL
|
|
|
|
AND t.archetype = :archetype
|
|
|
|
AND tu.last_read_post_number < CASE
|
2024-02-07 18:43:34 -05:00
|
|
|
WHEN u.admin OR u.moderator #{SiteSetting.whispers_allowed_groups_map.any? ? "OR gu2.group_id IN (:whisperers_group_ids)" : ""}
|
2020-09-10 05:13:11 -04:00
|
|
|
THEN t.highest_staff_post_number
|
|
|
|
ELSE t.highest_post_number
|
|
|
|
END
|
|
|
|
AND (COALESCE(tu.notification_level, 1) >= 2)
|
|
|
|
GROUP BY gu2.user_id, gu2.group_id
|
|
|
|
) AS Z ON Z.user_id = gu.user_id AND Z.group_id = gu.group_id
|
2020-09-03 02:02:15 -04:00
|
|
|
) AS X
|
|
|
|
WHERE X.user_id IN (
|
|
|
|
SELECT id
|
|
|
|
FROM users
|
|
|
|
WHERE last_seen_at IS NOT NULL
|
|
|
|
AND last_seen_at > :last_seen
|
|
|
|
ORDER BY last_seen_at DESC
|
|
|
|
LIMIT :limit
|
|
|
|
)
|
|
|
|
) Y
|
|
|
|
WHERE gu.user_id = Y.user_id AND gu.group_id = Y.group_id
|
|
|
|
SQL
|
2022-06-29 20:18:12 -04:00
|
|
|
archetype: Archetype.private_message,
|
|
|
|
last_seen: last_seen,
|
|
|
|
limit: limit,
|
|
|
|
now: 10.minutes.ago,
|
2024-02-07 18:43:34 -05:00
|
|
|
whisperers_group_ids: SiteSetting.whispers_allowed_groups_map,
|
2023-01-09 07:20:10 -05:00
|
|
|
)
|
2020-09-03 02:02:15 -04:00
|
|
|
end
|
|
|
|
|
2015-04-09 22:17:28 -04:00
|
|
|
protected
|
|
|
|
|
2017-04-20 15:47:25 -04:00
|
|
|
def set_notification_level
|
|
|
|
self.notification_level = group&.default_notification_level || 3
|
|
|
|
end
|
|
|
|
|
2015-04-09 22:17:28 -04:00
|
|
|
def set_primary_group
|
2018-11-13 19:28:41 -05:00
|
|
|
user.update!(primary_group: group) if group.primary_group
|
2015-04-09 22:17:28 -04:00
|
|
|
end
|
|
|
|
|
2022-08-12 08:45:09 -04:00
|
|
|
def remove_primary_and_flair_group
|
2021-09-20 21:29:12 -04:00
|
|
|
return if self.destroyed_by_association&.active_record == User # User is being destroyed, so don't try to update
|
2022-08-12 08:45:09 -04:00
|
|
|
|
|
|
|
updates = {}
|
|
|
|
updates[:primary_group_id] = nil if user.primary_group_id == group_id
|
|
|
|
updates[:flair_group_id] = nil if user.flair_group_id == group_id
|
|
|
|
|
|
|
|
user.update(updates) if updates.present?
|
2015-04-09 22:17:28 -04:00
|
|
|
end
|
|
|
|
|
2018-09-20 22:06:08 -04:00
|
|
|
def grant_other_available_title
|
|
|
|
if group.title.present? && group.title == user.title
|
2020-05-24 16:12:55 -04:00
|
|
|
user.update_attribute(:title, user.next_best_title)
|
2015-04-09 22:17:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_title
|
|
|
|
if group.title.present?
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(
|
|
|
|
"
|
2015-04-09 22:17:28 -04: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 16:52:05 -04:00
|
|
|
|
|
|
|
def grant_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2017-11-28 19:01:13 -05:00
|
|
|
|
2017-03-02 13:16:01 -05:00
|
|
|
TrustLevelGranter.grant(group.grant_trust_level, user)
|
2015-09-01 16:52:05 -04:00
|
|
|
end
|
2017-11-23 12:58:46 -05:00
|
|
|
|
|
|
|
def recalculate_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2020-07-27 12:40:10 -04:00
|
|
|
return if self.destroyed_by_association&.active_record == User # User is being destroyed, so don't try to recalculate
|
2017-11-23 12:58:46 -05:00
|
|
|
|
2022-08-29 06:00:48 -04:00
|
|
|
Promotion.recalculate(user, use_previous_trust_level: true)
|
2017-11-23 12:58:46 -05:00
|
|
|
end
|
2020-08-06 12:27:27 -04: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 17:20:23 -04:00
|
|
|
%i[muted regular tracking watching_first_post watching].each do |level|
|
2020-08-06 12:27:27 -04: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 17:20:23 -04:00
|
|
|
%i[muted regular tracking watching_first_post watching].each do |level|
|
2020-08-06 12:27:27 -04: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
|
2022-11-30 09:52:08 -05:00
|
|
|
|
|
|
|
def increase_group_user_count
|
|
|
|
Group.increment_counter(:user_count, self.group_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrease_group_user_count
|
|
|
|
Group.decrement_counter(:user_count, self.group_id)
|
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 22:35:14 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_users
|
|
|
|
#
|
2016-01-11 01:30:56 -05: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 05:56:25 -05:00
|
|
|
# notification_level :integer default(2), not null
|
2020-10-27 14:12:33 -04:00
|
|
|
# first_unread_pm_at :datetime not null
|
2013-05-23 22:35:14 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_group_users_on_group_id_and_user_id (group_id,user_id) UNIQUE
|
2015-09-17 20:43:53 -04:00
|
|
|
# index_group_users_on_user_id_and_group_id (user_id,group_id) UNIQUE
|
2013-05-23 22:35:14 -04:00
|
|
|
#
|