mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 09:26:54 -06:00
DEV: Plugin instance method for push_notification_filters (#14787)
This commit is contained in:
parent
836c0f5ffe
commit
67265a5045
1
.ruby-version
Normal file
1
.ruby-version
Normal file
@ -0,0 +1 @@
|
||||
3.0.1
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
#
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user