command_tower 0.19.0 → 0.21.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 (29) hide show
  1. checksums.yaml +4 -4
  2. data/app/models/command_tower/messaging/delivery_attempt.rb +5 -0
  3. data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +109 -1
  4. data/app/services/command_tower/messaging/channels/definition.rb +3 -0
  5. data/app/services/command_tower/messaging/channels.rb +15 -0
  6. data/app/services/command_tower/messaging/delivery_retry_policy.rb +97 -0
  7. data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +71 -23
  8. data/app/services/command_tower/messaging/execution/adapters/expo/http_client.rb +4 -0
  9. data/app/services/command_tower/messaging/execution/attempt_ledger.rb +42 -2
  10. data/app/services/command_tower/messaging/execution/operation_logger.rb +10 -0
  11. data/app/services/command_tower/messaging/execution/recovery.rb +64 -8
  12. data/app/services/command_tower/messaging/notification_types/declaration.rb +15 -1
  13. data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +53 -0
  14. data/app/services/command_tower/messaging/preferences/catalog.rb +3 -32
  15. data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +35 -1
  16. data/app/services/command_tower/messaging/preferences/channel_catalog.rb +24 -0
  17. data/app/services/command_tower/messaging/preferences/corrections.rb +50 -0
  18. data/app/services/command_tower/messaging/preferences/destination_locks.rb +33 -0
  19. data/app/services/command_tower/messaging/preferences/destinations.rb +121 -0
  20. data/app/services/command_tower/messaging/preferences/notification_assembly.rb +77 -0
  21. data/app/services/command_tower/messaging/preferences/setting_controls.rb +25 -0
  22. data/app/services/command_tower/messaging/preferences/settings_projection.rb +126 -0
  23. data/app/services/command_tower/messaging/preferences/update.rb +57 -40
  24. data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +19 -0
  25. data/docs/upgrades/0.20.0.md +29 -0
  26. data/docs/upgrades/0.21.0.md +31 -0
  27. data/docs/upgrades/README.md +2 -0
  28. data/lib/command_tower/version.rb +1 -1
  29. metadata +11 -2
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Messaging
5
+ module Preferences
6
+ class SettingsProjection
7
+ PUSH_CHANNEL = "push"
8
+
9
+ URGENCY_LABELS = {
10
+ "normal" => "Normal",
11
+ "time_sensitive" => "Time Sensitive",
12
+ "critical" => "Critical",
13
+ }.freeze
14
+
15
+ URGENCY_DESCRIPTIONS = {
16
+ "normal" => "Delivered with the device’s normal notification behavior.",
17
+ "time_sensitive" => "Asks the device to treat this alert as time sensitive.",
18
+ "critical" =>
19
+ "Highest declared urgency. Actual delivery behavior depends on the application and device capabilities.",
20
+ }.freeze
21
+
22
+ SettingEntry = Data.define(
23
+ :key,
24
+ :channel_key,
25
+ :control,
26
+ :label,
27
+ :description,
28
+ :editable,
29
+ :effective,
30
+ :options,
31
+ )
32
+
33
+ SingleSelectDefinition = Data.define(
34
+ :key,
35
+ :channel_key,
36
+ :label,
37
+ :description,
38
+ :effective,
39
+ :options,
40
+ )
41
+
42
+ def self.for_notification(declaration:, blocks:, destinations:)
43
+ return [].freeze unless push_destination?(declaration, destinations)
44
+
45
+ definitions = []
46
+ urgency = blocks[:urgency]
47
+ if urgency[:selectable]
48
+ definitions << urgency_definition(urgency)
49
+ end
50
+
51
+ sound = blocks[:sound]
52
+ if sound[:selectable]
53
+ definitions << sound_definition(sound)
54
+ end
55
+
56
+ build(definitions)
57
+ end
58
+
59
+ def self.build(definitions)
60
+ Array(definitions).filter_map do |definition|
61
+ project_single_select(definition)
62
+ end.freeze
63
+ end
64
+
65
+ def self.push_destination?(declaration, destinations)
66
+ declaration.allowed_channels.include?(PUSH_CHANNEL) &&
67
+ destinations.key?(PUSH_CHANNEL)
68
+ end
69
+
70
+ def self.urgency_definition(urgency_block)
71
+ options =
72
+ Array(urgency_block[:allowed]).map do |value|
73
+ {
74
+ value:,
75
+ label: URGENCY_LABELS.fetch(value),
76
+ description: URGENCY_DESCRIPTIONS.fetch(value),
77
+ }.freeze
78
+ end
79
+
80
+ SingleSelectDefinition.new(
81
+ key: "urgency",
82
+ channel_key: PUSH_CHANNEL,
83
+ label: "Urgency",
84
+ description: nil,
85
+ effective: urgency_block[:effective].to_s,
86
+ options: options.freeze,
87
+ )
88
+ end
89
+
90
+ def self.sound_definition(sound_block)
91
+ options =
92
+ Array(sound_block[:allowed]).map do |entry|
93
+ id = entry[:id] || entry["id"]
94
+ label = entry[:label] || entry["label"]
95
+ { value: id, label:, description: nil }.freeze
96
+ end
97
+
98
+ SingleSelectDefinition.new(
99
+ key: "sound",
100
+ channel_key: PUSH_CHANNEL,
101
+ label: "Sound",
102
+ description: nil,
103
+ effective: sound_block[:effective].to_s,
104
+ options: options.freeze,
105
+ )
106
+ end
107
+
108
+ def self.project_single_select(definition)
109
+ SettingControls.validate_implemented!("single_select")
110
+
111
+ SettingEntry.new(
112
+ key: definition.key,
113
+ channel_key: definition.channel_key,
114
+ control: "single_select",
115
+ label: definition.label,
116
+ description: definition.description,
117
+ editable: true,
118
+ effective: definition.effective,
119
+ options: definition.options,
120
+ ).to_h.freeze
121
+ end
122
+ private_class_method :project_single_select
123
+ end
124
+ end
125
+ end
126
+ end
@@ -25,10 +25,14 @@ module CommandTower
25
25
  validate_settings_visible!(declaration)
