diff --git a/app/models/notification/default_payload.rb b/app/models/notification/default_payload.rb index fc2d5290a..9cabc4988 100644 --- a/app/models/notification/default_payload.rb +++ b/app/models/notification/default_payload.rb @@ -23,6 +23,14 @@ class Notification::DefaultPayload notifications_url end + def category + "default" + end + + def high_priority? + false + end + private def card_url(card) Rails.application.routes.url_helpers.card_url(card, **url_options) diff --git a/app/models/notification/event_payload.rb b/app/models/notification/event_payload.rb index ebdb807b9..fba3a6cb2 100644 --- a/app/models/notification/event_payload.rb +++ b/app/models/notification/event_payload.rb @@ -36,6 +36,18 @@ class Notification::EventPayload < Notification::DefaultPayload end end + def category + case event.action + when "card_assigned" then "assignment" + when "comment_created" then "comment" + else "card" + end + end + + def high_priority? + event.action.card_assigned? + end + private def event notification.source diff --git a/app/models/notification/mention_payload.rb b/app/models/notification/mention_payload.rb index f07cbf20f..480771c15 100644 --- a/app/models/notification/mention_payload.rb +++ b/app/models/notification/mention_payload.rb @@ -13,6 +13,14 @@ class Notification::MentionPayload < Notification::DefaultPayload card_url(card) end + def category + "mention" + end + + def high_priority? + true + end + private def mention notification.source diff --git a/saas/app/models/notification/push_target/native.rb b/saas/app/models/notification/push_target/native.rb index 38f3e5eb4..4a4e77be3 100644 --- a/saas/app/models/notification/push_target/native.rb +++ b/saas/app/models/notification/push_target/native.rb @@ -24,7 +24,7 @@ class Notification::PushTarget::Native < Notification::PushTarget ApplicationPushNotification .with_apple( aps: { - category: notification_category, + category: payload.category, "mutable-content": 1, "interruption-level": interruption_level } @@ -41,7 +41,7 @@ class Notification::PushTarget::Native < Notification::PushTarget card_id: card&.id, card_title: card&.title, creator_name: notification.creator.name, - category: notification_category + category: payload.category ) .new( title: payload.title, @@ -49,39 +49,17 @@ class Notification::PushTarget::Native < Notification::PushTarget badge: notification.user.notifications.unread.count, sound: "default", thread_id: card&.id, - high_priority: high_priority_notification? + high_priority: payload.high_priority? ) end - def notification_category - case notification.source - when Event - case notification.source.action - when "card_assigned" then "assignment" - when "comment_created" then "comment" - else "card" - end - when Mention - "mention" - else - "default" - end - end - def interruption_level - high_priority_notification? ? "time-sensitive" : "active" - end - - def high_priority_notification? - case notification.source - when Event then notification.source.action.card_assigned? - when Mention then true - else false - end + payload.high_priority? ? "time-sensitive" : "active" end def creator_avatar_url - return unless notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached? - Rails.application.routes.url_helpers.url_for(notification.creator.avatar) + if notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached? + Rails.application.routes.url_helpers.url_for(notification.creator.avatar) + end end end diff --git a/saas/test/models/notification/push_target/native_test.rb b/saas/test/models/notification/push_target/native_test.rb index 93fe546c5..162b7a32c 100644 --- a/saas/test/models/notification/push_target/native_test.rb +++ b/saas/test/models/notification/push_target/native_test.rb @@ -10,39 +10,26 @@ class Notification::PushTarget::NativeTest < ActiveSupport::TestCase @user.push_subscriptions.delete_all end - test "notification_category returns assignment for card_assigned" do + test "payload category returns assignment for card_assigned" do notification = notifications(:logo_assignment_kevin) - @identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone") - push = Notification::PushTarget::Native.new(notification) - - assert_equal "assignment", push.send(:notification_category) + assert_equal "assignment", notification.payload.category end - test "notification_category returns comment for comment_created" do + test "payload category returns comment for comment_created" do notification = notifications(:layout_commented_kevin) - @identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone") - push = Notification::PushTarget::Native.new(notification) - - assert_equal "comment", push.send(:notification_category) + assert_equal "comment", notification.payload.category end - test "notification_category returns mention for mentions" do + test "payload category returns mention for mentions" do notification = notifications(:logo_card_david_mention_by_jz) - notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone") - push = Notification::PushTarget::Native.new(notification) - - assert_equal "mention", push.send(:notification_category) + assert_equal "mention", notification.payload.category end - test "notification_category returns card for other card events" do - @identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone") - - push = Notification::PushTarget::Native.new(@notification) - - assert_equal "card", push.send(:notification_category) + test "payload category returns card for other card events" do + assert_equal "card", @notification.payload.category end