FEATURE: mark last notification unread when removing timings

FEATURE: mark last notification unread when removing timings

This calls a bit more attention to the deferred topic by amending the “read” state of the last notification on the topic.

diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 00517b9..5b13b17 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -254,10 +254,25 @@ class TopicsController < ApplicationController
   end
 
   def destroy_timings
+    topic_id = params[:topic_id].to_i
+
     if params[:last].to_s == "1"
-      PostTiming.destroy_last_for(current_user, params[:topic_id])
+      PostTiming.destroy_last_for(current_user, topic_id)
     else
-      PostTiming.destroy_for(current_user.id, [params[:topic_id].to_i])
+      PostTiming.destroy_for(current_user.id, [topic_id])
+    end
+
+    last_notification = Notification
+      .where(
+        user_id: current_user.id,
+        topic_id: topic_id
+      )
+      .order(created_at: :desc)
+      .limit(1)
+      .first
+
+    if last_notification
+      last_notification.update!(read: false)
     end
 
     render body: nil
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 6cbec67..8269523 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -726,22 +726,42 @@ RSpec.describe TopicsController do
         freeze_time
 
         post1 = create_post
+        user = post1.user
+
         topic = post1.topic
 
         post2 = create_post(topic_id: topic.id)
 
-        PostTiming.create!(topic: topic, user: user, post_number: 1, msecs: 100)
         PostTiming.create!(topic: topic, user: user, post_number: 2, msecs: 100)
 
         user.user_stat.update!(first_unread_at: Time.now + 1.week)
 
-        TopicUser.create!(
-          topic: topic,
-          user: user,
+        topic_user = TopicUser.find_by(
+          topic_id: topic.id,
+          user_id: user.id,
+        )
+
+        topic_user.update!(
           last_read_post_number: 2,
           highest_seen_post_number: 2
         )
 
+        # ensure we have 2 notifications
+        # fake notification on topic but it is read
+        first_notification = Notification.create!(
+          user_id: user.id,
+          topic_id: topic.id,
+          data: "{}",
+          read: true,
+          notification_type: 1
+        )
+
+        freeze_time 1.minute.from_now
+        PostAlerter.post_created(post2)
+
+        second_notification = user.notifications.where(topic_id: topic.id).order(created_at: :desc).first
+        second_notification.update!(read: true)
+
         sign_in(user)
 
         delete "/t/#{topic.id}/timings.json?last=1"
@@ -754,6 +774,11 @@ RSpec.describe TopicsController do
         user.user_stat.reload
         expect(user.user_stat.first_unread_at).to eq_time(topic.updated_at)
 
+        first_notification.reload
+        second_notification.reload
+        expect(first_notification.read).to eq(true)
+        expect(second_notification.read).to eq(false)
+
         PostDestroyer.new(Fabricate(:admin), post2).destroy
 
         delete "/t/#{topic.id}/timings.json?last=1"

GitHub sha: f6db30a5

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: