Skip to content

A platform for community discussion. Free, open, simple.

FIX: correctly compute the window for email summaries

Régis Hanol5f6b6e9818eb
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 724d9172afd..0f33c4021ab 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -224,7 +224,8 @@ class UserNotifications < ActionMailer::Base
build_summary_for(user)
@unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE)
- @since = opts[:since] || user.last_seen_at || user.user_stat&.digest_attempted_at || 1.month.ago
+ @since = opts[:since].presence
+ @since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max
# Fetch some topics and posts to show
digest_opts = {
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index adc4f2137b2..5ab50623a9c 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -450,6 +450,29 @@ RSpec.describe UserNotifications do
expect(html).to match(' lang="pl-PL"')
expect(html).to match(' xml:lang="pl-PL"')
end
+
+ it "uses digest_attempted_at when user hasn't been seen in a while" do
+ user.update!(last_seen_at: 7.days.ago)
+ user.user_stat.update!(digest_attempted_at: 30.minutes.ago)
+ expect(email.to).to be_nil
+ end
+
+ it "uses last_seen_at when user has been sent a digest in a while" do
+ user.update!(last_seen_at: 30.minutes.ago)
+ user.user_stat.update!(digest_attempted_at: 7.days.ago)
+ expect(email.to).to be_nil
+ end
+
+ it "caps at 1 month when user has never been seen or sent a digest" do
+ old_topic = Fabricate(:topic, created_at: 2.months.ago)
+
+ user.update!(last_seen_at: nil)
+ user.user_stat.update!(digest_attempted_at: nil)
+ expect(email.to).to contain_exactly(user.email)
+
+ html = email.html_part.body.to_s
+ expect(html).not_to include(old_topic.title)
+ end
end
end