extract TopicNotifier class from topic

This commit is contained in:
Matt Van Horn 2013-05-23 23:06:38 -07:00
parent 247a0b3ea1
commit d7817cf314
4 changed files with 69 additions and 28 deletions

View File

@ -203,7 +203,7 @@ class TopicsController < ApplicationController
@topic = Topic.where(id: params[:topic_id].to_i).first @topic = Topic.where(id: params[:topic_id].to_i).first
guardian.ensure_can_see!(@topic) guardian.ensure_can_see!(@topic)
@topic.toggle_mute(current_user, v) @topic.toggle_mute(current_user)
render nothing: true render nothing: true
end end

View File

@ -112,9 +112,7 @@ class Topic < ActiveRecord::Base
after_create do after_create do
changed_to_category(category) changed_to_category(category)
TopicUser.change(user_id, id, notifier.created_topic! user_id
notification_level: TopicUser.notification_levels[:watching],
notifications_reason_id: TopicUser.notification_reasons[:created_topic])
if archetype == Archetype.private_message if archetype == Archetype.private_message
DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE) DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE)
else else
@ -162,10 +160,7 @@ class Topic < ActiveRecord::Base
end end
def sanitize_title def sanitize_title
if self.title.present? self.title = sanitize(title.to_s, tags: [], attributes: []).strip.presence
self.title = sanitize(title, tags: [], attributes: [])
self.title.strip!
end
end end
def new_version_required? def new_version_required?
@ -641,11 +636,6 @@ class Topic < ActiveRecord::Base
TopicUser.where('starred_at > ?', sinceDaysAgo.days.ago).group('date(starred_at)').order('date(starred_at)').count TopicUser.where('starred_at > ?', sinceDaysAgo.days.ago).group('date(starred_at)').order('date(starred_at)').count
end end
# Enable/disable the mute on the topic
def toggle_mute(user, muted)
TopicUser.change(user, self.id, notification_level: muted?(user) ? TopicUser.notification_levels[:regular] : TopicUser.notification_levels[:muted] )
end
def slug def slug
unless slug = read_attribute(:slug) unless slug = read_attribute(:slug)
return '' unless title.present? return '' unless title.present?
@ -661,17 +651,17 @@ class Topic < ActiveRecord::Base
end end
def title=(t) def title=(t)
slug = "" slug = (Slug.for(t.to_s).presence || "topic")
slug = (Slug.for(t).presence || "topic") if t.present?
write_attribute(:slug, slug) write_attribute(:slug, slug)
write_attribute(:title,t) write_attribute(:title,t)
end end
# NOTE: These are probably better off somewhere else.
# Having a model know about URLs seems a bit strange.
def last_post_url def last_post_url
"/t/#{slug}/#{id}/#{posts_count}" "/t/#{slug}/#{id}/#{posts_count}"
end end
def self.url(id, slug, post_number=nil) def self.url(id, slug, post_number=nil)
url = "#{Discourse.base_url}/t/#{slug}/#{id}" url = "#{Discourse.base_url}/t/#{slug}/#{id}"
url << "/#{post_number}" if post_number.to_i > 1 url << "/#{post_number}" if post_number.to_i > 1
@ -684,12 +674,6 @@ class Topic < ActiveRecord::Base
url url
end end
def muted?(user)
return false unless user && user.id
tu = topic_users.where(user_id: user.id).first
tu && tu.notification_level == TopicUser.notification_levels[:muted]
end
def clear_pin_for(user) def clear_pin_for(user)
return unless user.present? return unless user.present?
TopicUser.change(user.id, id, cleared_pinned_at: Time.now) TopicUser.change(user.id, id, cleared_pinned_at: Time.now)
@ -703,21 +687,36 @@ class Topic < ActiveRecord::Base
"#{Draft::EXISTING_TOPIC}#{id}" "#{Draft::EXISTING_TOPIC}#{id}"
end end
def notifier
@topic_notifier ||= TopicNotifier.new(self)
end
# notification stuff # notification stuff
def notify_watch!(user) def notify_watch!(user)
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:watching]) notifier.watch! user
end end
def notify_tracking!(user) def notify_tracking!(user)
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:tracking]) notifier.tracking! user
end end
def notify_regular!(user) def notify_regular!(user)
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:regular]) notifier.regular! user
end end
def notify_muted!(user) def notify_muted!(user)
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:muted]) notifier.muted! user
end
def muted?(user)
if user && user.id
notifier.muted?(user.id)
end
end
# Enable/disable the mute on the topic
def toggle_mute(user_id)
notifier.toggle_mute user_id
end end
def auto_close_days=(num_days) def auto_close_days=(num_days)

View File

@ -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

View File

@ -282,12 +282,12 @@ describe TopicsController do
end end
it "changes the user's starred flag when the parameter is present" do it "changes the user's starred flag when the parameter is present" do
Topic.any_instance.expects(:toggle_mute).with(@topic.user, true) Topic.any_instance.expects(:toggle_mute).with(@topic.user)
xhr :put, :mute, topic_id: @topic.id, starred: 'true' xhr :put, :mute, topic_id: @topic.id, starred: 'true'
end end
it "removes the user's starred flag when the parameter is not true" do it "removes the user's starred flag when the parameter is not true" do
Topic.any_instance.expects(:toggle_mute).with(@topic.user, false) Topic.any_instance.expects(:toggle_mute).with(@topic.user)
xhr :put, :unmute, topic_id: @topic.id, starred: 'false' xhr :put, :unmute, topic_id: @topic.id, starred: 'false'
end end