mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 21:26:00 +08: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)
|
def self.push_notification(user, payload)
|
||||||
return if user.do_not_disturb?
|
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?
|
if user.push_subscriptions.exists?
|
||||||
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
|
Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
|
||||||
end
|
end
|
||||||
|
@ -91,6 +91,8 @@ class DiscoursePluginRegistry
|
|||||||
|
|
||||||
define_filtered_register :presence_channel_prefixes
|
define_filtered_register :presence_channel_prefixes
|
||||||
|
|
||||||
|
define_filtered_register :push_notification_filters
|
||||||
|
|
||||||
def self.register_auth_provider(auth_provider)
|
def self.register_auth_provider(auth_provider)
|
||||||
self.auth_providers << auth_provider
|
self.auth_providers << auth_provider
|
||||||
end
|
end
|
||||||
|
@ -950,6 +950,12 @@ class Plugin::Instance
|
|||||||
DiscoursePluginRegistry.register_presence_channel_prefix([prefix, block], self)
|
DiscoursePluginRegistry.register_presence_channel_prefix([prefix, block], self)
|
||||||
end
|
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.
|
# 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.
|
# 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
|
describe "push_notification" do
|
||||||
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:') }
|
let(:mention_post) { create_post_with_alerts(user: user, raw: 'Hello @eviltrout :heart:') }
|
||||||
let(:topic) { mention_post.topic }
|
let(:topic) { mention_post.topic }
|
||||||
|
before do
|
||||||
it "pushes nothing to suspended users" do
|
|
||||||
SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push"
|
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|
|
2.times do |i|
|
||||||
UserApiKey.create!(user_id: evil_trout.id,
|
UserApiKey.create!(user_id: evil_trout.id,
|
||||||
client_id: "xxx#{i}",
|
client_id: "xxx#{i}",
|
||||||
@ -734,20 +730,33 @@ describe PostAlerter do
|
|||||||
scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },
|
scopes: ['notifications'].map { |name| UserApiKeyScope.new(name: name) },
|
||||||
push_url: "https://site2.com/push")
|
push_url: "https://site2.com/push")
|
||||||
end
|
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 }
|
expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "pushes nothing when the user is in 'do not disturb'" do
|
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)
|
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 }
|
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
|
it "correctly pushes notifications if configured correctly" do
|
||||||
Jobs.run_immediately!
|
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
|
body = nil
|
||||||
headers = nil
|
headers = nil
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user