2022-06-21 13:49:47 -05:00
|
|
|
# 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
|
|
|
|
|
2023-02-16 04:47:01 -06:00
|
|
|
return if topic.blank?
|
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
controller.instance_variable_set(:@topic, topic)
|
|
|
|
controller.instance_variable_set(
|
|
|
|
:@watching_topic,
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicUser.exists?(user: key_owner, notification_level: watching, topic_id: topic.id),
|
2022-06-21 13:49:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return if topic.category_id.blank?
|
2023-01-09 06:10:19 -06:00
|
|
|
if !CategoryUser.exists?(
|
|
|
|
user: key_owner,
|
|
|
|
notification_level: CategoryUser.watching_levels,
|
|
|
|
category_id: topic.category_id,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
2022-06-21 13:49:47 -05:00
|
|
|
|
|
|
|
controller.instance_variable_set(
|
|
|
|
:@watched_count,
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicUser
|
|
|
|
.joins(:topic)
|
|
|
|
.where(user: key_owner, notification_level: watching)
|
|
|
|
.where(topics: { category_id: topic.category_id })
|
|
|
|
.count,
|
2022-06-21 13:49:47 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribe(params)
|
|
|
|
updated = super(params)
|
|
|
|
|
|
|
|
topic = unsubscribe_key.associated_topic
|
|
|
|
return updated if topic.nil?
|
|
|
|
|
|
|
|
if params[:unwatch_topic]
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicUser.where(topic_id: topic.id, user_id: key_owner.id).update_all(
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
2022-06-21 13:49:47 -05:00
|
|
|
|
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:unwatch_category] && topic.category_id
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicUser
|
|
|
|
.joins(:topic)
|
2022-06-21 13:49:47 -05:00
|
|
|
.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])
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
CategoryUser.where(
|
|
|
|
user_id: key_owner.id,
|
|
|
|
category_id: topic.category_id,
|
|
|
|
notification_level: CategoryUser.watching_levels,
|
|
|
|
).destroy_all
|
2022-06-21 13:49:47 -05:00
|
|
|
|
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if params[:mute_topic]
|
2023-01-09 06:10:19 -06:00
|
|
|
TopicUser.where(topic_id: topic.id, user_id: key_owner.id).update_all(
|
|
|
|
notification_level: TopicUser.notification_levels[:muted],
|
|
|
|
)
|
2022-06-21 13:49:47 -05:00
|
|
|
|
|
|
|
updated = true
|
|
|
|
end
|
|
|
|
|
|
|
|
updated
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|