-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 | ||
|
||
def created_topic!(user_id) | ||
change_level user_id, :watching, :created_topic | ||
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.where(user_id: user_id).first | ||
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 |
2 comments
on commit d7817cf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm is there a concern that the toggle_mute
no longer takes a value?
I agree toggle
implies that you're swapping it with whatever was there so maybe a term like adjust_mute
would be more accurate. But previously, if a request came in for mute twice in a row, it would stay muted wouldn't it? Whereas now a second request for mute would unmute it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value was not being used anywhere, and it was (and is) a true toggle.
I thought about adding an unmute! method to go with mute!, if you think we could use it.
then it would look like:
def toggle_mute
muted?(user_id) ? unmute! : mute!
end
the parameter muted is not ever used in this method.