From 89aa2380c68434d76d5dd837aeb1b327171d0a8d Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 17 Apr 2014 15:14:54 -0400 Subject: [PATCH] Use "Top" logic for digest emails. Leave out muted categories. --- app/models/topic.rb | 14 ++++++++++++-- lib/topic_query_sql.rb | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index e145e41da..c7369cf80 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -261,18 +261,28 @@ class Topic < ActiveRecord::Base # Returns hot topics since a date for display in email digest. def self.for_digest(user, since) + score = "#{ListController.best_period_for(since)}_score" + topics = Topic .visible .secured(Guardian.new(user)) .where(closed: false, archived: false) .created_since(since) .listable_topics - .order(:percent_rank) + .joins("LEFT OUTER JOIN top_topics ON top_topics.topic_id = topics.id") + .order(TopicQuerySQL.order_top_for(score)) .limit(100) + # Remove category topics category_topic_ids = Category.pluck(:topic_id).compact! if category_topic_ids.present? - topics = topics.where("id NOT IN (?)", category_topic_ids) + topics = topics.where("topics.id NOT IN (?)", category_topic_ids) + end + + # Remove muted categories + muted_category_ids = CategoryUser.where(user_id: user.id, notification_level: CategoryUser.notification_levels[:muted]).pluck(:category_id) + if muted_category_ids.present? + topics = topics.where("topics.category_id NOT IN (?)", muted_category_ids) end topics diff --git a/lib/topic_query_sql.rb b/lib/topic_query_sql.rb index 0df8c3ce7..1256971c0 100644 --- a/lib/topic_query_sql.rb +++ b/lib/topic_query_sql.rb @@ -46,7 +46,7 @@ module TopicQuerySQL end def order_top_for(score) - "top_topics.#{score} DESC, topics.bumped_at DESC" + "COALESCE(top_topics.#{score}, 0) DESC, topics.bumped_at DESC" end def order_top_with_pinned_category_for(score)