FEATURE: set notification levels when added to a group (#10378)

* FEATURE: set notification levels when added to a group

This feature allows admins and group owners to define default
category and tag tracking levels that will be applied to user
preferences automatically at the time when users are added to the
group. Users are free to change those preferences afterwards.
When removed from a group, the user's notification preferences aren't
changed.
This commit is contained in:
Neil Lalonde
2020-08-06 12:27:27 -04:00
committed by GitHub
parent cd4f251891
commit 1ca81fbb95
27 changed files with 937 additions and 8 deletions

View File

@@ -29,6 +29,8 @@ class Group < ActiveRecord::Base
has_many :group_histories, dependent: :destroy
has_many :category_reviews, class_name: 'Category', foreign_key: :reviewable_by_group_id, dependent: :nullify
has_many :reviewables, foreign_key: :reviewable_by_group_id, dependent: :nullify
has_many :group_category_notification_defaults, dependent: :destroy
has_many :group_tag_notification_defaults, dependent: :destroy
belongs_to :flair_upload, class_name: 'Upload'
@@ -51,6 +53,7 @@ class Group < ActiveRecord::Base
after_commit :trigger_group_created_event, on: :create
after_commit :trigger_group_updated_event, on: :update
after_commit :trigger_group_destroyed_event, on: :destroy
after_commit :set_default_notifications, on: [:create, :update]
def expire_cache
ApplicationSerializer.expire_cache_fragment!("group_names")
@@ -382,6 +385,13 @@ class Group < ActiveRecord::Base
end
end
def self.set_category_and_tag_default_notification_levels!(user, group_name)
if group = lookup_group(group_name)
GroupUser.set_category_notifications(group, user)
GroupUser.set_tag_notifications(group, user)
end
end
def self.refresh_automatic_group!(name)
return unless id = AUTO_GROUPS[name]
@@ -755,6 +765,32 @@ class Group < ActiveRecord::Base
flair_icon.presence || flair_upload&.short_path
end
[:muted, :tracking, :watching, :watching_first_post].each do |level|
define_method("#{level}_category_ids=") do |category_ids|
@category_notifications ||= {}
@category_notifications[level] = category_ids
end
define_method("#{level}_tags=") do |tag_names|
@tag_notifications ||= {}
@tag_notifications[level] = tag_names
end
end
def set_default_notifications
if @category_notifications
@category_notifications.each do |level, category_ids|
GroupCategoryNotificationDefault.batch_set(self, level, category_ids)
end
end
if @tag_notifications
@tag_notifications.each do |level, tag_names|
GroupTagNotificationDefault.batch_set(self, level, tag_names)
end
end
end
def imap_mailboxes
return [] if self.imap_server.blank? ||
self.email_username.blank? ||