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.
- checksums.yaml +4 -4
- data/app/deserializers/command_tower/deserializers/messaging/preferences/update_deserializer.rb +10 -1
- data/app/errors/command_tower/errors/account/push_test_destination_error.rb +21 -0
- data/app/models/command_tower/messaging/delivery_attempt.rb +5 -0
- data/app/models/command_tower/messaging/inbox_item.rb +6 -0
- data/app/serializers/command_tower/serializers/messaging/inbox.rb +1 -1
- data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +2 -0
- data/app/services/command_tower/messaging/delivery_retry_policy.rb +97 -0
- data/app/services/command_tower/messaging/execution/adapter_result.rb +11 -1
- data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +199 -30
- data/app/services/command_tower/messaging/execution/adapters/expo/configuration.rb +5 -0
- data/app/services/command_tower/messaging/execution/adapters/expo/endpoint_ticket_history.rb +86 -0
- data/app/services/command_tower/messaging/execution/adapters/expo/http_client.rb +4 -0
- data/app/services/command_tower/messaging/execution/adapters/expo/interruption_level.rb +41 -0
- data/app/services/command_tower/messaging/execution/attempt_ledger.rb +45 -2
- data/app/services/command_tower/messaging/execution/expo_receipt_collection.rb +224 -0
- data/app/services/command_tower/messaging/execution/operation_logger.rb +65 -5
- data/app/services/command_tower/messaging/execution/recovery.rb +72 -8
- data/app/services/command_tower/messaging/inbox/reader.rb +15 -2
- data/app/services/command_tower/messaging/inbox/unread_count_result.rb +3 -2
- data/app/services/command_tower/messaging/notification_types/declaration.rb +19 -1
- data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +62 -0
- data/app/services/command_tower/messaging/notification_types/permitted_choice.rb +55 -0
- data/app/services/command_tower/messaging/notification_types/registry.rb +25 -0
- data/app/services/command_tower/messaging/notification_types.rb +3 -0
- data/app/services/command_tower/messaging/preferences/catalog.rb +7 -0
- data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +26 -1
- data/app/services/command_tower/messaging/preferences/effective_choice.rb +92 -0
- data/app/services/command_tower/messaging/preferences/preference_state.rb +24 -4
- data/app/services/command_tower/messaging/preferences/store.rb +10 -0
- data/app/services/command_tower/messaging/preferences/update.rb +36 -4
- data/app/services/command_tower/messaging/rendering/channel_renderer.rb +63 -3
- data/app/services/command_tower/messaging/rendering/inbox_document.rb +27 -0
- data/app/services/command_tower/messaging/rendering/push_presentation.rb +90 -0
- data/app/services/command_tower/messaging/rendering/rendered_push_payload.rb +54 -6
- data/app/services/command_tower/messaging/sound_catalog.rb +85 -0
- data/app/services/command_tower/services/account/push/send_self_test.rb +7 -1
- data/app/services/command_tower/services/messaging/communications/produce.rb +27 -0
- data/app/services/command_tower/services/messaging/inbox.rb +5 -1
- data/app/workflows/command_tower/workflows/me/error_mapping.rb +1 -0
- data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +24 -1
- data/db/migrate/20261012000001_add_provider_metadata_to_messaging_delivery_attempts.rb +7 -0
- data/db/migrate/20261012000002_add_finished_at_index_to_messaging_delivery_attempts.rb +8 -0
- data/docs/upgrades/0.19.0.md +40 -0
- data/docs/upgrades/0.20.0.md +29 -0
- data/docs/upgrades/README.md +2 -0
- data/lib/command_tower/configuration/messaging/config.rb +10 -0
- data/lib/command_tower/configuration/messaging/expo.rb +61 -0
- data/lib/command_tower/install/baseline.rb +2 -0
- data/lib/command_tower/version.rb +1 -1
- metadata +14 -2
|
@@ -23,10 +23,18 @@ module CommandTower
|
|
|
23
23
|
recover_queued!
|
|
24
24
|
recover_failed!
|
|
25
25
|
recover_stale_executing!
|
|
26
|
+
collect_expo_receipts!
|
|
26
27
|
end
|
|
27
28
|
|
|
28
29
|
private
|
|
29
30
|
|
|
31
|
+
# Runs last so it can never block a redrive. It only reads receipts and updates metadata.
|
|
32
|
+
def collect_expo_receipts!
|
|
33
|
+
ExpoReceiptCollection.call
|
|
34
|
+
rescue StandardError
|
|
35
|
+
OperationLogger.receipts_collected(update_failure_count: 1)
|
|
36
|
+
end
|
|
37
|
+
|
|
30
38
|
def grace_before
|
|
31
39
|
@grace_window.ago
|
|
32
40
|
end
|
|
@@ -35,30 +43,86 @@ module CommandTower
|
|
|
35
43
|
Messaging::ChannelDelivery
|
|
36
44
|
.queued_for_execution
|
|
37
45
|
.where(Messaging::ChannelDelivery.arel_table[:updated_at].lt(grace_before))
|
|
38
|
-
.find_each { |delivery|
|
|
46
|
+
.find_each { |delivery| redrive_queued_delivery!(delivery) }
|
|
39
47
|
end
|
|
40
48
|
|
|
41
49
|
def recover_failed!
|
|
42
50
|
Messaging::ChannelDelivery
|
|
43
51
|
.failed_retryable
|
|
44
52
|
.where(Messaging::ChannelDelivery.arel_table[:updated_at].lt(grace_before))
|
|
45
|
-
.
|
|
46
|
-
.find_each do |delivery|
|
|
47
|
-
reset_to_queued!(delivery)
|
|
48
|
-
redrive!(delivery.reload)
|
|
49
|
-
end
|
|
53
|
+
.find_each { |delivery| redrive_failed_delivery!(delivery) }
|
|
50
54
|
end
|
|
51
55
|
|
|
52
56
|
def recover_stale_executing!
|
|
53
57
|
Messaging::ChannelDelivery
|
|
54
58
|
.executing
|
|
55
59
|
.where(Messaging::ChannelDelivery.arel_table[:execution_claimed_at].lt(grace_before))
|
|
56
|
-
.find_each
|
|
57
|
-
|
|
60
|
+
.find_each { |delivery| redrive_stale_executing_delivery!(delivery) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def redrive_queued_delivery!(delivery)
|
|
64
|
+
if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
|
|
65
|
+
if Messaging::DeliveryRetryPolicy.push_redrive_eligible_while_queued?(delivery)
|
|
66
|
+
redrive!(delivery)
|
|
67
|
+
else
|
|
68
|
+
terminalize_push_uncertain!(delivery)
|
|
69
|
+
end
|
|
70
|
+
return
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
redrive!(delivery)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def redrive_failed_delivery!(delivery)
|
|
77
|
+
if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
|
|
78
|
+
if Messaging::DeliveryRetryPolicy.push_redrive_eligible_from_failed_retryable?(delivery)
|
|
79
|
+
reset_to_queued!(delivery)
|
|
80
|
+
redrive!(delivery.reload)
|
|
81
|
+
else
|
|
82
|
+
terminalize_push_uncertain!(delivery)
|
|
83
|
+
end
|
|
84
|
+
return
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
return if delivery.execution_attempt_count >= @max_attempts
|
|
88
|
+
|
|
89
|
+
reset_to_queued!(delivery)
|
|
90
|
+
redrive!(delivery.reload)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def redrive_stale_executing_delivery!(delivery)
|
|
94
|
+
max_attempts = Messaging::DeliveryRetryPolicy.max_execution_attempts_for(delivery)
|
|
95
|
+
return if delivery.execution_attempt_count >= max_attempts
|
|
58
96
|
|
|
97
|
+
if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
|
|
98
|
+
if Messaging::DeliveryRetryPolicy.push_redrive_eligible_while_queued?(delivery)
|
|
59
99
|
reset_to_queued!(delivery)
|
|
60
100
|
redrive!(delivery.reload)
|
|
101
|
+
else
|
|
102
|
+
terminalize_push_uncertain!(delivery)
|
|
61
103
|
end
|
|
104
|
+
return
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
reset_to_queued!(delivery)
|
|
108
|
+
redrive!(delivery.reload)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def terminalize_push_uncertain!(delivery)
|
|
112
|
+
now = Time.current
|
|
113
|
+
uncertain = Messaging::DeliveryRetryPolicy::SUBMISSION_UNCERTAIN
|
|
114
|
+
|
|
115
|
+
Messaging::DeliveryAttempt
|
|
116
|
+
.where(channel_delivery_id: delivery.id, status: Messaging::DeliveryAttempt::STATUS_STARTED, finished_at: nil)
|
|
117
|
+
.find_each do |attempt|
|
|
118
|
+
attempt.update!(
|
|
119
|
+
status: Messaging::DeliveryAttempt::STATUS_FAILED_TERMINAL,
|
|
120
|
+
finished_at: now,
|
|
121
|
+
error_code: uncertain,
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
delivery.update!(status: Messaging::ChannelDelivery::STATUS_FAILED_TERMINAL)
|
|
62
126
|
end
|
|
63
127
|
|
|
64
128
|
def reset_to_queued!(delivery)
|
|
@@ -65,13 +65,26 @@ module CommandTower
|
|
|
65
65
|
|
|
66
66
|
def unread_count(recipient_id:)
|
|
67
67
|
recipient_id = require_recipient_id!(recipient_id)
|
|
68
|
+
unread = Scope.for_recipient(recipient_id).merge(Messaging::InboxItem.unread)
|
|
68
69
|
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
UnreadCountResult.build(
|
|
71
|
+
recipient_id:,
|
|
72
|
+
count: unread.count,
|
|
73
|
+
badge_count: badge_eligible_count(unread),
|
|
74
|
+
)
|
|
71
75
|
end
|
|
72
76
|
|
|
73
77
|
private
|
|
74
78
|
|
|
79
|
+
# Unread rows whose declaration is badge_eligible. Unregistered keys are absent
|
|
80
|
+
# from that set. Channel preferences are not consulted.
|
|
81
|
+
def badge_eligible_count(unread)
|
|
82
|
+
keys = CommandTower::Messaging::NotificationTypes.enumerate.select(&:badge_eligible).map(&:key)
|
|
83
|
+
return 0 if keys.empty?
|
|
84
|
+
|
|
85
|
+
unread.where(messaging_communications: { notification_type_key: keys }).count
|
|
86
|
+
end
|
|
87
|
+
|
|
75
88
|
def require_recipient_id!(recipient_id)
|
|
76
89
|
raise ValidationError, "recipient_id is required" if blank?(recipient_id)
|
|
77
90
|
|
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
module CommandTower
|
|
4
4
|
module Messaging
|
|
5
5
|
module Inbox
|
|
6
|
-
UnreadCountResult = Data.define(:recipient_id, :count) do
|
|
7
|
-
def self.build(recipient_id:, count:)
|
|
6
|
+
UnreadCountResult = Data.define(:recipient_id, :count, :badge_count) do
|
|
7
|
+
def self.build(recipient_id:, count:, badge_count:)
|
|
8
8
|
new(
|
|
9
9
|
recipient_id:,
|
|
10
10
|
count: Integer(count),
|
|
11
|
+
badge_count: Integer(badge_count),
|
|
11
12
|
).freeze
|
|
12
13
|
end
|
|
13
14
|
end
|
|
@@ -23,6 +23,10 @@ module CommandTower
|
|
|
23
23
|
:delivery_status_visible,
|
|
24
24
|
:host_ownership,
|
|
25
25
|
:inbox_document_composer,
|
|
26
|
+
:badge_eligible,
|
|
27
|
+
:urgency,
|
|
28
|
+
:sound,
|
|
29
|
+
:deep_link_required,
|
|
26
30
|
) do
|
|
27
31
|
def self.build(
|
|
28
32
|
key:,
|
|
@@ -43,7 +47,11 @@ module CommandTower
|
|
|
43
47
|
retention: nil,
|
|
44
48
|
delivery_status_visible: nil,
|
|
45
49
|
host_ownership: nil,
|
|
46
|
-
inbox_document_composer: nil
|
|
50
|
+
inbox_document_composer: nil,
|
|
51
|
+
badge_eligible: false,
|
|
52
|
+
urgency: nil,
|
|
53
|
+
sound: nil,
|
|
54
|
+
deep_link_required: false
|
|
47
55
|
)
|
|
48
56
|
new(
|
|
49
57
|
key: key.nil? ? nil : key.to_s,
|
|
@@ -75,6 +83,16 @@ module CommandTower
|
|
|
75
83
|
# renders via `InboxDocumentRenderer#build_generic`. See
|
|
76
84
|
# artifacts/visual-improvements/rich-messaging/authority/RICH_MESSAGING_RUBY_INBOX_DEFINITION_DISCOVERY.md.
|
|
77
85
|
inbox_document_composer: inbox_document_composer.nil? ? nil : inbox_document_composer.to_s,
|
|
86
|
+
# Notification Product Behavior (NPB-1). All default to today's behavior.
|
|
87
|
+
# `badge_eligible`: unread inbox items of this type count toward the app-icon badge.
|
|
88
|
+
# `urgency` / `sound`: `{ default:, allowed: [ids] | "all" }`; nil is fixed
|
|
89
|
+
# "normal" / "default". Invalid input is kept so DeclarationValidator reports it.
|
|
90
|
+
badge_eligible: !!badge_eligible,
|
|
91
|
+
urgency: PermittedChoice.from(urgency, fallback_default: "normal"),
|
|
92
|
+
sound: PermittedChoice.from(sound, fallback_default: "default"),
|
|
93
|
+
# NPB-5. Push-capable types that are not inbox-backed must set this
|
|
94
|
+
# so Produce can reject a missing metadata deep_link.
|
|
95
|
+
deep_link_required: !!deep_link_required,
|
|
78
96
|
).freeze
|
|
79
97
|
end
|
|
80
98
|
|
|
@@ -72,9 +72,36 @@ module CommandTower
|
|
|
72
72
|
errors << "settings_visible must be a boolean"
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
+
validate_permitted_choice(
|
|
76
|
+
errors, declaration.urgency, field: "urgency",
|
|
77
|
+
valid_value: ->(value) { URGENCY_LEVELS.include?(value) },
|
|
78
|
+
invalid_message: "unknown urgency level"
|
|
79
|
+
)
|
|
80
|
+
validate_permitted_choice(
|
|
81
|
+
errors, declaration.sound, field: "sound",
|
|
82
|
+
valid_value: ->(value) { SoundCatalog::ID_FORMAT.match?(value) },
|
|
83
|
+
invalid_message: "malformed sound id"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
if push_capable?(declaration) && !declaration.inbox_available && !declaration.deep_link_required
|
|
87
|
+
errors << "push-capable types must be inbox-backed or declare deep_link_required"
|
|
88
|
+
end
|
|
89
|
+
|
|
75
90
|
errors
|
|
76
91
|
end
|
|
77
92
|
|
|
93
|
+
# Seal-time check: every sound a declaration names is registered in the catalog.
|
|
94
|
+
def self.sound_reference_errors(declaration, catalog_ids:)
|
|
95
|
+
choice = declaration&.sound
|
|
96
|
+
return [] unless choice.is_a?(PermittedChoice)
|
|
97
|
+
|
|
98
|
+
named = ([choice.default] + (choice.allowed.is_a?(Array) ? choice.allowed : [])).compact.uniq
|
|
99
|
+
missing = named - catalog_ids
|
|
100
|
+
return [] if missing.empty?
|
|
101
|
+
|
|
102
|
+
["sound references unregistered sound ids: #{missing.join(', ')}"]
|
|
103
|
+
end
|
|
104
|
+
|
|
78
105
|
def self.validate_category_consistency!(declaration, peers:)
|
|
79
106
|
errors = category_consistency_errors(declaration, peers:)
|
|
80
107
|
return if errors.empty?
|
|
@@ -131,6 +158,41 @@ module CommandTower
|
|
|
131
158
|
end
|
|
132
159
|
private_class_method :validate_non_negative_integer
|
|
133
160
|
|
|
161
|
+
def self.validate_permitted_choice(errors, choice, field:, valid_value:, invalid_message:)
|
|
162
|
+
unless choice.is_a?(PermittedChoice)
|
|
163
|
+
errors << "#{field} must be a hash with default and allowed"
|
|
164
|
+
return
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
default = choice.default
|
|
168
|
+
allowed = choice.allowed
|
|
169
|
+
values = []
|
|
170
|
+
|
|
171
|
+
if default.is_a?(String) && !default.strip.empty?
|
|
172
|
+
values << default
|
|
173
|
+
else
|
|
174
|
+
errors << "#{field} default must be present"
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# "all" permits every value in the universe, so only the default is checked.
|
|
178
|
+
if allowed.is_a?(Array) && allowed.any?
|
|
179
|
+
values.concat(allowed)
|
|
180
|
+
errors << "#{field} allowed contains duplicates" if allowed.uniq.size != allowed.size
|
|
181
|
+
errors << "#{field} default must be one of allowed" if default && allowed.exclude?(default)
|
|
182
|
+
elsif allowed != "all"
|
|
183
|
+
errors << "#{field} allowed must be \"all\" or a non-empty array"
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
bad = values.uniq.reject { |value| value.is_a?(String) && valid_value.call(value) }
|
|
187
|
+
errors << "#{field} has #{invalid_message}: #{bad.map(&:inspect).join(', ')}" if bad.any?
|
|
188
|
+
end
|
|
189
|
+
private_class_method :validate_permitted_choice
|
|
190
|
+
|
|
191
|
+
def self.push_capable?(declaration)
|
|
192
|
+
declaration.allowed_channels.is_a?(Array) && declaration.allowed_channels.include?("push")
|
|
193
|
+
end
|
|
194
|
+
private_class_method :push_capable?
|
|
195
|
+
|
|
134
196
|
def self.validate_channel_list(errors, channels, field:)
|
|
135
197
|
channels.each do |channel|
|
|
136
198
|
key = channel.to_s
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module NotificationTypes
|
|
6
|
+
# A declared default plus the values a member may choose from. Used by the
|
|
7
|
+
# `urgency` and `sound` declaration fields. `allowed` is either an Array of ids or
|
|
8
|
+
# the String "all" (every value in the universe). The mode is derived from
|
|
9
|
+
# `allowed`, never stored:
|
|
10
|
+
#
|
|
11
|
+
# fixed one permitted value
|
|
12
|
+
# restricted an explicit list of more than one
|
|
13
|
+
# full "all"
|
|
14
|
+
PermittedChoice = Data.define(:default, :allowed) do
|
|
15
|
+
# Normalizes a declaration input. `nil` becomes a choice fixed on `fallback_default`.
|
|
16
|
+
# A Hash is coerced. Any other value is returned unchanged so the validator can
|
|
17
|
+
# report it (mirrors how `default_preference_state` is handled).
|
|
18
|
+
def self.from(raw, fallback_default:)
|
|
19
|
+
return new(default: fallback_default, allowed: [fallback_default].freeze) if raw.nil?
|
|
20
|
+
return raw unless raw.is_a?(Hash)
|
|
21
|
+
|
|
22
|
+
hash = raw.transform_keys(&:to_sym)
|
|
23
|
+
default = hash[:default]&.to_s
|
|
24
|
+
allowed = hash[:allowed]
|
|
25
|
+
allowed =
|
|
26
|
+
if allowed.nil?
|
|
27
|
+
default.nil? ? nil : [default].freeze
|
|
28
|
+
elsif allowed.is_a?(Array)
|
|
29
|
+
allowed.map(&:to_s).freeze
|
|
30
|
+
elsif allowed.to_s == "all"
|
|
31
|
+
"all"
|
|
32
|
+
else
|
|
33
|
+
allowed
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
new(default:, allowed:)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def mode
|
|
40
|
+
return :full if allowed == "all"
|
|
41
|
+
|
|
42
|
+
allowed.size > 1 ? :restricted : :fixed
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def permitted(universe)
|
|
46
|
+
allowed == "all" ? universe.to_a : allowed
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def selectable?(universe)
|
|
50
|
+
permitted(universe).size > 1
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -88,10 +88,35 @@ module CommandTower
|
|
|
88
88
|
DeclarationValidator.validate!(declaration)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
|
+
validate_sound_references!
|
|
92
|
+
|
|
91
93
|
@declarations.each_value do |declaration|
|
|
92
94
|
peers = @declarations.values.reject { |peer| peer.equal?(declaration) || peer.key == declaration.key }
|
|
93
95
|
DeclarationValidator.validate_category_consistency!(declaration, peers:)
|
|
94
96
|
end
|
|
97
|
+
|
|
98
|
+
validate_home_deep_link_configuration!
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Sound ids are checked at seal, not register, so host initializer order cannot matter.
|
|
102
|
+
def validate_sound_references!
|
|
103
|
+
catalog_errors = SoundCatalog.validation_errors
|
|
104
|
+
raise InvalidDeclarationError, "invalid sound catalog: #{catalog_errors.join('; ')}" if catalog_errors.any?
|
|
105
|
+
|
|
106
|
+
@declarations.each_value do |declaration|
|
|
107
|
+
errors = DeclarationValidator.sound_reference_errors(declaration, catalog_ids: SoundCatalog.ids)
|
|
108
|
+
raise InvalidDeclarationError, "#{declaration.key}: #{errors.join('; ')}" if errors.any?
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Presence only. A Proc is configured and is not called here.
|
|
113
|
+
def validate_home_deep_link_configuration!
|
|
114
|
+
return if CommandTower.config.messaging.expo.adapter.to_s == "disabled"
|
|
115
|
+
return unless @declarations.values.any? { |declaration| declaration.allowed_channels.include?("push") }
|
|
116
|
+
return if CommandTower.config.messaging.expo.home_deep_link_configured?
|
|
117
|
+
|
|
118
|
+
raise InvalidDeclarationError,
|
|
119
|
+
"home_deep_link must be configured when a push-capable type is registered and the Expo adapter is enabled"
|
|
95
120
|
end
|
|
96
121
|
|
|
97
122
|
def type_sort_key(declaration)
|
|
@@ -75,6 +75,11 @@ module CommandTower
|
|
|
75
75
|
available =
|
|
76
76
|
declaration.allowed_channels & @platform_enabled_channels & recipient_ready_keys
|
|
77
77
|
|
|
78
|
+
blocks = EffectiveChoice.call(
|
|
79
|
+
declaration:,
|
|
80
|
+
stored: Store.find(recipient_id: @recipient_id, notification_type_key: declaration.key),
|
|
81
|
+
)
|
|
82
|
+
|
|
78
83
|
::CommandTower::Messaging::Preferences::CatalogNotificationResult.build(
|
|
79
84
|
key: declaration.key,
|
|
80
85
|
label: declaration.label,
|
|
@@ -88,6 +93,8 @@ module CommandTower
|
|
|
88
93
|
inbox_enabled:,
|
|
89
94
|
channels:,
|
|
90
95
|
stored_override_present: evaluation.stored_override_present,
|
|
96
|
+
urgency: blocks[:urgency],
|
|
97
|
+
sound: blocks[:sound],
|
|
91
98
|
)
|
|
92
99
|
end
|
|
93
100
|
end
|
|
@@ -16,6 +16,8 @@ module CommandTower
|
|
|
16
16
|
:inbox_enabled,
|
|
17
17
|
:channels,
|
|
18
18
|
:stored_override_present,
|
|
19
|
+
:urgency,
|
|
20
|
+
:sound,
|
|
19
21
|
) do
|
|
20
22
|
def self.build(
|
|
21
23
|
key:,
|
|
@@ -29,7 +31,9 @@ module CommandTower
|
|
|
29
31
|
available_channels:,
|
|
30
32
|
inbox_enabled:,
|
|
31
33
|
channels:,
|
|
32
|
-
stored_override_present
|
|
34
|
+
stored_override_present:,
|
|
35
|
+
urgency:,
|
|
36
|
+
sound:
|
|
33
37
|
)
|
|
34
38
|
frozen_channels =
|
|
35
39
|
channels.to_h.transform_keys(&:to_s).transform_values { |value| !!value }.freeze
|
|
@@ -47,8 +51,29 @@ module CommandTower
|
|
|
47
51
|
inbox_enabled: !!inbox_enabled,
|
|
48
52
|
channels: frozen_channels,
|
|
49
53
|
stored_override_present: !!stored_override_present,
|
|
54
|
+
urgency: freeze_block(urgency),
|
|
55
|
+
sound: freeze_block(sound),
|
|
50
56
|
).freeze
|
|
51
57
|
end
|
|
58
|
+
|
|
59
|
+
def self.freeze_block(block)
|
|
60
|
+
hash = block.to_h
|
|
61
|
+
allowed = hash[:allowed]
|
|
62
|
+
frozen_allowed =
|
|
63
|
+
if allowed.is_a?(Array) && allowed.first.is_a?(Hash)
|
|
64
|
+
allowed.map { |entry| entry.to_h.transform_keys(&:to_sym).freeze }.freeze
|
|
65
|
+
else
|
|
66
|
+
Array(allowed).map(&:to_s).freeze
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
{
|
|
70
|
+
default: hash[:default].to_s,
|
|
71
|
+
allowed: frozen_allowed,
|
|
72
|
+
selectable: !!hash[:selectable],
|
|
73
|
+
effective: hash[:effective].to_s,
|
|
74
|
+
}.freeze
|
|
75
|
+
end
|
|
76
|
+
private_class_method :freeze_block
|
|
52
77
|
end
|
|
53
78
|
end
|
|
54
79
|
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
# Resolves the urgency and sound a member actually gets, and the blocks
|
|
7
|
+
# the preferences API returns. IOS reads this at send time. It does not send.
|
|
8
|
+
class EffectiveChoice
|
|
9
|
+
def self.call(declaration:, stored:)
|
|
10
|
+
new(declaration:, stored:).call
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.urgency_ids(choice)
|
|
14
|
+
choice.permitted(NotificationTypes::URGENCY_LEVELS)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.sound_ids(choice)
|
|
18
|
+
ids = choice.allowed == "all" ? SoundCatalog.ids : Array(choice.allowed)
|
|
19
|
+
ids.select { |id| SoundCatalog.fetch(id) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.selectable?(ids)
|
|
23
|
+
ids.size > 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.validate_write!(declaration:, field:, value:)
|
|
27
|
+
choice = declaration.public_send(field)
|
|
28
|
+
ids = field == :urgency ? urgency_ids(choice) : sound_ids(choice)
|
|
29
|
+
unless value.is_a?(String) && !value.strip.empty?
|
|
30
|
+
raise InvalidPreferenceStateError, "preference #{field} must be a string"
|
|
31
|
+
end
|
|
32
|
+
unless selectable?(ids)
|
|
33
|
+
raise InvalidPreferenceWriteError, "#{field} is not selectable for notification type: #{declaration.key}"
|
|
34
|
+
end
|
|
35
|
+
return if ids.include?(value)
|
|
36
|
+
|
|
37
|
+
raise InvalidPreferenceWriteError, "#{field} is not permitted: #{value}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def initialize(declaration:, stored:)
|
|
41
|
+
@declaration = declaration
|
|
42
|
+
@stored = stored
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def call
|
|
46
|
+
{
|
|
47
|
+
urgency: urgency_block,
|
|
48
|
+
sound: sound_block,
|
|
49
|
+
}
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def urgency_block
|
|
55
|
+
choice = @declaration.urgency
|
|
56
|
+
ids = self.class.urgency_ids(choice)
|
|
57
|
+
{
|
|
58
|
+
default: choice.default,
|
|
59
|
+
allowed: ids,
|
|
60
|
+
selectable: self.class.selectable?(ids),
|
|
61
|
+
effective: resolve(stored: @stored&.urgency, ids:, fallback: choice.default),
|
|
62
|
+
}
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def sound_block
|
|
66
|
+
choice = @declaration.sound
|
|
67
|
+
ids = self.class.sound_ids(choice)
|
|
68
|
+
{
|
|
69
|
+
default: choice.default,
|
|
70
|
+
allowed: ids.map { |id| { id:, label: SoundCatalog.fetch(id)[:label] } },
|
|
71
|
+
selectable: self.class.selectable?(ids),
|
|
72
|
+
effective: resolve_sound(ids, choice.default),
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def resolve(stored:, ids:, fallback:)
|
|
77
|
+
return stored if stored && ids.include?(stored)
|
|
78
|
+
|
|
79
|
+
fallback
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def resolve_sound(ids, fallback)
|
|
83
|
+
stored = @stored&.sound
|
|
84
|
+
return stored if stored && ids.include?(stored)
|
|
85
|
+
return fallback if ids.include?(fallback)
|
|
86
|
+
|
|
87
|
+
SoundCatalog::SYSTEM_DEFAULT[:id]
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module CommandTower
|
|
4
4
|
module Messaging
|
|
5
5
|
module Preferences
|
|
6
|
-
PreferenceState = Data.define(:channels, :inbox) do
|
|
6
|
+
PreferenceState = Data.define(:channels, :inbox, :urgency, :sound) do
|
|
7
7
|
def self.normalize(raw)
|
|
8
8
|
return nil if raw.nil?
|
|
9
9
|
|
|
@@ -31,17 +31,35 @@ module CommandTower
|
|
|
31
31
|
|
|
32
32
|
inbox = hash.key?("inbox") ? !!hash["inbox"] : nil
|
|
33
33
|
|
|
34
|
-
new(
|
|
34
|
+
new(
|
|
35
|
+
channels:,
|
|
36
|
+
inbox:,
|
|
37
|
+
urgency: optional_string(hash["urgency"]),
|
|
38
|
+
sound: optional_string(hash["sound"]),
|
|
39
|
+
).freeze
|
|
35
40
|
end
|
|
36
41
|
|
|
37
42
|
def self.from_declaration_default(default_preference_state)
|
|
38
|
-
normalize(default_preference_state) || new(channels: {}.freeze, inbox: nil).freeze
|
|
43
|
+
normalize(default_preference_state) || new(channels: {}.freeze, inbox: nil, urgency: nil, sound: nil).freeze
|
|
39
44
|
end
|
|
40
45
|
|
|
46
|
+
def self.optional_string(value)
|
|
47
|
+
return nil unless value.is_a?(String)
|
|
48
|
+
|
|
49
|
+
stripped = value.strip
|
|
50
|
+
stripped.empty? ? nil : stripped
|
|
51
|
+
end
|
|
52
|
+
private_class_method :optional_string
|
|
53
|
+
|
|
41
54
|
def merge_over(base)
|
|
42
55
|
merged_channels = base.channels.merge(channels)
|
|
43
56
|
merged_inbox = inbox.nil? ? base.inbox : inbox
|
|
44
|
-
self.class.new(
|
|
57
|
+
self.class.new(
|
|
58
|
+
channels: merged_channels.freeze,
|
|
59
|
+
inbox: merged_inbox,
|
|
60
|
+
urgency: urgency.nil? ? base.urgency : urgency,
|
|
61
|
+
sound: sound.nil? ? base.sound : sound,
|
|
62
|
+
).freeze
|
|
45
63
|
end
|
|
46
64
|
|
|
47
65
|
def channel_enabled?(channel_key, default: true)
|
|
@@ -53,6 +71,8 @@ module CommandTower
|
|
|
53
71
|
def to_raw_hash
|
|
54
72
|
hash = { "channels" => channels.to_h }
|
|
55
73
|
hash["inbox"] = inbox unless inbox.nil?
|
|
74
|
+
hash["urgency"] = urgency unless urgency.nil?
|
|
75
|
+
hash["sound"] = sound unless sound.nil?
|
|
56
76
|
hash
|
|
57
77
|
end
|
|
58
78
|
end
|
|
@@ -35,6 +35,10 @@ module CommandTower
|
|
|
35
35
|
declaration = lookup_declaration!(key)
|
|
36
36
|
validate_writable!(declaration)
|
|
37
37
|
|
|
38
|
+
raw = preference_state.to_h.transform_keys(&:to_s)
|
|
39
|
+
validate_choice!(declaration, raw, "urgency")
|
|
40
|
+
validate_choice!(declaration, raw, "sound")
|
|
41
|
+
|
|
38
42
|
normalized = PreferenceState.normalize(preference_state)
|
|
39
43
|
if normalized.nil?
|
|
40
44
|
raise InvalidPreferenceWriteError, "preference_state must be present"
|
|
@@ -86,6 +90,12 @@ module CommandTower
|
|
|
86
90
|
"preference channels outside allowed set: #{extras.join(', ')}"
|
|
87
91
|
end
|
|
88
92
|
|
|
93
|
+
def validate_choice!(declaration, raw, field)
|
|
94
|
+
return unless raw.key?(field)
|
|
95
|
+
|
|
96
|
+
EffectiveChoice.validate_write!(declaration:, field: field.to_sym, value: raw[field])
|
|
97
|
+
end
|
|
98
|
+
|
|
89
99
|
def validate_inbox!(declaration, normalized)
|
|
90
100
|
return if normalized.inbox.nil?
|
|
91
101
|
return if declaration.inbox_available
|