FIX: Skip whisper posts when updating topic like count (#10157)

This commit is contained in:
Bianca Nenciu 2020-07-13 09:30:00 +03:00 committed by GitHub
parent 54d002f7db
commit 4492718864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 1 deletions

View File

@ -1071,7 +1071,13 @@ class Topic < ActiveRecord::Base
end
def update_action_counts
update_column(:like_count, Post.where(topic_id: id).sum(:like_count))
update_column(
:like_count,
Post
.where.not(post_type: Post.types[:whisper])
.where(topic_id: id)
.sum(:like_count)
)
end
def posters_summary(options = {}) # avatar lookup in options

View File

@ -0,0 +1,23 @@
# frozen_string_literal: true
class FixTopicLikeCount < ActiveRecord::Migration[6.0]
def up
return if DB.query_single("SELECT * FROM site_settings WHERE name = 'enable_whispers' AND value = 't'").empty?
DB.exec(<<~SQL, whisper: Post.types[:whisper])
UPDATE topics SET like_count = tbl.like_count
FROM (
SELECT topic_id, SUM(like_count) like_count
FROM posts
WHERE deleted_at IS NULL AND post_type <> :whisper
GROUP BY topic_id
) AS tbl
WHERE topics.id = tbl.topic_id
AND topics.like_count <> tbl.like_count
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -2634,4 +2634,26 @@ describe Topic do
expect(@topic.auto_close_threshold_reached?).to eq(true)
end
end
describe '#update_action_counts' do
let(:topic) { Fabricate(:topic) }
it 'updates like count without including whisper posts' do
post = Fabricate(:post, topic: topic)
whisper_post = Fabricate(:post, topic: topic, post_type: Post.types[:whisper])
topic.update_action_counts
expect(topic.like_count).to eq(0)
PostAction.create!(post: post, user: user, post_action_type_id: PostActionType.types[:like])
topic.update_action_counts
expect(topic.like_count).to eq(1)
PostAction.create!(post: whisper_post, user: user, post_action_type_id: PostActionType.types[:like])
topic.update_action_counts
expect(topic.like_count).to eq(1)
end
end
end