command_tower 0.23.0 → 0.24.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/admin/home_notices_controller.rb +39 -0
- data/app/controllers/command_tower/me/home_notices_controller.rb +79 -0
- data/app/deserializers/command_tower/deserializers/admin/home_notices/control_deserializer.rb +40 -0
- data/app/deserializers/command_tower/deserializers/home_notices/notice_id_deserializer.rb +42 -0
- data/app/errors/command_tower/errors/home_notices/linked_message_unavailable_error.rb +23 -0
- data/app/errors/command_tower/errors/home_notices/rate_limit_error.rb +25 -0
- data/app/errors/command_tower/errors/home_notices/unavailable_error.rb +22 -0
- data/app/jobs/command_tower/home_notices/prune_ended_job.rb +14 -0
- data/app/models/command_tower/home_notice.rb +41 -0
- data/app/models/command_tower/home_notice_control.rb +12 -0
- data/app/serializers/command_tower/serializers/home_notices/control_serializer.rb +14 -0
- data/app/serializers/command_tower/serializers/home_notices/dismissal_serializer.rb +19 -0
- data/app/serializers/command_tower/serializers/home_notices/mutation_serializer.rb +16 -0
- data/app/serializers/command_tower/serializers/home_notices/notice_serializer.rb +35 -0
- data/app/serializers/command_tower/serializers/home_notices/selection_serializer.rb +21 -0
- data/app/services/command_tower/home_notices/campaign.rb +28 -0
- data/app/services/command_tower/home_notices/campaigns/update_app.rb +77 -0
- data/app/services/command_tower/home_notices/catalog/validator.rb +92 -0
- data/app/services/command_tower/home_notices/catalog.rb +92 -0
- data/app/services/command_tower/home_notices/context.rb +10 -0
- data/app/services/command_tower/home_notices/delivery.rb +43 -0
- data/app/services/command_tower/home_notices/notice.rb +88 -0
- data/app/services/command_tower/home_notices/notice_record.rb +28 -0
- data/app/services/command_tower/messaging/contract/inbox.rb +29 -0
- data/app/services/command_tower/messaging/contract/requests/document_for_communication.rb +24 -0
- data/app/services/command_tower/messaging/contract/results/inbox_document.rb +17 -0
- data/app/services/command_tower/services/auth/purge_user_associated_data.rb +6 -0
- data/app/services/command_tower/services/home_notices/acknowledge.rb +149 -0
- data/app/services/command_tower/services/home_notices/check_mutation_eligibility.rb +84 -0
- data/app/services/command_tower/services/home_notices/check_rate_limit.rb +52 -0
- data/app/services/command_tower/services/home_notices/compose.rb +113 -0
- data/app/services/command_tower/services/home_notices/deliver.rb +111 -0
- data/app/services/command_tower/services/home_notices/dismiss.rb +147 -0
- data/app/services/command_tower/services/home_notices/load_candidates.rb +85 -0
- data/app/services/command_tower/services/home_notices/pick_notices.rb +139 -0
- data/app/services/command_tower/services/home_notices/prune_ended.rb +93 -0
- data/app/services/command_tower/services/home_notices/read_controls.rb +31 -0
- data/app/services/command_tower/services/home_notices/record_impression.rb +154 -0
- data/app/services/command_tower/services/home_notices/resolve_notice.rb +69 -0
- data/app/services/command_tower/services/home_notices/select.rb +136 -0
- data/app/services/command_tower/services/home_notices/update_control.rb +56 -0
- data/app/workflows/command_tower/workflows/admin/home_notices/update_control_workflow.rb +64 -0
- data/app/workflows/command_tower/workflows/home_notices/acknowledge_workflow.rb +140 -0
- data/app/workflows/command_tower/workflows/home_notices/dismiss_workflow.rb +107 -0
- data/app/workflows/command_tower/workflows/home_notices/prune_ended_workflow.rb +25 -0
- data/app/workflows/command_tower/workflows/home_notices/record_impression_workflow.rb +92 -0
- data/app/workflows/command_tower/workflows/home_notices/show_workflow.rb +44 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20261124200001_create_command_tower_home_notices.rb +43 -0
- data/db/migrate/20261124200002_create_command_tower_home_notice_controls.rb +12 -0
- data/docs/api_reference.md +108 -1
- data/docs/audit.md +12 -0
- data/docs/bootstrap/08-initializer.md +15 -0
- data/docs/bootstrap/09-rbac.md +6 -0
- data/docs/bootstrap/13-optional.md +12 -0
- data/docs/extending.md +2 -0
- data/docs/home_notices.md +236 -0
- data/docs/messaging_integration_guide.md +4 -0
- data/docs/models.md +4 -0
- data/docs/principal_capabilities.md +1 -0
- data/docs/ui.md +13 -0
- data/docs/upgrades/0.24.0.md +50 -0
- data/lib/command_tower/authorization/default.yml +10 -0
- data/lib/command_tower/configuration/config.rb +6 -0
- data/lib/command_tower/configuration/home_notices/config.rb +67 -0
- data/lib/command_tower/configuration/registry/audit/config.rb +61 -0
- data/lib/command_tower/configuration/registry/principal_capabilities/config.rb +1 -1
- data/lib/command_tower/engine.rb +1 -0
- data/lib/command_tower/install/baseline.rb +2 -0
- data/lib/command_tower/ui/document.rb +26 -5
- data/lib/command_tower/version.rb +1 -1
- data/lib/tasks/home_notices.rake +11 -0
- metadata +53 -2
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# `POST /me/home-notices/:id/dismiss`: rate limit, refuse impersonation, resolve, re-check
|
|
7
|
+
# eligibility, dismiss.
|
|
8
|
+
#
|
|
9
|
+
# Authorization sequence: the controller authorizes the `me_home_notices` entity before this
|
|
10
|
+
# runs. While impersonating (or when that is unknown) the answer is 403 and nothing is written.
|
|
11
|
+
# The notice is found for `user` only, so a foreign row is a 404, as is a notice that is killed
|
|
12
|
+
# or outside its window. A blocking notice is 422 and nothing is written. `show?` is never run.
|
|
13
|
+
# An unreadable kill switch or a database failure is 503 `home_notices_unavailable`. The shared
|
|
14
|
+
# mutation rate limit fails open when Redis is unavailable.
|
|
15
|
+
#
|
|
16
|
+
# `Services::HomeNotices::Dismiss` owns the transaction (conditional update plus audit).
|
|
17
|
+
class DismissWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
18
|
+
retry_strategy :none
|
|
19
|
+
|
|
20
|
+
def call(user:, notice_id:, impersonating:, app_version: nil, platform: nil)
|
|
21
|
+
limited = CommandTower::Services::HomeNotices::CheckRateLimit.call(user:, bucket: :mutation)
|
|
22
|
+
return failure(errors: limited.errors, http_status: :too_many_requests) if limited.failure?
|
|
23
|
+
return forbidden unless impersonating == false
|
|
24
|
+
|
|
25
|
+
request_context = build_context(impersonating:, app_version:, platform:)
|
|
26
|
+
resolved = CommandTower::Services::HomeNotices::ResolveNotice.call(user:, notice_id:, request_context:)
|
|
27
|
+
return rejected(resolved) if resolved.failure?
|
|
28
|
+
|
|
29
|
+
eligible = check_eligibility(resolved:, request_context:)
|
|
30
|
+
return rejected(eligible) if eligible.failure?
|
|
31
|
+
|
|
32
|
+
dismiss(user:, resolved:, request_context:)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def check_eligibility(resolved:, request_context:)
|
|
38
|
+
CommandTower::Services::HomeNotices::CheckMutationEligibility.call(
|
|
39
|
+
notice_class: resolved.data[:notice_class], row: resolved.data[:row], request_context:
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def build_context(impersonating:, app_version:, platform:)
|
|
44
|
+
CommandTower::HomeNotices::Context.new(
|
|
45
|
+
now: Time.current, time_zone: CommandTower::Current.client_time_zone, app_version:, platform:,
|
|
46
|
+
ui_catalog: CommandTower::Current.ui_catalog,
|
|
47
|
+
client_compatibility: CommandTower::Current.client_compatibility_recommendation, impersonating:
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def dismiss(user:, resolved:, request_context:)
|
|
52
|
+
dismissed = CommandTower::Services::HomeNotices::Dismiss.call(
|
|
53
|
+
user:, request_context:, **resolved.data.slice(:notice_class, :row, :period_key)
|
|
54
|
+
)
|
|
55
|
+
return rejected(dismissed) if dismissed.failure?
|
|
56
|
+
|
|
57
|
+
log_dismissed(user:, notice_class: resolved.data[:notice_class], data: dismissed.data)
|
|
58
|
+
respond(notice_class: resolved.data[:notice_class], data: dismissed.data)
|
|
59
|
+
rescue ActiveRecord::ActiveRecordError
|
|
60
|
+
unavailable
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def log_dismissed(user:, notice_class:, data:)
|
|
64
|
+
CommandTower::Events.publish(
|
|
65
|
+
category: :home_notices, name: "dismissed",
|
|
66
|
+
payload: { user_id: user.id, notice_key: notice_class.key, notice_id: data[:public_id],
|
|
67
|
+
recorded: data[:recorded] }
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def respond(notice_class:, data:)
|
|
72
|
+
success(
|
|
73
|
+
payload: CommandTower::Serializers::HomeNotices::DismissalSerializer.serialize(
|
|
74
|
+
notice_class:, public_id: data[:public_id], dismissed_until: data[:dismissed_until]
|
|
75
|
+
),
|
|
76
|
+
http_status: :ok
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Not found stays 404 and a blocking dismiss is 422; any other failure (controls unreadable)
|
|
81
|
+
# is 503.
|
|
82
|
+
def rejected(result)
|
|
83
|
+
error = result.errors.first
|
|
84
|
+
return forbidden if error.is_a?(CommandTower::Errors::ForbiddenError)
|
|
85
|
+
return invalid(result) if error.is_a?(CommandTower::Errors::ValidationError)
|
|
86
|
+
return unavailable unless error.is_a?(CommandTower::Errors::NotFoundError)
|
|
87
|
+
|
|
88
|
+
failure(errors: result.errors, http_status: :not_found)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def invalid(result)
|
|
92
|
+
failure(errors: result.errors, http_status: :unprocessable_entity)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def forbidden
|
|
96
|
+
failure(errors: [CommandTower::Errors::ForbiddenError.new], http_status: :forbidden)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def unavailable
|
|
100
|
+
failure(
|
|
101
|
+
errors: [CommandTower::Errors::HomeNotices::UnavailableError.new], http_status: :service_unavailable
|
|
102
|
+
)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# Job entry for the daily home notice prune. The host schedules `PruneEndedJob`
|
|
7
|
+
# (or the `command_tower:home_notices:prune` rake task); CommandTower ships no scheduler.
|
|
8
|
+
# A database failure is a failed result, which the job re-raises.
|
|
9
|
+
class PruneEndedWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
10
|
+
retry_strategy :none
|
|
11
|
+
|
|
12
|
+
def call
|
|
13
|
+
pruned = CommandTower::Services::HomeNotices::PruneEnded.call
|
|
14
|
+
if pruned.failure?
|
|
15
|
+
return failure(
|
|
16
|
+
errors: pruned.errors, http_status: :internal_server_error, meta: { propagate_to_job: true }
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
success(payload: { deleted: pruned.data[:deleted] }, http_status: :ok)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# `POST /me/home-notices/:id/impression`: rate limit, resolve, re-check eligibility, record.
|
|
7
|
+
#
|
|
8
|
+
# Authorization sequence: the controller authorizes the `me_home_notices` entity before this
|
|
9
|
+
# runs. The notice is found for `user` only, so a foreign row is a 404, as is a notice that is
|
|
10
|
+
# killed, outside its window, acknowledged or dismissed. While impersonating (or when that is
|
|
11
|
+
# unknown) nothing is written and the answer is `recorded: false`. An unreadable kill switch or
|
|
12
|
+
# a database failure is 503 `home_notices_unavailable`. The shared mutation rate limit fails
|
|
13
|
+
# open when Redis is unavailable.
|
|
14
|
+
#
|
|
15
|
+
# Logs `command_tower.home_notices.impression` with ids only.
|
|
16
|
+
class RecordImpressionWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
17
|
+
retry_strategy :none
|
|
18
|
+
|
|
19
|
+
def call(user:, notice_id:, impersonating:, app_version: nil, platform: nil)
|
|
20
|
+
limited = CommandTower::Services::HomeNotices::CheckRateLimit.call(user:, bucket: :mutation)
|
|
21
|
+
return failure(errors: limited.errors, http_status: :too_many_requests) if limited.failure?
|
|
22
|
+
|
|
23
|
+
request_context = build_context(impersonating:, app_version:, platform:)
|
|
24
|
+
resolved = CommandTower::Services::HomeNotices::ResolveNotice.call(user:, notice_id:, request_context:)
|
|
25
|
+
return rejected(resolved) if resolved.failure?
|
|
26
|
+
|
|
27
|
+
eligible = check_eligibility(resolved:, request_context:)
|
|
28
|
+
return rejected(eligible) if eligible.failure?
|
|
29
|
+
|
|
30
|
+
record(user:, resolved:, request_context:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def check_eligibility(resolved:, request_context:)
|
|
36
|
+
CommandTower::Services::HomeNotices::CheckMutationEligibility.call(
|
|
37
|
+
notice_class: resolved.data[:notice_class], row: resolved.data[:row], request_context:,
|
|
38
|
+
for_impression: true
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def build_context(impersonating:, app_version:, platform:)
|
|
43
|
+
CommandTower::HomeNotices::Context.new(
|
|
44
|
+
now: Time.current, time_zone: CommandTower::Current.client_time_zone, app_version:, platform:,
|
|
45
|
+
ui_catalog: CommandTower::Current.ui_catalog,
|
|
46
|
+
client_compatibility: CommandTower::Current.client_compatibility_recommendation, impersonating:
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def record(user:, resolved:, request_context:)
|
|
51
|
+
recorded = CommandTower::Services::HomeNotices::RecordImpression.call(
|
|
52
|
+
user:, request_context:, **resolved.data.slice(:notice_class, :row, :period_key)
|
|
53
|
+
)
|
|
54
|
+
return rejected(recorded) if recorded.failure?
|
|
55
|
+
|
|
56
|
+
log_impression(user:, notice_class: resolved.data[:notice_class], data: recorded.data)
|
|
57
|
+
respond(recorded.data[:recorded])
|
|
58
|
+
rescue ActiveRecord::ActiveRecordError
|
|
59
|
+
unavailable
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def respond(recorded)
|
|
63
|
+
success(
|
|
64
|
+
payload: CommandTower::Serializers::HomeNotices::MutationSerializer.serialize(recorded:),
|
|
65
|
+
http_status: :ok
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# A not-found result stays 404; any other failure (controls unreadable) is 503.
|
|
70
|
+
def rejected(result)
|
|
71
|
+
return unavailable unless result.errors.first.is_a?(CommandTower::Errors::NotFoundError)
|
|
72
|
+
|
|
73
|
+
failure(errors: result.errors, http_status: :not_found)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def unavailable
|
|
77
|
+
failure(
|
|
78
|
+
errors: [CommandTower::Errors::HomeNotices::UnavailableError.new], http_status: :service_unavailable
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def log_impression(user:, notice_class:, data:)
|
|
83
|
+
CommandTower::Events.publish(
|
|
84
|
+
category: :home_notices, name: "impression",
|
|
85
|
+
payload: { user_id: user.id, notice_key: notice_class.key, notice_id: data[:public_id],
|
|
86
|
+
recorded: data[:recorded], first_impression: data[:first_impression] }
|
|
87
|
+
)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Workflows
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# `GET /me/home-notices`: rate limit, select, serialize.
|
|
7
|
+
#
|
|
8
|
+
# Authorization sequence: the controller authorizes the `me_home_notices` entity before this
|
|
9
|
+
# runs. `impersonating` comes from the authenticated boundary: true or nil (unknown) redacts
|
|
10
|
+
# the documents and sets `readOnly`. A client below UI catalog 2 gets both notices null without
|
|
11
|
+
# selection. When the controls cannot be read, `Select` returns no picks, so the response is
|
|
12
|
+
# also both null.
|
|
13
|
+
class ShowWorkflow < CommandTower::Workflows::ApplicationWorkflow
|
|
14
|
+
retry_strategy :none
|
|
15
|
+
|
|
16
|
+
def call(user:, impersonating:, app_version: nil, platform: nil)
|
|
17
|
+
limited = CommandTower::Services::HomeNotices::CheckRateLimit.call(user:)
|
|
18
|
+
return failure(errors: limited.errors, http_status: :too_many_requests) if limited.failure?
|
|
19
|
+
|
|
20
|
+
picks = select_notices(user:, impersonating:, app_version:, platform:)
|
|
21
|
+
success(
|
|
22
|
+
payload: CommandTower::Serializers::HomeNotices::SelectionSerializer.serialize(
|
|
23
|
+
blocking: picks[:blocking], dismissible: picks[:dismissible], read_only: impersonating != false
|
|
24
|
+
),
|
|
25
|
+
http_status: :ok
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def select_notices(user:, impersonating:, app_version:, platform:)
|
|
32
|
+
return {} if CommandTower::Current.ui_catalog.to_i < CommandTower::Services::HomeNotices::Compose::MIN_CATALOG
|
|
33
|
+
|
|
34
|
+
request_context = CommandTower::HomeNotices::Context.new(
|
|
35
|
+
now: Time.current, time_zone: CommandTower::Current.client_time_zone, app_version:, platform:,
|
|
36
|
+
ui_catalog: CommandTower::Current.ui_catalog,
|
|
37
|
+
client_compatibility: CommandTower::Current.client_compatibility_recommendation, impersonating:
|
|
38
|
+
)
|
|
39
|
+
CommandTower::Services::HomeNotices::Select.call(user:, request_context:).data
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/config/routes.rb
CHANGED
|
@@ -119,11 +119,20 @@ CommandTower::Engine.routes.draw do
|
|
|
119
119
|
put "push/:id", to: "push#update"
|
|
120
120
|
delete "push/:id", to: "push#destroy"
|
|
121
121
|
|
|
122
|
+
constraints(->(_req) { CommandTower.config.home_notices.enabled }) do
|
|
123
|
+
get "home-notices", to: "home_notices#index"
|
|
124
|
+
# The id is a public id or `campaign:<key>` (dots and a colon), so the segment is not dot-limited.
|
|
125
|
+
post "home-notices/:id/impression", to: "home_notices#impression", constraints: { id: %r{[^/]+} }
|
|
126
|
+
post "home-notices/:id/acknowledge", to: "home_notices#acknowledge", constraints: { id: %r{[^/]+} }
|
|
127
|
+
post "home-notices/:id/dismiss", to: "home_notices#dismiss", constraints: { id: %r{[^/]+} }
|
|
128
|
+
end
|
|
129
|
+
|
|
122
130
|
get "experience-states", to: "experience_states#index"
|
|
123
131
|
post "experience-states/complete", to: "experience_states#complete"
|
|
124
132
|
end
|
|
125
133
|
|
|
126
134
|
namespace :admin do
|
|
135
|
+
patch "home-notices/:key", to: "home_notices#update", constraints: { key: %r{[^/]+} }
|
|
127
136
|
get "audit-events/filter-options", to: "audit/events#filter_options"
|
|
128
137
|
get "audit-events", to: "audit/events#index"
|
|
129
138
|
get "audit-events/:id", to: "audit/events#show"
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateCommandTowerHomeNotices < ActiveRecord::Migration[7.2]
|
|
4
|
+
def change
|
|
5
|
+
create_table :command_tower_home_notices do |t|
|
|
6
|
+
t.string :public_id, null: false, limit: 32
|
|
7
|
+
t.references :user, null: false, foreign_key: true
|
|
8
|
+
t.string :kind, null: false, limit: 16
|
|
9
|
+
t.string :key, null: false, limit: 128
|
|
10
|
+
t.string :campaign_key, limit: 128
|
|
11
|
+
t.string :period_key, null: false, default: "", limit: 191
|
|
12
|
+
t.string :identity, limit: 191
|
|
13
|
+
t.bigint :communication_id
|
|
14
|
+
t.datetime :starts_at
|
|
15
|
+
t.datetime :ends_at
|
|
16
|
+
t.json :metadata
|
|
17
|
+
t.integer :impression_count, null: false, default: 0
|
|
18
|
+
t.datetime :first_shown_at
|
|
19
|
+
t.datetime :last_shown_at
|
|
20
|
+
t.datetime :acknowledged_at
|
|
21
|
+
t.string :acknowledged_content_hash, limit: 64
|
|
22
|
+
t.datetime :dismissed_until
|
|
23
|
+
t.timestamps
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
add_index :command_tower_home_notices, :public_id, unique: true
|
|
27
|
+
add_index :command_tower_home_notices,
|
|
28
|
+
%i[user_id campaign_key period_key],
|
|
29
|
+
unique: true,
|
|
30
|
+
name: "index_ct_home_notices_on_user_campaign_period"
|
|
31
|
+
add_index :command_tower_home_notices,
|
|
32
|
+
%i[user_id identity],
|
|
33
|
+
unique: true,
|
|
34
|
+
name: "index_ct_home_notices_on_user_identity"
|
|
35
|
+
add_index :command_tower_home_notices,
|
|
36
|
+
%i[user_id kind acknowledged_at],
|
|
37
|
+
name: "index_ct_home_notices_on_user_kind_acknowledged"
|
|
38
|
+
add_index :command_tower_home_notices,
|
|
39
|
+
%i[user_id last_shown_at],
|
|
40
|
+
name: "index_ct_home_notices_on_user_last_shown"
|
|
41
|
+
add_index :command_tower_home_notices, :updated_at
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateCommandTowerHomeNoticeControls < ActiveRecord::Migration[7.2]
|
|
4
|
+
def change
|
|
5
|
+
create_table :command_tower_home_notice_controls, id: false do |t|
|
|
6
|
+
t.string :key, null: false, limit: 128, primary_key: true
|
|
7
|
+
t.boolean :enabled, null: false, default: true
|
|
8
|
+
t.bigint :updated_by_user_id
|
|
9
|
+
t.timestamps
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
data/docs/api_reference.md
CHANGED
|
@@ -190,7 +190,7 @@ Does **not** authorize `admin_impersonation` on the effective user. Shared front
|
|
|
190
190
|
| **Errors** | `401`; `403`; `412` `email_verification_required` when email verification gate applies |
|
|
191
191
|
| **Spec** | `spec/requests/command_tower/auth/principal_capabilities_spec.rb` |
|
|
192
192
|
|
|
193
|
-
Projection is effective entity grants ∩ curated `config.registry.principal_capabilities` (never role/group names). CommandTower seeds `admin_workspace`, `admin_users`, `admin_users_update`, `admin_rbac_assignments`, `admin_audit_events`, `admin_messaging_announcements`, `admin_messaging_gallery`, `admin_messaging_gallery_test_send`, `admin_impersonation`, `me_audit_events`. Hosts may register additive host-owned ids. Distinct from `/me` capabilities and from `GET /admin/workspace` (tool manifest). See [Principal capabilities](principal_capabilities.md).
|
|
193
|
+
Projection is effective entity grants ∩ curated `config.registry.principal_capabilities` (never role/group names). CommandTower seeds `admin_workspace`, `admin_users`, `admin_users_update`, `admin_rbac_assignments`, `admin_audit_events`, `admin_messaging_announcements`, `admin_messaging_gallery`, `admin_messaging_gallery_test_send`, `admin_impersonation`, `me_audit_events`, `me_home_notices`. Hosts may register additive host-owned ids. Distinct from `/me` capabilities and from `GET /admin/workspace` (tool manifest). See [Principal capabilities](principal_capabilities.md).
|
|
194
194
|
|
|
195
195
|
### `GET /auth/email/availability`
|
|
196
196
|
|
|
@@ -508,6 +508,113 @@ Durable completion facts for host-composed experiences. CommandTower stores opaq
|
|
|
508
508
|
|
|
509
509
|
---
|
|
510
510
|
|
|
511
|
+
## Home notices
|
|
512
|
+
|
|
513
|
+
### `GET /me/home-notices`
|
|
514
|
+
|
|
515
|
+
Returns at most one blocking and one dismissible notice for the signed-in user. Off by default: **404** unless `config.home_notices.enabled`. Selection, pacing and authoring: [Home Notices](home_notices.md).
|
|
516
|
+
|
|
517
|
+
| Header | Use |
|
|
518
|
+
|--------|-----|
|
|
519
|
+
| `X-UI-Catalog` | Below `2` returns both notices `null` |
|
|
520
|
+
| `X-App-Version`, `X-Client-Platform` | Passed to notices as context |
|
|
521
|
+
|
|
522
|
+
`data`: `{ blocking, dismissible, readOnly }`. Each notice is `null` or a document. `readOnly` is `true` while impersonating (documents are redacted) or when impersonation state is unknown. The response always carries `Cache-Control: no-store`.
|
|
523
|
+
|
|
524
|
+
| Status | When |
|
|
525
|
+
|--------|------|
|
|
526
|
+
| 200 | Success |
|
|
527
|
+
| 401 / 403 | Unauthenticated / no `me_home_notices` grant |
|
|
528
|
+
| 404 | `config.home_notices.enabled` is false |
|
|
529
|
+
| 429 | `home_notices_rate_limited`: over `config.home_notices.rate_limit_per_minute` (30 per user per minute). A limiter failure fails open |
|
|
530
|
+
|
|
531
|
+
**RBAC:** `me_home_notices`. Not in the dummy `member`; granted by the dummy `home_notices_reader` role and by `owner`. Also a principal capability.
|
|
532
|
+
|
|
533
|
+
**Spec:** `spec/requests/command_tower/me/home_notices_spec.rb`.
|
|
534
|
+
|
|
535
|
+
### `POST /me/home-notices/:id/impression`
|
|
536
|
+
|
|
537
|
+
Records that the signed-in user was shown a notice. `:id` is a notice `public_id` or `campaign:<key>` (a campaign with no row yet in the current period). The notice is looked up for the current user only. Routed only when `config.home_notices.enabled`; otherwise **404**. No request body.
|
|
538
|
+
|
|
539
|
+
`data`: `{ recorded }`. `recorded` is `true` when the impression was counted, `false` when nothing was written: the user is impersonating (or impersonation state is unknown), or a repeat impression arrived within 60 seconds of the last one.
|
|
540
|
+
|
|
541
|
+
| Status | When |
|
|
542
|
+
|--------|------|
|
|
543
|
+
| 200 | Success, including `recorded: false` |
|
|
544
|
+
| 401 / 403 | Unauthenticated / no `me_home_notices` grant |
|
|
545
|
+
| 404 | Feature disabled; unknown, foreign or killed notice; or the notice is outside its window, acknowledged or dismissed |
|
|
546
|
+
| 429 | `home_notices_rate_limited`: over `config.home_notices.mutation_rate_limit_per_minute` (120 per user per minute, shared with acknowledge and dismiss). A limiter failure fails open |
|
|
547
|
+
| 503 | `home_notices_unavailable`: the kill switch could not be read, or a database failure |
|
|
548
|
+
|
|
549
|
+
**Audit:** the first impression of a campaign period, or of a delivery, emits `home_notice_impression` (affected user: the current user; metadata `notice_id`, `key`, `kind`, `severity`). Later impressions only increment the row's count and emit nothing. A new campaign period audits again.
|
|
550
|
+
|
|
551
|
+
**RBAC:** `me_home_notices`.
|
|
552
|
+
|
|
553
|
+
**Spec:** `spec/requests/command_tower/me/home_notices_impression_spec.rb`.
|
|
554
|
+
|
|
555
|
+
### `POST /me/home-notices/:id/acknowledge`
|
|
556
|
+
|
|
557
|
+
Acknowledges a notice for the signed-in user. `:id` is a notice `public_id` or `campaign:<key>` (a campaign with no row yet in the current period). Works for blocking and dismissible notices. It never runs the notice's `show?`: a user who saw a notice can always clear it, even if `show?` is now false or raises. Routed only when `config.home_notices.enabled`; otherwise **404**. No request body.
|
|
558
|
+
|
|
559
|
+
`data`: `{ acknowledged, recorded, public_id }`. `acknowledged` is always `true` on success. `recorded` is `true` only when this request made the change; a repeat acknowledge is an idempotent success with `recorded: false`. A blocking acknowledgement stores the content hash of the shown document on the row (for blocking only); the hash is never returned and never audited.
|
|
560
|
+
|
|
561
|
+
| Status | When |
|
|
562
|
+
|--------|------|
|
|
563
|
+
| 200 | Success, including `recorded: false` |
|
|
564
|
+
| 401 / 403 | Unauthenticated / no `me_home_notices` grant. Also **403** while impersonating (or impersonation state is unknown); nothing is written |
|
|
565
|
+
| 404 | Feature disabled; unknown, foreign or killed notice |
|
|
566
|
+
| 429 | `home_notices_rate_limited`: over `config.home_notices.mutation_rate_limit_per_minute` (120 per user per minute, shared with impression and dismiss). A limiter failure fails open |
|
|
567
|
+
| 503 | `home_notices_unavailable`: the kill switch could not be read, or a database failure |
|
|
568
|
+
|
|
569
|
+
**Audit:** when this request changes the row, it emits `home_notice_acknowledged` (subject and affected user: the current user; metadata `notice_id`, `key`, `kind`, `severity`; never the content hash). A repeat acknowledge emits nothing.
|
|
570
|
+
|
|
571
|
+
**RBAC:** `me_home_notices`.
|
|
572
|
+
|
|
573
|
+
**Spec:** `spec/requests/command_tower/me/home_notices_clear_spec.rb`.
|
|
574
|
+
|
|
575
|
+
### `POST /me/home-notices/:id/dismiss`
|
|
576
|
+
|
|
577
|
+
Dismisses a dismissible notice for the signed-in user for 7 days (`dismissed_until`); the notice's window, kill switch and pacing still apply afterward. `:id` is as for acknowledge. It never runs `show?`. Routed only when `config.home_notices.enabled`; otherwise **404**. No request body.
|
|
578
|
+
|
|
579
|
+
`data`: `{ dismissed, recorded, public_id, dismissed_until }`. `dismissed` is always `true` on success. `recorded` is `true` only when this request made the change; a repeat dismiss while already dismissed into the future is an idempotent success with `recorded: false`.
|
|
580
|
+
|
|
581
|
+
| Status | When |
|
|
582
|
+
|--------|------|
|
|
583
|
+
| 200 | Success, including `recorded: false` |
|
|
584
|
+
| 401 / 403 | Unauthenticated / no `me_home_notices` grant. Also **403** while impersonating (or impersonation state is unknown); nothing is written |
|
|
585
|
+
| 404 | Feature disabled; unknown, foreign or killed notice |
|
|
586
|
+
| 422 | The notice is blocking: only dismissible notices can be dismissed; nothing is written |
|
|
587
|
+
| 429 | `home_notices_rate_limited`: over `config.home_notices.mutation_rate_limit_per_minute` (shared with impression and acknowledge). A limiter failure fails open |
|
|
588
|
+
| 503 | `home_notices_unavailable`: the kill switch could not be read, or a database failure |
|
|
589
|
+
|
|
590
|
+
**Audit:** when this request changes the row, it emits `home_notice_dismissed` (affected user: the current user; metadata `notice_id`, `key`, `kind`, `severity`). A repeat dismiss emits nothing.
|
|
591
|
+
|
|
592
|
+
**RBAC:** `me_home_notices`.
|
|
593
|
+
|
|
594
|
+
**Spec:** `spec/requests/command_tower/me/home_notices_clear_spec.rb`.
|
|
595
|
+
|
|
596
|
+
### `PATCH /admin/home-notices/:key`
|
|
597
|
+
|
|
598
|
+
The home-notice kill switch: turns one registered notice key on or off. Body `{ "enabled": boolean }` (strict boolean, required). `:key` is `[a-z0-9_]` segments joined by dots, at most 128 characters. Always sends `Cache-Control: no-store`. Runbook: [Home Notices](home_notices.md#kill-switch-runbook).
|
|
599
|
+
|
|
600
|
+
| Status | Meaning |
|
|
601
|
+
|---|---|
|
|
602
|
+
| 200 | `data: { key, enabled }`. Setting the state a key already has is a 200 no-op |
|
|
603
|
+
| 401 / 403 | Unauthenticated / no `admin_home_notices_kill` grant. Also **403** while impersonating |
|
|
604
|
+
| 404 | The key is not a registered notice |
|
|
605
|
+
| 422 | Malformed key, or `enabled` missing or not a boolean |
|
|
606
|
+
| 503 | `home_notices_unavailable`: a database failure |
|
|
607
|
+
|
|
608
|
+
**Audit:** a request that changes the effective state emits `home_notice_control_changed` (admin attribution; changes `enabled` from/to; metadata `key`). A no-op emits nothing.
|
|
609
|
+
|
|
610
|
+
**RBAC:** `admin_home_notices_kill`. Not in the principal-capabilities catalog and not in any default role except `owner` (`entities: true`).
|
|
611
|
+
|
|
612
|
+
**Spec:** `spec/requests/command_tower/admin/home_notices_spec.rb`.
|
|
613
|
+
|
|
614
|
+
**Fixtures.** Example `GET /me/home-notices` response bodies for every client seam (terms, long terms, campaign, update app with and without a URL, linked, redacted code with `readOnly: true`, both, none) are committed under `spec/fixtures/home_notices/`. Regenerate them from the dummy app with `make rake TASK=app:command_tower:home_notices:fixtures`. Files are sorted, pretty-printed JSON, so consumers can copy them byte for byte.
|
|
615
|
+
|
|
616
|
+
---
|
|
617
|
+
|
|
511
618
|
## Admin Workspace
|
|
512
619
|
|
|
513
620
|
### `GET /admin/workspace`
|
data/docs/audit.md
CHANGED
|
@@ -86,9 +86,21 @@ CommandTower.config.registry.audit.set_enabled!(:login_failed, true)
|
|
|
86
86
|
| `session_cleared` | no | yes |
|
|
87
87
|
| `login_failed` | no | yes |
|
|
88
88
|
| `messaging_inbox_item_viewed` | yes | yes |
|
|
89
|
+
| `home_notice_impression` | yes | no |
|
|
90
|
+
| `home_notice_acknowledged` | yes | no |
|
|
91
|
+
| `home_notice_dismissed` | yes | no |
|
|
92
|
+
| `home_notice_control_changed` | yes | no |
|
|
89
93
|
|
|
90
94
|
`messaging_inbox_item_viewed` is emitted by `Workflows::Messaging::Inbox::OpenWorkflow` once per first view of an inbox item, for every notification type (metadata: `inbox_item_id`, `communication_id`; retention `ninety_days`, not in account history). A repeat open, bulk read/unread, `Show`, and `Contract::Inbox` emit nothing. An impersonated open returns the detail but neither marks the item viewed nor audits.
|
|
91
95
|
|
|
96
|
+
`home_notice_impression` is emitted by `Services::HomeNotices::RecordImpression` (via `POST /me/home-notices/:id/impression`) once per first impression of a campaign period or of a delivery (affected user: the viewer; metadata `notice_id`, `key`, `kind`, `severity`; no changes; retention `ninety_days`; not in account history). The decision is an atomic write, so concurrent requests audit exactly once. A repeat impression, one inside the 60 s idempotency window, and an impersonated request emit nothing; a new campaign period audits again. See [home_notices.md](home_notices.md).
|
|
97
|
+
|
|
98
|
+
`home_notice_acknowledged` is emitted by `Services::HomeNotices::Acknowledge` (via `POST /me/home-notices/:id/acknowledge`) when the request itself changes the row (subject and affected user: the user; metadata `notice_id`, `key`, `kind`, `severity`; no changes; retention `one_year`; in account history). Metadata is ids only: the content hash of a blocking acknowledgement stays on the notice row. The decision is an atomic write in the same transaction as the audit, so concurrent requests audit exactly once. A repeat acknowledge and an impersonated request (refused with 403) emit nothing.
|
|
99
|
+
|
|
100
|
+
`home_notice_dismissed` is emitted by `Services::HomeNotices::Dismiss` (via `POST /me/home-notices/:id/dismiss`) when the request itself changes the row (affected user: the user; metadata `notice_id`, `key`, `kind`, `severity`; no changes; retention `ninety_days`; not in account history). Only dismissible notices can be dismissed. A repeat dismiss while already dismissed, a blocking dismiss (422) and an impersonated request (403) emit nothing.
|
|
101
|
+
|
|
102
|
+
`home_notice_control_changed` is emitted by `Workflows::Admin::HomeNotices::UpdateControlWorkflow` (via `PATCH /admin/home-notices/:key`) in the same transaction as the write, only when the request changes the effective state (admin attribution `admin_direct`; no affected user; metadata `key`; change `enabled` from/to; retention `permanent`; not in account history). A no-op, an unknown key (404) and an impersonated request (403) emit nothing. See [home_notices.md](home_notices.md#kill-switch-runbook).
|
|
103
|
+
|
|
92
104
|
`messaging_unsubscribed` is emitted by the one-click unsubscribe workflow when a token turns email off for a type (affected user: the token's user; change key `email_enabled`; `user_history`; retention `permanent`). See [messaging_integration_guide.md](messaging_integration_guide.md#email-compliance-defined-types).
|
|
93
105
|
|
|
94
106
|
`messaging_gallery_test_sent` is emitted by `Workflows::Admin::Messaging::Gallery::TestSendWorkflow` for each accepted gallery test send (affected user: the actor; metadata: `actor_id`, `notification_type_key`, `fixture`, never the address; retention `one_year`, not in account history).
|
|
@@ -77,6 +77,21 @@ config.application.url = "http://localhost:8081"
|
|
|
77
77
|
|
|
78
78
|
`8081` is one product’s Expo port. Use the port this host’s web app actually publishes, and put that same origin in `CORS_ALLOWED_ORIGINS` (step 01). Production SMTP is out of this manual’s deployment scope.
|
|
79
79
|
|
|
80
|
+
### Home Notices (optional)
|
|
81
|
+
|
|
82
|
+
Off by default. Authoring and boot checks: [`../home_notices.md`](../home_notices.md).
|
|
83
|
+
|
|
84
|
+
| Option | Default | Meaning |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| `config.home_notices.enabled` | `false` | Loads the catalog and checks it at boot |
|
|
87
|
+
| `config.home_notices.notices` | `[]` | Class names (Strings) of `Campaign` and `Delivery` subclasses |
|
|
88
|
+
| `config.home_notices.global_cooldown` | `8.hours` | Minimum time between blocking notices per user; at least 1 hour |
|
|
89
|
+
| `config.home_notices.blocking_daily_cap` | `2` | Blocking notices started per user per day |
|
|
90
|
+
| `config.home_notices.max_open_deliveries` | `20` | Open Delivery notices per user |
|
|
91
|
+
| `config.home_notices.rate_limit_per_minute` | `30` | `GET /me/home-notices` requests per user per minute |
|
|
92
|
+
| `config.home_notices.mutation_rate_limit_per_minute` | `120` | Shared bucket for impression, acknowledge and dismiss requests |
|
|
93
|
+
| `config.home_notices.update_app_campaign` | `true` | Whether the built-in `ct.update_app` campaign is active |
|
|
94
|
+
|
|
80
95
|
### What not to do
|
|
81
96
|
|
|
82
97
|
- Do not duplicate CommandTower default hashes “for completeness”
|
data/docs/bootstrap/09-rbac.md
CHANGED
|
@@ -80,6 +80,12 @@ The frontend workspace and user detail call these entities. Grant each one the r
|
|
|
80
80
|
|
|
81
81
|
`admin_impersonation` is what shows Impersonate. The frontend does not add a route for it.
|
|
82
82
|
|
|
83
|
+
### 4. Home Notices (optional)
|
|
84
|
+
|
|
85
|
+
`GET /me/home-notices` is authorized by the `me_home_notices` entity. It is not part of `member`; RBAC fails closed, so grant it explicitly on the host role that should see notices (the dummy uses `home_notices_reader`). See [`../home_notices.md`](../home_notices.md).
|
|
86
|
+
|
|
87
|
+
The kill switch `PATCH /admin/home-notices/:key` is authorized by the `admin_home_notices_kill` entity. It is not part of `member`, is not in the principal-capabilities catalog, and is not auto-granted to any admin role. RBAC fails closed: at least one host role must grant it, or every caller gets `403`, owner included. `owner` (`entities: true`) passes only after a host role maps the entity; grant it explicitly on any other role that should operate the switch.
|
|
88
|
+
|
|
83
89
|
## Stop
|
|
84
90
|
|
|
85
91
|
`config/rbac_groups.yml` exists with a `member` group that grants `me` (and the rest you need). `default_membership_role` is `"member"` (or you have a documented assign path). Next: [`10-roles-and-gates.md`](10-roles-and-gates.md).
|
|
@@ -19,6 +19,18 @@ When the host **emits** notifications or uses phone/Pushover:
|
|
|
19
19
|
|
|
20
20
|
Complete steps 00–12 first. This bootstrap tree does not duplicate the messaging guide.
|
|
21
21
|
|
|
22
|
+
## Home notices prune
|
|
23
|
+
|
|
24
|
+
When the host enables `config.home_notices`, schedule the prune daily. CommandTower ships no scheduler.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
make rake TASK=command_tower:home_notices:prune
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
(Inside this gem's dummy app the task is `app:command_tower:home_notices:prune`.)
|
|
31
|
+
|
|
32
|
+
Or enqueue `CommandTower::HomeNotices::PruneEndedJob` from the host scheduler. It deletes ended notice rows idle for 180 days, in batches. Contract: [`../home_notices.md`](../home_notices.md).
|
|
33
|
+
|
|
22
34
|
## Host tests
|
|
23
35
|
|
|
24
36
|
Shared FactoryBot definitions ship with the gem. In the host test boot path:
|
data/docs/extending.md
CHANGED
|
@@ -86,6 +86,8 @@ Product business events own **host** workflows that call:
|
|
|
86
86
|
|
|
87
87
|
`CommandTower::Messaging::Contract::Inbox` mutation (`mark_viewed_for_communication`) emits no audit and is called only from a workflow that owns the audit decision.
|
|
88
88
|
|
|
89
|
+
A host workflow may also call `CommandTower::Services::HomeNotices::Deliver` after `Produce` to push a Home Notice (a `Delivery` subclass) to the same user, passing the `communication_id` for a linked delivery. It is idempotent on `(user, identity)`. See [home_notices.md](home_notices.md#deliver).
|
|
90
|
+
|
|
89
91
|
Do not create one Messaging workflow per message type. Do not call the messaging handoff/execution pipeline directly. Summary: [messaging_integration_guide.md](messaging_integration_guide.md).
|
|
90
92
|
|
|
91
93
|
## Blank-host checklist
|