DEV: Plugin instance method for push_notification_filters (#14787)

This commit is contained in:
Mark VanLandingham 2021-11-03 12:21:33 -05:00 committed by GitHub
parent 836c0f5ffe
commit 67265a5045
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 24 deletions

1
.ruby-version Normal file
View File

@ -0,0 +1 @@
3.0.1

View File

@ -37,6 +37,10 @@ class PostAlerter
def self.push_notification(user, payload)
return if user.do_not_disturb?
DiscoursePluginRegistry.push_notification_filters.each do |filter|
return unless filter.call(user, payload)
end
if user.push_subscriptions.exists?
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
end

View File

@ -91,6 +91,8 @@ class DiscoursePluginRegistry
define_filtered_register :presence_channel_prefixes
define_filtered_register :push_notification_filters
def self.register_auth_provider(auth_provider)
self.auth_providers << auth_provider
end

View File

@ -950,6 +950,12 @@ class Plugin::Instance
DiscoursePluginRegistry.register_presence_channel_prefix([prefix, 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)
DiscoursePluginRegistry.register_push_notification_filter(block, self)
end
# Register a ReviewableScore setting_name associated with a reason.
# We'll use this to build a site setting link and add it to the reason's translation.
#

View File

@ -721,12 +721,8 @@ describe PostAlerter do
describe "push_notification" do
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:') }
let(:topic) { mention_post.topic }
it "pushes nothing to suspended users" do
before do
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
evil_trout.update_columns(suspended_till: 1.year.from_now)
2.times do |i|
UserApiKey.create!(user_id: evil_trout.id,
client_id: "xxx#{i}",
@ -734,20 +730,33 @@ describe PostAlerter do
scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },
push_url: "https://site2.com/push")
end
end
describe "DiscoursePluginRegistry#push_notification_filters" do
it "sends push notifications when all filters pass" do
Plugin::Instance.new.register_push_notification_filter do |user, payload|
true
end
expect { mention_post }.to change { Jobs::PushNotification.jobs.count }.by(1)
DiscoursePluginRegistry.reset!
end
it "does not send push notifications when a filters returns false" do
Plugin::Instance.new.register_push_notification_filter do |user, payload|
false
end
expect { mention_post }.not_to change { Jobs::PushNotification.jobs.count }
DiscoursePluginRegistry.reset!
end
end
it "pushes nothing to suspended users" do
evil_trout.update_columns(suspended_till: 1.year.from_now)
expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }
end
it "pushes nothing when the user is in 'do not disturb'" do
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
2.times do |i|
UserApiKey.create!(user_id: evil_trout.id,
client_id: "xxx#{i}",
application_name: "iPhone#{i}",
scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },
push_url: "https://site2.com/push")
end
Fabricate(:do_not_disturb_timing, user: evil_trout, starts_at: Time.zone.now, ends_at: 1.day.from_now)
expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }
@ -755,16 +764,6 @@ describe PostAlerter do
it "correctly pushes notifications if configured correctly" do
Jobs.run_immediately!
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
2.times do |i|
UserApiKey.create!(user_id: evil_trout.id,
client_id: "xxx#{i}",
application_name: "iPhone#{i}",
scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },
push_url: "https://site2.com/push")
end
body = nil
headers = nil