command_tower 0.17.0 → 0.19.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/README.md +8 -8
- data/app/controllers/command_tower/me/inbox_controller.rb +16 -0
- data/app/deserializers/command_tower/deserializers/messaging/inbox.rb +85 -0
- 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/jobs/command_tower/messaging/communications/produce_recipient_job.rb +2 -0
- data/app/models/command_tower/messaging/communication.rb +31 -0
- data/app/models/command_tower/messaging/inbox_item.rb +6 -0
- data/app/serializers/command_tower/serializers/messaging/inbox.rb +44 -1
- data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +2 -0
- data/app/services/command_tower/messaging/accept/coordinator.rb +16 -1
- data/app/services/command_tower/messaging/accept/persister.rb +13 -0
- data/app/services/command_tower/messaging/contract/mappers/communication_mapper.rb +7 -0
- data/app/services/command_tower/messaging/contract/results/communication_result.rb +1 -0
- data/app/services/command_tower/messaging/execution/adapter_result.rb +11 -1
- data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +146 -25
- 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/interruption_level.rb +41 -0
- data/app/services/command_tower/messaging/execution/attempt_ledger.rb +3 -0
- data/app/services/command_tower/messaging/execution/expo_receipt_collection.rb +224 -0
- data/app/services/command_tower/messaging/execution/operation_logger.rb +55 -5
- data/app/services/command_tower/messaging/execution/recovery.rb +8 -0
- data/app/services/command_tower/messaging/inbox/conversation_result.rb +31 -0
- data/app/services/command_tower/messaging/inbox/entry_result.rb +36 -0
- data/app/services/command_tower/messaging/inbox/reader.rb +246 -20
- data/app/services/command_tower/messaging/inbox/unread_count_result.rb +3 -2
- data/app/services/command_tower/messaging/inbox.rb +4 -0
- 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/inbox_document_renderer.rb +53 -36
- data/app/services/command_tower/messaging/rendering/inbox_presentation_resolver.rb +52 -0
- data/app/services/command_tower/messaging/rendering/inbox_presentation_snapshot.rb +64 -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/messaging.rb +5 -1
- data/app/services/command_tower/services/account/push/send_self_test.rb +7 -1
- data/app/services/command_tower/services/messaging/communications/produce.rb +31 -0
- data/app/services/command_tower/services/messaging/communications/produce_many.rb +6 -0
- data/app/services/command_tower/services/messaging/inbox.rb +52 -2
- data/app/workflows/command_tower/workflows/me/error_mapping.rb +1 -0
- data/app/workflows/command_tower/workflows/messaging/communications/produce_recipient_workflow.rb +5 -1
- data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +5 -1
- data/app/workflows/command_tower/workflows/messaging/inbox.rb +15 -1
- data/config/routes.rb +1 -0
- data/db/migrate/20261010000001_add_inbox_presentation_snapshot_to_messaging_communications.rb +7 -0
- data/db/migrate/20261010000002_add_conversation_identity_to_messaging_communications.rb +10 -0
- 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/authorization.md +1 -1
- data/docs/bootstrap/00-ownership.md +77 -0
- data/docs/bootstrap/01-docker-make-compose.md +267 -0
- data/docs/bootstrap/02-create-the-rails-app.md +75 -0
- data/docs/bootstrap/03-pin-the-gem.md +53 -0
- data/docs/bootstrap/04-secrets-and-env.md +59 -0
- data/docs/bootstrap/05-install-migrate-doctor.md +74 -0
- data/docs/bootstrap/06-mount-and-health.md +53 -0
- data/docs/bootstrap/07-execution-bases.md +38 -0
- data/docs/bootstrap/08-initializer.md +90 -0
- data/docs/bootstrap/09-rbac.md +83 -0
- data/docs/bootstrap/10-roles-and-gates.md +62 -0
- data/docs/bootstrap/11-auth-client-path.md +57 -0
- data/docs/bootstrap/12-smoke-check.md +81 -0
- data/docs/bootstrap/13-optional.md +50 -0
- data/docs/bootstrap/14-sanity-checks.md +44 -0
- data/docs/bootstrap/README.md +90 -0
- data/docs/cookie_authentication_guide.md +11 -0
- data/docs/extending.md +3 -3
- data/docs/host_integration_guide.md +3 -281
- data/docs/initializing.md +37 -28
- data/docs/messaging_integration_guide.md +1 -1
- data/docs/principal_capabilities.md +1 -1
- data/docs/upgrades/0.10.0.md +5 -5
- data/docs/upgrades/0.11.0.md +5 -5
- data/docs/upgrades/0.18.0.md +35 -0
- data/docs/upgrades/0.19.0.md +40 -0
- data/docs/upgrades/README.md +3 -1
- data/lib/command_tower/authorization/default.yml +1 -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 +4 -0
- data/lib/command_tower/version.rb +1 -1
- metadata +36 -2
|
@@ -36,6 +36,12 @@ module CommandTower
|
|
|
36
36
|
# fail open and then let a second node still execute composer domain
|
|
37
37
|
# I/O.
|
|
38
38
|
#
|
|
39
|
+
# A stored `inbox_presentation_snapshot` replaces that live resolution.
|
|
40
|
+
# Null means there is no retained presentation (legacy rows, or a
|
|
41
|
+
# document with no presentation node) and the live composer still runs.
|
|
42
|
+
# An omitted snapshot drops the presentation node and does not call
|
|
43
|
+
# the composer. A stored payload is rendered as-is.
|
|
44
|
+
#
|
|
39
45
|
# The composer's raw result is always projected down to the
|
|
40
46
|
# registration's declared `output_keys` before it reaches
|
|
41
47
|
# `content.blocks[].data` (corrective slice — presentation
|
|
@@ -50,6 +56,10 @@ module CommandTower
|
|
|
50
56
|
new(communication:, recipient_id:).render
|
|
51
57
|
end
|
|
52
58
|
|
|
59
|
+
def self.compose_shell(communication)
|
|
60
|
+
new(communication:, recipient_id: nil).compose_shell
|
|
61
|
+
end
|
|
62
|
+
|
|
53
63
|
def initialize(communication:, recipient_id:)
|
|
54
64
|
@communication = communication
|
|
55
65
|
@recipient_id = recipient_id
|
|
@@ -57,9 +67,16 @@ module CommandTower
|
|
|
57
67
|
end
|
|
58
68
|
|
|
59
69
|
def render
|
|
60
|
-
document =
|
|
70
|
+
document = compose_shell
|
|
71
|
+
snapshot = stored_presentation_snapshot
|
|
72
|
+
return resolve_presentation_blocks(document) if snapshot.nil?
|
|
73
|
+
return drop_presentation_blocks(document) if InboxPresentationSnapshot.omitted?(snapshot)
|
|
74
|
+
|
|
75
|
+
apply_stored_presentation(document, snapshot)
|
|
76
|
+
end
|
|
61
77
|
|
|
62
|
-
|
|
78
|
+
def compose_shell
|
|
79
|
+
build_from_type_composer || build_generic
|
|
63
80
|
end
|
|
64
81
|
|
|
65
82
|
private
|
|
@@ -107,42 +124,42 @@ module CommandTower
|
|
|
107
124
|
return nil if @presentation_attempted
|
|
108
125
|
|
|
109
126
|
@presentation_attempted = true
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
+
InboxPresentationResolver.resolve(communication:, recipient_id:, block:)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def stored_presentation_snapshot
|
|
131
|
+
raw = communication.inbox_presentation_snapshot
|
|
132
|
+
return nil if raw.blank?
|
|
133
|
+
return nil unless raw.respond_to?(:stringify_keys)
|
|
134
|
+
|
|
135
|
+
raw.stringify_keys
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def drop_presentation_blocks(document)
|
|
139
|
+
InboxDocument.build(blocks: document.blocks.reject { |block| block[:type] == "presentation" })
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def apply_stored_presentation(document, snapshot)
|
|
143
|
+
applied = false
|
|
144
|
+
blocks = document.blocks.filter_map do |block|
|
|
145
|
+
next block unless block[:type] == "presentation"
|
|
146
|
+
next nil if applied
|
|
147
|
+
|
|
148
|
+
applied = true
|
|
149
|
+
stored_presentation_block(snapshot)
|
|
127
150
|
end
|
|
128
151
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
# Fail-open: unregistered capability, unresolvable composer class,
|
|
141
|
-
# or a raising/failing composer must never surface as a 500 on the
|
|
142
|
-
# Inbox read path, and must never allow a subsequent presentation
|
|
143
|
-
# block in the same document to still attempt composition (the
|
|
144
|
-
# budget above is consumed regardless of this outcome).
|
|
145
|
-
nil
|
|
152
|
+
InboxDocument.build(blocks:)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def stored_presentation_block(snapshot)
|
|
156
|
+
data = snapshot["data"].respond_to?(:to_h) ? snapshot["data"].to_h.deep_symbolize_keys : {}
|
|
157
|
+
{
|
|
158
|
+
type: "presentation",
|
|
159
|
+
key: snapshot["key"].to_s,
|
|
160
|
+
variant: snapshot["variant"],
|
|
161
|
+
data:,
|
|
162
|
+
}.freeze
|
|
146
163
|
end
|
|
147
164
|
|
|
148
165
|
def paragraph_block(text)
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Rendering
|
|
6
|
+
# Resolves one presentation block through the inbox presentation registry.
|
|
7
|
+
# Returns the projected block, or nil when the capability is missing,
|
|
8
|
+
# the composer fails, or the composer raises. Does not persist and does
|
|
9
|
+
# not perform network I/O. Callers own the one-presentation budget.
|
|
10
|
+
class InboxPresentationResolver
|
|
11
|
+
def self.resolve(communication:, recipient_id:, block:)
|
|
12
|
+
new(communication:, recipient_id:, block:).resolve
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(communication:, recipient_id:, block:)
|
|
16
|
+
@communication = communication
|
|
17
|
+
@recipient_id = recipient_id
|
|
18
|
+
@block = block
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def resolve
|
|
22
|
+
definition = CommandTower.config.registry.inbox_presentations.fetch(block[:key])
|
|
23
|
+
raw_data = unwrap(definition.composer_class.constantize.call(communication:, recipient_id:))
|
|
24
|
+
return nil if raw_data.nil?
|
|
25
|
+
|
|
26
|
+
{
|
|
27
|
+
type: "presentation",
|
|
28
|
+
key: block[:key],
|
|
29
|
+
variant: block[:variant],
|
|
30
|
+
data: raw_data.to_h.slice(*definition.output_keys),
|
|
31
|
+
}.freeze
|
|
32
|
+
rescue StandardError
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
attr_reader :communication, :recipient_id, :block
|
|
39
|
+
|
|
40
|
+
def unwrap(composer_result)
|
|
41
|
+
if composer_result.respond_to?(:success?)
|
|
42
|
+
return nil unless composer_result.success?
|
|
43
|
+
|
|
44
|
+
composer_result.data
|
|
45
|
+
else
|
|
46
|
+
composer_result
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Rendering
|
|
6
|
+
# Captures the first presentation node's projected data at Accept time.
|
|
7
|
+
# Returns nil when the document has no presentation node. Returns an
|
|
8
|
+
# omitted marker when a presentation node was seen and capture failed.
|
|
9
|
+
# Never persists and never performs network I/O.
|
|
10
|
+
class InboxPresentationSnapshot
|
|
11
|
+
VERSION = 1
|
|
12
|
+
|
|
13
|
+
def self.capture(communication:, recipient_id:)
|
|
14
|
+
new(communication:, recipient_id:).capture
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.omitted?(snapshot)
|
|
18
|
+
snapshot.is_a?(Hash) && snapshot.stringify_keys["omitted"] == true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(communication:, recipient_id:)
|
|
22
|
+
@communication = communication
|
|
23
|
+
@recipient_id = recipient_id
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def capture
|
|
27
|
+
block = first_presentation_block
|
|
28
|
+
return nil if block.nil?
|
|
29
|
+
|
|
30
|
+
capture_seen_block(block)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
attr_reader :communication, :recipient_id
|
|
36
|
+
|
|
37
|
+
def capture_seen_block(block)
|
|
38
|
+
resolved = InboxPresentationResolver.resolve(communication:, recipient_id:, block:)
|
|
39
|
+
resolved.nil? ? omitted_marker : payload(resolved)
|
|
40
|
+
rescue StandardError
|
|
41
|
+
omitted_marker
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def first_presentation_block
|
|
45
|
+
document = InboxDocumentRenderer.compose_shell(communication)
|
|
46
|
+
document.blocks.find { |block| block[:type] == "presentation" }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def payload(resolved)
|
|
50
|
+
{
|
|
51
|
+
"v" => VERSION,
|
|
52
|
+
"key" => resolved[:key].to_s,
|
|
53
|
+
"variant" => resolved[:variant],
|
|
54
|
+
"data" => resolved[:data].deep_stringify_keys,
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def omitted_marker
|
|
59
|
+
{ "v" => VERSION, "omitted" => true }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -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(
|
|
16
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
@@ -13,7 +13,9 @@ module CommandTower
|
|
|
13
13
|
metadata: nil,
|
|
14
14
|
preference_state: nil,
|
|
15
15
|
platform_enabled_channels:,
|
|
16
|
-
message_overrides: nil
|
|
16
|
+
message_overrides: nil,
|
|
17
|
+
conversation_key: nil,
|
|
18
|
+
conversation_title: nil
|
|
17
19
|
)
|
|
18
20
|
Accept::Coordinator.call(
|
|
19
21
|
recipient_id:,
|
|
@@ -25,6 +27,8 @@ module CommandTower
|
|
|
25
27
|
preference_state:,
|
|
26
28
|
platform_enabled_channels:,
|
|
27
29
|
message_overrides:,
|
|
30
|
+
conversation_key:,
|
|
31
|
+
conversation_title:,
|
|
28
32
|
)
|
|
29
33
|
end
|
|
30
34
|
|
|
@@ -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?
|
|
@@ -15,6 +15,8 @@ module CommandTower
|
|
|
15
15
|
validate :body, is_a: String, required: true
|
|
16
16
|
validate :metadata, is_a: Hash, required: false
|
|
17
17
|
validate :platform_enabled_channels, is_a: Array, required: true
|
|
18
|
+
validate :conversation_key, is_a: String, required: false
|
|
19
|
+
validate :conversation_title, is_a: String, required: false
|
|
18
20
|
|
|
19
21
|
def call
|
|
20
22
|
recipient_result = CommandTower::Services::Messaging::Recipients.call(user: user)
|
|
@@ -23,6 +25,15 @@ module CommandTower
|
|
|
23
25
|
return
|
|
24
26
|
end
|
|
25
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
|
+
|
|
26
37
|
accept_result = CommandTower::Messaging.accept(
|
|
27
38
|
recipient_id: recipient_result.data[:recipient_id],
|
|
28
39
|
notification_type_key:,
|
|
@@ -33,6 +44,8 @@ module CommandTower
|
|
|
33
44
|
preference_state: nil,
|
|
34
45
|
platform_enabled_channels:,
|
|
35
46
|
message_overrides: nil,
|
|
47
|
+
conversation_key:,
|
|
48
|
+
conversation_title:,
|
|
36
49
|
)
|
|
37
50
|
|
|
38
51
|
# Use communication_status — ServiceResult strips Interactor reserved :status.
|
|
@@ -61,6 +74,24 @@ module CommandTower
|
|
|
61
74
|
rescue CommandTower::Messaging::Accept::Error
|
|
62
75
|
context.fail!(application_error: CommandTower::Errors::InternalError.new)
|
|
63
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
|
|
64
95
|
end
|
|
65
96
|
end
|
|
66
97
|
end
|
|
@@ -18,6 +18,8 @@ module CommandTower
|
|
|
18
18
|
validate :body, is_a: String, required: true
|
|
19
19
|
validate :platform_enabled_channels, is_a: Array, required: true
|
|
20
20
|
validate :metadata, is_a: Hash, required: false
|
|
21
|
+
validate :conversation_key, is_a: String, required: false
|
|
22
|
+
validate :conversation_title, is_a: String, required: false
|
|
21
23
|
validate :execution_mode, is_one: EXECUTION_MODES, required: false, default: :async
|
|
22
24
|
|
|
23
25
|
def call
|
|
@@ -128,6 +130,8 @@ module CommandTower
|
|
|
128
130
|
body:,
|
|
129
131
|
metadata:,
|
|
130
132
|
platform_enabled_channels:,
|
|
133
|
+
conversation_key:,
|
|
134
|
+
conversation_title:,
|
|
131
135
|
)
|
|
132
136
|
end
|
|
133
137
|
|
|
@@ -139,6 +143,8 @@ module CommandTower
|
|
|
139
143
|
"body" => body,
|
|
140
144
|
"platform_enabled_channels" => platform_enabled_channels,
|
|
141
145
|
"metadata" => metadata,
|
|
146
|
+
"conversation_key" => conversation_key,
|
|
147
|
+
"conversation_title" => conversation_title,
|
|
142
148
|
}
|
|
143
149
|
end
|
|
144
150
|
|