26
26
  validate_writable!(declaration)
27
27
 
28
+ submitted_destinations = nil
29
+
28
30
  if reset_request?
29
31
  persist_reset!
30
32
  else
31
- candidate = build_candidate_state!(declaration)
33
+ patch_hash = normalized_preference_hash
34
+ submitted_destinations = extract_submitted_destinations(patch_hash)
35
+ candidate = build_candidate_state!(declaration, patch_hash:)
32
36
  sparse = sparsify(declaration, candidate)
33
37
  persist_sparse!(sparse)
34
38
  end
@@ -39,7 +43,12 @@ module CommandTower
39
43
  platform_enabled_channels: @platform_enabled_channels,
40
44
  )
41
45
 
42
- build_notification_result(declaration, evaluation, recipient_ready_channel_keys)
46
+ build_notification_result(
47
+ declaration,
48
+ evaluation,
49
+ recipient_ready_channel_keys,
50
+ submitted_destinations:,
51
+ )
43
52
  end
44
53
 
45
54
  private
@@ -79,24 +88,60 @@ module CommandTower
79
88
  @preference_state.respond_to?(:to_h) && @preference_state.to_h.empty?
80
89
  end
81
90
 
82
- def build_candidate_state!(declaration)
91
+ def normalized_preference_hash
83
92
  unless @preference_state.respond_to?(:to_h)
84
93
  raise InvalidPreferenceStateError, "preference_state must be a hash-like object"
85
94
  end
86
95
 
87
- hash = @preference_state.to_h.transform_keys(&:to_s)
96
+ @preference_state.to_h.transform_keys(&:to_s)
97
+ end
98
+
99
+ def extract_submitted_destinations(hash)
100
+ submitted = {}
101
+ submitted["inbox"] = hash["inbox"] if hash.key?("inbox")
102
+ if hash.key?("channels") && hash["channels"].respond_to?(:to_h)
103
+ hash["channels"].to_h.each do |raw_key, raw_value|
104
+ submitted[raw_key.to_s] = raw_value
105
+ end
106
+ end
107
+ submitted.freeze
108
+ end
109
+
110
+ def build_candidate_state!(declaration, patch_hash: nil)
111
+ hash = patch_hash || normalized_preference_hash
88
112
  unknown_keys = hash.keys - %w[channels inbox urgency sound]
