FIX: send notification in user's locale if available. (#12215)

Previously, it was sending notifications in site's default locale.
This commit is contained in:
Vinoth Kannan 2021-02-25 23:40:37 +05:30 committed by GitHub
parent 93a0a906b5
commit 0e65c2b3c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 18 deletions

View File

@ -4,25 +4,27 @@ class PushNotificationPusher
TOKEN_VALID_FOR_SECONDS ||= 5 * 60 TOKEN_VALID_FOR_SECONDS ||= 5 * 60
def self.push(user, payload) def self.push(user, payload)
message = { I18n.with_locale(user.effective_locale) do
title: I18n.t( message = {
"discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}", title: I18n.t(
site_title: SiteSetting.title, "discourse_push_notifications.popup.#{Notification.types[payload[:notification_type]]}",
topic: payload[:topic_title], site_title: SiteSetting.title,
username: payload[:username] topic: payload[:topic_title],
), username: payload[:username]
body: payload[:excerpt], ),
badge: get_badge, body: payload[:excerpt],
icon: ActionController::Base.helpers.image_url("push-notifications/#{Notification.types[payload[:notification_type]]}.png"), badge: get_badge,
tag: "#{Discourse.current_hostname}-#{payload[:topic_id]}", icon: ActionController::Base.helpers.image_url("push-notifications/#{Notification.types[payload[:notification_type]]}.png"),
base_url: Discourse.base_url, tag: "#{Discourse.current_hostname}-#{payload[:topic_id]}",
url: payload[:post_url], base_url: Discourse.base_url,
hide_when_active: true url: payload[:post_url],
} hide_when_active: true
}
subscriptions(user).each do |subscription| subscriptions(user).each do |subscription|
subscription = JSON.parse(subscription.data) subscription = JSON.parse(subscription.data)
send_notification(user, subscription, message) send_notification(user, subscription, message)
end
end end
end end

View File

@ -16,4 +16,30 @@ RSpec.describe PushNotificationPusher do
.to eq(UrlHelper.absolute(upload.url)) .to eq(UrlHelper.absolute(upload.url))
end end
it "sends notification in user's locale" do
SiteSetting.allow_user_locale = true
user = Fabricate(:user, locale: 'pt_BR')
PushSubscription.create!(user_id: user.id, data: "{\"endpoint\": \"endpoint\"}")
PushNotificationPusher.expects(:send_notification).with(user, { "endpoint" => "endpoint" }, {
title: "system mencionou você em \"Topic\" - Discourse",
body: "description",
badge: "/assets/push-notifications/discourse.png",
icon: "/assets/push-notifications/mentioned.png",
tag: "test.localhost-1",
base_url: "http://test.localhost",
url: "https://example.com/t/1/2",
hide_when_active: true
}).once
PushNotificationPusher.push(user, {
topic_title: 'Topic',
username: 'system',
excerpt: 'description',
topic_id: 1,
post_url: "https://example.com/t/1/2",
notification_type: 1
})
end
end end