Use "Top" logic for digest emails. Leave out muted categories.

This commit is contained in:
Robin Ward 2014-04-17 15:14:54 -04:00
parent 2341118570
commit 89aa2380c6
2 changed files with 13 additions and 3 deletions

View file

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

View file

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