From 74956694e51b78d9aeeb22b9a482ed8d9664a7d1 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 19 Dec 2016 14:53:53 -0500 Subject: [PATCH] If summary email finds no topics, show topics more than 1 day old from new users --- app/mailers/user_notifications.rb | 6 +++++- app/models/topic.rb | 2 +- spec/mailers/user_notifications_spec.rb | 12 ++++++++++++ spec/models/topic_spec.rb | 7 +++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 184b4e088cc..cb1e2f5ca0b 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -135,7 +135,11 @@ class UserNotifications < ActionMailer::Base end # Now fetch some topics and posts to show - topics_for_digest = Topic.for_digest(user, min_date, limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics, top_order: true).to_a + digest_opts = {limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics, top_order: true} + topics_for_digest = Topic.for_digest(user, min_date, digest_opts).to_a + if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests) + topics_for_digest = Topic.for_digest(user, min_date, digest_opts.merge(include_tl0: true)).where('topics.created_at < ?', 24.hours.ago).to_a + end @popular_topics = topics_for_digest[0,SiteSetting.digest_topics] @other_new_for_you = topics_for_digest.size > SiteSetting.digest_topics ? topics_for_digest[SiteSetting.digest_topics..-1] : [] diff --git a/app/models/topic.rb b/app/models/topic.rb index 1cb5b301296..c7bf3638105 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -338,7 +338,7 @@ class Topic < ActiveRecord::Base .listable_topics .includes(:category) - unless user.user_option.try(:include_tl0_in_digests) + unless opts[:include_tl0] || user.user_option.try(:include_tl0_in_digests) topics = topics.where("COALESCE(users.trust_level, 0) > 0") end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index a427b6d3b00..d6359818c98 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -152,6 +152,18 @@ describe UserNotifications do end + context "with topics only from new users" do + let!(:new_today) { Fabricate(:topic, user: Fabricate(:user, trust_level: TrustLevel[0], created_at: 10.minutes.ago), title: "Hey everyone look at me") } + let!(:new_yesterday) { Fabricate(:topic, user: Fabricate(:user, trust_level: TrustLevel[0], created_at: 25.hours.ago), created_at: 25.hours.ago, title: "This topic is of interest to you") } + + it "returns topics from new users if they're more than 24 hours old" do + expect(subject.to).to eq([user.email]) + html = subject.html_part.body.to_s + expect(html).to include(new_yesterday.title) + expect(html).to_not include(new_today.title) + end + end + context "with new topics" do before do diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 400b0535f85..d87009fe458 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1354,6 +1354,13 @@ describe Topic do expect(Topic.for_digest(user, 1.year.ago, top_order: true)).to be_blank end + it "returns topics from TL0 users if given include_tl0" do + new_user = Fabricate(:user, trust_level: 0) + topic = Fabricate(:topic, user_id: new_user.id) + + expect(Topic.for_digest(user, 1.year.ago, top_order: true, include_tl0: true)).to eq([topic]) + end + it "returns topics from TL0 users if enabled in preferences" do new_user = Fabricate(:user, trust_level: 0) topic = Fabricate(:topic, user_id: new_user.id)