FIX: Concurrency issues with making topic embedded posts visible

FIX: Concurrency issues with making topic embedded posts visible

diff --git a/app/jobs/regular/make_embedded_topic_visible.rb b/app/jobs/regular/make_embedded_topic_visible.rb
new file mode 100644
index 0000000..c3c4121
--- /dev/null
+++ b/app/jobs/regular/make_embedded_topic_visible.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+module Jobs
+  class MakeEmbeddedTopicVisible < ::Jobs::Base
+
+    def execute(args)
+      raise Discourse::InvalidParameters.new(:topic_id) if args[:topic_id].blank?
+
+      if topic = Topic.find_by(id: args[:topic_id])
+        topic.update_status('visible', true, topic.user)
+      end
+    end
+
+  end
+end
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 81befd0..fab3de1 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -186,7 +186,6 @@ class PostCreator
         @post.link_post_uploads
         update_uploads_secure_status
         ensure_in_allowed_users if guardian.is_staff?
-        make_visible
         unarchive_message
         if !@opts[:import_mode]
           DraftSequence.next!(@user, draft_key)
@@ -415,17 +414,6 @@ class PostCreator
     end
   end
 
-  def make_visible
-    return unless SiteSetting.embed_unlisted?
-    return unless @post.post_number > 1
-    return if @post.topic.visible?
-    return if @post.post_type != Post.types[:regular]
-
-    if embed = @post.topic.topic_embed
-      @post.topic.update_status('visible', true, @user)
-    end
-  end
-
   def unarchive_message
     return unless @topic.private_message? && @topic.id
 
diff --git a/lib/post_jobs_enqueuer.rb b/lib/post_jobs_enqueuer.rb
index 8659cba..6f95899 100644
--- a/lib/post_jobs_enqueuer.rb
+++ b/lib/post_jobs_enqueuer.rb
@@ -18,6 +18,7 @@ class PostJobsEnqueuer
     unless skip_after_create?
       after_post_create
       after_topic_create
+      make_visible
     end
 
     if @topic.private_message?
@@ -44,6 +45,17 @@ class PostJobsEnqueuer
     @post.trigger_post_process(new_post: true)
   end
 
+  def make_visible
+    return unless SiteSetting.embed_unlisted?
+    return unless @post.post_number > 1
+    return if @topic.visible?
+    return if @post.post_type != Post.types[:regular]
+
+    if @topic.topic_embed.present?
+      Jobs.enqueue(:make_embedded_topic_visible, topic_id: @topic.id)
+    end
+  end
+
   def after_post_create
     TopicTrackingState.publish_unread(@post) if @post.post_number > 1
     TopicTrackingState.publish_latest(@topic, @post.whisper?)
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 59510b9..c45a220 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -75,6 +75,7 @@ describe TopicEmbed do
       end
 
       it "will make the topic unlisted if `embed_unlisted` is set until someone replies" do
+        Jobs.run_immediately!
         SiteSetting.embed_unlisted = true
         imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content")
         expect(imported_post.topic).not_to be_visible

GitHub sha: 25bed4f6