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

This commit is contained in:
Guo Xiang Tan 2020-05-12 14:25:20 +08:00
parent d301af39bd
commit 8fb99f218d
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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))