discourse/app/models/topic_notifier.rb

45 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-05-24 02:06:38 -04:00
class TopicNotifier
def initialize(topic)
@topic = topic
end
2017-07-27 21:20:09 -04:00
{ watch!: :watching,
track!: :tracking,
regular!: :regular,
mute!: :muted }.each_pair do |method_name, level|
2013-05-24 02:06:38 -04:00
define_method method_name do |user_id|
change_level user_id, level
end
end
def watch_topic!(user_id, reason = :created_topic)
change_level user_id, :watching, reason
2013-05-24 02:06:38 -04:00
end
# Enable/disable the mute on the topic
def toggle_mute(user_id)
change_level user_id, (muted?(user_id) ? levels[:regular] : levels[:muted])
end
def muted?(user_id)
tu = @topic.topic_users.find_by(user_id: user_id)
2013-05-24 02:06:38 -04:00
tu && tu.notification_level == levels[:muted]
end
private
def levels
@notification_levels ||= TopicUser.notification_levels
end
2017-07-27 21:20:09 -04:00
def change_level(user_id, level, reason = nil)
attrs = { notification_level: levels[level] }
2013-05-24 02:06:38 -04:00
attrs.merge!(notifications_reason_id: TopicUser.notification_reasons[reason]) if reason
TopicUser.change(user_id, @topic.id, attrs)
end
end