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

This commit is contained in:
Mark VanLandingham 2023-11-06 10:13:23 -06:00 committed by GitHub
parent c5e6e271a5
commit 047cae4b3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 12 deletions

View File

@ -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

View File

@ -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}"

View File

@ -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