command_tower 0.18.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/app/deserializers/command_tower/deserializers/messaging/preferences/update_deserializer.rb +10 -1
  3. data/app/errors/command_tower/errors/account/push_test_destination_error.rb +21 -0
  4. data/app/models/command_tower/messaging/delivery_attempt.rb +5 -0
  5. data/app/models/command_tower/messaging/inbox_item.rb +6 -0
  6. data/app/serializers/command_tower/serializers/messaging/inbox.rb +1 -1
  7. data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +2 -0
  8. data/app/services/command_tower/messaging/delivery_retry_policy.rb +97 -0
  9. data/app/services/command_tower/messaging/execution/adapter_result.rb +11 -1
  10. data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +199 -30
  11. data/app/services/command_tower/messaging/execution/adapters/expo/configuration.rb +5 -0
  12. data/app/services/command_tower/messaging/execution/adapters/expo/endpoint_ticket_history.rb +86 -0
  13. data/app/services/command_tower/messaging/execution/adapters/expo/http_client.rb +4 -0
  14. data/app/services/command_tower/messaging/execution/adapters/expo/interruption_level.rb +41 -0
  15. data/app/services/command_tower/messaging/execution/attempt_ledger.rb +45 -2
  16. data/app/services/command_tower/messaging/execution/expo_receipt_collection.rb +224 -0
  17. data/app/services/command_tower/messaging/execution/operation_logger.rb +65 -5
  18. data/app/services/command_tower/messaging/execution/recovery.rb +72 -8
  19. data/app/services/command_tower/messaging/inbox/reader.rb +15 -2
  20. data/app/services/command_tower/messaging/inbox/unread_count_result.rb +3 -2
  21. data/app/services/command_tower/messaging/notification_types/declaration.rb +19 -1
  22. data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +62 -0
  23. data/app/services/command_tower/messaging/notification_types/permitted_choice.rb +55 -0
  24. data/app/services/command_tower/messaging/notification_types/registry.rb +25 -0
  25. data/app/services/command_tower/messaging/notification_types.rb +3 -0
  26. data/app/services/command_tower/messaging/preferences/catalog.rb +7 -0
  27. data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +26 -1
  28. data/app/services/command_tower/messaging/preferences/effective_choice.rb +92 -0
  29. data/app/services/command_tower/messaging/preferences/preference_state.rb +24 -4
  30. data/app/services/command_tower/messaging/preferences/store.rb +10 -0
  31. data/app/services/command_tower/messaging/preferences/update.rb +36 -4
  32. data/app/services/command_tower/messaging/rendering/channel_renderer.rb +63 -3
  33. data/app/services/command_tower/messaging/rendering/inbox_document.rb +27 -0
  34. data/app/services/command_tower/messaging/rendering/push_presentation.rb +90 -0
  35. data/app/services/command_tower/messaging/rendering/rendered_push_payload.rb +54 -6
  36. data/app/services/command_tower/messaging/sound_catalog.rb +85 -0
  37. data/app/services/command_tower/services/account/push/send_self_test.rb +7 -1
  38. data/app/services/command_tower/services/messaging/communications/produce.rb +27 -0
  39. data/app/services/command_tower/services/messaging/inbox.rb +5 -1
  40. data/app/workflows/command_tower/workflows/me/error_mapping.rb +1 -0
  41. data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +24 -1
  42. data/db/migrate/20261012000001_add_provider_metadata_to_messaging_delivery_attempts.rb +7 -0
  43. data/db/migrate/20261012000002_add_finished_at_index_to_messaging_delivery_attempts.rb +8 -0
  44. data/docs/upgrades/0.19.0.md +40 -0
  45. data/docs/upgrades/0.20.0.md +29 -0
  46. data/docs/upgrades/README.md +2 -0
  47. data/lib/command_tower/configuration/messaging/config.rb +10 -0
  48. data/lib/command_tower/configuration/messaging/expo.rb +61 -0
  49. data/lib/command_tower/install/baseline.rb +2 -0
  50. data/lib/command_tower/version.rb +1 -1
  51. metadata +14 -2
@@ -85,7 +85,7 @@ module CommandTower
85
85
  end
86
86
 
87
87
  hash = @preference_state.to_h.transform_keys(&:to_s)
88
- unknown_keys = hash.keys - %w[channels inbox]
88
+ unknown_keys = hash.keys - %w[channels inbox urgency sound]
89
89
  if unknown_keys.any?
90
90
  raise InvalidPreferenceWriteError,
91
91
  "unsupported preference keys: #{unknown_keys.join(', ')}"
@@ -94,7 +94,12 @@ module CommandTower
94
94
  channels = parse_channels!(declaration, hash["channels"])
95
95
  inbox = parse_inbox!(declaration, hash)
96
96
 
97
- PreferenceState.new(channels: channels.freeze, inbox:).freeze
97
+ PreferenceState.new(
98
+ channels: channels.freeze,
99
+ inbox:,
100
+ urgency: parse_choice!(declaration, hash, "urgency"),
101
+ sound: parse_choice!(declaration, hash, "sound"),
102
+ ).freeze
98
103
  end
99
104
 
100
105
  def parse_channels!(declaration, channels_raw)
@@ -136,6 +141,13 @@ module CommandTower
136
141
  hash["inbox"]
137
142
  end
138
143
 
144
+ def parse_choice!(declaration, hash, field)
145
+ return nil unless hash.key?(field)
146
+
147
+ EffectiveChoice.validate_write!(declaration:, field: field.to_sym, value: hash[field])
148
+ hash[field].strip
149
+ end
150
+
139
151
  def sparsify(declaration, candidate)
140
152
  defaults = PreferenceState.from_declaration_default(declaration.default_preference_state)
141
153
  sparse_channels = {}
@@ -154,9 +166,22 @@ module CommandTower
154
166
  candidate.inbox
155
167
  end
156
168
 
157
- return nil if sparse_channels.empty? && sparse_inbox.nil?
169
+ sparse_urgency = sparse_choice(candidate.urgency, declaration.urgency.default)
170
+ sparse_sound = sparse_choice(candidate.sound, declaration.sound.default)
171
+ return nil if sparse_channels.empty? && sparse_inbox.nil? && sparse_urgency.nil? && sparse_sound.nil?
172
+
173
+ PreferenceState.new(
174
+ channels: sparse_channels.freeze,
175
+ inbox: sparse_inbox,
176
+ urgency: sparse_urgency,
177
+ sound: sparse_sound,
178
+ ).freeze
179
+ end
180
+
181
+ def sparse_choice(value, default)
182
+ return nil if value.nil? || value == default
158
183
 
159
- PreferenceState.new(channels: sparse_channels.freeze, inbox: sparse_inbox).freeze
184
+ value
160
185
  end
161
186
 
162
187
  def persist_reset!
@@ -227,6 +252,11 @@ module CommandTower
227
252
  available =
228
253
  declaration.allowed_channels & @platform_enabled_channels & recipient_ready_keys
229
254
 
255
+ blocks = EffectiveChoice.call(
256
+ declaration:,
257
+ stored: Store.find(recipient_id: @recipient_id, notification_type_key: @notification_type_key),
258
+ )
259
+
230
260
  ::CommandTower::Messaging::Preferences::CatalogNotificationResult.build(
231
261
  key: declaration.key,
232
262
  label: declaration.label,
@@ -240,6 +270,8 @@ module CommandTower
240
270
  inbox_enabled:,
241
271
  channels:,
242
272
  stored_override_present: evaluation.stored_override_present,
273
+ urgency: blocks[:urgency],
274
+ sound: blocks[:sound],
243
275
  )
244
276
  end
245
277
  end