89
113
  if unknown_keys.any?
90
114
  raise InvalidPreferenceWriteError,
91
115
  "unsupported preference keys: #{unknown_keys.join(', ')}"
92
116
  end
93
117
 
94
- channels = parse_channels!(declaration, hash["channels"])
95
- inbox = parse_inbox!(declaration, hash)
118
+ patch = build_patch_partial!(declaration, hash)
119
+ merged = patch.merge_over(stored_member_state)
120
+ DestinationLocks.apply(declaration:, state: merged)
121
+ end
122
+
123
+ def stored_member_state
124
+ Store.find(
125
+ recipient_id: @recipient_id,
126
+ notification_type_key: @notification_type_key,
127
+ ) || empty_stored_member_state
128
+ end
129
+
130
+ def empty_stored_member_state
131
+ PreferenceState.new(channels: {}.freeze, inbox: nil, urgency: nil, sound: nil).freeze
132
+ end
133
+
134
+ def build_patch_partial!(declaration, hash)
135
+ channels =
136
+ if hash.key?("channels")
137
+ parse_channels!(declaration, hash["channels"])
138
+ else
139
+ {}.freeze
140
+ end
96
141
 
97
142
  PreferenceState.new(
98
143
  channels: channels.freeze,
99
- inbox:,
144
+ inbox: parse_inbox!(declaration, hash),
100
145
  urgency: parse_choice!(declaration, hash, "urgency"),
101
146
  sound: parse_choice!(declaration, hash, "sound"),
102
147
  ).freeze
@@ -234,44 +279,16 @@ module CommandTower
234
279
  current.to_raw_hash == sparse.to_raw_hash
235
280
  end
236
281
 
237
- def build_notification_result(declaration, evaluation, recipient_ready_keys)
238
- effective_state = evaluation.effective_preference_state || {}
239
- effective_channels = effective_state["channels"] || {}
240
- channels =
241
- declaration.allowed_channels.to_h do |channel|
242
- [channel, !!effective_channels[channel]]
243
- end
244
-
245
- inbox_enabled =
246
- if effective_state.key?("inbox")
247
- !!effective_state["inbox"]
248
- else
249
- evaluation.inbox_permitted
250
- end
251
-
282
+ def build_notification_result(declaration, evaluation, recipient_ready_keys, submitted_destinations: nil)
252
283
  available =
253
284
  declaration.allowed_channels & @platform_enabled_channels & recipient_ready_keys
254
285
 
255
- blocks = EffectiveChoice.call(
286
+ NotificationAssembly.build(
256
287
  declaration:,
257
- stored: Store.find(recipient_id: @recipient_id, notification_type_key: @notification_type_key),
258
- )
259
-
260
- ::CommandTower::Messaging::Preferences::CatalogNotificationResult.build(
261
- key: declaration.key,
262
- label: declaration.label,
263
- description: declaration.description,
264
- order: declaration.type_order,
265
- configurable: declaration.user_configurable,
266
- mandatory: declaration.mandatory,
267
- inbox_available: declaration.inbox_available,
268
- allowed_channels: declaration.allowed_channels,
288
+ evaluation:,
269
289
  available_channels: available,
270
- inbox_enabled:,
271
- channels:,
272
- stored_override_present: evaluation.stored_override_present,
273
- urgency: blocks[:urgency],
274
- sound: blocks[:sound],
290
+ recipient_id: @recipient_id,
291
+ submitted_destinations:,
275
292
  )
276
293
  end
277
294
  end
@@ -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,
@@ -0,0 +1,29 @@
1
+ # Upgrade: CommandTower 0.20.0
2
+
3
+ **From:** `0.19.0`
4
+ **To:** `0.20.0`
5
+
6
+ Minor release: push delivery certainty — persist Expo ticket metadata reliably on MariaDB and stop duplicate Expo sends when submission is uncertain. Email retry policy and Inbox behavior are unchanged.
7
+
8
+ ## Host-visible changes
9
+
10
+ | Change | Host impact |
11
+ |--------|-------------|
12
+ | `DeliveryAttempt#provider_metadata` | JSON attribute caster (MariaDB LONGTEXT / `json_valid`). Accepted Expo ticket metadata persists on success and retryable non-submission paths. |
13
+ | Push execution recovery | Push-only duplicate-averse policy: at most one automatic retry when a finished attempt recorded `submission: not_submitted`. Uncertain outcomes (`submission_uncertain`), accepted tickets, and open `started` attempts without proof terminalize without another Expo POST. Push claim budget `PUSH_RETRY_LIMIT` = 2. Email keeps grace window and `MAX_ATTEMPTS` = 5. |
14
+ | `DeliverWorkflow` | Pre-send guard blocks Expo when prior push state does not allow another submission. |
15
+ | Expo adapter | Classifies pre-request failures as `not_submitted` (retryable once); post-dispatch uncertainty is terminal. |
16
+ | Observability | `messaging.execution.attempt.finalize_unpersisted` when attempt finalization cannot be persisted (diagnostic). |
17
+
18
+ ## Host actions
19
+
20
+ 1. Bump gem to `0.20.0` (or `>= 0.20.0`).
21
+ 2. **No new migration.** Run `bundle exec rails db:migrate` only if other pending host migrations exist.
22
+ 3. After deploy, legacy push rows with `started` attempts and null metadata are terminalized on the next recovery pass (no data repair script).
23
+
24
+ ## Not in this release
25
+
26
+ - No Inbox document or rendering changes.
27
+ - No email adapter or email retry cap changes.
28
+ - No new HTTP routes or workflows.
29
+ - No automatic backfill of historical `provider_metadata`.
@@ -0,0 +1,31 @@
1
+ # Upgrade: CommandTower 0.21.0
2
+
3
+ **From:** `0.20.0`
4
+ **To:** `0.21.0`
5
+
6
+ Minor release: member notification preferences expose a channel catalog, per-destination state, and ordered push settings, and preference PATCH merges onto stored member state. Legacy `urgency` and `sound` blocks remain. Push delivery and Inbox behavior are unchanged.
7
+
8
+ ## Host-visible changes
9
+
10
+ | Change | Host impact |
11
+ |--------|-------------|
12
+ | Preference GET / PATCH payload | Each settings-visible notification includes `channelCatalog` (`key`, `label`, `description`, `order`, `glyphKey`), `destinations` (`state`, `effective`, `reasonCode`, `reason`), and `settings` (`single_select` rows). `urgency` and `sound` blocks stay. |
13
+ | Channel catalog | Platform channels carry presentation copy: Inbox, Email, SMS, Push, Pushover (`description`, `order`, `glyphKey`). |
14
+ | `settings` | When Push is a destination, urgency and sound project as ordered `single_select` rows. Option labels and descriptions are server-owned. Sound options use catalog labels. |
15
+ | Preference PATCH | Partial bodies merge onto stored member state. Omitted `channels`, `inbox`, `urgency`, and `sound` are preserved. Validation still runs before any write. |
16
+ | `destination_locks` | Optional notification declaration (`forced_on` / `forced_off` per destination). Applied after the merge. Defaults to `{}`. Cannot force off a mandatory destination. |
17
+ | `corrections` | Present only when the member explicitly submitted a destination boolean that the server corrected (locks or mandatory required). Omitted keys do not emit corrections. |
18
+
19
+ ## Host actions
20
+
21
+ 1. Bump gem to `0.21.0` (or `>= 0.21.0`).
22
+ 2. **No new migration.** Run `bundle exec rails db:migrate` only if other pending host migrations exist.
23
+ 3. Clients that render notification settings should read `channelCatalog`, `destinations`, and `settings`. Existing `urgency` / `sound` consumers keep working.
24
+ 4. Set `destination_locks` on a notification declaration only when a destination must stay forced on or off.
25
+
26
+ ## Not in this release
27
+
28
+ - No new HTTP routes.
29
+ - No Inbox document or rendering changes.
30
+ - No push execution, Expo, or email retry changes.
31
+ - No change to preference PATCH keys (`channels`, `inbox`, `urgency`, `sound`).
@@ -4,6 +4,8 @@ Host-facing upgrade / change summaries for CommandTower releases.
4
4
 
5
5
  | Version | Summary |
6
6
  |---------|---------|
7
+ | [0.21.0](0.21.0.md) | Notification preference reads: channel catalog, destinations, settings rows; PATCH merges onto stored member state |
8
+ | [0.20.0](0.20.0.md) | Push delivery certainty: JSON `provider_metadata` caster; duplicate-averse push recovery and Expo submission guards |
7
9
  | [0.19.0](0.19.0.md) | Push presentation: badge, urgency, sound, deep links; Expo ticket and receipt metadata; preference urgency and sound |
8
10
  | [0.18.0](0.18.0.md) | Inbox conversations: optional Produce identity, grouped list entries, `GET /me/inbox/conversation` window of at most 5, accept-time presentation snapshot |
9
11
  | [0.17.0](0.17.0.md) | Client-version-compatibility gating (`observe`/`enforce`); Rich Messaging Inbox `content` documents; host-overridable rendering templates; shared email theming |
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CommandTower
4
- VERSION = "0.19.0"
4
+ VERSION = "0.21.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_tower
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - matt-taylor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-10-14 00:00:00.000000000 Z
11
+ date: 2026-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phonelib
@@ -572,6 +572,10 @@ files:
572
572
  - app/services/command_tower/messaging/preferences/catalog_category_result.rb
573
573
  - app/services/command_tower/messaging/preferences/catalog_notification_result.rb
574
574
  - app/services/command_tower/messaging/preferences/catalog_result.rb
575
+ - app/services/command_tower/messaging/preferences/channel_catalog.rb
576
+ - app/services/command_tower/messaging/preferences/corrections.rb
577
+ - app/services/command_tower/messaging/preferences/destination_locks.rb
578
+ - app/services/command_tower/messaging/preferences/destinations.rb
575
579
  - app/services/command_tower/messaging/preferences/effective_choice.rb
576
580
  - app/services/command_tower/messaging/preferences/error.rb
577
581
  - app/services/command_tower/messaging/preferences/evaluation_result.rb
@@ -579,9 +583,12 @@ files:
579
583
  - app/services/command_tower/messaging/preferences/invalid_preference_state_error.rb
580
584
  - app/services/command_tower/messaging/preferences/invalid_preference_write_error.rb
581
585
  - app/services/command_tower/messaging/preferences/not_settings_visible_error.rb
586
+ - app/services/command_tower/messaging/preferences/notification_assembly.rb
582
587
  - app/services/command_tower/messaging/preferences/preference_state.rb
583
588
  - app/services/command_tower/messaging/preferences/reason_classes.rb
584
589
  - app/services/command_tower/messaging/preferences/resolve.rb
590
+ - app/services/command_tower/messaging/preferences/setting_controls.rb
591
+ - app/services/command_tower/messaging/preferences/settings_projection.rb
585
592
  - app/services/command_tower/messaging/preferences/store.rb
586
593
  - app/services/command_tower/messaging/preferences/store_error.rb
587
594
  - app/services/command_tower/messaging/preferences/suppressed_destination.rb
@@ -860,6 +867,8 @@ files:
860
867
  - docs/upgrades/0.17.0.md
861
868
  - docs/upgrades/0.18.0.md
862
869
  - docs/upgrades/0.19.0.md
870
+ - docs/upgrades/0.20.0.md
871
+ - docs/upgrades/0.21.0.md
863
872
  - docs/upgrades/README.md
864
873
  - lib/command_tower.rb
865
874
  - lib/command_tower/admin_scope.rb