From ed6bdfe8cbe5504bfb124cdee080907ef16fd96f Mon Sep 17 00:00:00 2001
From: Karina Kwiatek <kjk@kjk.dog>
Date: Sun, 7 May 2023 20:39:09 +0200
Subject: [PATCH] Touch `inbox_updated_at` after marking entries as read

---
 app/controllers/inbox_controller.rb       |  5 ++++-
 spec/controllers/inbox_controller_spec.rb | 14 ++++++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/app/controllers/inbox_controller.rb b/app/controllers/inbox_controller.rb
index f6a1aff2..661d49f4 100644
--- a/app/controllers/inbox_controller.rb
+++ b/app/controllers/inbox_controller.rb
@@ -82,10 +82,13 @@ class InboxController < ApplicationController
       .where(questions: { user: @author_user, author_is_anonymous: false })
   end
 
+  # rubocop:disable Rails/SkipsModelValidations
   def mark_inbox_entries_as_read
     # using .dup to not modify @inbox -- useful in tests
-    @inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
+    @inbox&.dup&.update_all(new: false)
+    current_user.touch(:inbox_updated_at)
   end
+  # rubocop:enable Rails/SkipsModelValidations
 
   def increment_metric
     Retrospring::Metrics::QUESTIONS_ASKED.increment(
diff --git a/spec/controllers/inbox_controller_spec.rb b/spec/controllers/inbox_controller_spec.rb
index f2ec2c9f..c09663ba 100644
--- a/spec/controllers/inbox_controller_spec.rb
+++ b/spec/controllers/inbox_controller_spec.rb
@@ -3,7 +3,10 @@
 require "rails_helper"
 
 describe InboxController, type: :controller do
-  let(:user) { FactoryBot.create(:user) }
+  include ActiveSupport::Testing::TimeHelpers
+
+  let(:original_inbox_updated_at) { 1.day.ago }
+  let(:user) { FactoryBot.create(:user, inbox_updated_at: original_inbox_updated_at) }
 
   describe "#show" do
     shared_examples_for "sets the expected ivars" do
@@ -53,7 +56,7 @@ describe InboxController, type: :controller do
               more_data_available: false,
               inbox_count:         1,
               delete_id:           "ib-delete-all",
-              disabled:            nil
+              disabled:            nil,
             }
           end
         end
@@ -62,6 +65,13 @@ describe InboxController, type: :controller do
           expect { subject }.to change { inbox_entry.reload.new? }.from(true).to(false)
         end
 
+        it "updates the the timestamp used for caching" do
+          user.update(inbox_updated_at: original_inbox_updated_at)
+          travel 1.second do
+            expect { subject }.to change { user.reload.inbox_updated_at }.from(original_inbox_updated_at).to(Time.now.utc)
+          end
+        end
+
         context "when requested the turbo stream format" do
           subject { get :show, format: :turbo_stream }