@@ -14,11 +14,12 @@ module CommandTower
14
14
  TEMPLATE_FILENAME_PATTERN = /\A(?<basename>[a-z]+)\.(?<format>html|text)\.erb\z/
15
15
  DEFAULT_TITLE = "Notification"
16
16
 
17
- def self.render(communication:, channel_key:, recipient_address:)
17
+ def self.render(communication:, channel_key:, recipient_address:, channel_delivery: nil)
18
18
  new(
19
19
  communication:,
20
20
  channel_key:,
21
21
  recipient_address:,
22
+ channel_delivery:,
22
23
  ).render
23
24
  end
24
25
 
@@ -26,10 +27,11 @@ module CommandTower
26
27
  SUPPORTED_CHANNELS.include?(channel_key.to_s)
27
28
  end
28
29
 
29
- def initialize(communication:, channel_key:, recipient_address:)
30
+ def initialize(communication:, channel_key:, recipient_address:, channel_delivery: nil)
30
31
  @communication = communication
31
32
  @channel_key = channel_key.to_s
32
33
  @recipient_address = recipient_address
34
+ @channel_delivery = channel_delivery
33
35
  end
34
36
 
35
37
  def render
@@ -98,11 +100,18 @@ module CommandTower
98
100
  body = render_template("push.text.erb").to_s.strip
99
101
  raise ArgumentError, "body is required" if body.empty?
100
102
 
103
+ deep_link = resolve_push_deep_link
104
+ presentation = PushPresentation.call(communication: @communication, channel_delivery: @channel_delivery)
105
+
101
106
  RenderedPushPayload.build(
102
107
  recipient_address: address,
103
108
  title:,
104
109
  body:,
105
- deep_link: template_locals[:deep_link],
110
+ deep_link:,
111
+ sound: presentation.sound,
112
+ urgency: presentation.urgency,
113
+ thread_id: presentation.thread_id,
114
+ badge: presentation.badge,
106
115
  )
107
116
  end
108
117
 
@@ -164,6 +173,57 @@ module CommandTower
164
173
  deep_link.presence
165
174
  end
166
175
 
176
+ # Metadata deep_link wins. A live inbox item is a destination by itself.
177
+ # Otherwise substitute the host Home link. Never invent a link.
178
+ def resolve_push_deep_link
179
+ existing = deep_link_from_metadata
180
+ return existing if existing
181
+ return nil if live_inbox_item
182
+
183
+ home = CommandTower.config.messaging.expo.resolve_home_deep_link
184
+ if home.nil?
185
+ log_push_destination_fallback(reason_code: "home_deep_link_unresolved", level: :error)
186
+ raise RenderError.new(code: "render_failed", error_class: "HomeDeepLinkUnresolved")
187
+ end
188
+
189
+ if inbox_preference_disabled?
190
+ log_push_destination_fallback(reason_code: "inbox_disabled", level: :info)
191
+ else
192
+ log_push_destination_fallback(reason_code: "destination_missing", level: :error)
193
+ end
194
+ home
195
+ end
196
+
197
+ def live_inbox_item
198
+ item = @communication.inbox_item
199
+ return nil unless item&.live_for_push?
200
+
201
+ item
202
+ end
203
+
204
+ def inbox_preference_disabled?
205
+ decision = @communication.destination_plan&.decision
206
+ return false unless decision.is_a?(Hash)
207
+
208
+ excluded = decision["excluded_destinations"] || decision[:excluded_destinations]
209
+ Array(excluded).any? do |item|
210
+ next false unless item.is_a?(Hash)
211
+
212
+ destination = (item["destination"] || item[:destination]).to_s
213
+ reason = (item["reason_class"] || item[:reason_class]).to_s
214
+ destination == "inbox" && reason == Preferences::ReasonClasses::SUPPRESSED_BY_PREFERENCE
215
+ end
216
+ end
217
+
218
+ def log_push_destination_fallback(reason_code:, level:)
219
+ Execution::OperationLogger.push_destination_fallback(
220
+ communication: @communication,
221
+ reason_code:,
222
+ level:,
223
+ channel_delivery: @channel_delivery,
224
+ )
225
+ end
226
+
167
227
  def frozen_metadata
