DEV: Email notification filter plugin API (#24271)

This commit is contained in:
Mark VanLandingham 2023-11-08 10:29:00 -06:00 committed by GitHub
parent daf7608905
commit e3f8e9c0fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 0 deletions

View File

@ -138,6 +138,11 @@ class NotificationEmailer
email_user = EmailUser.new(notification, no_delay: no_delay)
email_method = Notification.types[notification.notification_type]
DiscoursePluginRegistry.email_notification_filters.each do |filter|
return unless filter.call(notification)
end
email_user.public_send(email_method) if email_user.respond_to? email_method
end
end

View File

@ -99,6 +99,7 @@ class DiscoursePluginRegistry
define_filtered_register :presence_channel_prefixes
define_filtered_register :email_notification_filters
define_filtered_register :push_notification_filters
define_filtered_register :notification_consolidation_plans

View File

@ -999,6 +999,12 @@ class Plugin::Instance
DiscoursePluginRegistry.register_presence_channel_prefix([prefix, block], self)
end
# Registers a new email notification filter. Notification is passed into block, and if all
# filters return `true`, the email notification will be sent.
def register_email_notification_filter(&block)
DiscoursePluginRegistry.register_email_notification_filter(block, self)
end
# Registers a new push notification filter. User and notification payload are passed into block, and if all
# filters return `true`, the push notification will be sent.
def register_push_notification_filter(&block)

View File

@ -274,4 +274,31 @@ RSpec.describe NotificationEmailer do
end
end
end
describe "with plugin-added email_notification_filters" do
let!(:plugin) { Plugin::Instance.new }
let!(:notification) { create_notification(:quoted) }
let(:no_delay) { true }
let(:type) { :user_quoted }
after { DiscoursePluginRegistry.reset! }
it "sends email when all filters return true" do
plugin.register_email_notification_filter { |_| true }
plugin.register_email_notification_filter { |_| true }
expect_enqueued_with(job: :user_email, args: { type: type }) do
NotificationEmailer.process_notification(notification, no_delay: no_delay)
end
end
it "doesn't send email when all one filter returns false" do
plugin.register_email_notification_filter { |_| true }
plugin.register_email_notification_filter { |_| false }
expect_not_enqueued_with(job: :user_email, args: { type: type }) do
NotificationEmailer.process_notification(notification, no_delay: no_delay)
end
end
end
end