correct unread resetting to handle nulls

correct unread resetting to handle nulls

Note, to avoid race conditions we are setting last_unread to 10 minutes ago if there is nothing unread.

This is safer in case of in progress transactions we don’t want to lose unread for any window of time.

diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb
index 48940f9..e56ed2d 100644
--- a/app/models/user_stat.rb
+++ b/app/models/user_stat.rb
@@ -11,9 +11,9 @@ class UserStat < ActiveRecord::Base
   end
 
   def self.update_first_unread(last_seen, limit: 10_000)
-    DB.exec(<<~SQL, min_date: last_seen, limit: limit)
+    DB.exec(<<~SQL, min_date: last_seen, limit: limit, now: 10.minutes.ago)
       UPDATE user_stats us
-      SET first_unread_at = Y.min_date
+      SET first_unread_at = COALESCE(Y.min_date, :now)
       FROM (
         SELECT u1.id user_id,
            X.min min_date

GitHub sha: 5b837f62