forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract TopicNotifier class from topic
- Loading branch information
1 parent
247a0b3
commit d7817cf
Showing
4 changed files
with
69 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters