mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Allow plugins to register a callback to ignore DraftSequence.
This commit is contained in:
parent
d301af39bd
commit
8fb99f218d
@ -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
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user