2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# This is meant to be used by plugins to trigger and listen to events
|
|
|
|
# So we can execute code when things happen.
|
2017-06-01 03:19:43 -04:00
|
|
|
class DiscourseEvent
|
2013-02-12 23:45:10 -05:00
|
|
|
# Defaults to a hash where default values are empty sets.
|
|
|
|
def self.events
|
|
|
|
@events ||= Hash.new { |hash, key| hash[key] = Set.new }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2023-01-09 05:26:39 -05:00
|
|
|
def self.trigger(event_name, *args, **kwargs)
|
2013-02-12 23:45:10 -05:00
|
|
|
events[event_name].each { |event| event.call(*args, **kwargs) }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
def self.on(event_name, &block)
|
2023-08-02 17:44:19 -04:00
|
|
|
case event_name
|
|
|
|
when :user_badge_removed
|
2023-04-27 09:07:02 -04:00
|
|
|
Discourse.deprecate(
|
|
|
|
"The :user_badge_removed event is deprecated. Please use :user_badge_revoked instead",
|
|
|
|
since: "3.1.0.beta5",
|
|
|
|
drop_from: "3.2.0.beta1",
|
|
|
|
output_in_test: true,
|
|
|
|
)
|
2023-08-02 17:44:19 -04:00
|
|
|
when :post_notification_alert
|
|
|
|
Discourse.deprecate(
|
|
|
|
"The :post_notification_alert event is deprecated. Please use :push_notification instead",
|
|
|
|
since: "3.2.0.beta1",
|
|
|
|
drop_from: "3.3.0.beta1",
|
|
|
|
output_in_test: true,
|
|
|
|
)
|
|
|
|
else
|
|
|
|
# ignore
|
2023-04-27 09:07:02 -04:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
events[event_name] << block
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-03-31 12:58:56 -04:00
|
|
|
def self.off(event_name, &block)
|
2020-11-11 14:46:13 -05:00
|
|
|
raise ArgumentError.new "DiscourseEvent.off must reference a block" if block.nil?
|
2015-03-31 12:58:56 -04:00
|
|
|
events[event_name].delete(block)
|
|
|
|
end
|
|
|
|
|
2021-06-22 14:00:04 -04:00
|
|
|
def self.all_off(event_name)
|
|
|
|
events.delete(event_name)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|