FEATURE: Allow plugins to register a callback to ignore DraftSequence.

FEATURE: Allow plugins to register a callback to ignore DraftSequence.

diff --git a/app/models/draft_sequence.rb b/app/models/draft_sequence.rb
index d77a6d4..ecdfff4 100644
--- a/app/models/draft_sequence.rb
+++ b/app/models/draft_sequence.rb
@@ -5,7 +5,7 @@ class DraftSequence < ActiveRecord::Base
     user_id = user
     user_id = user.id unless user.is_a?(Integer)
 
-    return 0 if user_id < 0
+    return 0 if invalid_user_id?(user_id)
 
     h = { user_id: user_id, draft_key: key }
     c = DraftSequence.find_by(h)
@@ -23,12 +23,22 @@ class DraftSequence < ActiveRecord::Base
     user_id = user
     user_id = user.id unless user.is_a?(Integer)
 
-    return nil if user_id < 0
+    return nil if invalid_user_id?(user_id)
 
     # perf critical path
     r, _ = DB.query_single('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key)
     r.to_i
   end
+
+  cattr_accessor :plugin_ignore_draft_sequence_callbacks
+  self.plugin_ignore_draft_sequence_callbacks = {}
+
+  def self.invalid_user_id?(user_id)
+    user_id < 0 || self.plugin_ignore_draft_sequence_callbacks.any? do |plugin, callback|
+      plugin.enabled? ? callback.call(user_id) : false
+    end
+  end
+  private_class_method :invalid_user_id?
 end
 
 # == Schema Information
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index d8bc0b4..c1904b6 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -409,6 +409,15 @@ class Plugin::Instance
     SeedFu.fixture_paths.concat(paths)
   end
 
+  # Applies to all sites in a multisite environment. Block is not called if
+  # plugin is not enabled. Block is called with `user_id` and has to return a
+  # boolean based on whether the given `user_id` should be ignored.
+  def register_ignore_draft_sequence_callback(&block)
+    reloadable_patch do |plugin|
+      ::DraftSequence.plugin_ignore_draft_sequence_callbacks[plugin] = block
+    end
+  end
+
   def listen_for(event_name)
     return unless self.respond_to?(event_name)
     DiscourseEvent.on(event_name, &self.method(event_name))

GitHub sha: 8fb99f21

Wait ā€¦ I need a lot more context here, can you explain the use of this hook?

For user accounts that are never accessed, there is no reason to create draft sequences for those accounts so Iā€™m adding a hook here to skip it.

1 Like