diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 6bc22d2c74d..0c20e433a23 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 } diff --git a/app/models/user_action_observer.rb b/app/models/user_action_observer.rb index a98ab14320a..b0be1230d13 100644 --- a/app/models/user_action_observer.rb +++ b/app/models/user_action_observer.rb @@ -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, diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 0e603955fab..2631ccf06b8 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -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 diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 36ace4d612e..c9359e8ba1a 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -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