From fa6aa7f62779aa5a8fb801a2403cd28c38f7ef9e Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Mon, 1 Jun 2020 17:39:16 -0400 Subject: [PATCH] FIX: Digest frequency issue on user creation If `default email digest frequency` was set to "Never", users would get a `digest_after_minutes` set to `nil` which triggered this error in the logs if/when the site eventually changed that setting and enabled digests: ``` NoMethodError (undefined method `>=' for nil:NilClass) /var/www/discourse/app/mailers/user_notifications.rb:227:in `digest' ``` --- app/mailers/user_notifications.rb | 2 +- app/models/user_option.rb | 3 ++- spec/models/user_option_spec.rb | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index d1e883f4f4a..6ccb86c8602 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -224,7 +224,7 @@ class UserNotifications < ActionMailer::Base @counts << { label_key: 'user_notifications.digest.liked_received', value: value, href: "#{Discourse.base_url}/my/notifications" } if value > 0 end - if @counts.size < 3 && user.user_option.digest_after_minutes >= 1440 + if @counts.size < 3 && user.user_option.digest_after_minutes.to_i >= 1440 value = summary_new_users_count(min_date) @counts << { label_key: 'user_notifications.digest.new_users', value: value, href: "#{Discourse.base_url}/about" } if value > 0 end diff --git a/app/models/user_option.rb b/app/models/user_option.rb index 10fd562c6ce..17bd684906d 100644 --- a/app/models/user_option.rb +++ b/app/models/user_option.rb @@ -68,9 +68,10 @@ class UserOption < ActiveRecord::Base self.email_digests = false else self.email_digests = true - self.digest_after_minutes ||= SiteSetting.default_email_digest_frequency.to_i end + self.digest_after_minutes ||= SiteSetting.default_email_digest_frequency.to_i + self.include_tl0_in_digests = SiteSetting.default_include_tl0_in_digests self.text_size = SiteSetting.default_text_size diff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rb index 30112859a68..715b1e67cc8 100644 --- a/spec/models/user_option_spec.rb +++ b/spec/models/user_option_spec.rb @@ -33,6 +33,20 @@ describe UserOption do it "should not hide the profile and presence by default" do expect(user.user_option.hide_profile_and_presence).to eq(false) end + + it "should correctly set digest frequency" do + SiteSetting.default_email_digest_frequency = 1440 + user = Fabricate(:user) + expect(user.user_option.email_digests).to eq(true) + expect(user.user_option.digest_after_minutes).to eq(1440) + end + + it "should correctly set digest frequency when disabled" do + SiteSetting.default_email_digest_frequency = 0 + user = Fabricate(:user) + expect(user.user_option.email_digests).to eq(false) + expect(user.user_option.digest_after_minutes).to eq(0) + end end describe "site settings" do