FIX: Moving posts was not updating reply_count and

`reply_to_post_number` so reply linkage was broken.
This commit is contained in:
Robin Ward 2014-08-20 14:14:56 -04:00
parent eb02619d1f
commit 506dca6d4e
3 changed files with 46 additions and 18 deletions

View File

@ -376,16 +376,16 @@ Discourse.Post = Discourse.Model.extend({
// Whether to show replies directly below
showRepliesBelow: function() {
var reply_count = this.get('reply_count');
var replyCount = this.get('reply_count');
// We don't show replies if there aren't any
if (reply_count === 0) return false;
if (replyCount === 0) return false;
// Always show replies if the setting `suppress_reply_directly_below` is false.
if (!Discourse.SiteSettings.suppress_reply_directly_below) return true;
// Always show replies if there's more than one
if (reply_count > 1) return true;
if (replyCount > 1) return true;
// If we have *exactly* one reply, we have to consider if it's directly below us
var topic = this.get('topic');

View File

@ -46,32 +46,50 @@ class PostMover
end
def move_each_post
with_max_post_number do |max_post_number|
posts.each_with_index do |post, offset|
post.is_first_post? ? copy(post) : move(post, offset + max_post_number)
max_post_number = destination_topic.max_post_number + 1
@move_map = {}
@reply_count = {}
posts.each_with_index do |post, offset|
unless post.is_first_post?
@move_map[post.post_number] = offset + max_post_number
else
@move_map[post.post_number] = 1
end
if post.reply_to_post_number.present?
@reply_count[post.reply_to_post_number] = (@reply_count[post.reply_to_post_number] || 0) + 1
end
end
posts.each do |post|
post.is_first_post? ? create_first_post(post) : move(post)
end
end
def copy(post)
PostCreator.create(
def create_first_post(post)
p = PostCreator.create(
post.user,
raw: post.raw,
topic_id: destination_topic.id,
acting_user: user
)
p.update_column(:reply_count, @reply_count[1] || 0)
end
def move(post, post_number)
def move(post)
@first_post_number_moved ||= post.post_number
Post.where(id: post.id, topic_id: original_topic.id).update_all(
[
['post_number = :post_number',
'reply_to_post_number = :reply_to_post_number',
'topic_id = :topic_id',
'sort_order = :post_number'
'sort_order = :post_number',
'reply_count = :reply_count',
].join(', '),
post_number: post_number,
reply_count: @reply_count[post.post_number] || 0,
post_number: @move_map[post.post_number],
reply_to_post_number: @move_map[post.reply_to_post_number],
topic_id: destination_topic.id
]
)
@ -112,10 +130,6 @@ class PostMover
)
end
def with_max_post_number
yield destination_topic.max_post_number + 1
end
def posts
@posts ||= begin
Post.where(id: post_ids).order(:created_at).tap do |posts|

View File

@ -8,9 +8,9 @@ describe PostMover do
let(:category) { Fabricate(:category, user: user) }
let!(:topic) { Fabricate(:topic, user: user) }
let!(:p1) { Fabricate(:post, topic: topic, user: user) }
let!(:p2) { Fabricate(:post, topic: topic, user: another_user, raw: "Has a link to [evil trout](http://eviltrout.com) which is a cool site.")}
let!(:p3) { Fabricate(:post, topic: topic, user: user)}
let!(:p4) { Fabricate(:post, topic: topic, user: user)}
let!(:p2) { 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)}
let!(:p3) { Fabricate(:post, topic: topic, reply_to_post_number: p1.post_number, user: user)}
let!(:p4) { Fabricate(:post, topic: topic, reply_to_post_number: p2.post_number, user: user)}
before do
# add a like to a post, enable observers so we get user actions
@ -120,11 +120,15 @@ describe PostMover do
p2.sort_order.should == 2
p2.post_number.should == 2
p2.topic_id.should == moved_to.id
p2.reply_count.should == 1
p2.reply_to_post_number.should be_nil
p4.reload
p4.post_number.should == 3
p4.sort_order.should == 3
p4.topic_id.should == moved_to.id
p4.reply_count.should == 0
p4.reply_to_post_number.should == 2
# Check out the original topic
topic.reload
@ -159,12 +163,19 @@ describe PostMover do
p1.sort_order.should == 1
p1.post_number.should == 1
p1.topic_id == topic.id
p1.reply_count.should == 0
# New first post
new_first = new_topic.posts.where(post_number: 1).first
new_first.reply_count.should == 1
# Second post is in a new topic
p2.reload
p2.post_number.should == 2
p2.sort_order.should == 2
p2.topic_id == new_topic.id
p2.reply_to_post_number.should == 1
p2.reply_count.should == 0
topic.reload
topic.posts.by_post_number.should =~ [p1, p3, p4]
@ -195,11 +206,14 @@ describe PostMover do
p2.sort_order.should == 3
p2.post_number.should == 3
p2.topic_id.should == moved_to.id
p2.reply_count.should == 1
p2.reply_to_post_number.should be_nil
p4.reload
p4.post_number.should == 4
p4.sort_order.should == 4
p4.topic_id.should == moved_to.id
p4.reply_to_post_number.should == p2.post_number
end
end