168
228
  metadata = @communication.metadata
169
229
  return {}.freeze unless metadata.is_a?(Hash)
@@ -92,6 +92,16 @@ module CommandTower
92
92
  self
93
93
  end
94
94
 
95
+ # ISO-8601 instant with an explicit offset, stored as UTC. Blank,
96
+ # unparseable, and offset-less values add no block.
97
+ def instant(at)
98
+ utc = normalized_instant(at)
99
+ return self unless utc
100
+
101
+ @blocks << { type: "instant", at: utc.iso8601 }.freeze
102
+ self
103
+ end
104
+
95
105
  def blocks
96
106
  @blocks
97
107
  end
@@ -116,6 +126,23 @@ module CommandTower
116
126
  path.is_a?(String) && path.start_with?("/") && !path.start_with?("//")
117
127
  end
118
128
 
129
+ EXPLICIT_ZONE_ISO = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})\z/
130
+
131
+ def normalized_instant(at)
132
+ case at
133
+ when Time
134
+ at.getutc
135
+ when String
136
+ stripped = at.strip
137
+ return nil if stripped.empty?
138
+ return nil unless EXPLICIT_ZONE_ISO.match?(stripped)
139
+
140
+ Time.iso8601(stripped).getutc
141
+ end
142
+ rescue ArgumentError
143
+ nil
144
+ end
145
+
119
146
  def safe_href?(href)
120
147
  return false unless href.is_a?(String) && !href.strip.empty?
121
148
 
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Messaging
5
+ module Rendering
6
+ # Resolves how a push should present itself, in provider-neutral terms: the member's
7
+ # effective urgency, the sound asset (nil for System Default), the conversation
8
+ # thread, and, for a badge-eligible type, the recipient's current badge-eligible
9
+ # unread count. Reads the member's stored preference once. Reads the unread count
10
+ # once, through Messaging::Inbox, only when the declaration is badge-eligible.
11
+ # The provider adapter maps the result to its own fields. Never raises for an
12
+ # unregistered type or an unreadable preference: it returns defaults and records
13
+ # a fallback diagnostic instead. An eligible type keeps its badge on that
14
+ # preference fallback. Any other error, including a count failure, surfaces.
15
+ class PushPresentation
16
+ Result = Data.define(:urgency, :sound, :thread_id, :badge)
17
+
18
+ DEFAULT_URGENCY = "normal"
19
+
20
+ def self.call(communication:, channel_delivery: nil)
21
+ new(communication:, channel_delivery:).call
22
+ end
23
+
24
+ def initialize(communication:, channel_delivery: nil)
25
+ @communication = communication
26
+ @channel_delivery = channel_delivery
27
+ end
28
+
29
+ def call
30
+ declaration = NotificationTypes.lookup(@communication.notification_type_key)
31
+ badge = badge_count(declaration)
32
+ blocks = Preferences::EffectiveChoice.call(declaration:, stored: stored_state(declaration))
33
+
34
+ build(
35
+ urgency: blocks.dig(:urgency, :effective),
36
+ sound: sound_asset(blocks.dig(:sound, :effective)),
37
+ badge:,
38
+ )
39
+ rescue NotificationTypes::NotFoundError
40
+ fall_back(reason_code: "type_unregistered")
41
+ rescue Preferences::StoreError, Preferences::InvalidPreferenceStateError
42
+ fall_back(reason_code: "preference_unreadable", badge:)
43
+ end
44
+
45
+ private
46
+
47
+ # A type the member cannot configure ignores any stored override, as the
48
+ # channel evaluation does.
49
+ def stored_state(declaration)
50
+ return nil unless declaration.user_configurable
51
+
52
+ Preferences::Store.find(
53
+ recipient_id: @communication.user_id,
54
+ notification_type_key: declaration.key,
55
+ )
56
+ end
57
+
58
+ def sound_asset(sound_id)
59
+ SoundCatalog.fetch(sound_id)&.fetch(:asset)
60
+ end
61
+
62
+ # Nil for a type that is not badge-eligible, so the adapter omits Expo `badge`.
63
+ # The count is NPB-4's absolute eligible unread total, including 0.
64
+ def badge_count(declaration)
65
+ return nil unless declaration.badge_eligible
66
+
67
+ Inbox.unread_count(recipient_id: @communication.user_id).badge_count
68
+ end
69
+
70
+ def build(urgency:, sound:, badge: nil)
71
+ Result.new(
72
+ urgency:,
73
+ sound:,
74
+ thread_id: @communication.conversation_key.presence,
75
+ badge:,
76
+ ).freeze
77
+ end
78
+
79
+ def fall_back(reason_code:, badge: nil)
80
+ Execution::OperationLogger.push_presentation_fallback(
81
+ communication: @communication,
82
+ reason_code:,
83
+ channel_delivery: @channel_delivery,
84
+ )
85
+ build(urgency: DEFAULT_URGENCY, sound: nil, badge:)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -6,14 +6,33 @@ module CommandTower
6
6
  # Provider-neutral Expo push rendered payload. recipient_address is the opaque
