FIX: Digest frequency issue on user creation

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'
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index d1e883f..6ccb86c 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 10fd562..17bd684 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 3011285..715b1e6 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

GitHub sha: fa6aa7f6

1 Like