From b805037825155d6fd64c08eeae844e6262252722 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 8 Oct 2019 15:39:23 -0300 Subject: [PATCH] FIX: Decrement posts read count when destroying post timings (#8172) --- app/models/post_timing.rb | 4 ++++ spec/models/post_timing_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/models/post_timing.rb b/app/models/post_timing.rb index 21a2e31d824..96ee2f1782e 100644 --- a/app/models/post_timing.rb +++ b/app/models/post_timing.rb @@ -71,6 +71,8 @@ class PostTiming < ActiveRecord::Base last_read_post_number: last_read ) + topic.posts.find_by(post_number: post_number).decrement!(:reads) + if !topic.private_message? set_minimum_first_unread!(user_id: user.id, date: topic.updated_at) end @@ -87,6 +89,8 @@ class PostTiming < ActiveRecord::Base .where('user_id = ? and topic_id in (?)', user_id, topic_ids) .delete_all + Post.where(topic_id: topic_ids).update_all('reads = reads - 1') + date = Topic.listable_topics.where(id: topic_ids).minimum(:updated_at) if date diff --git a/spec/models/post_timing_spec.rb b/spec/models/post_timing_spec.rb index 9700484312e..cb18d50bbd1 100644 --- a/spec/models/post_timing_spec.rb +++ b/spec/models/post_timing_spec.rb @@ -164,4 +164,24 @@ describe PostTiming do end + describe 'decrementing posts read count when destroying post timings' do + let(:initial_read_count) { 0 } + let(:post) { Fabricate(:post, reads: initial_read_count) } + + before do + PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]]) + end + + it '#destroy_last_for decrements the reads count for a post' do + PostTiming.destroy_last_for(post.user, post.topic_id) + + expect(post.reload.reads).to eq initial_read_count + end + + it '#destroy_for decrements the reads count for a post' do + PostTiming.destroy_for(post.user, [post.topic_id]) + + expect(post.reload.reads).to eq initial_read_count + end + end end