7
7
  # first eligible endpoint id for the ChannelRenderer gate — never the Expo token.
8
8
  # Fan-out uses AdapterRequest#eligible_endpoint_ids.
9
+ #
10
+ # `sound` is the host sound asset file name, or nil for System Default. `urgency` is
11
+ # the member's effective urgency level. `thread_id` is the conversation key, or nil
12
+ # for a standalone message. `badge` is the absolute badge-eligible unread count, or
13
+ # nil when the type is not badge-eligible. The provider adapter maps them to its
14
+ # own fields. A badge of 0 is a real count.
9
15
  RenderedPushPayload = Data.define(
10
16
  :recipient_address,
11
17
  :title,
12
18
  :body,
13
19
  :deep_link,
20
+ :sound,
21
+ :urgency,
22
+ :thread_id,
23
+ :badge,
14
24
  ) do
15
- def self.build(recipient_address:, title:, body:, deep_link: nil)
16
- values = [recipient_address, title, body, deep_link]
25
+ def self.build(
26
+ recipient_address:,
27
+ title:,
28
+ body:,
29
+ deep_link: nil,
30
+ sound: nil,
31
+ urgency: "normal",
32
+ thread_id: nil,
33
+ badge: nil
34
+ )
35
+ values = [recipient_address, title, body, deep_link, sound, urgency, thread_id, badge]
17
36
  raise ArgumentError, "arbitrary hashes are not accepted" if values.any? { |v| v.is_a?(Hash) }
18
37
  raise ArgumentError, "ActiveRecord objects are not accepted" if values.any? { |v| active_record_like?(v) }
19
38
  raise ArgumentError, "raw exception objects are not accepted" if values.any? { |v| v.is_a?(Exception) }
@@ -21,16 +40,22 @@ module CommandTower
21
40
  validate_required_string!(recipient_address, "recipient_address")
22
41
  validate_required_string!(title, "title")
23
42
  validate_required_string!(body, "body")
24
- unless deep_link.nil?
25
- raise ArgumentError, "deep_link must be a String" unless deep_link.is_a?(String)
26
- raise ArgumentError, "deep_link must not be blank when present" if deep_link.strip.empty?
27
- end
43
+ validate_optional_string!(deep_link, "deep_link")
44
+ validate_optional_string!(sound, "sound")
45
+ validate_urgency!(urgency)
46
+ validate_optional_string!(thread_id, "thread_id")
47
+ validate_badge!(badge)
48
+ raise ArgumentError, "thread_id must be at most 128 characters" if thread_id && thread_id.length > 128
28
49
 
