FIX: Replies to whispers *must* be whispers

This commit is contained in:
Robin Ward 2015-09-25 11:28:41 -04:00
parent 9e2934f635
commit 64598ceaba
2 changed files with 36 additions and 3 deletions

View file

@ -168,7 +168,7 @@ class PostCreator
end end
def self.before_create_tasks(post) def self.before_create_tasks(post)
set_reply_user_id(post) set_reply_info(post)
post.word_count = post.raw.scan(/\w+/).size post.word_count = post.raw.scan(/\w+/).size
post.post_number ||= Topic.next_post_number(post.topic_id, post.reply_to_post_number.present?) 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 post.last_version_at ||= Time.now
end end
def self.set_reply_user_id(post) def self.set_reply_info(post)
return unless post.reply_to_post_number.present? 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 end
protected protected

View file

@ -274,6 +274,31 @@ describe PostCreator do
end end
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 context 'uniqueness' do
let!(:topic) { Fabricate(:topic, user: user) } let!(:topic) { Fabricate(:topic, user: user) }