FIX: Staff was getting 500 when editing post in deleted topic

This commit is contained in:
Robin Ward 2015-11-13 11:35:04 -05:00
parent 6b1cc8d486
commit 3939331dec
4 changed files with 45 additions and 14 deletions

View File

@ -130,6 +130,9 @@ class PostsController < ApplicationController
post = Post.where(id: params[:id])
post = post.with_deleted if guardian.is_staff?
post = post.first
raise Discourse::NotFound if post.blank?
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?
if too_late_to(:edit, post)
@ -155,15 +158,18 @@ class PostsController < ApplicationController
opts[:skip_validations] = true
end
revisor = PostRevisor.new(post)
topic = post.topic
topic = Topic.with_deleted.find(post.topic_id) if guardian.is_staff?
revisor = PostRevisor.new(post, topic)
revisor.revise!(current_user, changes, opts)
return render_json_error(post) if post.errors.present?
return render_json_error(post.topic) if post.topic.errors.present?
return render_json_error(topic) if topic.errors.present?
post_serializer = PostSerializer.new(post, scope: guardian, root: false)
post_serializer.draft_sequence = DraftSequence.current(current_user, post.topic.draft_key)
link_counts = TopicLink.counts_for(guardian,post.topic, [post])
post_serializer.draft_sequence = DraftSequence.current(current_user, topic.draft_key)
link_counts = TopicLink.counts_for(guardian, topic, [post])
post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?
result = { post: post_serializer.as_json }

View File

@ -46,6 +46,7 @@ class UserActionObserver < ActiveRecord::Observer
def self.log_post(model)
# first post gets nada
return if model.is_first_post?
return if model.topic.blank?
row = {
action_type: UserAction::REPLY,

View File

@ -200,7 +200,7 @@ class PostAlerter
def notify_users(users, type, post)
users = [users] unless users.is_a?(Array)
if post.topic.private_message?
if post.topic.try(:private_message?)
whitelist = allowed_users(post)
users.reject! {|u| !whitelist.include?(u)}
end

View File

@ -288,16 +288,18 @@ describe PostsController do
include_examples 'action requires login', :put, :update, id: 2
describe 'when logged in' do
let(:post) { Fabricate(:post, user: logged_in_as) }
let(:update_params) do
{
id: post.id,
post: { raw: 'edited body', edit_reason: 'typo' },
image_sizes: { 'http://image.com/image.jpg' => {'width' => 123, 'height' => 456} },
}
end
let(:moderator) { Fabricate(:moderator) }
let(:post) { Fabricate(:post, user: log_in) }
let(:update_params) do
{
id: post.id,
post: { raw: 'edited body', edit_reason: 'typo' },
image_sizes: { 'http://image.com/image.jpg' => {'width' => 123, 'height' => 456} },
}
end
describe 'when logged in as a regular user' do
let(:logged_in_as) { log_in }
it 'does not allow to update when edit time limit expired' do
Guardian.any_instance.stubs(:can_edit?).with(post).returns(false)
@ -342,6 +344,28 @@ describe PostsController do
xhr :put, :update, update_params
end
it "doesn't allow updating of deleted posts" do
first_post = post.topic.ordered_posts.first
PostDestroyer.new(moderator, first_post).destroy
xhr :put, :update, update_params
expect(response).not_to be_success
end
end
describe "when logged in as staff" do
let(:logged_in_as) { log_in(:moderator) }
it "supports updating posts in deleted topics" do
first_post = post.topic.ordered_posts.first
PostDestroyer.new(moderator, first_post).destroy
xhr :put, :update, update_params
expect(response).to be_success
post.reload
expect(post.raw).to eq('edited body')
end
end
end