diff --git a/app/models/topic.rb b/app/models/topic.rb index 20745168bb5..93365f66994 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -325,6 +325,7 @@ class Topic < ActiveRecord::Base .visible .secured(Guardian.new(user)) .joins("LEFT OUTER JOIN topic_users ON topic_users.topic_id = topics.id AND topic_users.user_id = #{user.id.to_i}") + .joins("LEFT OUTER JOIN category_users ON category_users.category_id = topics.category_id AND category_users.user_id = #{user.id.to_i}") .joins("LEFT OUTER JOIN users ON users.id = topics.user_id") .where(closed: false, archived: false) .where("COALESCE(topic_users.notification_level, 1) <> ?", TopicUser.notification_levels[:muted]) @@ -338,7 +339,7 @@ class Topic < ActiveRecord::Base if !!opts[:top_order] topics = topics.joins("LEFT OUTER JOIN top_topics ON top_topics.topic_id = topics.id") - .order(TopicQuerySQL.order_top_for(score)) + .order(TopicQuerySQL.order_top_with_notification_levels(score)) end if opts[:limit] diff --git a/lib/topic_query_sql.rb b/lib/topic_query_sql.rb index 9decf581a6c..003c0000c6a 100644 --- a/lib/topic_query_sql.rb +++ b/lib/topic_query_sql.rb @@ -51,5 +51,9 @@ module TopicQuerySQL topics.bumped_at DESC" end + def order_top_with_notification_levels(score) + "COALESCE(category_users.notification_level, 1) DESC, COALESCE(top_topics.#{score}, 0) DESC, topics.bumped_at DESC" + end + end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 9bac6b9344d..1869cb233ae 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1382,6 +1382,16 @@ describe Topic do expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to eq([topic]) end + it "sorts by category notification levels" do + category1, category2 = Fabricate(:category), Fabricate(:category) + 2.times {|i| Fabricate(:topic, category: category1) } + topic1 = Fabricate(:topic, category: category2) + 2.times {|i| Fabricate(:topic, category: category1) } + CategoryUser.create(user: user, category: category2, notification_level: CategoryUser.notification_levels[:watching]) + for_digest = Topic.for_digest(user, 1.year.ago, top_order: true) + expect(for_digest.first).to eq(topic1) + end + end describe 'secured' do