mirror of
https://github.com/discourse/discourse.git
synced 2025-03-26 22:49:25 +08:00
FIX: Send push notifications for category/tag watching notifications (#24196)
Problem and solution are outlined here on Meta - https://meta.discourse.org/t/watching-a-category-does-not-cause-push-notifications/282794
This commit is contained in:
parent
fd07c943ad
commit
88874389d2
@ -392,6 +392,7 @@ class PostAlerter
|
|||||||
private_message
|
private_message
|
||||||
group_mentioned
|
group_mentioned
|
||||||
watching_first_post
|
watching_first_post
|
||||||
|
watching_category_or_tag
|
||||||
event_reminder
|
event_reminder
|
||||||
event_invitation
|
event_invitation
|
||||||
].map { |t| Notification.types[t] }
|
].map { |t| Notification.types[t] }
|
||||||
|
@ -59,6 +59,18 @@ RSpec.describe PostAlerter do
|
|||||||
PostAlerter.post_created(post)
|
PostAlerter.post_created(post)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def setup_push_notification_subscription_for(user:)
|
||||||
|
2.times do |i|
|
||||||
|
UserApiKey.create!(
|
||||||
|
user_id: user.id,
|
||||||
|
client_id: "xxx#{i}",
|
||||||
|
application_name: "iPhone#{i}",
|
||||||
|
scopes: ["notifications"].map { |name| UserApiKeyScope.new(name: name) },
|
||||||
|
push_url: "https://site2.com/push",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "with private message" do
|
context "with private message" do
|
||||||
it "notifies for pms correctly" do
|
it "notifies for pms correctly" do
|
||||||
pm = Fabricate(:topic, archetype: "private_message", category_id: nil)
|
pm = Fabricate(:topic, archetype: "private_message", category_id: nil)
|
||||||
@ -1172,15 +1184,7 @@ RSpec.describe PostAlerter do
|
|||||||
|
|
||||||
before do
|
before 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"
|
||||||
2.times do |i|
|
setup_push_notification_subscription_for(user: evil_trout)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "DiscoursePluginRegistry#push_notification_filters" do
|
describe "DiscoursePluginRegistry#push_notification_filters" do
|
||||||
@ -1506,6 +1510,24 @@ RSpec.describe PostAlerter do
|
|||||||
params: [[user], post],
|
params: [[user], post],
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "sends a push notification when user has a push subscription" do
|
||||||
|
setup_push_notification_subscription_for(user: user)
|
||||||
|
|
||||||
|
level = CategoryUser.notification_levels[:watching_first_post]
|
||||||
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
||||||
|
events =
|
||||||
|
DiscourseEvent.track_events(:push_notification) do
|
||||||
|
PostAlerter.new.after_save_post(post, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(
|
||||||
|
events.detect do |e|
|
||||||
|
e[:params][0] == user &&
|
||||||
|
e[:params][1][:notification_type] == Notification.types[:watching_first_post]
|
||||||
|
end,
|
||||||
|
).to be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with replies" do
|
context "with replies" do
|
||||||
@ -1828,6 +1850,23 @@ RSpec.describe PostAlerter do
|
|||||||
notification_data = JSON.parse(notification.data)
|
notification_data = JSON.parse(notification.data)
|
||||||
expect(notification_data["display_username"]).to eq(I18n.t("embed.replies", count: 2))
|
expect(notification_data["display_username"]).to eq(I18n.t("embed.replies", count: 2))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "sends a push notification when user has a push subscription" do
|
||||||
|
setup_push_notification_subscription_for(user: user)
|
||||||
|
|
||||||
|
topic = Fabricate(:topic, category: category)
|
||||||
|
post = Fabricate(:post, topic: topic)
|
||||||
|
level = CategoryUser.notification_levels[:watching]
|
||||||
|
CategoryUser.set_notification_level_for_category(user, level, category.id)
|
||||||
|
events = DiscourseEvent.track_events(:push_notification) { PostAlerter.post_created(post) }
|
||||||
|
|
||||||
|
expect(
|
||||||
|
events.detect do |e|
|
||||||
|
e[:params][0] == user &&
|
||||||
|
e[:params][1][:notification_type] == Notification.types[:watching_category_or_tag]
|
||||||
|
end,
|
||||||
|
).to be_present
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user