29
50
  new(
30
51
  recipient_address:,
31
52
  title:,
32
53
  body:,
33
54
  deep_link:,
55
+ sound:,
56
+ urgency:,
57
+ thread_id:,
58
+ badge:,
34
59
  ).freeze
35
60
  end
36
61
 
@@ -40,6 +65,29 @@ module CommandTower
40
65
  end
41
66
  private_class_method :validate_required_string!
42
67
 
68
+ def self.validate_optional_string!(value, field_name)
69
+ return if value.nil?
70
+
71
+ raise ArgumentError, "#{field_name} must be a String" unless value.is_a?(String)
72
+ raise ArgumentError, "#{field_name} must not be blank when present" if value.strip.empty?
73
+ end
74
+ private_class_method :validate_optional_string!
75
+
76
+ def self.validate_urgency!(urgency)
77
+ return if NotificationTypes::URGENCY_LEVELS.include?(urgency)
78
+
79
+ raise ArgumentError, "urgency must be one of #{NotificationTypes::URGENCY_LEVELS.join(', ')}"
80
+ end
81
+ private_class_method :validate_urgency!
82
+
83
+ def self.validate_badge!(badge)
84
+ return if badge.nil?
85
+ return if badge.is_a?(Integer) && badge >= 0
86
+
87
+ raise ArgumentError, "badge must be a nonnegative integer"
88
+ end
89
+ private_class_method :validate_badge!
90
+
43
91
  def self.active_record_like?(value)
44
92
  defined?(ActiveRecord::Base) && value.is_a?(ActiveRecord::Base)
45
93
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Messaging
5
+ # Registered push sounds. The engine owns System Default, a logical id that needs no
6
+ # bundled native asset. Hosts append custom entries through
7
+ # `config.messaging.sounds` (`{ id:, label:, asset: }`); `asset` is the native file
8
+ # reference. Whether the file is physically bundled is verified later (IOS), not here.
9
+ module SoundCatalog
10
+ module_function
11
+
12
+ ID_FORMAT = /\A[a-z][a-z0-9_]*\z/
13
+ # iOS plays a custom push sound only for a bundled file name with one of these extensions.
14
+ ASSET_EXTENSIONS = %w[.wav .aiff .caf].freeze
15
+ SYSTEM_DEFAULT = { id: "default", label: "System Default", asset: nil }.freeze
16
+
17
+ def entries
18
+ [SYSTEM_DEFAULT] + host_entries.grep(Hash).map { |entry| entry.transform_keys(&:to_sym).freeze }
19
+ end
20
+
21
+ def ids
22
+ entries.pluck(:id)
23
+ end
24
+
25
+ def fetch(id)
26
+ entries.find { |entry| entry[:id] == id.to_s }
27
+ end
28
+
29
+ # Messages for an invalid host catalog; empty when valid. Registry seal raises on them.
30
+ def validation_errors
31
+ errors = []
32
+ seen = [SYSTEM_DEFAULT[:id]]
33
+
34
+ host_entries.each_with_index do |entry, index|
35
+ label = "sounds[#{index}]"
36
+ unless entry.is_a?(Hash)
37
+ errors << "#{label} must be a hash"
38
+ next
39
+ end
40
+
41
+ entry = entry.transform_keys(&:to_sym)
42
+ errors.concat(entry_errors(entry, label:, seen:))
43
+ seen << entry[:id]
44
+ end
45
+
46
+ errors
47
+ end
48
+
49
+ def host_entries
50
+ Array(CommandTower.config.messaging.sounds)
51
+ end
52
+ private_class_method :host_entries
53
+
54
+ def entry_errors(entry, label:, seen:)
55
+ id = entry[:id]
56
+ errors = []
57
+
58
+ if !id.is_a?(String) || !id.match?(ID_FORMAT)
59
+ errors << "#{label} id must match #{ID_FORMAT.inspect}"
60
+ elsif seen.include?(id)
61
+ reason = id == SYSTEM_DEFAULT[:id] ? "reserved for System Default" : "duplicated"
62
+ errors << "#{label} id #{id.inspect} is #{reason}"
63
+ end
64
+
65
+ errors << "#{label} label must be present" unless present_string?(entry[:label])
66
+ errors.concat(asset_errors(entry[:asset], label:))
67
+ errors
68
+ end
69
+ private_class_method :entry_errors
70
+
71
+ def asset_errors(asset, label:)
72
+ return ["#{label} asset must be present"] unless present_string?(asset)
73
+ return [] if ASSET_EXTENSIONS.include?(File.extname(asset).downcase)
74
+
75
+ ["#{label} asset must end in #{ASSET_EXTENSIONS.join(', ')}"]
76
+ end
77
+ private_class_method :asset_errors
78
+
79
+ def present_string?(value)
80
+ value.is_a?(String) && !value.strip.empty?
81
+ end
82
+ private_class_method :present_string?
83
+ end
84
+ end
85
+ end
@@ -31,13 +31,19 @@ module CommandTower
31
31
  return
