diff --git a/app/models/queued_post.rb b/app/models/queued_post.rb index 0df3dee71f4..eaab5d6d6a5 100644 --- a/app/models/queued_post.rb +++ b/app/models/queued_post.rb @@ -7,6 +7,14 @@ class QueuedPost < ActiveRecord::Base belongs_to :approved_by, class_name: "User" belongs_to :rejected_by, class_name: "User" + def create_pending_action + UserAction.log_action!(action_type: UserAction::PENDING, + user_id: user_id, + acting_user_id: user_id, + target_topic_id: topic_id, + queued_post_id: id) + end + def self.states @states ||= Enum.new(:new, :approved, :rejected) end @@ -72,6 +80,10 @@ class QueuedPost < ActiveRecord::Base row_count = QueuedPost.where('id = ? AND state <> ?', id, state_val).update_all(updates) raise InvalidStateTransition.new if row_count == 0 + if [:rejected, :approved].include?(state) + UserAction.where(queued_post_id: id).destroy_all + end + # Update the record in memory too, and clear the dirty flag updates.each {|k, v| send("#{k}=", v) } changes_applied diff --git a/lib/post_enqueuer.rb b/lib/post_enqueuer.rb index 64a6accc501..a62caa46447 100644 --- a/lib/post_enqueuer.rb +++ b/lib/post_enqueuer.rb @@ -24,11 +24,7 @@ class PostEnqueuer end if queued_post.save - UserAction.log_action!(action_type: UserAction::PENDING, - user_id: @user.id, - acting_user_id: @user.id, - target_topic_id: args[:topic_id], - queued_post_id: queued_post.id) + queued_post.create_pending_action else add_errors_from(queued_post) end diff --git a/spec/components/post_enqueuer_spec.rb b/spec/components/post_enqueuer_spec.rb index 964ba891bcf..b7d6c372966 100644 --- a/spec/components/post_enqueuer_spec.rb +++ b/spec/components/post_enqueuer_spec.rb @@ -10,9 +10,6 @@ describe PostEnqueuer do let(:enqueuer) { PostEnqueuer.new(user, 'new_post') } it 'enqueues the post' do - - old_count = user.user_actions.count - qp = enqueuer.enqueue(raw: 'This should be enqueued', topic_id: topic.id, post_options: { reply_to_post_number: 1 }) @@ -21,7 +18,7 @@ describe PostEnqueuer do expect(qp).to be_present expect(qp.topic).to eq(topic) expect(qp.user).to eq(user) - expect(UserAction.where(user_id: user.id).count).to eq(old_count + 1) + expect(UserAction.where(user_id: user.id, action_type: UserAction::PENDING).count).to eq(1) end end diff --git a/spec/models/queued_post_spec.rb b/spec/models/queued_post_spec.rb index 7ddeaed5b52..d316fb001ae 100644 --- a/spec/models/queued_post_spec.rb +++ b/spec/models/queued_post_spec.rb @@ -26,6 +26,7 @@ describe QueuedPost do end it "follows the correct workflow for approval" do + qp.create_pending_action post = qp.approve!(admin) # Creates the post with the attributes @@ -39,6 +40,9 @@ describe QueuedPost do expect(qp.state).to eq(QueuedPost.states[:approved]) expect(qp.approved_at).to be_present + # It removes the pending action + expect(UserAction.where(queued_post_id: qp.id).count).to eq(0) + # We can't approve twice expect(-> { qp.approve!(admin) }).to raise_error(QueuedPost::InvalidStateTransition) end @@ -50,6 +54,7 @@ describe QueuedPost do end it "follows the correct workflow for rejection" do + qp.create_pending_action qp.reject!(admin) # Updates the QP record @@ -57,6 +62,9 @@ describe QueuedPost do expect(qp.state).to eq(QueuedPost.states[:rejected]) expect(qp.rejected_at).to be_present + # It removes the pending action + expect(UserAction.where(queued_post_id: qp.id).count).to eq(0) + # We can't reject twice expect(-> { qp.reject!(admin) }).to raise_error(QueuedPost::InvalidStateTransition) end @@ -115,7 +123,6 @@ describe QueuedPost do end context "visibility" do - it "works as expected in the invisible queue" do qp = Fabricate(:queued_post, queue: 'invisible') expect(qp).to_not be_visible