command_tower 0.26.0 → 0.27.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/controllers/command_tower/me/inbox_controller.rb +61 -2
- data/app/deserializers/command_tower/deserializers/messaging/inbox.rb +12 -0
- data/app/errors/command_tower/errors/messaging/inbox_impersonation_forbidden_error.rb +23 -0
- data/app/jobs/command_tower/messaging/inbox/badge_sync_job.rb +20 -0
- data/app/models/command_tower/messaging/communication.rb +6 -0
- data/app/models/command_tower/messaging/inbox_item.rb +25 -5
- data/app/serializers/command_tower/serializers/messaging/inbox.rb +18 -0
- data/app/services/command_tower/messaging/accept/coordinator.rb +5 -2
- data/app/services/command_tower/messaging/accept/persister.rb +15 -0
- data/app/services/command_tower/messaging/accept/request_normalizer.rb +1 -0
- data/app/services/command_tower/messaging/accept/thread_restorer.rb +93 -0
- data/app/services/command_tower/messaging/contract/inbox.rb +49 -0
- data/app/services/command_tower/messaging/contract/requests/resolve_for_event.rb +25 -0
- data/app/services/command_tower/messaging/contract/results/inbox_resolution.rb +18 -0
- data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +3 -0
- data/app/services/command_tower/messaging/inbox/badge_sync_scheduler.rb +74 -0
- data/app/services/command_tower/messaging/inbox/entry_result.rb +11 -2
- data/app/services/command_tower/messaging/inbox/impersonation_audit.rb +40 -0
- data/app/services/command_tower/messaging/inbox/mutator.rb +124 -18
- data/app/services/command_tower/messaging/inbox/operation_logger.rb +11 -0
- data/app/services/command_tower/messaging/inbox/reader.rb +7 -14
- data/app/services/command_tower/messaging/inbox/record_views_result.rb +11 -0
- data/app/services/command_tower/messaging/inbox/resolution_result.rb +16 -0
- data/app/services/command_tower/messaging/inbox.rb +16 -0
- data/app/services/command_tower/messaging/notification_types/declaration.rb +2 -1
- data/app/services/command_tower/messaging/rendering/push_presentation.rb +8 -10
- data/app/services/command_tower/messaging.rb +3 -1
- data/app/services/command_tower/services/messaging/communications/produce.rb +2 -0
- data/app/services/command_tower/services/messaging/inbox.rb +31 -1
- data/app/workflows/command_tower/workflows/messaging/error_mapping.rb +2 -1
- data/app/workflows/command_tower/workflows/messaging/inbox/sync_app_icon_badge_workflow.rb +99 -0
- data/app/workflows/command_tower/workflows/messaging/inbox.rb +70 -6
- data/config/routes.rb +2 -0
- data/db/migrate/20261127000000_add_resolved_at_to_messaging_inbox_items.rb +7 -0
- data/db/migrate/20261127000001_add_collapse_key_to_messaging_communications.rb +7 -0
- data/docs/api_reference.md +21 -6
- data/docs/audit.md +3 -0
- data/docs/messaging_integration_guide.md +20 -0
- data/docs/ui.md +1 -1
- data/docs/upgrades/0.27.0.md +65 -0
- data/docs/upgrades/README.md +1 -0
- data/lib/command_tower/authorization/default.yml +2 -0
- data/lib/command_tower/configuration/messaging/config.rb +6 -0
- data/lib/command_tower/configuration/messaging/inbox.rb +30 -0
- data/lib/command_tower/configuration/registry/audit/config.rb +16 -0
- data/lib/command_tower/install/baseline.rb +2 -0
- data/lib/command_tower/version.rb +1 -1
- metadata +16 -2
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
# One audit event per request when an operator changes a recipient's inbox while impersonating.
|
|
7
|
+
# Called inside the mutation transaction: the audit write is part of it, so a failed audit
|
|
8
|
+
# (including a missing admin attribution) rolls the change back. Metadata is ids only.
|
|
9
|
+
class ImpersonationAudit
|
|
10
|
+
EVENT = :messaging_inbox_changed_while_impersonating
|
|
11
|
+
|
|
12
|
+
# Intentionally independent from Mutator::BULK_MAX_IDS: the audit payload is size-capped
|
|
13
|
+
# (a mark-all-read can change far more rows than a bulk request may name).
|
|
14
|
+
MAX_AUDITED_IDS = 50
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
def impersonating?
|
|
18
|
+
CommandTower::Current.impersonation_active == true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# No-op for a normal session or when nothing changed.
|
|
22
|
+
def record_changes(recipient_id:, action:, changed_ids:, up_to: nil)
|
|
23
|
+
return unless impersonating?
|
|
24
|
+
return if changed_ids.empty?
|
|
25
|
+
|
|
26
|
+
metadata = {
|
|
27
|
+
action: action.to_s,
|
|
28
|
+
inbox_item_ids: changed_ids.first(MAX_AUDITED_IDS),
|
|
29
|
+
changed_count: changed_ids.size
|
|
30
|
+
}
|
|
31
|
+
metadata[:up_to] = up_to unless up_to.nil?
|
|
32
|
+
metadata[:truncated] = true if changed_ids.size > MAX_AUDITED_IDS
|
|
33
|
+
|
|
34
|
+
CommandTower::Audit::Emit.call(name: EVENT, affected_user: recipient_id, metadata:)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -32,6 +32,17 @@ module CommandTower
|
|
|
32
32
|
nil
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# Host-driven, keyed by the communication idempotency namespace. Idempotent,
|
|
36
|
+
# never touches viewed_at, emits no audit. Returns nil when there is no live
|
|
37
|
+
# inbox item for the recipient and event.
|
|
38
|
+
def resolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
39
|
+
set_resolved(recipient_id:, notification_type_key:, host_event_identity:, resolved: true)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def unresolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
43
|
+
set_resolved(recipient_id:, notification_type_key:, host_event_identity:, resolved: false)
|
|
44
|
+
end
|
|
45
|
+
|
|
35
46
|
def mark_unviewed(recipient_id:, inbox_item_id:)
|
|
36
47
|
mutate(recipient_id:, inbox_item_id:, operation: :mark_unviewed)
|
|
37
48
|
end
|
|
@@ -68,8 +79,92 @@ module CommandTower
|
|
|
68
79
|
bulk_mutate(recipient_id:, inbox_item_ids:, operation: :delete)
|
|
69
80
|
end
|
|
70
81
|
|
|
82
|
+
# Scroll-read: marks every available id viewed in one transaction. Unlike bulk_mark_viewed,
|
|
83
|
+
# ids that are missing, another recipient's or deleted do not fail the batch; they are
|
|
84
|
+
# returned together as unavailable_ids. newly_viewed lists only first views.
|
|
85
|
+
def record_views(recipient_id:, inbox_item_ids:)
|
|
86
|
+
recipient_id = require_recipient_id!(recipient_id)
|
|
87
|
+
requested_ids = require_inbox_item_ids!(inbox_item_ids)
|
|
88
|
+
|
|
89
|
+
viewed_ids = []
|
|
90
|
+
newly_viewed = []
|
|
91
|
+
|
|
92
|
+
ActiveRecord::Base.transaction do
|
|
93
|
+
records_by_id = Scope.for_recipient(recipient_id).lock.where(id: requested_ids).index_by { |record| record.id.to_s }
|
|
94
|
+
requested_ids.each do |requested_id|
|
|
95
|
+
record = records_by_id[requested_id.to_s]
|
|
96
|
+
next if record.nil? || record.deleted?
|
|
97
|
+
|
|
98
|
+
viewed_ids << record.id
|
|
99
|
+
newly_viewed << { id: record.id, communication_id: record.communication_id } if apply_mark_viewed(record) == :changed
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
log_bulk_lifecycle(recipient_id:, operation: :mark_viewed, changed_ids: newly_viewed.pluck(:id))
|
|
104
|
+
|
|
105
|
+
unavailable_ids = requested_ids.reject { |requested_id| viewed_ids.include?(requested_id.to_i) }
|
|
106
|
+
RecordViewsResult.new(viewed_ids:, newly_viewed:, unavailable_ids:).freeze
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Marks every row that counts as unread (live, not archived, not resolved) with
|
|
110
|
+
# id <= up_to viewed, in one transaction. Rows above the watermark, archived,
|
|
111
|
+
# resolved and deleted rows are left untouched, so an Archived-segment call is a no-op.
|
|
112
|
+
# Locked in id order so concurrent callers cannot deadlock.
|
|
113
|
+
def mark_all_read(recipient_id:, up_to:)
|
|
114
|
+
recipient_id = require_recipient_id!(recipient_id)
|
|
115
|
+
raise ValidationError, "up_to must be a positive integer" unless up_to.is_a?(Integer) && up_to.positive?
|
|
116
|
+
|
|
117
|
+
changed_ids = []
|
|
118
|
+
|
|
119
|
+
ActiveRecord::Base.transaction do
|
|
120
|
+
Scope.for_recipient(recipient_id).merge(Messaging::InboxItem.counts_as_unread)
|
|
121
|
+
.where(id: ..up_to).order(:id).lock.each do |record|
|
|
122
|
+
changed_ids << record.id if apply_mark_viewed(record) == :changed
|
|
123
|
+
end
|
|
124
|
+
audit_impersonated_changes(recipient_id:, action: :mark_all_read, changed_ids:, up_to:)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
log_bulk_lifecycle(recipient_id:, operation: :mark_viewed, changed_ids:)
|
|
128
|
+
schedule_badge_sync(recipient_id) if changed_ids.any?
|
|
129
|
+
|
|
130
|
+
BulkResult.build(ids: changed_ids, changed_count: changed_ids.size)
|
|
131
|
+
end
|
|
132
|
+
|
|
71
133
|
private
|
|
72
134
|
|
|
135
|
+
def schedule_badge_sync(recipient_id)
|
|
136
|
+
BadgeSyncScheduler.schedule_after_commit(recipient_id)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def set_resolved(recipient_id:, notification_type_key:, host_event_identity:, resolved:)
|
|
140
|
+
recipient_id = require_recipient_id!(recipient_id)
|
|
141
|
+
raise ValidationError, "notification_type_key is required" if blank?(notification_type_key)
|
|
142
|
+
raise ValidationError, "host_event_identity is required" if blank?(host_event_identity)
|
|
143
|
+
|
|
144
|
+
communication = Messaging::Communication.find_by_idempotency_namespace(
|
|
145
|
+
user_id: recipient_id, notification_type_key:, host_event_identity:,
|
|
146
|
+
)
|
|
147
|
+
return nil if communication.nil?
|
|
148
|
+
|
|
149
|
+
result = ActiveRecord::Base.transaction do
|
|
150
|
+
flip_resolved(recipient_id:, communication_id: communication.id, resolved:)
|
|
151
|
+
end
|
|
152
|
+
schedule_badge_sync(recipient_id) if result&.changed
|
|
153
|
+
result
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def flip_resolved(recipient_id:, communication_id:, resolved:)
|
|
157
|
+
record = Scope.find_for_recipient_communication(recipient_id:, communication_id:, lock: true)
|
|
158
|
+
return nil if record.nil? || record.deleted?
|
|
159
|
+
|
|
160
|
+
changed = record.resolved? != resolved
|
|
161
|
+
record.update!(resolved_at: resolved ? Time.current : nil) if changed
|
|
162
|
+
|
|
163
|
+
ResolutionResult.new(
|
|
164
|
+
communication_id:, inbox_item_id: record.id, resolved: record.resolved?, changed:,
|
|
165
|
+
).freeze
|
|
166
|
+
end
|
|
167
|
+
|
|
73
168
|
def mutate(recipient_id:, inbox_item_id:, operation:)
|
|
74
169
|
recipient_id = require_recipient_id!(recipient_id)
|
|
75
170
|
inbox_item_id = require_inbox_item_id!(inbox_item_id)
|
|
@@ -89,10 +184,12 @@ module CommandTower
|
|
|
89
184
|
outcome = apply(operation, record)
|
|
90
185
|
record.reload
|
|
91
186
|
result = ItemResult.from_record(record)
|
|
187
|
+
audit_impersonated_changes(recipient_id:, action: operation, changed_ids: outcome == :changed ? [record.id] : [])
|
|
92
188
|
end
|
|
93
189
|
|
|
94
190
|
if outcome == :changed
|
|
95
|
-
OperationLogger.lifecycle_changed(operation:, item: result)
|
|
191
|
+
OperationLogger.after_commit { OperationLogger.lifecycle_changed(operation:, item: result) }
|
|
192
|
+
schedule_badge_sync(recipient_id)
|
|
96
193
|
end
|
|
97
194
|
|
|
98
195
|
result = result.with(newly_viewed: true) if operation == :mark_viewed && outcome == :changed
|
|
@@ -114,9 +211,11 @@ module CommandTower
|
|
|
114
211
|
ids << record.id
|
|
115
212
|
changed_ids << record.id if apply(operation, record) == :changed
|
|
116
213
|
end
|
|
214
|
+
audit_impersonated_changes(recipient_id:, action: :"bulk_#{operation}", changed_ids:)
|
|
117
215
|
end
|
|
118
216
|
|
|
119
217
|
log_bulk_lifecycle(recipient_id:, operation:, changed_ids:)
|
|
218
|
+
schedule_badge_sync(recipient_id) if changed_ids.any?
|
|
120
219
|
|
|
121
220
|
BulkResult.build(ids:, changed_count: changed_ids.size)
|
|
122
221
|
end
|
|
@@ -144,26 +243,33 @@ module CommandTower
|
|
|
144
243
|
requested_ids.map { |requested_id| records_by_id.fetch(requested_id.to_s) }
|
|
145
244
|
end
|
|
146
245
|
|
|
147
|
-
#
|
|
246
|
+
# Called inside the mutation transaction; emits at most one audit event per request.
|
|
247
|
+
def audit_impersonated_changes(recipient_id:, action:, changed_ids:, up_to: nil)
|
|
248
|
+
ImpersonationAudit.record_changes(recipient_id:, action:, changed_ids:, up_to:)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Logs after the outermost transaction commits, one lifecycle event per actually-changed item.
|
|
148
252
|
def log_bulk_lifecycle(recipient_id:, operation:, changed_ids:)
|
|
149
253
|
return if changed_ids.empty?
|
|
150
254
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
255
|
+
OperationLogger.after_commit do
|
|
256
|
+
records_by_id =
|
|
257
|
+
Scope
|
|
258
|
+
.for_recipient(recipient_id)
|
|
259
|
+
.includes(:communication)
|
|
260
|
+
.where(id: changed_ids)
|
|
261
|
+
.index_by(&:id)
|
|
262
|
+
|
|
263
|
+
changed_ids.each do |changed_id|
|
|
264
|
+
record = records_by_id[changed_id]
|
|
265
|
+
next if record.nil?
|
|
266
|
+
|
|
267
|
+
OperationLogger.lifecycle_changed(
|
|
268
|
+
operation:,
|
|
269
|
+
item: ItemResult.from_record(record),
|
|
270
|
+
bulk: true,
|
|
271
|
+
)
|
|
272
|
+
end
|
|
167
273
|
end
|
|
168
274
|
end
|
|
169
275
|
|
|
@@ -13,6 +13,17 @@ module CommandTower
|
|
|
13
13
|
}.freeze
|
|
14
14
|
|
|
15
15
|
class << self
|
|
16
|
+
# Runs the block once the outermost transaction has committed (immediately when none is
|
|
17
|
+
# open), so a rolled-back change never logs a lifecycle event. Logging is best-effort and
|
|
18
|
+
# must not fail a request whose change is already committed.
|
|
19
|
+
def after_commit
|
|
20
|
+
ActiveRecord.after_all_transactions_commit do
|
|
21
|
+
yield
|
|
22
|
+
rescue StandardError
|
|
23
|
+
nil
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
16
27
|
def lifecycle_changed(operation:, item:, bulk: false)
|
|
17
28
|
event = EVENT_BY_OPERATION.fetch(operation)
|
|
18
29
|
Contract::Observability::Publisher.info(
|
|
@@ -46,7 +46,7 @@ module CommandTower
|
|
|
46
46
|
scope:,
|
|
47
47
|
subject: latest.communication.conversation_title,
|
|
48
48
|
message_count: records.size,
|
|
49
|
-
unread_count: records.count
|
|
49
|
+
unread_count: records.count(&:unread_in_scope?),
|
|
50
50
|
inbox_item_ids: records.map(&:id),
|
|
51
51
|
message_ids: window ? window.fetch(:message_ids) : records.map(&:id),
|
|
52
52
|
window: window&.except(:message_ids),
|
|
@@ -77,26 +77,17 @@ module CommandTower
|
|
|
77
77
|
|
|
78
78
|
def unread_count(recipient_id:)
|
|
79
79
|
recipient_id = require_recipient_id!(recipient_id)
|
|
80
|
-
unread = Scope.for_recipient(recipient_id).merge(Messaging::InboxItem.
|
|
80
|
+
unread = Scope.for_recipient(recipient_id).merge(Messaging::InboxItem.counts_as_unread)
|
|
81
81
|
|
|
82
82
|
UnreadCountResult.build(
|
|
83
83
|
recipient_id:,
|
|
84
84
|
count: unread.count,
|
|
85
|
-
badge_count:
|
|
85
|
+
badge_count: unread.count,
|
|
86
86
|
)
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
private
|
|
90
90
|
|
|
91
|
-
# Unread rows whose declaration is badge_eligible. Unregistered keys are absent
|
|
92
|
-
# from that set. Channel preferences are not consulted.
|
|
93
|
-
def badge_eligible_count(unread)
|
|
94
|
-
keys = CommandTower::Messaging::NotificationTypes.enumerate.select(&:badge_eligible).map(&:key)
|
|
95
|
-
return 0 if keys.empty?
|
|
96
|
-
|
|
97
|
-
unread.where(messaging_communications: { notification_type_key: keys }).count
|
|
98
|
-
end
|
|
99
|
-
|
|
100
91
|
def require_recipient_id!(recipient_id)
|
|
101
92
|
raise ValidationError, "recipient_id is required" if blank?(recipient_id)
|
|
102
93
|
|
|
@@ -234,8 +225,10 @@ module CommandTower
|
|
|
234
225
|
preview: conversation ? latest.communication.title : nil,
|
|
235
226
|
activity_at: latest.created_at,
|
|
236
227
|
message_count: group.size,
|
|
237
|
-
unread_count: group.count
|
|
228
|
+
unread_count: group.count(&:unread_in_scope?),
|
|
238
229
|
inbox_item_ids: group.map(&:id),
|
|
230
|
+
notification_type_key: latest.communication.notification_type_key,
|
|
231
|
+
unread_inbox_item_ids: group.select(&:unread_in_scope?).map(&:id),
|
|
239
232
|
)
|
|
240
233
|
end
|
|
241
234
|
|
|
@@ -277,7 +270,7 @@ module CommandTower
|
|
|
277
270
|
end
|
|
278
271
|
|
|
279
272
|
def resolve_anchor(records, around)
|
|
280
|
-
return records.find
|
|
273
|
+
return records.find(&:unread_in_scope?) || records.last if around.to_s == OLDEST_UNREAD
|
|
281
274
|
|
|
282
275
|
find_member!(records, around)
|
|
283
276
|
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
# viewed_ids: every available requested id. newly_viewed: [{ id:, communication_id: }] for first views only.
|
|
7
|
+
# unavailable_ids: missing, foreign and deleted ids, lumped with no per-id reason.
|
|
8
|
+
RecordViewsResult = Data.define(:viewed_ids, :newly_viewed, :unavailable_ids)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
# Outcome of resolving or unresolving one inbox item. changed is true only for
|
|
7
|
+
# the call that flipped resolved_at.
|
|
8
|
+
ResolutionResult = Data.define(
|
|
9
|
+
:communication_id,
|
|
10
|
+
:inbox_item_id,
|
|
11
|
+
:resolved,
|
|
12
|
+
:changed
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -29,6 +29,14 @@ module CommandTower
|
|
|
29
29
|
Mutator.mark_viewed_for_communication(recipient_id:, communication_id:)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
def resolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
33
|
+
Mutator.resolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def unresolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
37
|
+
Mutator.unresolve_for_event(recipient_id:, notification_type_key:, host_event_identity:)
|
|
38
|
+
end
|
|
39
|
+
|
|
32
40
|
def mark_viewed(recipient_id:, inbox_item_id:)
|
|
33
41
|
Mutator.mark_viewed(recipient_id:, inbox_item_id:)
|
|
34
42
|
end
|
|
@@ -49,6 +57,14 @@ module CommandTower
|
|
|
49
57
|
Mutator.delete(recipient_id:, inbox_item_id:)
|
|
50
58
|
end
|
|
51
59
|
|
|
60
|
+
def record_views(recipient_id:, inbox_item_ids:)
|
|
61
|
+
Mutator.record_views(recipient_id:, inbox_item_ids:)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def mark_all_read(recipient_id:, up_to:)
|
|
65
|
+
Mutator.mark_all_read(recipient_id:, up_to:)
|
|
66
|
+
end
|
|
67
|
+
|
|
52
68
|
def bulk_mark_viewed(recipient_id:, inbox_item_ids:)
|
|
53
69
|
Mutator.bulk_mark_viewed(recipient_id:, inbox_item_ids:)
|
|
54
70
|
end
|
|
@@ -93,7 +93,8 @@ module CommandTower
|
|
|
93
93
|
# artifacts/visual-improvements/rich-messaging/authority/RICH_MESSAGING_RUBY_INBOX_DEFINITION_DISCOVERY.md.
|
|
94
94
|
inbox_document_composer: inbox_document_composer.nil? ? nil : inbox_document_composer.to_s,
|
|
95
95
|
# Notification Product Behavior (NPB-1). All default to today's behavior.
|
|
96
|
-
# `badge_eligible`:
|
|
96
|
+
# `badge_eligible`: DEPRECATED, accepted and ignored. Every push badge and the
|
|
97
|
+
# served `badgeCount` equal the single unread `count` for every type.
|
|
97
98
|
# `urgency` / `sound`: `{ default:, allowed: [ids] | "all" }`; nil is fixed
|
|
98
99
|
# "normal" / "default". Invalid input is kept so DeclarationValidator reports it.
|
|
99
100
|
badge_eligible: !!badge_eligible,
|
|
@@ -5,12 +5,12 @@ module CommandTower
|
|
|
5
5
|
module Rendering
|
|
6
6
|
# Resolves how a push should present itself, in provider-neutral terms: the member's
|
|
7
7
|
# effective urgency, the sound asset (nil for System Default), the conversation
|
|
8
|
-
# thread, and
|
|
9
|
-
#
|
|
10
|
-
# once, through Messaging::Inbox
|
|
8
|
+
# thread, and the recipient's current unread count as the badge (every registered
|
|
9
|
+
# type; `badge_eligible` is deprecated and ignored). Reads the member's stored
|
|
10
|
+
# preference once. Reads the unread count once, through Messaging::Inbox.
|
|
11
11
|
# The provider adapter maps the result to its own fields. Never raises for an
|
|
12
12
|
# unregistered type or an unreadable preference: it returns defaults and records
|
|
13
|
-
# a fallback diagnostic instead.
|
|
13
|
+
# a fallback diagnostic instead. A registered type keeps its badge on that
|
|
14
14
|
# preference fallback. Any other error, including a count failure, surfaces.
|
|
15
15
|
class PushPresentation
|
|
16
16
|
Result = Data.define(:urgency, :sound, :thread_id, :badge)
|
|
@@ -28,7 +28,7 @@ module CommandTower
|
|
|
28
28
|
|
|
29
29
|
def call
|
|
30
30
|
declaration = NotificationTypes.lookup(@communication.notification_type_key)
|
|
31
|
-
badge = badge_count
|
|
31
|
+
badge = badge_count
|
|
32
32
|
blocks = Preferences::EffectiveChoice.call(declaration:, stored: stored_state(declaration))
|
|
33
33
|
|
|
34
34
|
build(
|
|
@@ -59,11 +59,9 @@ module CommandTower
|
|
|
59
59
|
SoundCatalog.fetch(sound_id)&.fetch(:asset)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
def badge_count
|
|
65
|
-
return nil unless declaration.badge_eligible
|
|
66
|
-
|
|
62
|
+
# Every type carries the badge: the recipient's absolute unread count, including 0.
|
|
63
|
+
# `badge_eligible` is deprecated and ignored.
|
|
64
|
+
def badge_count
|
|
67
65
|
Inbox.unread_count(recipient_id: @communication.user_id).badge_count
|
|
68
66
|
end
|
|
69
67
|
|
|
@@ -15,7 +15,8 @@ module CommandTower
|
|
|
15
15
|
platform_enabled_channels:,
|
|
16
16
|
message_overrides: nil,
|
|
17
17
|
conversation_key: nil,
|
|
18
|
-
conversation_title: nil
|
|
18
|
+
conversation_title: nil,
|
|
19
|
+
collapse_key: nil
|
|
19
20
|
)
|
|
20
21
|
Accept::Coordinator.call(
|
|
21
22
|
recipient_id:,
|
|
@@ -29,6 +30,7 @@ module CommandTower
|
|
|
29
30
|
message_overrides:,
|
|
30
31
|
conversation_key:,
|
|
31
32
|
conversation_title:,
|
|
33
|
+
collapse_key:,
|
|
32
34
|
)
|
|
33
35
|
end
|
|
34
36
|
|
|
@@ -18,6 +18,7 @@ module CommandTower
|
|
|
18
18
|
validate :platform_enabled_channels, is_a: Array, required: true
|
|
19
19
|
validate :conversation_key, is_a: String, required: false
|
|
20
20
|
validate :conversation_title, is_a: String, required: false
|
|
21
|
+
validate :collapse_key, is_a: String, required: false
|
|
21
22
|
|
|
22
23
|
def call
|
|
23
24
|
recipient_result = CommandTower::Services::Messaging::Recipients.call(user: user)
|
|
@@ -60,6 +61,7 @@ module CommandTower
|
|
|
60
61
|
message_overrides: nil,
|
|
61
62
|
conversation_key:,
|
|
62
63
|
conversation_title:,
|
|
64
|
+
collapse_key:,
|
|
63
65
|
)
|
|
64
66
|
|
|
65
67
|
# Use communication_status — ServiceResult strips Interactor reserved :status.
|
|
@@ -130,7 +130,8 @@ module CommandTower
|
|
|
130
130
|
with_recipient do |recipient_id|
|
|
131
131
|
result = CommandTower::Messaging::Inbox.unread_count(recipient_id:)
|
|
132
132
|
context.count = result.count
|
|
133
|
-
|
|
133
|
+
# One number everywhere: the deprecated badgeCount field equals count.
|
|
134
|
+
context.badge_count = result.count
|
|
134
135
|
end
|
|
135
136
|
end
|
|
136
137
|
end
|
|
@@ -156,6 +157,33 @@ module CommandTower
|
|
|
156
157
|
end)
|
|
157
158
|
end
|
|
158
159
|
|
|
160
|
+
class RecordViews < CommandTower::Services::ApplicationService
|
|
161
|
+
include FailureMapping
|
|
162
|
+
validate :user, is_a: User, required: true
|
|
163
|
+
validate :inbox_item_ids, is_a: Array, required: true
|
|
164
|
+
|
|
165
|
+
def call
|
|
166
|
+
with_recipient do |recipient_id|
|
|
167
|
+
result = CommandTower::Messaging::Inbox.record_views(recipient_id:, inbox_item_ids:)
|
|
168
|
+
context.record_views = {
|
|
169
|
+
viewed_ids: result.viewed_ids, unavailable_ids: result.unavailable_ids,
|
|
170
|
+
changed_count: result.newly_viewed.size, newly_viewed: result.newly_viewed
|
|
171
|
+
}
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
class MarkAllRead < CommandTower::Services::ApplicationService
|
|
177
|
+
include FailureMapping
|
|
178
|
+
include BulkMutation
|
|
179
|
+
validate :user, is_a: User, required: true
|
|
180
|
+
validate :up_to, is_a: Integer, required: true
|
|
181
|
+
|
|
182
|
+
def call
|
|
183
|
+
super { |recipient_id| CommandTower::Messaging::Inbox.mark_all_read(recipient_id:, up_to:) }
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
159
187
|
class << self
|
|
160
188
|
def item(item)
|
|
161
189
|
{ id: item.id, title: item.title, status: item.status, viewed_at: item.viewed_at, created_at: item.created_at, updated_at: item.updated_at }
|
|
@@ -173,6 +201,8 @@ module CommandTower
|
|
|
173
201
|
unread_count: entry.unread_count,
|
|
174
202
|
read: entry.read,
|
|
175
203
|
inbox_item_ids: entry.inbox_item_ids,
|
|
204
|
+
notification_type_key: entry.notification_type_key,
|
|
205
|
+
unread_inbox_item_ids: entry.unread_inbox_item_ids,
|
|
176
206
|
}
|
|
177
207
|
end
|
|
178
208
|
|
|
@@ -17,7 +17,8 @@ module CommandTower
|
|
|
17
17
|
:conflict
|
|
18
18
|
when CommandTower::Errors::NotFoundError
|
|
19
19
|
:not_found
|
|
20
|
-
when CommandTower::Errors::Messaging::GalleryTestSendImpersonatingError
|
|
20
|
+
when CommandTower::Errors::Messaging::GalleryTestSendImpersonatingError,
|
|
21
|
+
CommandTower::Errors::Messaging::InboxImpersonationForbiddenError
|
|
21
22
|
:forbidden
|
|
22
23
|
when CommandTower::Errors::Messaging::GalleryTestSendRateLimitError
|
|
23
24
|
:too_many_requests
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module Messaging
|
|
6
|
+
module Inbox
|
|
7
|
+
# Job-entry orchestration: send a silent `{to, badge}` push so the app icon badge matches the
|
|
8
|
+
# recipient's unread count.
|
|
9
|
+
#
|
|
10
|
+
# Coalescing: BadgeSyncScheduler takes the pending marker (`SET NX EX`) at enqueue time and
|
|
11
|
+
# enqueues the job with a short wait, so a burst of changes yields one job. This job clears the
|
|
12
|
+
# marker first and only then reads the count, so it sees every change made before the clear. A
|
|
13
|
+
# change committed after the clear takes a new marker and sends again (trailing edge). Redis
|
|
14
|
+
# failures fail open: the badge is sent without coalescing.
|
|
15
|
+
#
|
|
16
|
+
# Best effort: send failures are logged by class only (never tokens, bodies or payloads) and
|
|
17
|
+
# never raised. Nothing is sent when the flag is off, while impersonating, or unless the Expo
|
|
18
|
+
# adapter is `http`.
|
|
19
|
+
class SyncAppIconBadgeWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
20
|
+
retry_strategy :none
|
|
21
|
+
|
|
22
|
+
PUSH_CHANNEL_KEY = "push"
|
|
23
|
+
|
|
24
|
+
def call(user_id:)
|
|
25
|
+
reason = skip_reason
|
|
26
|
+
return noop(reason) if reason
|
|
27
|
+
|
|
28
|
+
deliver(user_id)
|
|
29
|
+
rescue StandardError => e
|
|
30
|
+
log_failure(user_id:, error: e)
|
|
31
|
+
success(payload: { outcome: :failed }, http_status: :ok)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def skip_reason
|
|
37
|
+
return :disabled unless CommandTower.config.messaging.inbox.badge_sync_push == true
|
|
38
|
+
return :impersonating if CommandTower::Messaging::Inbox::ImpersonationAudit.impersonating?
|
|
39
|
+
|
|
40
|
+
:adapter_not_http unless expo_configuration.adapter_name == "http"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Marker is cleared before the count is read so a later change is never lost.
|
|
44
|
+
def deliver(user_id)
|
|
45
|
+
clear_pending_marker(user_id)
|
|
46
|
+
tokens = push_tokens(user_id)
|
|
47
|
+
return noop(:no_devices) if tokens.empty?
|
|
48
|
+
|
|
49
|
+
badge = CommandTower::Messaging::Inbox.unread_count(recipient_id: user_id).badge_count
|
|
50
|
+
send_badge(tokens:, badge:)
|
|
51
|
+
success(payload: { outcome: :sent, devices: tokens.size, badge: }, http_status: :ok)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def noop(reason)
|
|
55
|
+
success(payload: { outcome: :noop, reason: }, http_status: :ok)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def expo_configuration
|
|
59
|
+
CommandTower::Messaging::Execution::Adapters::Expo::Configuration.new
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def marker_key(user_id)
|
|
63
|
+
CommandTower::Messaging::Inbox::BadgeSyncScheduler.marker_key(user_id)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def clear_pending_marker(user_id)
|
|
67
|
+
CommandTower::RedisConnection.with { |redis| redis.del(marker_key(user_id)) }
|
|
68
|
+
rescue Redis::BaseError, Timeout::Error
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def push_tokens(user_id)
|
|
73
|
+
views = CommandTower::Messaging::Endpoints.list(owner_user_id: user_id, channel_key: PUSH_CHANNEL_KEY)
|
|
74
|
+
views.filter_map do |view|
|
|
75
|
+
next unless view.lifecycle_state == "active" && view.verification_state == "verified"
|
|
76
|
+
|
|
77
|
+
CommandTower::Messaging::Endpoints::SecretReader.read!(owner_user_id: user_id, endpoint_id: view.id)
|
|
78
|
+
rescue CommandTower::Messaging::Endpoints::Error
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def send_badge(tokens:, badge:)
|
|
84
|
+
client = CommandTower::Messaging::Execution::Adapters::Expo::HttpClient.new(
|
|
85
|
+
configuration: expo_configuration
|
|
86
|
+
)
|
|
87
|
+
client.send_messages(tokens.map { |token| { "to" => token, "badge" => badge } })
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def log_failure(user_id:, error:)
|
|
91
|
+
Rails.logger.warn(
|
|
92
|
+
"[CommandTower::Messaging] badge sync failed user_id=#{user_id} reason=#{error.class.name}"
|
|
93
|
+
)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|