From dc0266cc22e16b3af28978dcc761c67df8bd624b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 18 Jun 2014 11:23:31 +1000 Subject: [PATCH] FEATURE: correct muted category implementation - Don't change tracking state on muted categories - Exclude muted sub categories from parent --- app/models/category_user.rb | 9 ---- .../20140618001820_dont_auto_muto_topics.rb | 10 +++++ lib/topic_creator.rb | 5 --- lib/topic_query.rb | 43 +++++++++++-------- spec/models/category_user_spec.rb | 3 -- 5 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 db/migrate/20140618001820_dont_auto_muto_topics.rb diff --git a/app/models/category_user.rb b/app/models/category_user.rb index 914a0c8e5..830787caf 100644 --- a/app/models/category_user.rb +++ b/app/models/category_user.rb @@ -60,15 +60,6 @@ class CategoryUser < ActiveRecord::Base end end - - def self.auto_mute_new_topic(topic) - apply_default_to_topic( - topic, - TopicUser.notification_levels[:muted], - TopicUser.notification_reasons[:auto_mute_category] - ) - end - private def self.apply_default_to_topic(topic, level, reason) diff --git a/db/migrate/20140618001820_dont_auto_muto_topics.rb b/db/migrate/20140618001820_dont_auto_muto_topics.rb new file mode 100644 index 000000000..f91070a39 --- /dev/null +++ b/db/migrate/20140618001820_dont_auto_muto_topics.rb @@ -0,0 +1,10 @@ +class DontAutoMutoTopics < ActiveRecord::Migration + def change + # muting all new topics was a mistake, revert it + execute 'DELETE FROM topic_users WHERE notification_level = 0 and notifications_reason_id =7 AND first_visited_at IS NULL' + + execute 'UPDATE topic_users SET notification_level = 1, + notifications_reason_id = NULL + WHERE notification_level = 0 AND notifications_reason_id =7' + end +end diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index fe17b6daf..8ec4254f2 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -19,17 +19,12 @@ class TopicCreator process_private_message save_topic watch_topic - auto_mute_topic @topic end private - def auto_mute_topic - CategoryUser.auto_mute_new_topic(@topic) - end - def watch_topic unless @opts[:auto_track] == false @topic.notifier.watch_topic!(@topic.user_id) diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 2456c99bb..6ee936258 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -210,6 +210,13 @@ class TopicQuery result.order("topics.#{sort_column} #{sort_dir}") end + def get_category_id(category_id_or_slug) + return nil unless category_id_or_slug + category_id = category_id_or_slug.to_i + category_id = Category.where(slug: category_id_or_slug).pluck(:id).first if category_id == 0 + category_id + end + # Create results based on a bunch of default options def default_results(options={}) @@ -224,19 +231,14 @@ class TopicQuery .references('tu') end - category_id = nil - if options[:category].present? - category_id = options[:category].to_i - category_id = Category.where(slug: options[:category]).pluck(:id).first if category_id == 0 - - if category_id - if options[:no_subcategories] - result = result.where('categories.id = ?', category_id) - else - result = result.where('categories.id = ? or categories.parent_category_id = ?', category_id, category_id) - end - result = result.references(:categories) + category_id = get_category_id(options[:category]) + if category_id + if options[:no_subcategories] + result = result.where('categories.id = ?', category_id) + else + result = result.where('categories.id = ? or categories.parent_category_id = ?', category_id, category_id) end + result = result.references(:categories) end result = apply_ordering(result, options) @@ -287,18 +289,25 @@ class TopicQuery def latest_results(options={}) result = default_results(options) - result = remove_muted_categories(result, @user) unless options[:category].present? + result = remove_muted_categories(result, @user, exclude: options[:category]) result end - def remove_muted_categories(list, user) + def remove_muted_categories(list, user, opts) + category_id = get_category_id(opts[:exclude]) if opts if user list = list.where("NOT EXISTS( SELECT 1 FROM category_users cu WHERE cu.user_id = ? AND cu.category_id = topics.category_id AND - cu.notification_level = ? - )", user.id, CategoryUser.notification_levels[:muted]).references('cu') + cu.notification_level = ? AND + cu.category_id <> ? + )", + user.id, + CategoryUser.notification_levels[:muted], + category_id || -1 + ) + .references('cu') end list @@ -314,7 +323,7 @@ class TopicQuery def new_results(options={}) result = TopicQuery.new_filter(default_results(options.reverse_merge(:unordered => true)), @user.treat_as_new_topic_start_date) - result = remove_muted_categories(result, @user) unless options[:category].present? + result = remove_muted_categories(result, @user, exclude: options[:category]) suggested_ordering(result, options) end diff --git a/spec/models/category_user_spec.rb b/spec/models/category_user_spec.rb index 62a7e9f14..e1a1808d6 100644 --- a/spec/models/category_user_spec.rb +++ b/spec/models/category_user_spec.rb @@ -50,9 +50,6 @@ describe CategoryUser do tu.notification_level.should == TopicUser.notification_levels[:tracking] tu.notifications_reason_id.should == TopicUser.notification_reasons[:auto_track_category] - tu = TopicUser.get(muted_post.topic, user) - tu.notification_level.should == TopicUser.notification_levels[:muted] - tu.notifications_reason_id.should == TopicUser.notification_reasons[:auto_mute_category] end end