2016-07-07 03:56:40 +08:00
|
|
|
require_dependency 'notification_levels'
|
|
|
|
|
2013-05-24 10:48:32 +08:00
|
|
|
class GroupUser < ActiveRecord::Base
|
|
|
|
belongs_to :group, counter_cache: "user_count"
|
|
|
|
belongs_to :user
|
2015-04-10 10:17:28 +08:00
|
|
|
|
|
|
|
after_save :update_title
|
|
|
|
after_destroy :remove_title
|
|
|
|
|
|
|
|
after_save :set_primary_group
|
2017-11-24 01:58:46 +08:00
|
|
|
after_destroy :remove_primary_group, :recalculate_trust_level
|
2015-04-10 10:17:28 +08:00
|
|
|
|
2017-04-21 03:47:25 +08:00
|
|
|
before_create :set_notification_level
|
2015-09-02 04:52:05 +08:00
|
|
|
after_save :grant_trust_level
|
|
|
|
|
2016-07-07 03:56:40 +08:00
|
|
|
def self.notification_levels
|
|
|
|
NotificationLevels.all
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:17:28 +08:00
|
|
|
protected
|
|
|
|
|
2017-04-21 03:47:25 +08:00
|
|
|
def set_notification_level
|
|
|
|
self.notification_level = group&.default_notification_level || 3
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:17:28 +08:00
|
|
|
def set_primary_group
|
|
|
|
if group.primary_group
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("
|
2017-07-28 09:20:09 +08:00
|
|
|
UPDATE users
|
|
|
|
SET primary_group_id = :id
|
|
|
|
WHERE id = :user_id",
|
|
|
|
id: group.id, user_id: user_id
|
|
|
|
)
|
2015-04-10 10:17:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_primary_group
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("
|
2017-07-28 09:20:09 +08:00
|
|
|
UPDATE users
|
|
|
|
SET primary_group_id = NULL
|
|
|
|
WHERE id = :user_id AND primary_group_id = :id",
|
|
|
|
id: group.id, user_id: user_id
|
|
|
|
)
|
2015-04-10 10:17:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_title
|
|
|
|
if group.title.present?
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("
|
2017-07-28 09:20:09 +08:00
|
|
|
UPDATE users SET title = NULL
|
|
|
|
WHERE title = :title AND id = :id",
|
|
|
|
id: user_id, title: group.title
|
|
|
|
)
|
2015-04-10 10:17:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_title
|
|
|
|
if group.title.present?
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("
|
2017-07-28 09:20:09 +08:00
|
|
|
UPDATE users SET title = :title
|
|
|
|
WHERE (title IS NULL OR title = '') AND id = :id",
|
|
|
|
id: user_id, title: group.title
|
|
|
|
)
|
2015-04-10 10:17:28 +08:00
|
|
|
end
|
|
|
|
end
|
2015-09-02 04:52:05 +08:00
|
|
|
|
|
|
|
def grant_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
2017-11-29 08:01:13 +08:00
|
|
|
|
2017-11-24 04:55:44 +08:00
|
|
|
if (user.group_locked_trust_level || 0) < group.grant_trust_level
|
2017-11-29 08:01:13 +08:00
|
|
|
user.update!(group_locked_trust_level: group.grant_trust_level)
|
2017-11-24 04:55:44 +08:00
|
|
|
end
|
2017-11-29 08:01:13 +08:00
|
|
|
|
2017-03-03 02:16:01 +08:00
|
|
|
TrustLevelGranter.grant(group.grant_trust_level, user)
|
2015-09-02 04:52:05 +08:00
|
|
|
end
|
2017-11-24 01:58:46 +08:00
|
|
|
|
|
|
|
def recalculate_trust_level
|
|
|
|
return if group.grant_trust_level.nil?
|
|
|
|
|
|
|
|
# Find the highest level of the user's remaining groups
|
|
|
|
highest_level = GroupUser
|
|
|
|
.where(user_id: user.id)
|
|
|
|
.includes(:group)
|
|
|
|
.maximum("groups.grant_trust_level")
|
|
|
|
|
2018-04-10 14:19:08 +08:00
|
|
|
user.update!(group_locked_trust_level: highest_level)
|
|
|
|
Promotion.recalculate(user)
|
2017-11-24 01:58:46 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 10:48:32 +08:00
|
|
|
end
|
|
|
|
|
2013-05-24 10:35:14 +08:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: group_users
|
|
|
|
#
|
2016-01-11 14:30:56 +08: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 18:56:25 +08:00
|
|
|
# notification_level :integer default(2), not null
|
2013-05-24 10:35:14 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_group_users_on_group_id_and_user_id (group_id,user_id) UNIQUE
|
2015-09-18 08:43:53 +08:00
|
|
|
# index_group_users_on_user_id_and_group_id (user_id,group_id) UNIQUE
|
2013-05-24 10:35:14 +08:00
|
|
|
#
|