2013-05-24 02:06:38 -04:00
|
|
|
class TopicNotifier
|
|
|
|
def initialize(topic)
|
|
|
|
@topic = topic
|
|
|
|
end
|
|
|
|
|
|
|
|
{ :watch! => :watching,
|
|
|
|
:tracking! => :tracking,
|
|
|
|
:regular! => :regular,
|
|
|
|
:muted! => :muted }.each_pair do |method_name, level|
|
|
|
|
|
|
|
|
define_method method_name do |user_id|
|
|
|
|
change_level user_id, level
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-01-22 01:46:52 -05:00
|
|
|
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)
|
2014-05-06 09:41:59 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def change_level(user_id, level, reason=nil)
|
|
|
|
attrs = {notification_level: levels[level]}
|
|
|
|
attrs.merge!(notifications_reason_id: TopicUser.notification_reasons[reason]) if reason
|
|
|
|
TopicUser.change(user_id, @topic.id, attrs)
|
|
|
|
end
|
|
|
|
end
|