From 646cdfa4493d7809c0afdc19adc23ba619c3c789 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 23 Apr 2019 17:29:46 -0400 Subject: [PATCH] FIX: Approved posts were not enqueueing alerts --- app/models/reviewable.rb | 3 +++ app/models/reviewable_queued_post.rb | 10 +++++++++- lib/reviewable/perform_result.rb | 10 ++++++++-- spec/models/reviewable_queued_post_spec.rb | 9 ++++++++- spec/models/reviewable_spec.rb | 1 + 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb index bf468b208ce..3ac334b472e 100644 --- a/app/models/reviewable.rb +++ b/app/models/reviewable.rb @@ -230,6 +230,9 @@ class Reviewable < ActiveRecord::Base recalculate_score if result.recalculate_score end end + if result && result.after_commit + result.after_commit.call + end Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count result diff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rb index 1ab4dc2a5bf..e05f164bc1b 100644 --- a/app/models/reviewable_queued_post.rb +++ b/app/models/reviewable_queued_post.rb @@ -92,7 +92,15 @@ class ReviewableQueuedPost < Reviewable post_number: created_post.post_number ) - create_result(:success, :approved) { |result| result.created_post = created_post } + create_result(:success, :approved) do |result| + result.created_post = created_post + + # Do sidekiq work outside of the transaction + result.after_commit = -> { + creator.enqueue_jobs + creator.trigger_after_events + } + end end def perform_reject_post(performed_by, args) diff --git a/lib/reviewable/perform_result.rb b/lib/reviewable/perform_result.rb index 239e0da8006..bc19ba9f0c2 100644 --- a/lib/reviewable/perform_result.rb +++ b/lib/reviewable/perform_result.rb @@ -3,8 +3,14 @@ class Reviewable < ActiveRecord::Base include ActiveModel::Serialization attr_reader :reviewable, :status, :created_post, :created_post_topic - attr_accessor :transition_to, :remove_reviewable_ids, :errors, :recalculate_score, - :update_flag_stats + attr_accessor( + :transition_to, + :remove_reviewable_ids, + :errors, + :recalculate_score, + :update_flag_stats, + :after_commit + ) def initialize(reviewable, status) @status = status diff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rb index 1ab47bdea4e..6075f4d4729 100644 --- a/spec/models/reviewable_queued_post_spec.rb +++ b/spec/models/reviewable_queued_post_spec.rb @@ -44,9 +44,16 @@ RSpec.describe ReviewableQueuedPost, type: :model do it "creates a post" do topic_count, post_count = Topic.count, Post.count - result = reviewable.perform(moderator, :approve_post) + result = nil + + Jobs.run_immediately! + event = DiscourseEvent.track(:before_create_notifications_for_users) do + result = reviewable.perform(moderator, :approve_post) + end + expect(result.success?).to eq(true) expect(result.created_post).to be_present + expect(event).to be_present expect(result.created_post).to be_valid expect(result.created_post.topic).to eq(topic) expect(result.created_post.custom_fields['hello']).to eq('world') diff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rb index 9155f8cf9a9..8b8ae2ea1b6 100644 --- a/spec/models/reviewable_spec.rb +++ b/spec/models/reviewable_spec.rb @@ -203,6 +203,7 @@ RSpec.describe Reviewable, type: :model do it "triggers a notification on pending -> approve" do reviewable = Fabricate(:reviewable_queued_post) + Jobs.stubs(:enqueue) Jobs.expects(:enqueue).with(:notify_reviewable, has_key(:reviewable_id)) reviewable.perform(moderator, :approve_post) end