Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Improve push notification message for watching_category_or_tag notifications (#24228)

Mark VanLandingham047cae4b3f31
diff --git a/app/services/push_notification_pusher.rb b/app/services/push_notification_pusher.rb
index 0c6891d8591..aafc6d4786b 100644
--- a/app/services/push_notification_pusher.rb
+++ b/app/services/push_notification_pusher.rb
@@ -20,14 +20,7 @@ class PushNotificationPusher
ActionController::Base.helpers.image_url("push-notifications/#{notification_icon_name}.png")
message = {
- title:
- payload[:translated_title] ||
- I18n.t(
- "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}",
- site_title: SiteSetting.title,
- topic: payload[:topic_title],
- username: payload[:username],
- ),
+ title: payload[:translated_title] || title(payload),
body: payload[:excerpt],
badge: get_badge,
icon: notification_icon,
@@ -43,6 +36,27 @@ class PushNotificationPusher
message
end
+ def self.title(payload)
+ translation_key =
+ case payload[:notification_type]
+ when Notification.types[:watching_category_or_tag]
+ # For watching_category_or_tag, the notification could be for either a new post or new topic.
+ # Instead of duplicating translations, we can rely on 'watching_first_post' for new topics,
+ # and 'posted' for new posts.
+ type = payload[:post_number] == 1 ? "watching_first_post" : "posted"
+ "discourse_push_notifications.popup.#{type}"
+ else
+ "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}"
+ end
+
+ I18n.t(
+ translation_key,
+ site_title: SiteSetting.title,
+ topic: payload[:topic_title],
+ username: payload[:username],
+ )
+ end
+
def self.subscriptions(user)
user.push_subscriptions
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 1eeb575e68f..757cb978c7c 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -5190,7 +5190,6 @@ en:
private_message: '%{username} sent you a private message in "%{topic}" - %{site_title}'
linked: '%{username} linked to your post from "%{topic}" - %{site_title}'
watching_first_post: '%{username} created a new topic "%{topic}" - %{site_title}'
- watching_category_or_tag: '%{username} created a new post "%{topic}" - %{site_title}'
confirm_title: "Notifications enabled - %{site_title}"
confirm_body: "Success! Notifications have been enabled."
custom: "Notification from %{username} on %{site_title}"
diff --git a/spec/services/push_notification_pusher_spec.rb b/spec/services/push_notification_pusher_spec.rb
index e55bbd0ff0c..18e298e108f 100644
--- a/spec/services/push_notification_pusher_spec.rb
+++ b/spec/services/push_notification_pusher_spec.rb
@@ -14,6 +14,8 @@ RSpec.describe PushNotificationPusher do
context "with user" do
fab!(:user) { Fabricate(:user) }
+ let(:topic_title) { "Topic" }
+ let(:username) { "system" }
def create_subscription
data = <<~JSON
@@ -28,16 +30,17 @@ RSpec.describe PushNotificationPusher do
PushSubscription.create!(user_id: user.id, data: data)
end
- def execute_push(notification_type: 1)
+ def execute_push(notification_type: 1, post_number: 1)
PushNotificationPusher.push(
user,
{
- topic_title: "Topic",
- username: "system",
+ topic_title: topic_title,
+ username: username,
excerpt: "description",
topic_id: 1,
post_url: "https://example.com/t/1/2",
notification_type: notification_type,
+ post_number: post_number,
},
)
end
@@ -157,5 +160,41 @@ RSpec.describe PushNotificationPusher do
subscription.reload
expect(subscription.error_count).to eq(1)
end
+
+ describe "`watching_category_or_tag` notifications" do
+ it "Uses the 'watching_first_post' translation when new topic was created" do
+ message =
+ execute_push(
+ notification_type: Notification.types[:watching_category_or_tag],
+ post_number: 1,
+ )
+
+ expect(message[:title]).to eq(
+ I18n.t(
+ "discourse_push_notifications.popup.watching_first_post",
+ site_title: SiteSetting.title,
+ topic: topic_title,
+ username: username,
+ ),
+ )
+ end
+
+ it "Uses the 'posted' translation when new post was created" do
+ message =
+ execute_push(
+ notification_type: Notification.types[:watching_category_or_tag],
+ post_number: 2,
+ )
+
+ expect(message[:title]).to eq(
+ I18n.t(
+ "discourse_push_notifications.popup.posted",
+ site_title: SiteSetting.title,
+ topic: topic_title,
+ username: username,
+ ),
+ )
+ end
+ end
end
end