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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 11db3a037def5e1d1e3ac6a6001ba985450be322f4389037f6d37a3fad174989
|
|
4
|
+
data.tar.gz: 4656ae382e062ca352ce1b35ba7396e716b194eefe74da7db8b442af5265df65
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ed392262822af31f07fac4dccaf791022b6d9631925474c0ee9aeaec48e6f89ce07589ca86f616a4e519047d8c79c8fb517f3e94a3399a8fe89c67d3bbfec86f
|
|
7
|
+
data.tar.gz: a84a371f2252025c2142518dd4c1a83c26a768ff068e516b2e1e99e9ce4908bf66e6aaa4849472bf7e8f1b0caec29c1ccbf4580c5f163fd1c471f1b9a9728fed
|
|
@@ -2,14 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
module CommandTower
|
|
4
4
|
module Me
|
|
5
|
-
class InboxController < CommandTower::ApplicationController
|
|
5
|
+
class InboxController < CommandTower::ApplicationController # rubocop:disable Metrics/ClassLength
|
|
6
6
|
include CommandTower::Auth::AuthenticationBoundary
|
|
7
7
|
include CommandTower::Auth::AuthorizationBoundary
|
|
8
8
|
|
|
9
9
|
before_action :authenticate_request!
|
|
10
10
|
before_action :authorize_request!
|
|
11
|
+
|
|
12
|
+
# What each action does while impersonating. Every routed action needs an entry; a missing one
|
|
13
|
+
# is refused.
|
|
14
|
+
# passive_skip - reads and view tracking: the action itself redacts or skips.
|
|
15
|
+
# allow_audited - state changes the operator may make; the Mutator writes one audit event inside its transaction.
|
|
16
|
+
# refuse - 403 `inbox_impersonation_forbidden`.
|
|
17
|
+
IMPERSONATION_POLICY = {
|
|
18
|
+
index: :passive_skip,
|
|
19
|
+
show: :passive_skip,
|
|
20
|
+
conversation: :passive_skip,
|
|
21
|
+
unread_count: :passive_skip,
|
|
22
|
+
open: :passive_skip,
|
|
23
|
+
views: :passive_skip,
|
|
24
|
+
archive: :allow_audited,
|
|
25
|
+
mark_all_read: :allow_audited,
|
|
26
|
+
bulk_read: :allow_audited,
|
|
27
|
+
bulk_unread: :allow_audited,
|
|
28
|
+
bulk_archive: :allow_audited,
|
|
29
|
+
bulk_restore: :allow_audited,
|
|
30
|
+
destroy: :refuse,
|
|
31
|
+
bulk_delete: :refuse
|
|
32
|
+
}.freeze
|
|
33
|
+
|
|
34
|
+
before_action :enforce_impersonation_policy!
|
|
11
35
|
# Inbox reads can carry per-recipient, impersonation-redacted content: never cache them.
|
|
12
|
-
before_action :set_no_store, only: %i[index show open conversation]
|
|
36
|
+
before_action :set_no_store, only: %i[index show open conversation unread_count]
|
|
13
37
|
|
|
14
38
|
def index
|
|
15
39
|
deserialized = CommandTower::Deserializers::Messaging::Inbox::ListDeserializer.call(params)
|
|
@@ -57,6 +81,31 @@ module CommandTower
|
|
|
57
81
|
render_application_result(CommandTower::Workflows::Messaging::Inbox::UnreadCountWorkflow.call(user: current_user))
|
|
58
82
|
end
|
|
59
83
|
|
|
84
|
+
def views
|
|
85
|
+
# Impersonation is decided before the body is validated.
|
|
86
|
+
if CommandTower::Current.impersonation_active == true
|
|
87
|
+
return render_application_result(CommandTower::Workflows::Messaging::Inbox::RecordViewsWorkflow.call(
|
|
88
|
+
user: current_user, inbox_item_ids: [], impersonated: true
|
|
89
|
+
))
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
deserialized = CommandTower::Deserializers::Messaging::Inbox::BulkIdsDeserializer.call(params)
|
|
93
|
+
return render_deserializer_errors unless deserialized.success?
|
|
94
|
+
|
|
95
|
+
render_application_result(CommandTower::Workflows::Messaging::Inbox::RecordViewsWorkflow.call(
|
|
96
|
+
user: current_user, inbox_item_ids: deserialized.input.inbox_item_ids
|
|
97
|
+
))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def mark_all_read
|
|
101
|
+
deserialized = CommandTower::Deserializers::Messaging::Inbox::MarkAllReadDeserializer.call(params)
|
|
102
|
+
return render_deserializer_errors unless deserialized.success?
|
|
103
|
+
|
|
104
|
+
render_application_result(CommandTower::Workflows::Messaging::Inbox::MarkAllReadWorkflow.call(
|
|
105
|
+
user: current_user, up_to: deserialized.input.up_to
|
|
106
|
+
))
|
|
107
|
+
end
|
|
108
|
+
|
|
60
109
|
def bulk_read
|
|
61
110
|
run_bulk_workflow(CommandTower::Workflows::Messaging::Inbox::BulkReadWorkflow)
|
|
62
111
|
end
|
|
@@ -86,6 +135,16 @@ module CommandTower
|
|
|
86
135
|
render_application_result(workflow.call(user: current_user, inbox_item_id: deserialized.input.inbox_item_id, **extra))
|
|
87
136
|
end
|
|
88
137
|
|
|
138
|
+
def enforce_impersonation_policy!
|
|
139
|
+
return unless impersonating_flag
|
|
140
|
+
return unless IMPERSONATION_POLICY.fetch(action_name.to_sym, :refuse) == :refuse
|
|
141
|
+
|
|
142
|
+
render_application_result(CommandTower::Workflows::WorkflowResult.failure(
|
|
143
|
+
errors: [CommandTower::Errors::Messaging::InboxImpersonationForbiddenError.new],
|
|
144
|
+
http_status: :forbidden
|
|
145
|
+
))
|
|
146
|
+
end
|
|
147
|
+
|
|
89
148
|
def set_no_store
|
|
90
149
|
response.headers["Cache-Control"] = "no-store"
|
|
91
150
|
end
|
|
@@ -156,6 +156,18 @@ module CommandTower
|
|
|
156
156
|
success(Input.new(inbox_item_ids: values.uniq))
|
|
157
157
|
end
|
|
158
158
|
end
|
|
159
|
+
|
|
160
|
+
# `upTo` is an opaque watermark: the client sends the largest id it loaded.
|
|
161
|
+
class MarkAllReadDeserializer < ShowDeserializer
|
|
162
|
+
Input = Data.define(:up_to)
|
|
163
|
+
|
|
164
|
+
def call(params)
|
|
165
|
+
value = positive_integer(params[:upTo] || params[:up_to])
|
|
166
|
+
return failure(errors: { message: "invalid_up_to" }) unless value
|
|
167
|
+
|
|
168
|
+
success(Input.new(up_to: value))
|
|
169
|
+
end
|
|
170
|
+
end
|
|
159
171
|
end
|
|
160
172
|
end
|
|
161
173
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Errors
|
|
5
|
+
module Messaging
|
|
6
|
+
# Destructive inbox actions are refused while impersonating: the operator must not delete
|
|
7
|
+
# the impersonated user's messages.
|
|
8
|
+
class InboxImpersonationForbiddenError < CommandTower::Errors::ApplicationError
|
|
9
|
+
def code
|
|
10
|
+
"inbox_impersonation_forbidden"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def message
|
|
14
|
+
"This inbox action is unavailable while impersonating a user."
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def log_level
|
|
18
|
+
:warn
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
# Sets the recipient's app icon badge with a silent, badge-only push. Takes the user id only;
|
|
7
|
+
# the count and device tokens are read when the job runs.
|
|
8
|
+
class BadgeSyncJob < CommandTower::ApplicationJob
|
|
9
|
+
queue_as :messaging_handoff
|
|
10
|
+
|
|
11
|
+
def perform(user_id)
|
|
12
|
+
CommandTower::Workflows::Messaging::Inbox::SyncAppIconBadgeWorkflow.call_from_job(
|
|
13
|
+
job: self,
|
|
14
|
+
user_id:
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -46,8 +46,13 @@ module CommandTower
|
|
|
46
46
|
|
|
47
47
|
CONVERSATION_KEY_FORMAT = /\A[A-Za-z0-9][A-Za-z0-9._:-]{0,127}\z/
|
|
48
48
|
|
|
49
|
+
# Push collapse identity (Expo collapseId). Its own pattern and 64-char limit; it is never
|
|
50
|
+
# derived from conversation_key, which is a thread identity with a different lifetime.
|
|
51
|
+
COLLAPSE_KEY_FORMAT = /\A[A-Za-z0-9][A-Za-z0-9._:-]{0,63}\z/
|
|
52
|
+
|
|
49
53
|
validates :notification_type_key, :title, :body, presence: true
|
|
50
54
|
validates :execution_handoff_status, presence: true
|
|
55
|
+
validates :collapse_key, format: { with: COLLAPSE_KEY_FORMAT }, allow_nil: true
|
|
51
56
|
validate :conversation_identity_is_a_pair
|
|
52
57
|
|
|
53
58
|
before_validation :normalize_conversation_identity
|
|
@@ -84,6 +89,7 @@ module CommandTower
|
|
|
84
89
|
def normalize_conversation_identity
|
|
85
90
|
self.conversation_key = nil if conversation_key.blank?
|
|
86
91
|
self.conversation_title = nil if conversation_title.blank?
|
|
92
|
+
self.collapse_key = nil if collapse_key.blank?
|
|
87
93
|
end
|
|
88
94
|
|
|
89
95
|
def conversation_identity_is_a_pair
|
|
@@ -19,9 +19,18 @@ module CommandTower
|
|
|
19
19
|
scope :not_deleted, -> { where(deleted_at: nil) }
|
|
20
20
|
scope :not_archived, -> { where(archived_at: nil) }
|
|
21
21
|
scope :archived, -> { where.not(archived_at: nil) }
|
|
22
|
-
scope :
|
|
23
|
-
scope :
|
|
24
|
-
scope :
|
|
22
|
+
scope :unresolved, -> { where(resolved_at: nil) }
|
|
23
|
+
scope :resolved, -> { where.not(resolved_at: nil) }
|
|
24
|
+
scope :default_list, -> { not_deleted.not_archived.unresolved }
|
|
25
|
+
scope :archived_list, -> { not_deleted.archived.unresolved }
|
|
26
|
+
# Global rule (unread-count, push badge, mark-all-read): not deleted, archived,
|
|
27
|
+
# resolved or viewed.
|
|
28
|
+
scope :counts_as_unread, -> { default_list.where(viewed_at: nil) }
|
|
29
|
+
# Alias kept for existing callers.
|
|
30
|
+
scope :unread, -> { counts_as_unread }
|
|
31
|
+
# Per-entry rule used by the Reader in both Inbox and Archived scopes: archived rows
|
|
32
|
+
# keep reporting their own unread members. Resolved rows never count.
|
|
33
|
+
scope :unread_in_scope, -> { not_deleted.unresolved.where(viewed_at: nil) }
|
|
25
34
|
|
|
26
35
|
def viewed?
|
|
27
36
|
viewed_at.present?
|
|
@@ -41,8 +50,19 @@ module CommandTower
|
|
|
41
50
|
!deleted?
|
|
42
51
|
end
|
|
43
52
|
|
|
44
|
-
def
|
|
45
|
-
|
|
53
|
+
def resolved?
|
|
54
|
+
resolved_at.present?
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Instance form of the `counts_as_unread` scope.
|
|
58
|
+
def counts_as_unread?
|
|
59
|
+
!viewed? && !archived? && !deleted? && !resolved?
|
|
60
|
+
end
|
|
61
|
+
alias unread? counts_as_unread?
|
|
62
|
+
|
|
63
|
+
# Instance form of the `unread_in_scope` scope.
|
|
64
|
+
def unread_in_scope?
|
|
65
|
+
!viewed? && !deleted? && !resolved?
|
|
46
66
|
end
|
|
47
67
|
|
|
48
68
|
# Denormalized label helper — timestamps remain authoritative for filters.
|
|
@@ -17,6 +17,8 @@ module CommandTower
|
|
|
17
17
|
unreadCount: entry.fetch(:unread_count),
|
|
18
18
|
read: entry.fetch(:read),
|
|
19
19
|
inboxItemIds: entry.fetch(:inbox_item_ids),
|
|
20
|
+
notificationTypeKey: entry[:notification_type_key],
|
|
21
|
+
unreadInboxItemIds: entry.fetch(:unread_inbox_item_ids, []),
|
|
20
22
|
}
|
|
21
23
|
end
|
|
22
24
|
end
|
|
@@ -71,12 +73,28 @@ module CommandTower
|
|
|
71
73
|
end
|
|
72
74
|
end
|
|
73
75
|
|
|
76
|
+
class RecordViewsSerializer
|
|
77
|
+
def self.serialize(result)
|
|
78
|
+
payload = { viewedIds: result.fetch(:viewed_ids), changedCount: result.fetch(:changed_count),
|
|
79
|
+
unavailableIds: result.fetch(:unavailable_ids) }
|
|
80
|
+
payload[:skipped] = result[:skipped] if result[:skipped]
|
|
81
|
+
payload
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
74
85
|
class UnreadCountSerializer
|
|
75
86
|
def self.serialize(payload)
|
|
76
87
|
{ count: payload.fetch(:count), badgeCount: payload.fetch(:badge_count) }
|
|
77
88
|
end
|
|
78
89
|
end
|
|
79
90
|
|
|
91
|
+
# `meta.unreadCount` on every inbox mutation: the same one number as GET /me/inbox/unread-count.
|
|
92
|
+
class UnreadCountMetaSerializer
|
|
93
|
+
def self.serialize(payload)
|
|
94
|
+
{ unreadCount: UnreadCountSerializer.serialize(payload) }
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
80
98
|
class PaginationMetaSerializer
|
|
81
99
|
def self.serialize(pagination)
|
|
82
100
|
{ limit: pagination.fetch(:limit), offset: pagination.fetch(:offset),
|
|
@@ -21,7 +21,8 @@ module CommandTower
|
|
|
21
21
|
platform_enabled_channels:,
|
|
22
22
|
message_overrides: nil,
|
|
23
23
|
conversation_key: nil,
|
|
24
|
-
conversation_title: nil
|
|
24
|
+
conversation_title: nil,
|
|
25
|
+
collapse_key: nil
|
|
25
26
|
)
|
|
26
27
|
@recipient_id = recipient_id
|
|
27
28
|
@notification_type_key = notification_type_key
|
|
@@ -34,6 +35,7 @@ module CommandTower
|
|
|
34
35
|
@message_overrides = message_overrides
|
|
35
36
|
@conversation_key = conversation_key
|
|
36
37
|
@conversation_title = conversation_title
|
|
38
|
+
@collapse_key = collapse_key
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
def call
|
|
@@ -49,6 +51,7 @@ module CommandTower
|
|
|
49
51
|
message_overrides: @message_overrides,
|
|
50
52
|
conversation_key: @conversation_key,
|
|
51
53
|
conversation_title: @conversation_title,
|
|
54
|
+
collapse_key: @collapse_key,
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
OperationLogger.around(request:) do
|
|
@@ -209,7 +212,7 @@ module CommandTower
|
|
|
209
212
|
|
|
210
213
|
def conversation_pair_invalid?(error)
|
|
211
214
|
names = error.record.errors.attribute_names.map(&:to_sym)
|
|
212
|
-
names.any? && (names - %i[conversation_key conversation_title]).empty?
|
|
215
|
+
names.any? && (names - %i[conversation_key conversation_title collapse_key]).empty?
|
|
213
216
|
end
|
|
214
217
|
|
|
215
218
|
def resolve_record_not_unique!(error, fingerprint)
|
|
@@ -33,6 +33,7 @@ module CommandTower
|
|
|
33
33
|
metadata_version: @request[:metadata_version],
|
|
34
34
|
conversation_key: @request[:conversation_key],
|
|
35
35
|
conversation_title: @request[:conversation_title],
|
|
36
|
+
collapse_key: @request[:collapse_key],
|
|
36
37
|
)
|
|
37
38
|
assign_presentation_snapshot!(communication)
|
|
38
39
|
|
|
@@ -57,11 +58,25 @@ module CommandTower
|
|
|
57
58
|
end
|
|
58
59
|
end
|
|
59
60
|
|
|
61
|
+
schedule_thread_restore(communication)
|
|
60
62
|
reload_aggregate(communication.id)
|
|
61
63
|
end
|
|
62
64
|
|
|
63
65
|
private
|
|
64
66
|
|
|
67
|
+
# D7. Persister runs only for genuinely new items (replays return before it). The restore
|
|
68
|
+
# owns its transaction, so it runs after the outermost commit, where a deadlock can be
|
|
69
|
+
# retried in place.
|
|
70
|
+
def schedule_thread_restore(communication)
|
|
71
|
+
return unless CommandTower.config.messaging.inbox.restore_archived_thread_on_new_message
|
|
72
|
+
return if communication.conversation_key.blank?
|
|
73
|
+
|
|
74
|
+
inbox_item_id = communication.inbox_item&.id
|
|
75
|
+
return if inbox_item_id.nil?
|
|
76
|
+
|
|
77
|
+
ActiveRecord.after_all_transactions_commit { ThreadRestorer.call(inbox_item_id) }
|
|
78
|
+
end
|
|
79
|
+
|
|
65
80
|
def assign_presentation_snapshot!(communication)
|
|
66
81
|
snapshot = CommandTower::Messaging::Rendering::InboxPresentationSnapshot.capture(
|
|
67
82
|
communication:,
|
|
@@ -45,6 +45,7 @@ module CommandTower
|
|
|
45
45
|
|
|
46
46
|
# Defined types (a non-nil metadata_version) derive title and body from the definition, so
|
|
47
47
|
# they are not part of the basis; the version is, so a changed definition conflicts.
|
|
48
|
+
# conversation_key, conversation_title and collapse_key are delivery hints and stay out of it.
|
|
48
49
|
def canonical_payload
|
|
49
50
|
shared = {
|
|
50
51
|
"metadata" => normalize_value(@metadata),
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Accept
|
|
6
|
+
# D7. After a new inbox item commits, restores the recipient's archived, non-deleted,
|
|
7
|
+
# non-resolved members of the same conversation. Owns its transaction (so it runs after
|
|
8
|
+
# the outermost commit and can retry a deadlock in place). Locks members in id order.
|
|
9
|
+
# A lost restore leaves a partial thread, which the reader already tolerates.
|
|
10
|
+
class ThreadRestorer
|
|
11
|
+
DEADLOCK_RETRIES = 3
|
|
12
|
+
|
|
13
|
+
def self.call(inbox_item_id)
|
|
14
|
+
new(inbox_item_id).call
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(inbox_item_id)
|
|
18
|
+
@inbox_item_id = inbox_item_id
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def call
|
|
22
|
+
attempts = 0
|
|
23
|
+
begin
|
|
24
|
+
attempts += 1
|
|
25
|
+
restore_members
|
|
26
|
+
rescue ActiveRecord::Deadlocked
|
|
27
|
+
retry if attempts <= DEADLOCK_RETRIES
|
|
28
|
+
|
|
29
|
+
log_failure("deadlock")
|
|
30
|
+
end
|
|
31
|
+
rescue StandardError => e
|
|
32
|
+
log_failure(e.class.name)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def restore_members
|
|
36
|
+
ActiveRecord::Base.transaction do
|
|
37
|
+
ids = lock_member_ids
|
|
38
|
+
next if ids.empty?
|
|
39
|
+
|
|
40
|
+
apply_restore(ids)
|
|
41
|
+
Messaging::Inbox::BadgeSyncScheduler.schedule_after_commit(item.communication.user_id)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def item
|
|
48
|
+
return @item if defined?(@item)
|
|
49
|
+
|
|
50
|
+
@item = Messaging::InboxItem.includes(:communication).find_by(id: @inbox_item_id)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def conversation_communication_ids
|
|
54
|
+
return Messaging::Communication.none if item.nil?
|
|
55
|
+
|
|
56
|
+
Messaging::Communication.where(
|
|
57
|
+
user_id: item.communication.user_id,
|
|
58
|
+
conversation_key: item.communication.conversation_key
|
|
59
|
+
).select(:id)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def lock_member_ids
|
|
63
|
+
Messaging::InboxItem
|
|
64
|
+
.where(communication_id: conversation_communication_ids)
|
|
65
|
+
.where.not(id: @inbox_item_id)
|
|
66
|
+
.archived.not_deleted.unresolved
|
|
67
|
+
.order(:id)
|
|
68
|
+
.lock
|
|
69
|
+
.pluck(:id)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Same result as the Mutator's apply_restore, set-based. update_all is deliberate: the
|
|
73
|
+
# rows are already locked and there are no validations or callbacks on this model.
|
|
74
|
+
def apply_restore(ids)
|
|
75
|
+
Messaging::InboxItem.where(id: ids).update_all( # rubocop:disable Rails/SkipsModelValidations
|
|
76
|
+
[
|
|
77
|
+
"archived_at = NULL, updated_at = ?, status = CASE WHEN viewed_at IS NULL THEN ? ELSE ? END",
|
|
78
|
+
Time.current,
|
|
79
|
+
Messaging::InboxItem::STATUS_CREATED,
|
|
80
|
+
Messaging::InboxItem::STATUS_VIEWED
|
|
81
|
+
]
|
|
82
|
+
)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def log_failure(reason)
|
|
86
|
+
Rails.logger.warn(
|
|
87
|
+
"[CommandTower::Messaging] thread restore failed inbox_item_id=#{@inbox_item_id} reason=#{reason}"
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -31,6 +31,23 @@ module CommandTower
|
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# Per recipient, idempotent, never touches viewed_at, emits no audit (the
|
|
35
|
+
# calling host owns it). Returns Results::InboxResolution, or nil when there
|
|
36
|
+
# is no live inbox item for the recipient and event. Badge sync scheduling
|
|
37
|
+
# belongs to the scheduler, which is a no-op while impersonating.
|
|
38
|
+
def resolve_for_event(request)
|
|
39
|
+
guarded("inbox.resolve_for_event", request, Requests::ResolveForEvent) do
|
|
40
|
+
resolution(CommandTower::Messaging::Inbox.resolve_for_event(**event_args(request)))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Inverse of resolve_for_event; same guarantees and nil rule.
|
|
45
|
+
def unresolve_for_event(request)
|
|
46
|
+
guarded("inbox.unresolve_for_event", request, Requests::ResolveForEvent) do
|
|
47
|
+
resolution(CommandTower::Messaging::Inbox.unresolve_for_event(**event_args(request)))
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
34
51
|
# Read-only, no audit. Returns Results::InboxDocument carrying the catalog 2
|
|
35
52
|
# UI::Document, or nil when the communication is missing, not the recipient's,
|
|
36
53
|
# deleted or archived, not inbox-backed, or its type has no MessageDefinition.
|
|
@@ -77,6 +94,25 @@ module CommandTower
|
|
|
77
94
|
).freeze
|
|
78
95
|
end
|
|
79
96
|
|
|
97
|
+
def event_args(request)
|
|
98
|
+
{
|
|
99
|
+
recipient_id: request.recipient_id,
|
|
100
|
+
notification_type_key: request.notification_type_key,
|
|
101
|
+
host_event_identity: request.host_event_identity
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def resolution(result)
|
|
106
|
+
return nil if result.nil?
|
|
107
|
+
|
|
108
|
+
Results::InboxResolution.new(
|
|
109
|
+
communication_id: result.communication_id,
|
|
110
|
+
inbox_item_id: result.inbox_item_id,
|
|
111
|
+
resolved: result.resolved,
|
|
112
|
+
changed: result.changed
|
|
113
|
+
).freeze
|
|
114
|
+
end
|
|
115
|
+
|
|
80
116
|
def ensure_request!(request, expected_class)
|
|
81
117
|
return if request.is_a?(expected_class)
|
|
82
118
|
|
|
@@ -84,9 +120,22 @@ module CommandTower
|
|
|
84
120
|
end
|
|
85
121
|
|
|
86
122
|
def validate!(request)
|
|
123
|
+
if request.is_a?(Requests::ResolveForEvent)
|
|
124
|
+
validate_event!(request)
|
|
125
|
+
return
|
|
126
|
+
end
|
|
127
|
+
|
|
87
128
|
raise Contract::ValidationError, "communication_id is required" if request.communication_id.nil?
|
|
88
129
|
raise Contract::ValidationError, "recipient_id is required" if request.recipient_id.nil?
|
|
89
130
|
end
|
|
131
|
+
|
|
132
|
+
def validate_event!(request)
|
|
133
|
+
raise Contract::ValidationError, "recipient_id is required" if request.recipient_id.nil?
|
|
134
|
+
|
|
135
|
+
%i[notification_type_key host_event_identity].each do |field|
|
|
136
|
+
raise Contract::ValidationError, "#{field} is required" if request.public_send(field).to_s.strip.empty?
|
|
137
|
+
end
|
|
138
|
+
end
|
|
90
139
|
end
|
|
91
140
|
end
|
|
92
141
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Contract
|
|
6
|
+
module Requests
|
|
7
|
+
# Per-recipient resolve/unresolve, addressed by the communication's idempotency
|
|
8
|
+
# namespace (recipient, notification type, host event identity).
|
|
9
|
+
ResolveForEvent = Data.define(
|
|
10
|
+
:recipient_id,
|
|
11
|
+
:notification_type_key,
|
|
12
|
+
:host_event_identity
|
|
13
|
+
) do
|
|
14
|
+
def self.build(recipient_id:, notification_type_key:, host_event_identity:)
|
|
15
|
+
new(
|
|
16
|
+
recipient_id:,
|
|
17
|
+
notification_type_key:,
|
|
18
|
+
host_event_identity:
|
|
19
|
+
).freeze
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Contract
|
|
6
|
+
module Results
|
|
7
|
+
# Outcome of resolving or unresolving a communication's inbox item. changed is
|
|
8
|
+
# true only for the call that flipped resolved_at.
|
|
9
|
+
InboxResolution = Data.define(
|
|
10
|
+
:communication_id,
|
|
11
|
+
:inbox_item_id,
|
|
12
|
+
:resolved,
|
|
13
|
+
:changed
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -181,6 +181,9 @@ module CommandTower
|
|
|
181
181
|
)
|
|
182
182
|
message["interruptionLevel"] = interruption_level if interruption_level
|
|
183
183
|
message["threadId"] = rendered.thread_id if rendered.thread_id
|
|
184
|
+
# Only the producer-supplied collapse_key. Never conversation_key (D10).
|
|
185
|
+
collapse_key = communication&.collapse_key
|
|
186
|
+
message["collapseId"] = collapse_key if collapse_key.present?
|
|
184
187
|
# nil omits the field. 0 is a real count and must be sent. In Ruby, 0 is truthy.
|
|
185
188
|
message["badge"] = rendered.badge unless rendered.badge.nil?
|
|
186
189
|
data = {}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Inbox
|
|
6
|
+
# Schedules a badge-only push after an inbox change commits. A no-op unless
|
|
7
|
+
# messaging.inbox.badge_sync_push is on, and never while an operator is impersonating
|
|
8
|
+
# (the impersonator must not move the real user's app icon badge). Enqueue failures are
|
|
9
|
+
# swallowed: the badge is best-effort and must never fail the inbox mutation.
|
|
10
|
+
class BadgeSyncScheduler
|
|
11
|
+
PENDING_TTL_SECONDS = 60
|
|
12
|
+
ENQUEUE_WAIT = 5.seconds
|
|
13
|
+
|
|
14
|
+
def self.marker_key(recipient_id)
|
|
15
|
+
"command_tower:messaging:inbox:badge_sync:pending:#{recipient_id}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.schedule_after_commit(recipient_id)
|
|
19
|
+
new(recipient_id).schedule_after_commit
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.enqueue(recipient_id)
|
|
23
|
+
new(recipient_id).enqueue
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(recipient_id)
|
|
27
|
+
@recipient_id = recipient_id
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def schedule_after_commit
|
|
31
|
+
return unless enabled?
|
|
32
|
+
return if ImpersonationAudit.impersonating?
|
|
33
|
+
|
|
34
|
+
ActiveRecord.after_all_transactions_commit { enqueue }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def enqueue
|
|
38
|
+
return nil unless claim_pending_marker
|
|
39
|
+
|
|
40
|
+
Messaging::Inbox::BadgeSyncJob.set(wait: ENQUEUE_WAIT).perform_later(@recipient_id)
|
|
41
|
+
rescue StandardError => e
|
|
42
|
+
release_pending_marker
|
|
43
|
+
Rails.logger.warn(
|
|
44
|
+
"[CommandTower::Messaging] badge sync enqueue failed user_id=#{@recipient_id} reason=#{e.class.name}"
|
|
45
|
+
)
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def enabled?
|
|
52
|
+
CommandTower.config.messaging.inbox.badge_sync_push == true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# True when this call owns the enqueue; false when a job is already pending for the recipient
|
|
56
|
+
# (it clears the marker before reading the count, so it will see this change). Fails open
|
|
57
|
+
# (true) when Redis is unavailable.
|
|
58
|
+
def claim_pending_marker
|
|
59
|
+
CommandTower::RedisConnection.with do |redis|
|
|
60
|
+
redis.set(self.class.marker_key(@recipient_id), "1", nx: true, ex: PENDING_TTL_SECONDS) ? true : false
|
|
61
|
+
end
|
|
62
|
+
rescue Redis::BaseError, Timeout::Error
|
|
63
|
+
true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def release_pending_marker
|
|
67
|
+
CommandTower::RedisConnection.with { |redis| redis.del(self.class.marker_key(@recipient_id)) }
|
|
68
|
+
rescue Redis::BaseError, Timeout::Error
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -14,8 +14,11 @@ module CommandTower
|
|
|
14
14
|
:unread_count,
|
|
15
15
|
:read,
|
|
16
16
|
:inbox_item_ids,
|
|
17
|
+
:notification_type_key,
|
|
18
|
+
:unread_inbox_item_ids,
|
|
17
19
|
) do
|
|
18
|
-
def self.build(kind:, id:, title:, preview:, activity_at:, message_count:, unread_count:, inbox_item_ids
|
|
20
|
+
def self.build(kind:, id:, title:, preview:, activity_at:, message_count:, unread_count:, inbox_item_ids:,
|
|
21
|
+
notification_type_key: nil, unread_inbox_item_ids: [])
|
|
19
22
|
kind = kind.to_s
|
|
20
23
|
new(
|
|
21
24
|
kind:,
|
|
@@ -27,9 +30,15 @@ module CommandTower
|
|
|
27
30
|
message_count: Integer(message_count),
|
|
28
31
|
unread_count: Integer(unread_count),
|
|
29
32
|
read: Integer(unread_count).zero?,
|
|
30
|
-
inbox_item_ids:
|
|
33
|
+
inbox_item_ids: integer_ids(inbox_item_ids),
|
|
34
|
+
notification_type_key: notification_type_key&.to_s,
|
|
35
|
+
unread_inbox_item_ids: integer_ids(unread_inbox_item_ids),
|
|
31
36
|
).freeze
|
|
32
37
|
end
|
|
38
|
+
|
|
39
|
+
def self.integer_ids(values)
|
|
40
|
+
Array(values).map { |value| Integer(value) }.freeze
|
|
41
|
+
end
|
|
33
42
|
end
|
|
34
43
|
end
|
|
35
44
|
end
|