diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index 272eb1c3f..6bfcbf614 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -22,11 +22,15 @@ class PostMover def to_new_topic(title, category_id=nil) @move_type = PostMover.move_types[:new_topic] + post = Post.find_by(id: post_ids.first) + raise Discourse::InvalidParameters unless post + Topic.transaction do move_posts_to Topic.create!( - user: user, + user: post.user, title: title, - category_id: category_id + category_id: category_id, + created_at: post.created_at ) end end diff --git a/db/migrate/20151107041044_fix_incorrect_topic_creator_after_move.rb b/db/migrate/20151107041044_fix_incorrect_topic_creator_after_move.rb new file mode 100644 index 000000000..e7ffdf049 --- /dev/null +++ b/db/migrate/20151107041044_fix_incorrect_topic_creator_after_move.rb @@ -0,0 +1,12 @@ +class FixIncorrectTopicCreatorAfterMove < ActiveRecord::Migration + def up + execute "UPDATE topics SET user_id = p.user_id + FROM posts p + WHERE p.topic_id = topics.id AND + p.post_number = 1 AND + p.user_id <> topics.user_id" + end + + def down + end +end diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index d67ef6e6e..cdbea639b 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -64,7 +64,7 @@ describe PostMover do expect(TopicUser.find_by(user_id: user.id, topic_id: topic.id).last_read_post_number).to eq(p3.post_number) expect(new_topic).to be_present - expect(new_topic.featured_user1_id).to eq(another_user.id) + expect(new_topic.featured_user1_id).to eq(p4.user_id) expect(new_topic.like_count).to eq(1) expect(new_topic.category).to eq(category) @@ -112,7 +112,7 @@ describe PostMover do moved_to.reload expect(moved_to.posts_count).to eq(3) expect(moved_to.highest_post_number).to eq(3) - expect(moved_to.featured_user1_id).to eq(another_user.id) + expect(moved_to.user_id).to eq(p1.user_id) expect(moved_to.like_count).to eq(1) expect(moved_to.category_id).to eq(SiteSetting.uncategorized_category_id)