SPEC: make sure digest doesn't pick any topics in categories that are muted

This commit is contained in:
Régis Hanol 2014-11-03 16:57:50 +01:00
parent a7fa1b33e5
commit fd5677808c
4 changed files with 34 additions and 32 deletions

View File

@ -99,7 +99,7 @@ class CategoriesController < ApplicationController
category_id = params[:category_id].to_i category_id = params[:category_id].to_i
notification_level = params[:notification_level].to_i notification_level = params[:notification_level].to_i
CategoryUser.set_notification_level_for_category(current_user, notification_level , category_id) CategoryUser.set_notification_level_for_category(current_user, notification_level, category_id)
render json: success_json render json: success_json
end end

View File

@ -250,9 +250,7 @@ class UserNotifications < ActionMailer::Base
) )
template = "user_notifications.user_#{notification_type}" template = "user_notifications.user_#{notification_type}"
if post.topic.private_message? template << "_pm" if post.topic.private_message?
template << "_pm"
end
email_opts = { email_opts = {
topic_title: title, topic_title: title,

View File

@ -16,19 +16,17 @@ class CategoryUser < ActiveRecord::Base
end end
def self.auto_track_new_topic(topic) def self.auto_track_new_topic(topic)
apply_default_to_topic( apply_default_to_topic(topic,
topic, TopicUser.notification_levels[:tracking],
TopicUser.notification_levels[:tracking], TopicUser.notification_reasons[:auto_track_category]
TopicUser.notification_reasons[:auto_track_category] )
)
end end
def self.auto_watch_new_topic(topic) def self.auto_watch_new_topic(topic)
apply_default_to_topic( apply_default_to_topic(topic,
topic, TopicUser.notification_levels[:watching],
TopicUser.notification_levels[:watching], TopicUser.notification_reasons[:auto_watch_category]
TopicUser.notification_reasons[:auto_watch_category] )
)
end end
def self.batch_set(user, level, category_ids) def self.batch_set(user, level, category_ids)
@ -49,8 +47,6 @@ class CategoryUser < ActiveRecord::Base
def self.set_notification_level_for_category(user, level, category_id) def self.set_notification_level_for_category(user, level, category_id)
record = CategoryUser.where(user: user, category_id: category_id).first record = CategoryUser.where(user: user, category_id: category_id).first
# oder CategoryUser.where(user: user, category_id: category_id).destroy_all
# und danach mir create anlegen.
if record.present? if record.present?
record.notification_level = level record.notification_level = level
@ -62,23 +58,21 @@ class CategoryUser < ActiveRecord::Base
def self.apply_default_to_topic(topic, level, reason) def self.apply_default_to_topic(topic, level, reason)
# Can not afford to slow down creation of topics when a pile of users are watching new topics, reverting to SQL for max perf here # Can not afford to slow down creation of topics when a pile of users are watching new topics, reverting to SQL for max perf here
sql = <<SQL sql = <<-SQL
INSERT INTO topic_users(user_id, topic_id, notification_level, notifications_reason_id) INSERT INTO topic_users(user_id, topic_id, notification_level, notifications_reason_id)
SELECT user_id, :topic_id, :level, :reason SELECT user_id, :topic_id, :level, :reason
FROM category_users FROM category_users
WHERE notification_level = :level AND WHERE notification_level = :level
category_id = :category_id AND AND category_id = :category_id
NOT EXISTS(SELECT 1 FROM topic_users WHERE topic_id = :topic_id AND user_id = category_users.user_id) AND NOT EXISTS(SELECT 1 FROM topic_users WHERE topic_id = :topic_id AND user_id = category_users.user_id)
SQL SQL
exec_sql( exec_sql(sql,
sql, topic_id: topic.id,
topic_id: topic.id, category_id: topic.category_id,
category_id: topic.category_id, level: level,
level: level, reason: reason
reason: reason )
)
end end
private_class_method :apply_default_to_topic private_class_method :apply_default_to_topic

View File

@ -1181,6 +1181,16 @@ describe Topic do
Topic.for_digest(user, 1.year.ago, top_order: true).should == [topic] Topic.for_digest(user, 1.year.ago, top_order: true).should == [topic]
end end
it "doesn't return topics from muted categories" do
user = Fabricate(:user)
category = Fabricate(:category)
topic = Fabricate(:topic, category: category)
CategoryUser.set_notification_level_for_category(user, CategoryUser.notification_levels[:muted], category.id)
Topic.for_digest(user, 1.year.ago, top_order: true).should be_blank
end
end end
describe 'secured' do describe 'secured' do