mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 07:49:48 +08:00
FEATURE: Custom unsubscribe options (#17090)
With this change, plugins can create custom unsubscribe keys, extend the unsubscribe view with custom preferences, and decide how they are updated.
This commit is contained in:
62
lib/email_controller_helper/topic_email_unsubscriber.rb
Normal file
62
lib/email_controller_helper/topic_email_unsubscriber.rb
Normal file
@ -0,0 +1,62 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module EmailControllerHelper
|
||||
class TopicEmailUnsubscriber < BaseEmailUnsubscriber
|
||||
def prepare_unsubscribe_options(controller)
|
||||
super(controller)
|
||||
watching = TopicUser.notification_levels[:watching]
|
||||
|
||||
topic = unsubscribe_key.associated_topic
|
||||
|
||||
controller.instance_variable_set(:@topic, topic)
|
||||
controller.instance_variable_set(
|
||||
:@watching_topic,
|
||||
TopicUser.exists?(user: key_owner, notification_level: watching, topic_id: topic.id)
|
||||
)
|
||||
|
||||
return if topic.category_id.blank?
|
||||
return if !CategoryUser.exists?(user: key_owner, notification_level: CategoryUser.watching_levels, category_id: topic.category_id)
|
||||
|
||||
controller.instance_variable_set(
|
||||
:@watched_count,
|
||||
TopicUser.joins(:topic)
|
||||
.where(user: key_owner, notification_level: watching).where(topics: { category_id: topic.category_id }).count
|
||||
)
|
||||
end
|
||||
|
||||
def unsubscribe(params)
|
||||
updated = super(params)
|
||||
|
||||
topic = unsubscribe_key.associated_topic
|
||||
return updated if topic.nil?
|
||||
|
||||
if params[:unwatch_topic]
|
||||
TopicUser.where(topic_id: topic.id, user_id: key_owner.id)
|
||||
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
||||
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params[:unwatch_category] && topic.category_id
|
||||
TopicUser.joins(:topic)
|
||||
.where(user: key_owner, notification_level: TopicUser.notification_levels[:watching])
|
||||
.where(topics: { category_id: topic.category_id })
|
||||
.update_all(notification_level: TopicUser.notification_levels[:tracking])
|
||||
|
||||
CategoryUser
|
||||
.where(user_id: key_owner.id, category_id: topic.category_id, notification_level: CategoryUser.watching_levels)
|
||||
.destroy_all
|
||||
|
||||
updated = true
|
||||
end
|
||||
|
||||
if params[:mute_topic]
|
||||
TopicUser.where(topic_id: topic.id, user_id: key_owner.id).update_all(notification_level: TopicUser.notification_levels[:muted])
|
||||
|
||||
updated = true
|
||||
end
|
||||
|
||||
updated
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user