FIX: Approved posts were not enqueueing alerts

This commit is contained in:
Robin Ward 2019-04-23 17:29:46 -04:00
parent b9339e15a1
commit 646cdfa449
5 changed files with 29 additions and 4 deletions

View File

@ -230,6 +230,9 @@ class Reviewable < ActiveRecord::Base
recalculate_score if result.recalculate_score recalculate_score if result.recalculate_score
end end
end end
if result && result.after_commit
result.after_commit.call
end
Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count Jobs.enqueue(:notify_reviewable, reviewable_id: self.id) if update_count
result result

View File

@ -92,7 +92,15 @@ class ReviewableQueuedPost < Reviewable
post_number: created_post.post_number 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 end
def perform_reject_post(performed_by, args) def perform_reject_post(performed_by, args)

View File

@ -3,8 +3,14 @@ class Reviewable < ActiveRecord::Base
include ActiveModel::Serialization include ActiveModel::Serialization
attr_reader :reviewable, :status, :created_post, :created_post_topic attr_reader :reviewable, :status, :created_post, :created_post_topic
attr_accessor :transition_to, :remove_reviewable_ids, :errors, :recalculate_score, attr_accessor(
:update_flag_stats :transition_to,
:remove_reviewable_ids,
:errors,
:recalculate_score,
:update_flag_stats,
:after_commit
)
def initialize(reviewable, status) def initialize(reviewable, status)
@status = status @status = status

View File

@ -44,9 +44,16 @@ RSpec.describe ReviewableQueuedPost, type: :model do
it "creates a post" do it "creates a post" do
topic_count, post_count = Topic.count, Post.count 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.success?).to eq(true)
expect(result.created_post).to be_present expect(result.created_post).to be_present
expect(event).to be_present
expect(result.created_post).to be_valid expect(result.created_post).to be_valid
expect(result.created_post.topic).to eq(topic) expect(result.created_post.topic).to eq(topic)
expect(result.created_post.custom_fields['hello']).to eq('world') expect(result.created_post.custom_fields['hello']).to eq('world')

View File

@ -203,6 +203,7 @@ RSpec.describe Reviewable, type: :model do
it "triggers a notification on pending -> approve" do it "triggers a notification on pending -> approve" do
reviewable = Fabricate(:reviewable_queued_post) reviewable = Fabricate(:reviewable_queued_post)
Jobs.stubs(:enqueue)
Jobs.expects(:enqueue).with(:notify_reviewable, has_key(:reviewable_id)) Jobs.expects(:enqueue).with(:notify_reviewable, has_key(:reviewable_id))
reviewable.perform(moderator, :approve_post) reviewable.perform(moderator, :approve_post)
end end