DEV: Improve support for plugins. (#7332)

DEV: Improve support for plugins. (#7332)

  • DEV: Allow NewPostManager handlers handle PMs.
  • DEV: Add custom fields topic option to PostCreator.
  • DEV: Add topic_id to serializer data.
  • DEV: Wrap topic title from notification item in a span.
diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6
index 6328b94..620231c 100644
--- a/app/assets/javascripts/discourse/models/composer.js.es6
+++ b/app/assets/javascripts/discourse/models/composer.js.es6
@@ -724,6 +724,7 @@ const Composer = RestModel.extend({
     }
 
     const props = {
+      topic_id: this.topic.id,
       raw: this.reply,
       raw_old: this.editConflict ? null : this.originalText,
       edit_reason: opts.editReason,
diff --git a/app/assets/javascripts/discourse/widgets/notification-item.js.es6 b/app/assets/javascripts/discourse/widgets/notification-item.js.es6
index 93e1315..a298e1a 100644
--- a/app/assets/javascripts/discourse/widgets/notification-item.js.es6
+++ b/app/assets/javascripts/discourse/widgets/notification-item.js.es6
@@ -85,6 +85,11 @@ createWidget("notification-item", {
     }
 
     if (this.attrs.fancy_title) {
+      if (this.attrs.topic_id) {
+        return `<span data-topic-id="${this.attrs.topic_id}">${
+          this.attrs.fancy_title
+        }</span>`;
+      }
       return this.attrs.fancy_title;
     }
 
diff --git a/app/assets/stylesheets/common/base/menu-panel.scss b/app/assets/stylesheets/common/base/menu-panel.scss
index 3c49e59..edb8a16 100644
--- a/app/assets/stylesheets/common/base/menu-panel.scss
+++ b/app/assets/stylesheets/common/base/menu-panel.scss
@@ -169,7 +169,7 @@
         display: none;
       }
 
-      span {
+      span:first-child {
         color: $primary;
       }
 
diff --git a/app/assets/stylesheets/common/base/user.scss b/app/assets/stylesheets/common/base/user.scss
index 032a427..676baa7 100644
--- a/app/assets/stylesheets/common/base/user.scss
+++ b/app/assets/stylesheets/common/base/user.scss
@@ -671,7 +671,7 @@
       margin-right: 0.5em;
     }
 
-    span {
+    span:first-child {
       color: $primary;
     }
 
diff --git a/app/assets/stylesheets/common/components/user-stream-item.scss b/app/assets/stylesheets/common/components/user-stream-item.scss
index 99afbdf..aff93ed 100644
--- a/app/assets/stylesheets/common/components/user-stream-item.scss
+++ b/app/assets/stylesheets/common/components/user-stream-item.scss
@@ -102,7 +102,7 @@
     p {
       display: inline-block;
 
-      span {
+      span:first-child {
         color: $primary;
       }
     }
diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rb
index cd01e20..b03bec8 100644
--- a/lib/new_post_manager.rb
+++ b/lib/new_post_manager.rb
@@ -22,7 +22,7 @@ class NewPostManager
   end
 
   def self.clear_handlers!
-    @sorted_handlers = [{ priority: 0, proc: method(:default_handler) }]
+    @sorted_handlers = []
   end
 
   def self.add_handler(priority = 0, &block)
@@ -178,22 +178,17 @@ class NewPostManager
       return result
     end
 
-    # We never queue private messages
-    return perform_create_post if @args[:archetype] == Archetype.private_message
-
-    if args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?
-      return perform_create_post
-    end
-
     # Perform handlers until one returns a result
-    handled = NewPostManager.handlers.any? do |handler|
+    NewPostManager.handlers.any? do |handler|
       result = handler.call(self)
       return result if result
-
-      false
     end
 
-    perform_create_post unless handled
+    # We never queue private messages
+    return perform_create_post if @args[:archetype] == Archetype.private_message ||
+                                  (args[:topic_id] && Topic.where(id: args[:topic_id], archetype: Archetype.private_message).exists?)
+
+    NewPostManager.default_handler(self) || perform_create_post
   end
 
   # Enqueue this post
diff --git a/lib/post_creator.rb b/lib/post_creator.rb
index 95363cd..70f60c9 100644
--- a/lib/post_creator.rb
+++ b/lib/post_creator.rb
@@ -56,6 +56,7 @@ class PostCreator
   #     pinned_at             - Topic pinned time (optional)
   #     pinned_globally       - Is the topic pinned globally (optional)
   #     shared_draft          - Is the topic meant to be a shared draft
+  #     topic_opts            - Options to be overwritten for topic
   #
   def initialize(user, opts)
     # TODO: we should reload user in case it is tainted, should take in a user_id as opposed to user
@@ -410,7 +411,8 @@ class PostCreator
   def create_topic
     return if @topic
     begin
-      topic_creator = TopicCreator.new(@user, guardian, @opts)
+      opts = @opts[:topic_opts] ? @opts.merge(@opts[:topic_opts]) : @opts
+      topic_creator = TopicCreator.new(@user, guardian, opts)
       @topic = topic_creator.create
     rescue ActiveRecord::Rollback
       rollback_from_errors!(topic_creator)
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index c45a870..e46f2f6 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -37,6 +37,9 @@ class TopicCreator
   def create
     topic = Topic.new(setup_topic_params)
     setup_tags(topic)
+    if fields = @opts[:custom_fields]
+      topic.custom_fields = fields
+    end
 
     DiscourseEvent.trigger(:before_create_topic, topic, self)
 
diff --git a/spec/components/new_post_manager_spec.rb b/spec/components/new_post_manager_spec.rb
index 6c87345..269ebcb 100644
--- a/spec/components/new_post_manager_spec.rb
+++ b/spec/components/new_post_manager_spec.rb
@@ -212,7 +212,7 @@ describe NewPostManager do
       handler = -> { nil }
 
       NewPostManager.add_handler(&handler)
-      expect(NewPostManager.handlers).to eq([default_handler, handler])
+      expect(NewPostManager.handlers).to eq([handler])
     end
 
     it "can be added in high priority" do
@@ -223,7 +223,7 @@ describe NewPostManager do
       NewPostManager.add_handler(100, &a)
       NewPostManager.add_handler(50, &b)
       NewPostManager.add_handler(101, &c)
-      expect(NewPostManager.handlers).to eq([c, a, b, default_handler])
+      expect(NewPostManager.handlers).to eq([c, a, b])
     end
 
   end
diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb
index 90dcaf3..c4decf5 100644
--- a/spec/components/post_creator_spec.rb
+++ b/spec/components/post_creator_spec.rb
@@ -54,6 +54,11 @@ describe PostCreator do
       expect { creator.create }.to raise_error(Discourse::InvalidAccess)
     end
 
+    it "can be created with custom fields" do
+      post = PostCreator.create(user, basic_topic_params.merge(topic_opts: { custom_fields: { hello: "world" } }))
+      expect(post.topic.custom_fields).to eq("hello" => "world")
+    end
+
     context "reply to post number" do
       it "omits reply to post number if received on a new topic" do
         p = PostCreator.new(user, basic_topic_params.merge(reply_to_post_number: 3)).create

GitHub sha: da39d66e

2 Likes

I always prefer targetting a class rather than a child by number. Is there any way to add a class and use that instead?

1 Like

That is what I usually prefer too, but in this case the element is coming from a translated string. Changing the string would result in many outdated translations and put another responsibility on translators (ensure that they keep the class).

2 Likes

Thanks that makes sense!

3 Likes