Move category and high_priority to payload classes
Use polymorphism instead of case statements in Native push target: - DefaultPayload#category returns "default", #high_priority? returns false - EventPayload#category returns "assignment"/"comment"/"card" based on action - MentionPayload#category returns "mention", #high_priority? returns true This simplifies the Native push target by delegating source-specific logic to the appropriate payload classes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,14 @@ class Notification::DefaultPayload
|
|||||||
notifications_url
|
notifications_url
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def category
|
||||||
|
"default"
|
||||||
|
end
|
||||||
|
|
||||||
|
def high_priority?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def card_url(card)
|
def card_url(card)
|
||||||
Rails.application.routes.url_helpers.card_url(card, **url_options)
|
Rails.application.routes.url_helpers.card_url(card, **url_options)
|
||||||
|
|||||||
@@ -36,6 +36,18 @@ class Notification::EventPayload < Notification::DefaultPayload
|
|||||||
end
|
end
|
||||||
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
|
private
|
||||||
def event
|
def event
|
||||||
notification.source
|
notification.source
|
||||||
|
|||||||
@@ -13,6 +13,14 @@ class Notification::MentionPayload < Notification::DefaultPayload
|
|||||||
card_url(card)
|
card_url(card)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def category
|
||||||
|
"mention"
|
||||||
|
end
|
||||||
|
|
||||||
|
def high_priority?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def mention
|
def mention
|
||||||
notification.source
|
notification.source
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class Notification::PushTarget::Native < Notification::PushTarget
|
|||||||
ApplicationPushNotification
|
ApplicationPushNotification
|
||||||
.with_apple(
|
.with_apple(
|
||||||
aps: {
|
aps: {
|
||||||
category: notification_category,
|
category: payload.category,
|
||||||
"mutable-content": 1,
|
"mutable-content": 1,
|
||||||
"interruption-level": interruption_level
|
"interruption-level": interruption_level
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ class Notification::PushTarget::Native < Notification::PushTarget
|
|||||||
card_id: card&.id,
|
card_id: card&.id,
|
||||||
card_title: card&.title,
|
card_title: card&.title,
|
||||||
creator_name: notification.creator.name,
|
creator_name: notification.creator.name,
|
||||||
category: notification_category
|
category: payload.category
|
||||||
)
|
)
|
||||||
.new(
|
.new(
|
||||||
title: payload.title,
|
title: payload.title,
|
||||||
@@ -49,39 +49,17 @@ class Notification::PushTarget::Native < Notification::PushTarget
|
|||||||
badge: notification.user.notifications.unread.count,
|
badge: notification.user.notifications.unread.count,
|
||||||
sound: "default",
|
sound: "default",
|
||||||
thread_id: card&.id,
|
thread_id: card&.id,
|
||||||
high_priority: high_priority_notification?
|
high_priority: payload.high_priority?
|
||||||
)
|
)
|
||||||
end
|
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
|
def interruption_level
|
||||||
high_priority_notification? ? "time-sensitive" : "active"
|
payload.high_priority? ? "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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def creator_avatar_url
|
def creator_avatar_url
|
||||||
return unless notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached?
|
if notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached?
|
||||||
Rails.application.routes.url_helpers.url_for(notification.creator.avatar)
|
Rails.application.routes.url_helpers.url_for(notification.creator.avatar)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -10,39 +10,26 @@ class Notification::PushTarget::NativeTest < ActiveSupport::TestCase
|
|||||||
@user.push_subscriptions.delete_all
|
@user.push_subscriptions.delete_all
|
||||||
end
|
end
|
||||||
|
|
||||||
test "notification_category returns assignment for card_assigned" do
|
test "payload category returns assignment for card_assigned" do
|
||||||
notification = notifications(:logo_assignment_kevin)
|
notification = notifications(:logo_assignment_kevin)
|
||||||
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
|
|
||||||
|
|
||||||
push = Notification::PushTarget::Native.new(notification)
|
assert_equal "assignment", notification.payload.category
|
||||||
|
|
||||||
assert_equal "assignment", push.send(:notification_category)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "notification_category returns comment for comment_created" do
|
test "payload category returns comment for comment_created" do
|
||||||
notification = notifications(:layout_commented_kevin)
|
notification = notifications(:layout_commented_kevin)
|
||||||
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
|
|
||||||
|
|
||||||
push = Notification::PushTarget::Native.new(notification)
|
assert_equal "comment", notification.payload.category
|
||||||
|
|
||||||
assert_equal "comment", push.send(:notification_category)
|
|
||||||
end
|
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 = 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", notification.payload.category
|
||||||
|
|
||||||
assert_equal "mention", push.send(:notification_category)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "notification_category returns card for other card events" do
|
test "payload category returns card for other card events" do
|
||||||
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
|
assert_equal "card", @notification.payload.category
|
||||||
|
|
||||||
push = Notification::PushTarget::Native.new(@notification)
|
|
||||||
|
|
||||||
assert_equal "card", push.send(:notification_category)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user