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.
- checksums.yaml +4 -4
- data/app/models/command_tower/messaging/delivery_attempt.rb +5 -0
- data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +109 -1
- data/app/services/command_tower/messaging/channels/definition.rb +3 -0
- data/app/services/command_tower/messaging/channels.rb +15 -0
- data/app/services/command_tower/messaging/delivery_retry_policy.rb +97 -0
- data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +71 -23
- data/app/services/command_tower/messaging/execution/adapters/expo/http_client.rb +4 -0
- data/app/services/command_tower/messaging/execution/attempt_ledger.rb +42 -2
- data/app/services/command_tower/messaging/execution/operation_logger.rb +10 -0
- data/app/services/command_tower/messaging/execution/recovery.rb +64 -8
- data/app/services/command_tower/messaging/notification_types/declaration.rb +15 -1
- data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +53 -0
- data/app/services/command_tower/messaging/preferences/catalog.rb +3 -32
- data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +35 -1
- data/app/services/command_tower/messaging/preferences/channel_catalog.rb +24 -0
- data/app/services/command_tower/messaging/preferences/corrections.rb +50 -0
- data/app/services/command_tower/messaging/preferences/destination_locks.rb +33 -0
- data/app/services/command_tower/messaging/preferences/destinations.rb +121 -0
- data/app/services/command_tower/messaging/preferences/notification_assembly.rb +77 -0
- data/app/services/command_tower/messaging/preferences/setting_controls.rb +25 -0
- data/app/services/command_tower/messaging/preferences/settings_projection.rb +126 -0
- data/app/services/command_tower/messaging/preferences/update.rb +57 -40
- data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +19 -0
- data/docs/upgrades/0.20.0.md +29 -0
- data/docs/upgrades/0.21.0.md +31 -0
- data/docs/upgrades/README.md +2 -0
- data/lib/command_tower/version.rb +1 -1
- metadata +11 -2
|
@@ -43,30 +43,86 @@ module CommandTower
|
|
|
43
43
|
Messaging::ChannelDelivery
|
|
44
44
|
.queued_for_execution
|
|
45
45
|
.where(Messaging::ChannelDelivery.arel_table[:updated_at].lt(grace_before))
|
|
46
|
-
.find_each { |delivery|
|
|
46
|
+
.find_each { |delivery| redrive_queued_delivery!(delivery) }
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def recover_failed!
|
|
50
50
|
Messaging::ChannelDelivery
|
|
51
51
|
.failed_retryable
|
|
52
52
|
.where(Messaging::ChannelDelivery.arel_table[:updated_at].lt(grace_before))
|
|
53
|
-
.
|
|
54
|
-
.find_each do |delivery|
|
|
55
|
-
reset_to_queued!(delivery)
|
|
56
|
-
redrive!(delivery.reload)
|
|
57
|
-
end
|
|
53
|
+
.find_each { |delivery| redrive_failed_delivery!(delivery) }
|
|
58
54
|
end
|
|
59
55
|
|
|
60
56
|
def recover_stale_executing!
|
|
61
57
|
Messaging::ChannelDelivery
|
|
62
58
|
.executing
|
|
63
59
|
.where(Messaging::ChannelDelivery.arel_table[:execution_claimed_at].lt(grace_before))
|
|
64
|
-
.find_each
|
|
65
|
-
|
|
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
|
|
66
96
|
|
|
97
|
+
if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
|
|
98
|
+
if Messaging::DeliveryRetryPolicy.push_redrive_eligible_while_queued?(delivery)
|
|
67
99
|
reset_to_queued!(delivery)
|
|
68
100
|
redrive!(delivery.reload)
|
|
101
|
+
else
|
|
102
|
+
terminalize_push_uncertain!(delivery)
|
|
69
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)
|
|
70
126
|
end
|
|
71
127
|
|
|
72
128
|
def reset_to_queued!(delivery)
|
|
@@ -27,6 +27,7 @@ module CommandTower
|
|
|
27
27
|
:urgency,
|
|
28
28
|
:sound,
|
|
29
29
|
:deep_link_required,
|
|
30
|
+
:destination_locks,
|
|
30
31
|
) do
|
|
31
32
|
def self.build(
|
|
32
33
|
key:,
|
|
@@ -51,7 +52,8 @@ module CommandTower
|
|
|
51
52
|
badge_eligible: false,
|
|
52
53
|
urgency: nil,
|
|
53
54
|
sound: nil,
|
|
54
|
-
deep_link_required: false
|
|
55
|
+
deep_link_required: false,
|
|
56
|
+
destination_locks: {}
|
|
55
57
|
)
|
|
56
58
|
new(
|
|
57
59
|
key: key.nil? ? nil : key.to_s,
|
|
@@ -93,9 +95,21 @@ module CommandTower
|
|
|
93
95
|
# NPB-5. Push-capable types that are not inbox-backed must set this
|
|
94
96
|
# so Produce can reject a missing metadata deep_link.
|
|
95
97
|
deep_link_required: !!deep_link_required,
|
|
98
|
+
destination_locks: freeze_destination_locks(destination_locks),
|
|
96
99
|
).freeze
|
|
97
100
|
end
|
|
98
101
|
|
|
102
|
+
def self.freeze_destination_locks(locks)
|
|
103
|
+
return {}.freeze if locks.nil? || locks.empty?
|
|
104
|
+
|
|
105
|
+
unless locks.respond_to?(:to_h)
|
|
106
|
+
return {}.freeze
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
locks.to_h.transform_keys(&:to_s).transform_values(&:to_s).freeze
|
|
110
|
+
end
|
|
111
|
+
private_class_method :freeze_destination_locks
|
|
112
|
+
|
|
99
113
|
def self.freeze_channel_list(channels)
|
|
100
114
|
return nil if channels.nil?
|
|
101
115
|
|
|
@@ -87,6 +87,8 @@ module CommandTower
|
|
|
87
87
|
errors << "push-capable types must be inbox-backed or declare deep_link_required"
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
+
validate_destination_locks(errors, declaration)
|
|
91
|
+
|
|
90
92
|
errors
|
|
91
93
|
end
|
|
92
94
|
|
|
@@ -193,6 +195,57 @@ module CommandTower
|
|
|
193
195
|
end
|
|
194
196
|
private_class_method :push_capable?
|
|
195
197
|
|
|
198
|
+
def self.validate_destination_locks(errors, declaration)
|
|
199
|
+
locks = declaration.destination_locks
|
|
200
|
+
return if locks.nil? || locks.empty?
|
|
201
|
+
|
|
202
|
+
unless locks.is_a?(Hash)
|
|
203
|
+
errors << "destination_locks must be a hash"
|
|
204
|
+
return
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
defaults =
|
|
208
|
+
::CommandTower::Messaging::Preferences::PreferenceState.from_declaration_default(
|
|
209
|
+
declaration.default_preference_state,
|
|
210
|
+
)
|
|
211
|
+
required_channels =
|
|
212
|
+
if declaration.mandatory
|
|
213
|
+
declaration.default_channels & declaration.allowed_channels
|
|
214
|
+
else
|
|
215
|
+
[]
|
|
216
|
+
end
|
|
217
|
+
required_inbox =
|
|
218
|
+
declaration.mandatory && declaration.inbox_available && defaults.inbox != false
|
|
219
|
+
|
|
220
|
+
locks.each do |raw_key, raw_value|
|
|
221
|
+
key = raw_key.to_s
|
|
222
|
+
value = raw_value.to_s
|
|
223
|
+
|
|
224
|
+
unless %w[forced_on forced_off].include?(value)
|
|
225
|
+
errors << "destination_locks[#{key.inspect}] must be forced_on or forced_off"
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
permitted =
|
|
229
|
+
if key == "inbox"
|
|
230
|
+
declaration.inbox_available
|
|
231
|
+
else
|
|
232
|
+
declaration.allowed_channels.include?(key)
|
|
233
|
+
end
|
|
234
|
+
unless permitted
|
|
235
|
+
errors << "destination_locks contains unknown destination: #{key.inspect}"
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
next unless value == "forced_off" && declaration.mandatory
|
|
239
|
+
|
|
240
|
+
if key == "inbox" && required_inbox
|
|
241
|
+
errors << "destination_locks cannot force_off mandatory inbox"
|
|
242
|
+
elsif required_channels.include?(key)
|
|
243
|
+
errors << "destination_locks cannot force_off mandatory channel: #{key}"
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
end
|
|
247
|
+
private_class_method :validate_destination_locks
|
|
248
|
+
|
|
196
249
|
def self.validate_channel_list(errors, channels, field:)
|
|
197
250
|
channels.each do |channel|
|
|
198
251
|
key = channel.to_s
|
|
@@ -58,43 +58,14 @@ module CommandTower
|
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def build_notification(declaration, evaluation, recipient_ready_keys)
|
|
61
|
-
effective_state = evaluation.effective_preference_state || {}
|
|
62
|
-
effective_channels = effective_state["channels"] || {}
|
|
63
|
-
channels =
|
|
64
|
-
declaration.allowed_channels.to_h do |channel|
|
|
65
|
-
[channel, !!effective_channels[channel]]
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
inbox_enabled =
|
|
69
|
-
if effective_state.key?("inbox")
|
|
70
|
-
!!effective_state["inbox"]
|
|
71
|
-
else
|
|
72
|
-
evaluation.inbox_permitted
|
|
73
|
-
end
|
|
74
|
-
|
|
75
61
|
available =
|
|
76
62
|
declaration.allowed_channels & @platform_enabled_channels & recipient_ready_keys
|
|
77
63
|
|
|
78
|
-
|
|
64
|
+
NotificationAssembly.build(
|
|
79
65
|
declaration:,
|
|
80
|
-
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
::CommandTower::Messaging::Preferences::CatalogNotificationResult.build(
|
|
84
|
-
key: declaration.key,
|
|
85
|
-
label: declaration.label,
|
|
86
|
-
description: declaration.description,
|
|
87
|
-
order: declaration.type_order,
|
|
88
|
-
configurable: declaration.user_configurable,
|
|
89
|
-
mandatory: declaration.mandatory,
|
|
90
|
-
inbox_available: declaration.inbox_available,
|
|
91
|
-
allowed_channels: declaration.allowed_channels,
|
|
66
|
+
evaluation:,
|
|
92
67
|
available_channels: available,
|
|
93
|
-
|
|
94
|
-
channels:,
|
|
95
|
-
stored_override_present: evaluation.stored_override_present,
|
|
96
|
-
urgency: blocks[:urgency],
|
|
97
|
-
sound: blocks[:sound],
|
|
68
|
+
recipient_id: @recipient_id,
|
|
98
69
|
)
|
|
99
70
|
end
|
|
100
71
|
end
|
|
@@ -18,6 +18,10 @@ module CommandTower
|
|
|
18
18
|
:stored_override_present,
|
|
19
19
|
:urgency,
|
|
20
20
|
:sound,
|
|
21
|
+
:channel_catalog,
|
|
22
|
+
:destinations,
|
|
23
|
+
:corrections,
|
|
24
|
+
:settings,
|
|
21
25
|
) do
|
|
22
26
|
def self.build(
|
|
23
27
|
key:,
|
|
@@ -33,7 +37,11 @@ module CommandTower
|
|
|
33
37
|
channels:,
|
|
34
38
|
stored_override_present:,
|
|
35
39
|
urgency:,
|
|
36
|
-
sound
|
|
40
|
+
sound:,
|
|
41
|
+
channel_catalog: [],
|
|
42
|
+
destinations: {},
|
|
43
|
+
corrections: nil,
|
|
44
|
+
settings: []
|
|
37
45
|
)
|
|
38
46
|
frozen_channels =
|
|
39
47
|
channels.to_h.transform_keys(&:to_s).transform_values { |value| !!value }.freeze
|
|
@@ -53,9 +61,35 @@ module CommandTower
|
|
|
53
61
|
stored_override_present: !!stored_override_present,
|
|
54
62
|
urgency: freeze_block(urgency),
|
|
55
63
|
sound: freeze_block(sound),
|
|
64
|
+
channel_catalog: freeze_channel_catalog(channel_catalog),
|
|
65
|
+
destinations: freeze_destinations(destinations),
|
|
66
|
+
corrections: freeze_corrections(corrections),
|
|
67
|
+
settings: freeze_settings(settings),
|
|
56
68
|
).freeze
|
|
57
69
|
end
|
|
58
70
|
|
|
71
|
+
def self.freeze_settings(settings)
|
|
72
|
+
Array(settings).map(&:freeze).freeze
|
|
73
|
+
end
|
|
74
|
+
private_class_method :freeze_settings
|
|
75
|
+
|
|
76
|
+
def self.freeze_channel_catalog(entries)
|
|
77
|
+
Array(entries).map(&:freeze).freeze
|
|
78
|
+
end
|
|
79
|
+
private_class_method :freeze_channel_catalog
|
|
80
|
+
|
|
81
|
+
def self.freeze_destinations(destinations)
|
|
82
|
+
destinations.to_h.transform_keys(&:to_s).transform_values(&:freeze).freeze
|
|
83
|
+
end
|
|
84
|
+
private_class_method :freeze_destinations
|
|
85
|
+
|
|
86
|
+
def self.freeze_corrections(corrections)
|
|
87
|
+
return nil if corrections.nil?
|
|
88
|
+
|
|
89
|
+
Array(corrections).map(&:freeze).freeze
|
|
90
|
+
end
|
|
91
|
+
private_class_method :freeze_corrections
|
|
92
|
+
|
|
59
93
|
def self.freeze_block(block)
|
|
60
94
|
hash = block.to_h
|
|
61
95
|
allowed = hash[:allowed]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
class ChannelCatalog
|
|
7
|
+
def self.entries_for(declaration)
|
|
8
|
+
::CommandTower::Messaging::Channels::DEFINITIONS.filter_map do |definition|
|
|
9
|
+
next if definition.inbox? && !declaration.inbox_available
|
|
10
|
+
next if definition.external? && !declaration.allowed_channels.include?(definition.key)
|
|
11
|
+
|
|
12
|
+
{
|
|
13
|
+
key: definition.key,
|
|
14
|
+
label: definition.label,
|
|
15
|
+
description: definition.description,
|
|
16
|
+
order: definition.order,
|
|
17
|
+
glyph_key: definition.glyph_key,
|
|
18
|
+
}.freeze
|
|
19
|
+
end.freeze
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
class Corrections
|
|
7
|
+
CorrectionEntry = Data.define(
|
|
8
|
+
:channel_key,
|
|
9
|
+
:submitted,
|
|
10
|
+
:effective,
|
|
11
|
+
:reason_code,
|
|
12
|
+
:reason,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
def self.build(submitted:, destinations:)
|
|
16
|
+
return [].freeze if submitted.nil? || submitted.empty?
|
|
17
|
+
|
|
18
|
+
submitted.filter_map do |channel_key, submitted_value|
|
|
19
|
+
destination = destinations[channel_key.to_s]
|
|
20
|
+
next if destination.nil?
|
|
21
|
+
|
|
22
|
+
entry = normalize_destination(destination)
|
|
23
|
+
|
|
24
|
+
next if submitted_value == entry.effective
|
|
25
|
+
|
|
26
|
+
CorrectionEntry.new(
|
|
27
|
+
channel_key: channel_key.to_s,
|
|
28
|
+
submitted: !!submitted_value,
|
|
29
|
+
effective: entry.effective,
|
|
30
|
+
reason_code: entry.reason_code,
|
|
31
|
+
reason: entry.reason,
|
|
32
|
+
)
|
|
33
|
+
end.freeze
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.normalize_destination(destination)
|
|
37
|
+
return destination if destination.is_a?(Destinations::DestinationEntry)
|
|
38
|
+
|
|
39
|
+
Destinations::DestinationEntry.new(
|
|
40
|
+
state: destination.fetch(:state) { destination["state"] },
|
|
41
|
+
effective: destination.fetch(:effective) { destination["effective"] },
|
|
42
|
+
reason_code: destination.fetch(:reason_code) { destination["reasonCode"] },
|
|
43
|
+
reason: destination.fetch(:reason) { destination["reason"] },
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
private_class_method :normalize_destination
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
class DestinationLocks
|
|
7
|
+
def self.apply(declaration:, state:)
|
|
8
|
+
locks = declaration.destination_locks
|
|
9
|
+
return state if locks.nil? || locks.empty?
|
|
10
|
+
|
|
11
|
+
channels = state.channels.to_h.dup
|
|
12
|
+
inbox = state.inbox
|
|
13
|
+
|
|
14
|
+
locks.each do |key, lock|
|
|
15
|
+
enabled = lock.to_s == "forced_on"
|
|
16
|
+
if key.to_s == "inbox"
|
|
17
|
+
inbox = enabled
|
|
18
|
+
else
|
|
19
|
+
channels[key.to_s] = enabled
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
PreferenceState.new(
|
|
24
|
+
channels: channels.freeze,
|
|
25
|
+
inbox:,
|
|
26
|
+
urgency: state.urgency,
|
|
27
|
+
sound: state.sound,
|
|
28
|
+
).freeze
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
class Destinations
|
|
7
|
+
REASONS = {
|
|
8
|
+
"required" => "This notification is required on this destination.",
|
|
9
|
+
"locked_on" => "This destination is locked on.",
|
|
10
|
+
"locked_off" => "This destination is locked off.",
|
|
11
|
+
"unavailable" => "This destination is unavailable.",
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
DestinationEntry = Data.define(:state, :effective, :reason_code, :reason)
|
|
15
|
+
|
|
16
|
+
def self.call(declaration:, evaluation:, available_channels:)
|
|
17
|
+
new(declaration:, evaluation:, available_channels:).call
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(declaration:, evaluation:, available_channels:)
|
|
21
|
+
@declaration = declaration
|
|
22
|
+
@evaluation = evaluation
|
|
23
|
+
@available_channels = Array(available_channels).map(&:to_s).freeze
|
|
24
|
+
@locks = declaration.destination_locks || {}.freeze
|
|
25
|
+
@defaults = PreferenceState.from_declaration_default(declaration.default_preference_state)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def call
|
|
29
|
+
ChannelCatalog.entries_for(@declaration).to_h do |entry|
|
|
30
|
+
key = entry[:key]
|
|
31
|
+
[key, resolve(key).to_h.freeze]
|
|
32
|
+
end.freeze
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def resolve(key)
|
|
38
|
+
if key == "inbox"
|
|
39
|
+
resolve_inbox
|
|
40
|
+
else
|
|
41
|
+
resolve_channel(key)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def resolve_channel(channel)
|
|
46
|
+
if unavailable_external?(channel)
|
|
47
|
+
return build_entry(state: "unavailable", effective: false, reason_code: "unavailable")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
lock = @locks[channel]
|
|
51
|
+
return build_entry(state: "forced_off", effective: false, reason_code: "locked_off") if lock == "forced_off"
|
|
52
|
+
return build_entry(state: "forced_on", effective: true, reason_code: "locked_on") if lock == "forced_on"
|
|
53
|
+
|
|
54
|
+
member_on = member_channel_enabled?(channel)
|
|
55
|
+
if mandatory_required_channel?(channel) && !member_on
|
|
56
|
+
return build_entry(state: "forced_on", effective: true, reason_code: "required")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
build_entry(state: "editable", effective: member_on, reason_code: nil)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def resolve_inbox
|
|
63
|
+
lock = @locks["inbox"]
|
|
64
|
+
return build_entry(state: "forced_off", effective: false, reason_code: "locked_off") if lock == "forced_off"
|
|
65
|
+
return build_entry(state: "forced_on", effective: true, reason_code: "locked_on") if lock == "forced_on"
|
|
66
|
+
|
|
67
|
+
member_on = member_inbox_enabled?
|
|
68
|
+
if mandatory_required_inbox? && !member_on
|
|
69
|
+
return build_entry(state: "forced_on", effective: true, reason_code: "required")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
build_entry(state: "editable", effective: member_on, reason_code: nil)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def unavailable_external?(channel)
|
|
76
|
+
@declaration.allowed_channels.include?(channel) && !@available_channels.include?(channel)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def mandatory_required_channel?(channel)
|
|
80
|
+
@declaration.mandatory &&
|
|
81
|
+
(@declaration.default_channels & @declaration.allowed_channels).include?(channel)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def mandatory_required_inbox?
|
|
85
|
+
@declaration.mandatory && @declaration.inbox_available && @defaults.inbox != false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def member_channel_enabled?(channel)
|
|
89
|
+
state = @evaluation.effective_preference_state || {}
|
|
90
|
+
channels = state["channels"] || {}
|
|
91
|
+
if channels.key?(channel)
|
|
92
|
+
!!channels[channel]
|
|
93
|
+
else
|
|
94
|
+
@defaults.channel_enabled?(channel, default: true)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def member_inbox_enabled?
|
|
99
|
+
state = @evaluation.effective_preference_state || {}
|
|
100
|
+
if state.key?("inbox")
|
|
101
|
+
!!state["inbox"]
|
|
102
|
+
elsif @defaults.inbox.nil?
|
|
103
|
+
true
|
|
104
|
+
else
|
|
105
|
+
@defaults.inbox
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def build_entry(state:, effective:, reason_code:)
|
|
110
|
+
reason = reason_code.nil? ? nil : REASONS.fetch(reason_code)
|
|
111
|
+
DestinationEntry.new(
|
|
112
|
+
state:,
|
|
113
|
+
effective: !!effective,
|
|
114
|
+
reason_code:,
|
|
115
|
+
reason:,
|
|
116
|
+
)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
class NotificationAssembly
|
|
7
|
+
def self.build(
|
|
8
|
+
declaration:,
|
|
9
|
+
evaluation:,
|
|
10
|
+
available_channels:,
|
|
11
|
+
recipient_id:,
|
|
12
|
+
submitted_destinations: nil
|
|
13
|
+
)
|
|
14
|
+
effective_state = evaluation.effective_preference_state || {}
|
|
15
|
+
effective_channels = effective_state["channels"] || {}
|
|
16
|
+
channels =
|
|
17
|
+
declaration.allowed_channels.to_h do |channel|
|
|
18
|
+
[channel, !!effective_channels[channel]]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
inbox_enabled =
|
|
22
|
+
if effective_state.key?("inbox")
|
|
23
|
+
!!effective_state["inbox"]
|
|
24
|
+
else
|
|
25
|
+
evaluation.inbox_permitted
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
channel_catalog = ChannelCatalog.entries_for(declaration)
|
|
29
|
+
destinations = Destinations.call(
|
|
30
|
+
declaration:,
|
|
31
|
+
evaluation:,
|
|
32
|
+
available_channels:,
|
|
33
|
+
)
|
|
34
|
+
corrections =
|
|
35
|
+
if submitted_destinations.nil?
|
|
36
|
+
nil
|
|
37
|
+
else
|
|
38
|
+
built = Corrections.build(submitted: submitted_destinations, destinations:)
|
|
39
|
+
built.empty? ? nil : built
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
blocks = EffectiveChoice.call(
|
|
43
|
+
declaration:,
|
|
44
|
+
stored: Store.find(recipient_id:, notification_type_key: declaration.key),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
settings = SettingsProjection.for_notification(
|
|
48
|
+
declaration:,
|
|
49
|
+
blocks:,
|
|
50
|
+
destinations:,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
CatalogNotificationResult.build(
|
|
54
|
+
key: declaration.key,
|
|
55
|
+
label: declaration.label,
|
|
56
|
+
description: declaration.description,
|
|
57
|
+
order: declaration.type_order,
|
|
58
|
+
configurable: declaration.user_configurable,
|
|
59
|
+
mandatory: declaration.mandatory,
|
|
60
|
+
inbox_available: declaration.inbox_available,
|
|
61
|
+
allowed_channels: declaration.allowed_channels,
|
|
62
|
+
available_channels:,
|
|
63
|
+
inbox_enabled:,
|
|
64
|
+
channels:,
|
|
65
|
+
stored_override_present: evaluation.stored_override_present,
|
|
66
|
+
urgency: blocks[:urgency],
|
|
67
|
+
sound: blocks[:sound],
|
|
68
|
+
channel_catalog:,
|
|
69
|
+
destinations:,
|
|
70
|
+
corrections:,
|
|
71
|
+
settings:,
|
|
72
|
+
)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Preferences
|
|
6
|
+
module SettingControls
|
|
7
|
+
IMPLEMENTED = %w[single_select].freeze
|
|
8
|
+
RESERVED = %w[boolean multi_select read_only].freeze
|
|
9
|
+
|
|
10
|
+
class UnsupportedControlError < StandardError; end
|
|
11
|
+
|
|
12
|
+
def self.validate_implemented!(control)
|
|
13
|
+
normalized = control.to_s
|
|
14
|
+
if RESERVED.include?(normalized) && !IMPLEMENTED.include?(normalized)
|
|
15
|
+
raise UnsupportedControlError,
|
|
16
|
+
"setting control #{normalized.inspect} is reserved and not implemented"
|
|
17
|
+
end
|
|
18
|
+
return if IMPLEMENTED.include?(normalized)
|
|
19
|
+
|
|
20
|
+
raise UnsupportedControlError, "unknown setting control: #{normalized.inspect}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|