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
|
@@ -45,6 +45,15 @@ module CommandTower
|
|
|
45
45
|
info("messaging.execution.recovered", channel_delivery:)
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
+
# One aggregate event per recovery sweep. Counts only; nothing when nothing happened.
|
|
49
|
+
def receipts_collected(counts)
|
|
50
|
+
counts = counts.to_h.slice(*ExpoReceiptCollection::COUNT_KEYS)
|
|
51
|
+
return if counts.values.sum.zero?
|
|
52
|
+
|
|
53
|
+
failed = counts[:poll_failure_count].to_i.positive? || counts[:update_failure_count].to_i.positive?
|
|
54
|
+
emit(failed ? :error : :info, "messaging.execution.receipts.collected", **counts)
|
|
55
|
+
end
|
|
56
|
+
|
|
48
57
|
def attempt_started(channel_delivery:, delivery_attempt:)
|
|
49
58
|
emit_attempt("messaging.execution.attempt.started", channel_delivery:, delivery_attempt:)
|
|
50
59
|
end
|
|
@@ -62,6 +71,28 @@ module CommandTower
|
|
|
62
71
|
)
|
|
63
72
|
end
|
|
64
73
|
|
|
74
|
+
def push_destination_fallback(communication:, reason_code:, level:, channel_delivery: nil)
|
|
75
|
+
emit(
|
|
76
|
+
level,
|
|
77
|
+
"messaging.execution.push_destination.fallback",
|
|
78
|
+
channel_delivery:,
|
|
79
|
+
communication_id: communication.id,
|
|
80
|
+
notification_type_key: communication.notification_type_key.to_s,
|
|
81
|
+
reason_code:,
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def push_presentation_fallback(communication:, reason_code:, channel_delivery: nil)
|
|
86
|
+
emit(
|
|
87
|
+
:warn,
|
|
88
|
+
"messaging.execution.push_presentation.fallback",
|
|
89
|
+
channel_delivery:,
|
|
90
|
+
communication_id: communication.id,
|
|
91
|
+
notification_type_key: communication.notification_type_key.to_s,
|
|
92
|
+
reason_code:,
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
|
|
65
96
|
def attempt_create_failed(channel_delivery:, error:)
|
|
66
97
|
emit(
|
|
67
98
|
:error,
|
|
@@ -110,32 +141,35 @@ module CommandTower
|
|
|
110
141
|
)
|
|
111
142
|
end
|
|
112
143
|
|
|
113
|
-
def adapter_accepted(channel_delivery:, delivery_attempt:)
|
|
144
|
+
def adapter_accepted(channel_delivery:, delivery_attempt:, provider_metadata: nil)
|
|
114
145
|
emit_adapter(
|
|
115
146
|
:info,
|
|
116
147
|
"messaging.execution.adapter.accepted",
|
|
117
148
|
channel_delivery:,
|
|
118
149
|
delivery_attempt:,
|
|
150
|
+
**provider_metadata_counts(provider_metadata),
|
|
119
151
|
)
|
|
120
152
|
end
|
|
121
153
|
|
|
122
|
-
def adapter_retryable(channel_delivery:, delivery_attempt:, error_code:)
|
|
154
|
+
def adapter_retryable(channel_delivery:, delivery_attempt:, error_code:, provider_metadata: nil)
|
|
123
155
|
emit_adapter(
|
|
124
156
|
:error,
|
|
125
157
|
"messaging.execution.adapter.retryable",
|
|
126
158
|
channel_delivery:,
|
|
127
159
|
delivery_attempt:,
|
|
128
160
|
error_code:,
|
|
161
|
+
**provider_metadata_counts(provider_metadata),
|
|
129
162
|
)
|
|
130
163
|
end
|
|
131
164
|
|
|
132
|
-
def adapter_terminal(channel_delivery:, delivery_attempt:, error_code:)
|
|
165
|
+
def adapter_terminal(channel_delivery:, delivery_attempt:, error_code:, provider_metadata: nil)
|
|
133
166
|
emit_adapter(
|
|
134
167
|
:error,
|
|
135
168
|
"messaging.execution.adapter.terminal",
|
|
136
169
|
channel_delivery:,
|
|
137
170
|
delivery_attempt:,
|
|
138
171
|
error_code:,
|
|
172
|
+
**provider_metadata_counts(provider_metadata),
|
|
139
173
|
)
|
|
140
174
|
end
|
|
141
175
|
|
|
@@ -178,16 +212,32 @@ module CommandTower
|
|
|
178
212
|
)
|
|
179
213
|
end
|
|
180
214
|
|
|
181
|
-
|
|
215
|
+
# extra: optional error_code and provider_metadata counts.
|
|
216
|
+
def emit_adapter(level, event, channel_delivery:, delivery_attempt:, **extra)
|
|
182
217
|
emit(
|
|
183
218
|
level,
|
|
184
219
|
event,
|
|
185
220
|
channel_delivery:,
|
|
186
221
|
delivery_attempt_id: delivery_attempt.id,
|
|
187
|
-
error_code
|
|
222
|
+
error_code: nil,
|
|
223
|
+
**extra,
|
|
188
224
|
)
|
|
189
225
|
end
|
|
190
226
|
|
|
227
|
+
# Counts only; the per-device record stays on the attempt row.
|
|
228
|
+
def provider_metadata_counts(provider_metadata)
|
|
229
|
+
endpoints = provider_metadata.is_a?(Hash) ? provider_metadata.dig("expo", "endpoints") : nil
|
|
230
|
+
return {} unless endpoints.is_a?(Array)
|
|
231
|
+
|
|
232
|
+
ticket_statuses = endpoints.pluck("ticket_status")
|
|
233
|
+
{
|
|
234
|
+
endpoint_count: endpoints.size,
|
|
235
|
+
ticket_ok_count: ticket_statuses.count("ok"),
|
|
236
|
+
ticket_error_count: ticket_statuses.count { |status| %w[error missing].include?(status) },
|
|
237
|
+
receipt_pending_count: endpoints.count { |endpoint| endpoint["receipt_status"] == "pending" },
|
|
238
|
+
}
|
|
239
|
+
end
|
|
240
|
+
|
|
191
241
|
def info(event, channel_delivery:)
|
|
192
242
|
emit(:info, event, channel_delivery:)
|
|
193
243
|
end
|
|
@@ -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
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
ConversationResult = Data.define(
|
|
7
|
+
:scope,
|
|
8
|
+
:subject,
|
|
9
|
+
:message_count,
|
|
10
|
+
:unread_count,
|
|
11
|
+
:inbox_item_ids,
|
|
12
|
+
:message_ids,
|
|
13
|
+
:window,
|
|
14
|
+
) do
|
|
15
|
+
def self.build(scope:, subject:, message_count:, unread_count:, inbox_item_ids:, message_ids: nil, window: nil)
|
|
16
|
+
ids = Array(inbox_item_ids).map { |value| Integer(value) }.freeze
|
|
17
|
+
rendered = message_ids.nil? ? ids : Array(message_ids).map { |value| Integer(value) }.freeze
|
|
18
|
+
new(
|
|
19
|
+
scope: scope.to_s,
|
|
20
|
+
subject: subject.to_s,
|
|
21
|
+
message_count: Integer(message_count),
|
|
22
|
+
unread_count: Integer(unread_count),
|
|
23
|
+
inbox_item_ids: ids,
|
|
24
|
+
message_ids: rendered,
|
|
25
|
+
window: window&.freeze,
|
|
26
|
+
).freeze
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
EntryResult = Data.define(
|
|
7
|
+
:kind,
|
|
8
|
+
:id,
|
|
9
|
+
:title,
|
|
10
|
+
:preview,
|
|
11
|
+
:activity_at,
|
|
12
|
+
:created_at,
|
|
13
|
+
:message_count,
|
|
14
|
+
:unread_count,
|
|
15
|
+
:read,
|
|
16
|
+
:inbox_item_ids,
|
|
17
|
+
) do
|
|
18
|
+
def self.build(kind:, id:, title:, preview:, activity_at:, message_count:, unread_count:, inbox_item_ids:)
|
|
19
|
+
kind = kind.to_s
|
|
20
|
+
new(
|
|
21
|
+
kind:,
|
|
22
|
+
id: kind == "conversation" ? id.to_s : Integer(id),
|
|
23
|
+
title: title.to_s,
|
|
24
|
+
preview: preview&.to_s,
|
|
25
|
+
activity_at:,
|
|
26
|
+
created_at: activity_at,
|
|
27
|
+
message_count: Integer(message_count),
|
|
28
|
+
unread_count: Integer(unread_count),
|
|
29
|
+
read: Integer(unread_count).zero?,
|
|
30
|
+
inbox_item_ids: Array(inbox_item_ids).map { |value| Integer(value) }.freeze,
|
|
31
|
+
).freeze
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -6,6 +6,9 @@ module CommandTower
|
|
|
6
6
|
class Reader
|
|
7
7
|
DEFAULT_LIMIT = 50
|
|
8
8
|
MAX_LIMIT = 100
|
|
9
|
+
WINDOW_LIMIT = 5
|
|
10
|
+
WINDOW_MAX_OLDER = 2
|
|
11
|
+
OLDEST_UNREAD = "oldest_unread"
|
|
9
12
|
SCOPE_INBOX = "inbox"
|
|
10
13
|
SCOPE_ARCHIVED = "archived"
|
|
11
14
|
ALLOWED_SCOPES = [SCOPE_INBOX, SCOPE_ARCHIVED].freeze
|
|
@@ -17,28 +20,36 @@ module CommandTower
|
|
|
17
20
|
offset = normalize_offset!(offset)
|
|
18
21
|
scope = normalize_scope!(scope)
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
else
|
|
24
|
-
Messaging::InboxItem.default_list
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
scoped = Scope.for_recipient(recipient_id).merge(list_scope)
|
|
28
|
-
total_count = scoped.count
|
|
29
|
-
records =
|
|
30
|
-
scoped
|
|
31
|
-
.includes(:communication)
|
|
32
|
-
.order(Arel.sql("messaging_inbox_items.created_at DESC, messaging_inbox_items.id DESC"))
|
|
33
|
-
.limit(limit)
|
|
34
|
-
.offset(offset)
|
|
35
|
-
.to_a
|
|
23
|
+
scoped = Scope.for_recipient(recipient_id).merge(list_scope_for(scope))
|
|
24
|
+
page_rows = entry_page_rows(scoped, limit:, offset:)
|
|
25
|
+
members = page_members(scoped, page_rows)
|
|
36
26
|
|
|
37
27
|
ListResult.build(
|
|
38
|
-
items:
|
|
28
|
+
items: page_rows.map { |row| build_entry(row, members) },
|
|
39
29
|
limit:,
|
|
40
30
|
offset:,
|
|
41
|
-
total_count
|
|
31
|
+
total_count: entry_count(scoped),
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def conversation(recipient_id:, key:, scope: SCOPE_INBOX, around: nil, before_id: nil, after_id: nil, limit: nil)
|
|
36
|
+
recipient_id = require_recipient_id!(recipient_id)
|
|
37
|
+
raise ValidationError, "key is required" if blank?(key)
|
|
38
|
+
|
|
39
|
+
scope = normalize_scope!(scope)
|
|
40
|
+
records = members_for_key(recipient_id:, key:, scope:)
|
|
41
|
+
raise NotFoundError, "conversation not found" if records.empty?
|
|
42
|
+
|
|
43
|
+
window = conversation_window(records, around:, before_id:, after_id:, limit:)
|
|
44
|
+
latest = records.last
|
|
45
|
+
ConversationResult.build(
|
|
46
|
+
scope:,
|
|
47
|
+
subject: latest.communication.conversation_title,
|
|
48
|
+
message_count: records.size,
|
|
49
|
+
unread_count: records.count { |record| record.viewed_at.nil? },
|
|
50
|
+
inbox_item_ids: records.map(&:id),
|
|
51
|
+
message_ids: window ? window.fetch(:message_ids) : records.map(&:id),
|
|
52
|
+
window: window&.except(:message_ids),
|
|
42
53
|
)
|
|
43
54
|
end
|
|
44
55
|
|
|
@@ -54,13 +65,26 @@ module CommandTower
|
|
|
54
65
|
|
|
55
66
|
def unread_count(recipient_id:)
|
|
56
67
|
recipient_id = require_recipient_id!(recipient_id)
|
|
68
|
+
unread = Scope.for_recipient(recipient_id).merge(Messaging::InboxItem.unread)
|
|
57
69
|
|
|
58
|
-
|
|
59
|
-
|
|
70
|
+
UnreadCountResult.build(
|
|
71
|
+
recipient_id:,
|
|
72
|
+
count: unread.count,
|
|
73
|
+
badge_count: badge_eligible_count(unread),
|
|
74
|
+
)
|
|
60
75
|
end
|
|
61
76
|
|
|
62
77
|
private
|
|
63
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
|
+
|
|
64
88
|
def require_recipient_id!(recipient_id)
|
|
65
89
|
raise ValidationError, "recipient_id is required" if blank?(recipient_id)
|
|
66
90
|
|
|
@@ -106,6 +130,208 @@ module CommandTower
|
|
|
106
130
|
value
|
|
107
131
|
end
|
|
108
132
|
|
|
133
|
+
def list_scope_for(scope)
|
|
134
|
+
if scope == SCOPE_ARCHIVED
|
|
135
|
+
Messaging::InboxItem.archived_list
|
|
136
|
+
else
|
|
137
|
+
Messaging::InboxItem.default_list
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def entry_page_rows(scoped, limit:, offset:)
|
|
142
|
+
sql = <<~SQL.squish
|
|
143
|
+
SELECT conversation_key, inbox_item_id AS latest_id, item_created_at AS activity_at
|
|
144
|
+
FROM (
|
|
145
|
+
SELECT
|
|
146
|
+
members.*,
|
|
147
|
+
ROW_NUMBER() OVER (
|
|
148
|
+
PARTITION BY entry_group
|
|
149
|
+
ORDER BY item_created_at DESC, inbox_item_id DESC
|
|
150
|
+
) AS recency_rank
|
|
151
|
+
FROM (#{member_groups_sql(scoped)}) members
|
|
152
|
+
) ranked
|
|
153
|
+
WHERE recency_rank = 1
|
|
154
|
+
ORDER BY activity_at DESC, latest_id DESC
|
|
155
|
+
LIMIT #{limit} OFFSET #{offset}
|
|
156
|
+
SQL
|
|
157
|
+
|
|
158
|
+
Messaging::InboxItem.connection.select_all(sql).to_a
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def entry_count(scoped)
|
|
162
|
+
sql = <<~SQL.squish
|
|
163
|
+
SELECT COUNT(*) FROM (
|
|
164
|
+
SELECT entry_group
|
|
165
|
+
FROM (#{member_groups_sql(scoped)}) members
|
|
166
|
+
GROUP BY entry_group
|
|
167
|
+
) entry_groups
|
|
168
|
+
SQL
|
|
169
|
+
|
|
170
|
+
Messaging::InboxItem.connection.select_value(sql).to_i
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def member_groups_sql(scoped)
|
|
174
|
+
scoped.select(Arel.sql(<<~SQL.squish)).to_sql
|
|
175
|
+
messaging_inbox_items.id AS inbox_item_id,
|
|
176
|
+
messaging_inbox_items.created_at AS item_created_at,
|
|
177
|
+
messaging_communications.conversation_key AS conversation_key,
|
|
178
|
+
CASE
|
|
179
|
+
WHEN messaging_communications.conversation_key IS NULL
|
|
180
|
+
THEN CONCAT('m:', messaging_inbox_items.id)
|
|
181
|
+
ELSE CONCAT('c:', messaging_communications.conversation_key)
|
|
182
|
+
END AS entry_group
|
|
183
|
+
SQL
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def page_members(scoped, page_rows)
|
|
187
|
+
keys = page_rows.filter_map { |row| row_value(row, "conversation_key").presence }
|
|
188
|
+
standalone_ids = page_rows.filter_map do |row|
|
|
189
|
+
next if row_value(row, "conversation_key").present?
|
|
190
|
+
|
|
191
|
+
Integer(row_value(row, "latest_id"))
|
|
192
|
+
end
|
|
193
|
+
ids = standalone_ids.dup
|
|
194
|
+
if keys.any?
|
|
195
|
+
ids.concat(
|
|
196
|
+
scoped.where(messaging_communications: { conversation_key: keys }).pluck("messaging_inbox_items.id"),
|
|
197
|
+
)
|
|
198
|
+
end
|
|
199
|
+
return [] if ids.empty?
|
|
200
|
+
|
|
201
|
+
scoped.where(messaging_inbox_items: { id: ids }).includes(:communication).to_a
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def build_entry(row, members)
|
|
205
|
+
latest_id = Integer(row_value(row, "latest_id"))
|
|
206
|
+
key = row_value(row, "conversation_key").presence
|
|
207
|
+
group =
|
|
208
|
+
if key
|
|
209
|
+
members.select { |member| member.communication.conversation_key == key }
|
|
210
|
+
else
|
|
211
|
+
members.select { |member| member.id == latest_id }
|
|
212
|
+
end
|
|
213
|
+
group = group.sort_by { |member| [member.created_at, member.id] }
|
|
214
|
+
latest = group.find { |member| member.id == latest_id }
|
|
215
|
+
raise InvariantError, "latest inbox member missing" if latest.nil?
|
|
216
|
+
|
|
217
|
+
conversation = key.present?
|
|
218
|
+
EntryResult.build(
|
|
219
|
+
kind: conversation ? "conversation" : "message",
|
|
220
|
+
id: conversation ? key : latest.id,
|
|
221
|
+
title: conversation ? latest.communication.conversation_title : latest.communication.title,
|
|
222
|
+
preview: conversation ? latest.communication.title : nil,
|
|
223
|
+
activity_at: latest.created_at,
|
|
224
|
+
message_count: group.size,
|
|
225
|
+
unread_count: group.count { |member| member.viewed_at.nil? },
|
|
226
|
+
inbox_item_ids: group.map(&:id),
|
|
227
|
+
)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def conversation_window(records, around:, before_id:, after_id:, limit:)
|
|
231
|
+
windowed = !around.nil? || !before_id.nil? || !after_id.nil?
|
|
232
|
+
unless windowed
|
|
233
|
+
raise ValidationError, "invalid_limit" unless limit.nil?
|
|
234
|
+
|
|
235
|
+
return
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
modes = [around, before_id, after_id].count { |value| !value.nil? }
|
|
239
|
+
raise ValidationError, "invalid_window" unless modes == 1
|
|
240
|
+
|
|
241
|
+
page_limit = normalize_window_limit!(limit)
|
|
242
|
+
cursor = nil
|
|
243
|
+
anchor = nil
|
|
244
|
+
slice =
|
|
245
|
+
if !around.nil?
|
|
246
|
+
anchor = resolve_anchor(records, around)
|
|
247
|
+
around_slice(records, anchor, page_limit)
|
|
248
|
+
elsif !before_id.nil?
|
|
249
|
+
cursor = find_member!(records, before_id)
|
|
250
|
+
records.select { |record| before_record?(record, cursor) }.last(page_limit)
|
|
251
|
+
else
|
|
252
|
+
cursor = find_member!(records, after_id)
|
|
253
|
+
records.select { |record| before_record?(cursor, record) }.first(page_limit)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
flags = window_flags(records, slice, cursor)
|
|
257
|
+
{
|
|
258
|
+
message_ids: slice.map(&:id),
|
|
259
|
+
anchor_inbox_item_id: anchor&.id,
|
|
260
|
+
has_before: flags.fetch(:has_before),
|
|
261
|
+
has_after: flags.fetch(:has_after),
|
|
262
|
+
before_cursor: flags.fetch(:before_cursor),
|
|
263
|
+
after_cursor: flags.fetch(:after_cursor),
|
|
264
|
+
}
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def resolve_anchor(records, around)
|
|
268
|
+
return records.find { |record| record.viewed_at.nil? } || records.last if around.to_s == OLDEST_UNREAD
|
|
269
|
+
|
|
270
|
+
find_member!(records, around)
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def around_slice(records, anchor, limit)
|
|
274
|
+
index = records.index(anchor)
|
|
275
|
+
before_count = [WINDOW_MAX_OLDER, index, limit - 1].min
|
|
276
|
+
after_count = [limit - 1 - before_count, records.length - 1 - index].min
|
|
277
|
+
records[(index - before_count)..(index + after_count)]
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def find_member!(records, inbox_item_id)
|
|
281
|
+
id = Integer(inbox_item_id)
|
|
282
|
+
records.find { |record| record.id == id } || raise(ValidationError, "invalid_cursor")
|
|
283
|
+
rescue ArgumentError, TypeError
|
|
284
|
+
raise ValidationError, "invalid_cursor"
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def window_flags(records, slice, cursor)
|
|
288
|
+
if slice.any?
|
|
289
|
+
first = slice.first
|
|
290
|
+
last = slice.last
|
|
291
|
+
{
|
|
292
|
+
has_before: records.any? { |record| before_record?(record, first) },
|
|
293
|
+
has_after: records.any? { |record| before_record?(last, record) },
|
|
294
|
+
before_cursor: first.id,
|
|
295
|
+
after_cursor: last.id,
|
|
296
|
+
}
|
|
297
|
+
else
|
|
298
|
+
{
|
|
299
|
+
has_before: !cursor.nil? && records.any? { |record| before_record?(record, cursor) },
|
|
300
|
+
has_after: !cursor.nil? && records.any? { |record| before_record?(cursor, record) },
|
|
301
|
+
before_cursor: nil,
|
|
302
|
+
after_cursor: nil,
|
|
303
|
+
}
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def before_record?(candidate, boundary)
|
|
308
|
+
return candidate.id < boundary.id if candidate.created_at == boundary.created_at
|
|
309
|
+
|
|
310
|
+
candidate.created_at < boundary.created_at
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def normalize_window_limit!(limit)
|
|
314
|
+
value = limit.nil? ? WINDOW_LIMIT : Integer(limit)
|
|
315
|
+
raise ValidationError, "invalid_limit" if value < 1 || value > WINDOW_LIMIT
|
|
316
|
+
|
|
317
|
+
value
|
|
318
|
+
rescue ArgumentError, TypeError
|
|
319
|
+
raise ValidationError, "invalid_limit"
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def members_for_key(recipient_id:, key:, scope:)
|
|
323
|
+
Scope.for_recipient(recipient_id)
|
|
324
|
+
.merge(list_scope_for(scope))
|
|
325
|
+
.includes(:communication)
|
|
326
|
+
.where(messaging_communications: { conversation_key: key })
|
|
327
|
+
.order(Arel.sql("messaging_inbox_items.created_at ASC, messaging_inbox_items.id ASC"))
|
|
328
|
+
.to_a
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def row_value(row, key)
|
|
332
|
+
row[key] || row[key.to_sym]
|
|
333
|
+
end
|
|
334
|
+
|
|
109
335
|
def blank?(value)
|
|
110
336
|
value.nil? || (value.respond_to?(:empty?) && value.empty?) ||
|
|
111
337
|
(value.is_a?(String) && value.strip.empty?)
|
|
@@ -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
|
|
@@ -13,6 +13,10 @@ module CommandTower
|
|
|
13
13
|
Reader.show(recipient_id:, inbox_item_id:)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
def conversation(recipient_id:, key:, scope: Reader::SCOPE_INBOX, around: nil, before_id: nil, after_id: nil, limit: nil)
|
|
17
|
+
Reader.conversation(recipient_id:, key:, scope:, around:, before_id:, after_id:, limit:)
|
|
18
|
+
end
|
|
19
|
+
|
|
16
20
|
def unread_count(recipient_id:)
|
|
17
21
|
Reader.unread_count(recipient_id:)
|
|
18
22
|
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
|