32
32
  end
33
33
 
34
+ deep_link = CommandTower.config.messaging.expo.resolve_self_test_deep_link
35
+ if deep_link.nil?
36
+ context.fail!(application_error: CommandTower::Errors::Account::PushTestDestinationError.new)
37
+ return
38
+ end
39
+
34
40
  produce_result = CommandTower::Services::Messaging::Communications::Produce.call(
35
41
  user:,
36
42
  notification_type_key: NOTIFICATION_TYPE_KEY,
37
43
  host_event_identity: "push_delivery_test/#{user.id}/#{SecureRandom.uuid}",
38
44
  title: TITLE,
39
45
  body: BODY,
40
- metadata: {},
46
+ metadata: { "deep_link" => deep_link },
41
47
  platform_enabled_channels: CommandTower::Services::Messaging::Preferences::PlatformEnabledChannels.call,
42
48
  )
43
49
  unless produce_result.success?
@@ -25,6 +25,15 @@ module CommandTower
25
25
  return
26
26
  end
27
27
 
28
+ if missing_required_deep_link?
29
+ context.fail!(
30
+ application_error: CommandTower::Errors::ValidationError.new(
31
+ details: { messaging: "deep_link is required" },
32
+ ),
33
+ )
34
+ return
35
+ end
36
+
28
37
  accept_result = CommandTower::Messaging.accept(
29
38
  recipient_id: recipient_result.data[:recipient_id],
30
39
  notification_type_key:,
@@ -65,6 +74,24 @@ module CommandTower
65
74
  rescue CommandTower::Messaging::Accept::Error
66
75
  context.fail!(application_error: CommandTower::Errors::InternalError.new)
67
76
  end
77
+
78
+ private
79
+
80
+ def missing_required_deep_link?
81
+ return false unless CommandTower::Messaging::NotificationTypes.registered?(notification_type_key)
82
+
83
+ declaration = CommandTower::Messaging::NotificationTypes.lookup(notification_type_key)
84
+ return false unless declaration.deep_link_required
85
+
86
+ !metadata_deep_link_present?
87
+ end
88
+
89
+ def metadata_deep_link_present?
90
+ return false unless metadata.is_a?(Hash)
91
+
92
+ value = metadata["deep_link"] || metadata[:deep_link]
93
+ value.is_a?(String) && !value.strip.empty?
94
+ end
68
95
  end
69
96
  end
70
97
  end
@@ -116,7 +116,11 @@ module CommandTower
116
116
  validate :user, is_a: User, required: true
117
117
 
118
118
  def call
119
- with_recipient { |recipient_id| context.count = CommandTower::Messaging::Inbox.unread_count(recipient_id:).count }
119
+ with_recipient do |recipient_id|
120
+ result = CommandTower::Messaging::Inbox.unread_count(recipient_id:)
121
+ context.count = result.count
122
+ context.badge_count = result.badge_count
123
+ end
120
124
  end
121
125
  end
122
126
 
@@ -26,6 +26,7 @@ module CommandTower
26
26
  CommandTower::Errors::Account::PushoverVerificationFailedError,
27
27
  CommandTower::Errors::Account::PushEndpointNotFoundError,
28
28
  CommandTower::Errors::Account::PushTestNoEndpointError,
29
+ CommandTower::Errors::Account::PushTestDestinationError,
29
30
  CommandTower::Errors::ValidationError
30
31
  :unprocessable_entity
31
32
  when CommandTower::Errors::Account::PhoneVerificationThrottledError,
@@ -108,6 +108,25 @@ module CommandTower
108
108
  )
