FIX: Approved posts were not enqueueing alerts

FIX: Approved posts were not enqueueing alerts

diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index bf468b2..3ac334b 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 1ab4dc2..e05f164 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 239e0da..bc19ba9 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 1ab47bd..6075f4d 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 9155f8c..8b8ae2e 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

GitHub sha: 646cdfa4

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

This commit has been mentioned on Discourse Meta. There might be relevant details there:

1 Like