FIX: fallback to default push notification icon if none exists (#16961)

Due to some changes we started notifying via push notifications on other
families of notifications. There are a total of about 30 or so possible
notification you could get, some can be pushed.

This fallback means that if for any reason we are unable to find an icon
for a push notification we just fallback to the Discourse logo.

Also go with a simple reply icon for watching first post.

Note, that in production `image_url` can return an exception if an image is
missing. This is not the case in test / development.
This commit is contained in:
Sam 2022-06-01 12:00:05 +10:00 committed by GitHub
parent bc8f651501
commit 7b4e338c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -5,7 +5,14 @@ class PushNotificationPusher
CONNECTION_TIMEOUT_SECONDS = 5
def self.push(user, payload)
message = nil
I18n.with_locale(user.effective_locale) do
notification_icon_name = Notification.types[payload[:notification_type]]
if !File.exist?(File.expand_path("../../app/assets/images/push-notifications/#{notification_icon_name}.png", __dir__))
notification_icon_name = "discourse"
end
notification_icon = 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]]}",
@ -15,7 +22,7 @@ class PushNotificationPusher
),
body: payload[:excerpt],
badge: get_badge,
icon: ActionController::Base.helpers.image_url("push-notifications/#{Notification.types[payload[:notification_type]]}.png"),
icon: notification_icon,
tag: payload[:tag] || "#{Discourse.current_hostname}-#{payload[:topic_id]}",
base_url: Discourse.base_url,
url: payload[:post_url],
@ -26,6 +33,8 @@ class PushNotificationPusher
send_notification(user, subscription, message)
end
end
message
end
def self.subscriptions(user)

View File

@ -30,17 +30,27 @@ RSpec.describe PushNotificationPusher do
PushSubscription.create!(user_id: user.id, data: data)
end
def execute_push
def execute_push(notification_type: 1)
PushNotificationPusher.push(user, {
topic_title: 'Topic',
username: 'system',
excerpt: 'description',
topic_id: 1,
post_url: "https://example.com/t/1/2",
notification_type: 1
notification_type: notification_type
})
end
it "correctly guesses an image if missing" do
message = execute_push(notification_type: -1)
expect(message[:icon]).to eq("/assets/push-notifications/discourse.png")
end
it "correctly finds image if exists" do
message = execute_push(notification_type: 1)
expect(message[:icon]).to eq("/assets/push-notifications/mentioned.png")
end
it "sends notification in user's locale" do
SiteSetting.allow_user_locale = true
user.update!(locale: 'pt_BR')