109
109
  end
110
110
 
111
+ unless CommandTower::Messaging::DeliveryRetryPolicy.push_may_submit_to_provider?(
112
+ delivery,
113
+ current_attempt_id: attempt.id,
114
+ )
115
+ ledger.finalize_terminal(
116
+ delivery:,
117
+ attempt:,
118
+ error_code: CommandTower::Messaging::DeliveryRetryPolicy::SUBMISSION_UNCERTAIN,
119
+ )
120
+ return success(
121
+ payload: {
122
+ outcome: :terminal,
123
+ channel_delivery_id: delivery.id,
124
+ error_code: CommandTower::Messaging::DeliveryRetryPolicy::SUBMISSION_UNCERTAIN,
125
+ },
126
+ http_status: :ok,
127
+ )
128
+ end
129
+
111
130
  request = CommandTower::Messaging::Execution::AdapterRequest.build(
112
131
  channel_delivery_id: delivery.id,
113
132
  communication_id: delivery.communication_id,
@@ -154,7 +173,7 @@ module CommandTower
154
173
 
155
174
  def load_delivery(channel_delivery_id)
156
175
  CommandTower::Messaging::ChannelDelivery.includes(
157
- communication: %i[user destination_plan],
176
+ communication: %i[user destination_plan inbox_item],
158
177
  ).find_by(id: channel_delivery_id)
159
178
  end
160
179
 
@@ -172,6 +191,7 @@ module CommandTower
172
191
  communication:,
173
192
  channel_key: delivery.channel_key,
174
193
  recipient_address:,
194
+ channel_delivery: delivery,
175
195
  )
176
196
 
177
197
  CommandTower::Messaging::Execution::OperationLogger.render_succeeded(
@@ -204,18 +224,21 @@ module CommandTower
204
224
  CommandTower::Messaging::Execution::OperationLogger.adapter_accepted(
205
225
  channel_delivery: delivery,
206
226
  delivery_attempt: attempt,
227
+ provider_metadata: result.provider_metadata,
207
228
  )
208
229
  elsif result.retryable_failure?
209
230
  CommandTower::Messaging::Execution::OperationLogger.adapter_retryable(
210
231
  channel_delivery: delivery,
211
232
  delivery_attempt: attempt,
212
233
  error_code: result.error_code,
234
+ provider_metadata: result.provider_metadata,
213
235
  )
214
236
  else
215
237
  CommandTower::Messaging::Execution::OperationLogger.adapter_terminal(
216
238
  channel_delivery: delivery,
217
239
  delivery_attempt: attempt,
218
240
  error_code: result.error_code,
241
+ provider_metadata: result.provider_metadata,
219
242
  )
220
243
  end
221
244
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddProviderMetadataToMessagingDeliveryAttempts < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_column :messaging_delivery_attempts, :provider_metadata, :json
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddFinishedAtIndexToMessagingDeliveryAttempts < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_index :messaging_delivery_attempts, :finished_at,
6
+ name: "index_messaging_delivery_attempts_on_finished_at"
7
+ end
8
+ end