FIX: Don't update `User#last_seen_at` when PG is in readonly.

FIX: Don’t update User#last_seen_at when PG is in readonly.

diff --git a/lib/auth/default_current_user_provider.rb b/lib/auth/default_current_user_provider.rb
index a01e70f..7f1bf24 100644
--- a/lib/auth/default_current_user_provider.rb
+++ b/lib/auth/default_current_user_provider.rb
@@ -241,6 +241,8 @@ class Auth::DefaultCurrentUserProvider
   end
 
   def should_update_last_seen?
+    return false if Discourse.pg_readonly_mode?
+
     if @request.xhr?
       @env["HTTP_DISCOURSE_VISIBLE".freeze] == "true".freeze
     elsif !!(@env[API_KEY_ENV]) || !!(@env[USER_API_KEY_ENV])
diff --git a/lib/discourse.rb b/lib/discourse.rb
index cd0858f..bc382d7 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -355,6 +355,10 @@ module Discourse
     recently_readonly? || $redis.mget(*keys).compact.present?
   end
 
+  def self.pg_readonly_mode?
+    $redis.get(PG_READONLY_MODE_KEY).present?
+  end
+
   def self.last_read_only
     @last_read_only ||= DistributedCache.new('last_read_only', namespace: false)
   end
diff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rb
index 1c2fec8..33a5fa1 100644
--- a/lib/middleware/request_tracker.rb
+++ b/lib/middleware/request_tracker.rb
@@ -72,7 +72,7 @@ class Middleware::RequestTracker
 
   def self.log_request_on_site(data, host)
     RailsMultisite::ConnectionManagement.with_hostname(host) do
-      unless Discourse.readonly_mode?([Discourse::PG_READONLY_MODE_KEY])
+      unless Discourse.pg_readonly_mode?
         log_request(data)
       end
     end
diff --git a/spec/components/auth/default_current_user_provider_spec.rb b/spec/components/auth/default_current_user_provider_spec.rb
index a141b15..d4ba090 100644
--- a/spec/components/auth/default_current_user_provider_spec.rb
+++ b/spec/components/auth/default_current_user_provider_spec.rb
@@ -160,18 +160,27 @@ describe Auth::DefaultCurrentUserProvider do
           ).should_update_last_seen?).to eq(false)
   end
 
-  it "should not update last seen for suspended users" do
-    user = Fabricate(:user)
-    provider = provider('/')
-    cookies = {}
-    provider.log_on_user(user, {}, cookies)
-    unhashed_token = cookies["_t"][:value]
+  describe "#current_user" do
+    let(:unhashed_token) do
+      provider = provider('/')
+      cookies = {}
+      provider.log_on_user(Fabricate(:user), {}, cookies)
+      cookies["_t"][:value]
+    end
+
+    after do
+      $redis.flushall
+    end
+
+    it "should not update last seen for suspended users" do
+      user = Fabricate(:user)
+      provider = provider('/')
+      cookies = {}
+      provider.log_on_user(user, {}, cookies)
+      unhashed_token = cookies["_t"][:value]
+
+      freeze_time
 
-    freeze_time
-    Sidekiq::Testing.inline! do
-      # Need to clear this key from redis, otherwise
-      # this test could fail if run twice in 1 minute
-      $redis.del("user:#{user.id}:#{Time.now.to_date}")
       provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
       u = provider2.current_user
       u.reload
@@ -191,6 +200,22 @@ describe Auth::DefaultCurrentUserProvider do
       expect(u.last_seen_at).to eq(nil)
     end
 
+    describe "when readonly mode is enabled due to postgres" do
+      before do
+        Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+      end
+
+      after do
+        Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
+      end
+
+      it "should not update last seen at" do
+        provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
+        u = provider2.current_user
+        u.reload
+        expect(u.last_seen_at).to eq(nil)
+      end
+    end
   end
 
   it "should update ajax reqs with discourse visible" do

GitHub sha: c732ae9c