diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 182cb0a3660..21b07963f57 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -168,7 +168,7 @@ class PostCreator end def self.before_create_tasks(post) - set_reply_user_id(post) + set_reply_info(post) post.word_count = post.raw.scan(/\w+/).size post.post_number ||= Topic.next_post_number(post.topic_id, post.reply_to_post_number.present?) @@ -181,10 +181,18 @@ class PostCreator post.last_version_at ||= Time.now end - def self.set_reply_user_id(post) + def self.set_reply_info(post) return unless post.reply_to_post_number.present? - post.reply_to_user_id ||= Post.select(:user_id).find_by(topic_id: post.topic_id, post_number: post.reply_to_post_number).try(:user_id) + reply_info = Post.where(topic_id: post.topic_id, post_number: post.reply_to_post_number) + .select(:user_id, :post_type) + .first + + if reply_info.present? + post.reply_to_user_id ||= reply_info.user_id + whisper_type = Post.types[:whisper] + post.post_type = whisper_type if reply_info.post_type == whisper_type + end end protected diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 8ec10e457b3..38b4e52a0c3 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -274,6 +274,31 @@ describe PostCreator do end end + context 'whisper' do + let!(:topic) { Fabricate(:topic, user: user) } + + it 'forces replies to whispers to be whispers' do + whisper = PostCreator.new(user, + topic_id: topic.id, + reply_to_post_number: 1, + post_type: Post.types[:whisper], + raw: 'this is a whispered reply').create + + expect(whisper).to be_present + expect(whisper.post_type).to eq(Post.types[:whisper]) + + whisper_reply = PostCreator.new(user, + topic_id: topic.id, + reply_to_post_number: whisper.post_number, + post_type: Post.types[:regular], + raw: 'replying to a whisper this time').create + + expect(whisper_reply).to be_present + expect(whisper_reply.post_type).to eq(Post.types[:whisper]) + expect(true).to eq(false) + end + end + context 'uniqueness' do let!(:topic) { Fabricate(:topic, user: user) }