FIX: Update reply count when moving posts

This commit is contained in:
Gerhard Schlager 2019-07-22 21:42:24 +02:00
parent 271ddac467
commit 845fd42153
2 changed files with 26 additions and 6 deletions

View File

@ -104,6 +104,10 @@ class PostMover
PostReply.where("reply_id IN (:post_ids) OR post_id IN (:post_ids)", post_ids: post_ids).each do |post_reply|
if post_reply.post && post_reply.reply && post_reply.reply.topic_id != post_reply.post.topic_id
Post
.where("id = ? AND reply_count > 0", post_reply.post.id)
.update_all("reply_count = reply_count - 1")
PostReply
.where(reply_id: post_reply.reply.id, post_id: post_reply.post.id)
.delete_all

View File

@ -28,14 +28,17 @@ describe PostMover do
fab!(:another_user) { evil_trout }
fab!(:category) { Fabricate(:category, user: user) }
fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:p1) { Fabricate(:post, topic: topic, user: user, created_at: 3.hours.ago) }
fab!(:p1) { Fabricate(:post, topic: topic, user: user, created_at: 3.hours.ago, reply_count: 2) }
fab!(:p2) do
Fabricate(:post,
Fabricate(
:post,
topic: topic,
user: another_user,
raw: "Has a link to [evil trout](http://eviltrout.com) which is a cool site.",
reply_to_post_number: p1.post_number)
reply_to_post_number: p1.post_number,
reply_count: 1
)
end
fab!(:p3) { Fabricate(:post, topic: topic, reply_to_post_number: p1.post_number, user: user) }
@ -46,8 +49,8 @@ describe PostMover do
before do
SiteSetting.tagging_enabled = true
Jobs.run_immediately!
p1.replies << p3
p2.replies << p4
p1.replies.push(p2, p3)
p2.replies.push(p4)
UserActionManager.enable
@like = PostActionCreator.like(another_user, p4)
end
@ -563,7 +566,7 @@ describe PostMover do
expect(p1.sort_order).to eq(1)
expect(p1.post_number).to eq(1)
expect(p1.topic_id).to eq(topic.id)
expect(p1.reply_count).to eq(0)
expect(p1.reply_count).to eq(1)
# New first post
new_first = new_topic.posts.where(post_number: 1).first
@ -676,6 +679,19 @@ describe PostMover do
expect(new_topic.posts.by_post_number.last.raw).to eq(p2.raw)
expect(new_topic.posts_count).to eq(2)
end
it "corrects reply_counts within original topic" do
expect do
topic.move_posts(user, [p4.id], title: "new testing topic name 1")
end.to change { PostReply.count }.by(-1)
expect(p1.reload.reply_count).to eq(2)
expect(p2.reload.reply_count).to eq(0)
expect do
topic.move_posts(user, [p2.id, p3.id], title: "new testing topic name 2")
end.to change { PostReply.count }.by(-2)
expect(p1.reload.reply_count).to eq(0)
end
end
end