command_tower 0.22.0 → 0.23.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.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/command_tower/admin/messaging/gallery_controller.rb +35 -0
  3. data/app/controllers/command_tower/admin/messaging/gallery_test_sends_controller.rb +54 -0
  4. data/app/controllers/command_tower/me/inbox_controller.rb +19 -4
  5. data/app/controllers/command_tower/messaging/unsubscribe_controller.rb +43 -0
  6. data/app/controllers/concerns/command_tower/execution/http_boundary.rb +36 -1
  7. data/app/errors/command_tower/errors/messaging/announcement_type_not_supported_error.rb +23 -0
  8. data/app/errors/command_tower/errors/messaging/gallery_test_send_failed_error.rb +26 -0
  9. data/app/errors/command_tower/errors/messaging/gallery_test_send_impersonating_error.rb +23 -0
  10. data/app/errors/command_tower/errors/messaging/gallery_test_send_rate_limit_error.rb +25 -0
  11. data/app/errors/command_tower/errors/messaging/unsubscribe_rate_limit_error.rb +25 -0
  12. data/app/jobs/command_tower/messaging/communications/produce_recipient_job.rb +2 -2
  13. data/app/mailers/command_tower/messaging/channel_mailer.rb +2 -0
  14. data/app/models/command_tower/messaging/communication.rb +10 -0
  15. data/app/serializers/command_tower/serializers/admin/messaging/gallery_serializer.rb +67 -0
  16. data/app/services/command_tower/email_theme/resolver.rb +13 -12
  17. data/app/services/command_tower/messaging/accept/coordinator.rb +57 -4
  18. data/app/services/command_tower/messaging/accept/persister.rb +3 -0
  19. data/app/services/command_tower/messaging/accept/request_normalizer.rb +9 -4
  20. data/app/services/command_tower/messaging/accept/resolve_communication_slots.rb +114 -0
  21. data/app/services/command_tower/messaging/announcement_definition.rb +88 -0
  22. data/app/services/command_tower/messaging/contract/inbox.rb +65 -0
  23. data/app/services/command_tower/messaging/contract/mappers/communication_mapper.rb +9 -0
  24. data/app/services/command_tower/messaging/contract/requests/mark_viewed_for_communication.rb +21 -0
  25. data/app/services/command_tower/messaging/contract/requests/viewed_for_communication.rb +21 -0
  26. data/app/services/command_tower/messaging/contract/results/communication_result.rb +3 -0
  27. data/app/services/command_tower/messaging/contract/results/inbox_view_mark.rb +18 -0
  28. data/app/services/command_tower/messaging/definitions/document_builder.rb +157 -0
  29. data/app/services/command_tower/messaging/definitions/render_context.rb +18 -0
  30. data/app/services/command_tower/messaging/inbox/item_result.rb +5 -1
  31. data/app/services/command_tower/messaging/inbox/mutator.rb +17 -0
  32. data/app/services/command_tower/messaging/inbox/reader.rb +12 -0
  33. data/app/services/command_tower/messaging/inbox/scope.rb +8 -0
  34. data/app/services/command_tower/messaging/inbox.rb +8 -0
  35. data/app/services/command_tower/messaging/message_definition/metadata.rb +35 -0
  36. data/app/services/command_tower/messaging/message_definition.rb +151 -0
  37. data/app/services/command_tower/messaging/notification_types/declaration.rb +12 -1
  38. data/app/services/command_tower/messaging/notification_types/message_definition_checks.rb +143 -0
  39. data/app/services/command_tower/messaging/notification_types/registry.rb +2 -0
  40. data/app/services/command_tower/messaging/rendering/channel_renderer.rb +73 -6
  41. data/app/services/command_tower/messaging/rendering/compliance_footer.rb +80 -0
  42. data/app/services/command_tower/messaging/rendering/defined_email_renderer.rb +141 -0
  43. data/app/services/command_tower/messaging/rendering/inbox_document_renderer.rb +79 -4
  44. data/app/services/command_tower/messaging/rendering/rendered_payload.rb +13 -1
  45. data/app/services/command_tower/messaging/rendering/unsubscribe_headers.rb +51 -0
  46. data/app/services/command_tower/messaging/unsubscribe/token_verifier.rb +26 -0
  47. data/app/services/command_tower/services/messaging/announcements/resolve_produce_arguments.rb +74 -0
  48. data/app/services/command_tower/services/messaging/communications/explicit_copy_rule.rb +50 -0
  49. data/app/services/command_tower/services/messaging/communications/produce.rb +18 -4
  50. data/app/services/command_tower/services/messaging/communications/produce_many.rb +20 -2
  51. data/app/services/command_tower/services/messaging/gallery/channel_failed.rb +31 -0
  52. data/app/services/command_tower/services/messaging/gallery/check_test_send_rate_limit.rb +47 -0
  53. data/app/services/command_tower/services/messaging/gallery/list_gallery_types.rb +44 -0
  54. data/app/services/command_tower/services/messaging/gallery/preview_builder.rb +122 -0
  55. data/app/services/command_tower/services/messaging/gallery/preview_channels.rb +51 -0
  56. data/app/services/command_tower/services/messaging/gallery/preview_checks.rb +85 -0
  57. data/app/services/command_tower/services/messaging/gallery/preview_email.rb +56 -0
  58. data/app/services/command_tower/services/messaging/gallery/render_gallery_preview.rb +85 -0
  59. data/app/services/command_tower/services/messaging/gallery/test_send.rb +55 -0
  60. data/app/services/command_tower/services/messaging/inbox.rb +36 -8
  61. data/app/services/command_tower/services/messaging/unsubscribe/check_rate_limit.rb +57 -0
  62. data/app/services/command_tower/services/messaging/unsubscribe/issue_token.rb +25 -0
  63. data/app/services/command_tower/services/messaging/unsubscribe/opt_out_of_email.rb +65 -0
  64. data/app/services/command_tower/services/messaging/unsubscribe/verify_token.rb +42 -0
  65. data/app/services/command_tower/services/users/record_last_known_time_zone.rb +40 -0
  66. data/app/workflows/command_tower/workflows/admin/messaging/create_announcement_workflow.rb +16 -3
  67. data/app/workflows/command_tower/workflows/admin/messaging/gallery/index_workflow.rb +29 -0
  68. data/app/workflows/command_tower/workflows/admin/messaging/gallery/show_workflow.rb +40 -0
  69. data/app/workflows/command_tower/workflows/admin/messaging/gallery/test_send_workflow.rb +103 -0
  70. data/app/workflows/command_tower/workflows/messaging/communications/produce_recipient_workflow.rb +2 -2
  71. data/app/workflows/command_tower/workflows/messaging/error_mapping.rb +8 -1
  72. data/app/workflows/command_tower/workflows/messaging/inbox.rb +30 -5
  73. data/app/workflows/command_tower/workflows/messaging/unsubscribe/one_click_workflow.rb +56 -0
  74. data/app/workflows/command_tower/workflows/messaging/unsubscribe/preferences_redirect_workflow.rb +24 -0
  75. data/config/routes.rb +9 -0
  76. data/db/migrate/20261124000001_add_message_definition_columns_to_messaging_communications.rb +9 -0
  77. data/docs/admin_workspace.md +2 -1
  78. data/docs/api_reference.md +78 -20
  79. data/docs/audit.md +9 -0
  80. data/docs/authentication_authorization_guide.md +1 -1
  81. data/docs/bootstrap/09-rbac.md +2 -0
  82. data/docs/bootstrap/11-auth-client-path.md +1 -1
  83. data/docs/cookie_authentication_guide.md +2 -0
  84. data/docs/extending.md +4 -0
  85. data/docs/messaging_integration_guide.md +40 -15
  86. data/docs/principal_capabilities.md +2 -0
  87. data/docs/ui.md +64 -2
  88. data/docs/upgrades/0.23.0.md +64 -0
  89. data/docs/upgrades/README.md +1 -0
  90. data/lib/command_tower/authorization/default.yml +8 -0
  91. data/lib/command_tower/configuration/messaging/compliance.rb +47 -0
  92. data/lib/command_tower/configuration/messaging/config.rb +25 -0
  93. data/lib/command_tower/configuration/registry/admin_workspace/config.rb +9 -0
  94. data/lib/command_tower/configuration/registry/admin_workspace/tool_definition.rb +10 -1
  95. data/lib/command_tower/configuration/registry/audit/config.rb +38 -0
  96. data/lib/command_tower/configuration/registry/principal_capabilities/config.rb +2 -0
  97. data/lib/command_tower/current.rb +1 -0
  98. data/lib/command_tower/email_theme/default_ui_tokens.json +18 -0
  99. data/lib/command_tower/email_theme/palette.rb +75 -0
  100. data/lib/command_tower/email_theme/token_file.rb +81 -0
  101. data/lib/command_tower/engine.rb +7 -0
  102. data/lib/command_tower/execution.rb +2 -1
  103. data/lib/command_tower/install/baseline.rb +1 -0
  104. data/lib/command_tower/rspec/message_definition.rb +125 -0
  105. data/lib/command_tower/ui/document.rb +18 -4
  106. data/lib/command_tower/ui/email/renderer/text_nodes.rb +2 -1
  107. data/lib/command_tower/ui/email/renderer.rb +17 -5
  108. data/lib/command_tower/ui/email/text_part.rb +53 -0
  109. data/lib/command_tower/ui/fragments/markdown.rb +121 -0
  110. data/lib/command_tower/ui/markdown_subset/chunker.rb +91 -0
  111. data/lib/command_tower/ui/markdown_subset/inline.rb +86 -0
  112. data/lib/command_tower/ui/markdown_subset/segment.rb +15 -0
  113. data/lib/command_tower/ui/markdown_subset/walker.rb +129 -0
  114. data/lib/command_tower/ui/nodes/instant.rb +3 -0
  115. data/lib/command_tower/ui/redaction.rb +44 -0
  116. data/lib/command_tower/ui/required_facts/matchers/text.rb +22 -0
  117. data/lib/command_tower/ui/required_facts/matchers/time.rb +37 -0
  118. data/lib/command_tower/ui/required_facts/verdict.rb +54 -0
  119. data/lib/command_tower/ui/text_derivation.rb +18 -17
  120. data/lib/command_tower/ui/time_formatter.rb +5 -3
  121. data/lib/command_tower/ui.rb +6 -0
  122. data/lib/command_tower/version.rb +1 -1
  123. data/lib/command_tower.rb +2 -0
  124. metadata +78 -2
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Services
5
+ module Messaging
6
+ module Unsubscribe
7
+ # Turns the email channel off for one user and one notification type, through the existing
8
+ # preferences update. Every refusal (unknown user, unknown type, mandatory, not user
9
+ # configurable, email not allowed, not settings-visible) is the same ValidationError, so a
10
+ # caller cannot tell them apart. `changed` is false when email was already off.
11
+ class OptOutOfEmail < CommandTower::Services::ApplicationService
12
+ validate :user_id, is_a: Integer, required: true
13
+ validate :notification_type_key, is_a: String, required: true
14
+
15
+ def call
16
+ user = User.find_by(id: user_id)
17
+ context.fail!(application_error: invalid_error) unless user && eligible?
18
+
19
+ context.user = user
20
+ context.changed = !already_off?
21
+ update_preference(user) if context.changed
22
+ end
23
+
24
+ private
25
+
26
+ def eligible?
27
+ declaration = CommandTower::Messaging::NotificationTypes.lookup(notification_type_key)
28
+ CommandTower::Messaging::Rendering::UnsubscribeHeaders.eligible?(declaration)
29
+ rescue CommandTower::Messaging::NotificationTypes::NotFoundError
30
+ false
31
+ end
32
+
33
+ def already_off?
34
+ evaluation = CommandTower::Messaging::Preferences.resolve(
35
+ notification_type_key:,
36
+ recipient_id: user_id
37
+ )
38
+ evaluation.effective_preference_state.fetch("channels").fetch("email", true) == false
39
+ end
40
+
41
+ def update_preference(user)
42
+ update = CommandTower::Services::Messaging::Preferences::Update.call(
43
+ user:,
44
+ notification_type_key:,
45
+ preference_state: { "channels" => { "email" => false } }
46
+ )
47
+ return if update.success?
48
+
49
+ context.fail!(application_error: collapse_refusal(update.errors.first))
50
+ end
51
+
52
+ # A refusal from the preferences update is indistinguishable from any other refusal.
53
+ def collapse_refusal(error)
54
+ refusal = error.is_a?(CommandTower::Errors::NotFoundError) || error.is_a?(CommandTower::Errors::ValidationError)
55
+ refusal ? invalid_error : error
56
+ end
57
+
58
+ def invalid_error
59
+ CommandTower::Errors::ValidationError.new(details: { token: "invalid" })
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Services
5
+ module Messaging
6
+ module Unsubscribe
7
+ # Verifies a one-click unsubscribe token. Every failure (tampered, expired, wrong purpose,
8
+ # malformed payload) is the same ValidationError with no detail, so a caller cannot tell them apart.
9
+ class VerifyToken < CommandTower::Services::ApplicationService
10
+ validate :token, is_a: String, required: true
11
+
12
+ def call
13
+ user_id, type_key = verified_payload
14
+ return context.fail!(application_error: invalid_token_error) unless user_id && type_key
15
+
16
+ context.user_id = user_id
17
+ context.notification_type_key = type_key
18
+ end
19
+
20
+ private
21
+
22
+ # @return [Array(Integer, String), nil] nil unless the payload is well formed
23
+ def verified_payload
24
+ signer = CommandTower::Messaging::Unsubscribe::TokenVerifier
25
+ payload = signer.verifier.verified(token, purpose: signer::PURPOSE)
26
+ return unless payload.is_a?(Hash)
27
+
28
+ user_id = payload["u"]
29
+ type_key = payload["t"]
30
+ [user_id, type_key] if user_id.is_a?(Integer) && type_key.is_a?(String) && type_key.present?
31
+ rescue ActiveSupport::MessageVerifier::InvalidSignature, ArgumentError, JSON::ParserError
32
+ nil
33
+ end
34
+
35
+ def invalid_token_error
36
+ CommandTower::Errors::ValidationError.new(details: { token: "invalid" })
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Services
5
+ module Users
6
+ # Stores the zone a client last sent in X-Time-Zone on the user. Writes only when the zone
7
+ # changed and the previous write is more than an hour old. Fails open and never logs
8
+ # (services do not use Rails.logger).
9
+ class RecordLastKnownTimeZone < CommandTower::Services::ApplicationService
10
+ MIN_WRITE_INTERVAL = 1.hour
11
+
12
+ validate :user, is_a: User, required: true
13
+ validate :zone, is_a: String, required: true
14
+
15
+ def call
16
+ context.written = false
17
+ return unless write_due?
18
+
19
+ write
20
+ context.written = true
21
+ rescue StandardError
22
+ context.written = false
23
+ end
24
+
25
+ private
26
+
27
+ def write_due?
28
+ return false if user.last_known_timezone == zone
29
+
30
+ last_write = user.last_known_timezone_update
31
+ last_write.nil? || last_write <= MIN_WRITE_INTERVAL.ago
32
+ end
33
+
34
+ def write
35
+ user.update_columns(last_known_timezone: zone, last_known_timezone_update: Time.current) # rubocop:disable Rails/SkipsModelValidations
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -12,13 +12,26 @@ module CommandTower
12
12
  platform_enabled_channels =
13
13
  CommandTower::Services::Messaging::Preferences::PlatformEnabledChannels.call
14
14
 
15
+ arguments = CommandTower::Services::Messaging::Announcements::ResolveProduceArguments.call(
16
+ notification_type_key: input.notification_type_key,
17
+ title: input.title,
18
+ body: input.body,
19
+ metadata: input.metadata
20
+ )
21
+ unless arguments.success?
22
+ return failure(
23
+ errors: arguments.errors,
24
+ http_status: CommandTower::Workflows::Messaging::ErrorMapping.http_status_for(arguments.errors.first)
25
+ )
26
+ end
27
+
15
28
  result = CommandTower::Services::Messaging::Communications::ProduceMany.call(
16
29
  user_ids:,
17
30
  notification_type_key: input.notification_type_key,
18
31
  campaign_identity: input.campaign_identity,
19
- title: input.title,
20
- body: input.body,
21
- metadata: input.metadata,
32
+ title: arguments.data[:title],
33
+ body: arguments.data[:body],
34
+ metadata: arguments.data[:metadata],
22
35
  platform_enabled_channels:,
23
36
  execution_mode: input.execution_mode,
24
37
  )
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Workflows
5
+ module Admin
6
+ module Messaging
7
+ module Gallery
8
+ # Lists the notification types the gallery can preview. Authorization is the controller's
9
+ # RBAC boundary (entity `admin_messaging_gallery`).
10
+ class IndexWorkflow < CommandTower::Workflows::ApplicationWorkflow
11
+ retry_strategy :none
12
+
13
+ def call
14
+ listed = CommandTower::Services::Messaging::Gallery::ListGalleryTypes.call
15
+ return failure(errors: listed.errors, http_status: :internal_server_error) unless listed.success?
16
+
17
+ success(
18
+ payload: CommandTower::Serializers::Admin::Messaging::GallerySerializer.serialize_list(
19
+ listed.data[:types]
20
+ ),
21
+ http_status: :ok
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Workflows
5
+ module Admin
6
+ module Messaging
7
+ module Gallery
8
+ # Previews one fixture of one defined notification type across every allowed channel.
9
+ # Authorization is the controller's RBAC boundary (entity `admin_messaging_gallery`).
10
+ class ShowWorkflow < CommandTower::Workflows::ApplicationWorkflow
11
+ retry_strategy :none
12
+
13
+ def call(notification_type_key:, fixture: nil, zone: nil)
14
+ rendered = CommandTower::Services::Messaging::Gallery::RenderGalleryPreview.call(
15
+ notification_type_key:, fixture:, zone:
16
+ )
17
+ return rendered_failure(rendered) unless rendered.success?
18
+
19
+ success(
20
+ payload: CommandTower::Serializers::Admin::Messaging::GallerySerializer.serialize_preview(
21
+ rendered.data[:preview]
22
+ ),
23
+ http_status: :ok
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ def rendered_failure(rendered)
30
+ failure(
31
+ errors: rendered.errors,
32
+ http_status: CommandTower::Workflows::Messaging::ErrorMapping.http_status_for(rendered.errors.first)
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Workflows
5
+ module Admin
6
+ module Messaging
7
+ module Gallery
8
+ # Sends one gallery fixture as an email to the real actor's own address, through the
9
+ # existing adapter boundary (`SelectAdapter`: the fake adapter when enabled, else the
10
+ # configured email adapter). Authorization is the controller's RBAC boundary (entity
11
+ # `admin_messaging_gallery_test_send`).
12
+ #
13
+ # - Refused with `gallery_test_send_impersonating` (403) while impersonating or when the
14
+ # actor is not the effective user.
15
+ # - Rate limited per real actor (see `CheckTestSendRateLimit`); a Redis error fails closed.
16
+ # - Creates no `Communication`. Audits `messaging_gallery_test_sent` with ids and the type
17
+ # key only, never the address.
18
+ class TestSendWorkflow < CommandTower::Workflows::ApplicationWorkflow
19
+ retry_strategy :none
20
+
21
+ Delivery = Data.define(:channel_key)
22
+
23
+ def call(actor:, effective_user:, notification_type_key:, fixture: nil, zone: nil)
24
+ return refuse_impersonation if impersonating?(actor, effective_user)
25
+
26
+ built = CommandTower::Services::Messaging::Gallery::TestSend.call(
27
+ notification_type_key:, fixture:, zone:, actor:
28
+ )
29
+ return failed(built) unless built.success?
30
+
31
+ limit = CommandTower::Services::Messaging::Gallery::CheckTestSendRateLimit.call(actor_id: actor.id)
32
+ return failed(limit) unless limit.success?
33
+
34
+ send_and_audit(built.data, actor:, notification_type_key:, fixture:)
35
+ end
36
+
37
+ private
38
+
39
+ def send_and_audit(data, actor:, notification_type_key:, fixture:)
40
+ outcome = deliver(data)
41
+ return failed_delivery(outcome) unless outcome.success?
42
+
43
+ audit_sent(actor:, notification_type_key:, fixture:)
44
+ success(
45
+ payload: { "notificationTypeKey" => notification_type_key, "sent" => true }, http_status: :accepted
46
+ )
47
+ end
48
+
49
+ def impersonating?(actor, effective_user)
50
+ CommandTower::Current.impersonation_active == true || actor.id != effective_user.id
51
+ end
52
+
53
+ def refuse_impersonation
54
+ failure(
55
+ errors: [CommandTower::Errors::Messaging::GalleryTestSendImpersonatingError.new],
56
+ http_status: :forbidden
57
+ )
58
+ end
59
+
60
+ def deliver(data)
61
+ delivery = Delivery.new(channel_key: "email")
62
+ CommandTower::Messaging::Execution::SelectAdapter.call(delivery:).call(request: adapter_request(data))
63
+ end
64
+
65
+ def adapter_request(data)
66
+ content = data[:content]
67
+ rendered = CommandTower::Messaging::Rendering::RenderedPayload.build(
68
+ recipient_address: data[:recipient], subject: content[:subject],
69
+ text_body: content[:text], html_body: content[:html]
70
+ )
71
+ id = "gallery-test-#{SecureRandom.uuid}"
72
+ CommandTower::Messaging::Execution::AdapterRequest.build(
73
+ channel_delivery_id: id, communication_id: id, channel_key: "email", attempt_id: id, rendered:
74
+ )
75
+ end
76
+
77
+ def audit_sent(actor:, notification_type_key:, fixture:)
78
+ audit(
79
+ :messaging_gallery_test_sent,
80
+ affected_user: actor,
81
+ changes: {},
82
+ metadata: { actor_id: actor.id, notification_type_key:, fixture: }.compact,
83
+ attribution_mode: :self_service
84
+ )
85
+ end
86
+
87
+ def failed(result)
88
+ failure(
89
+ errors: result.errors,
90
+ http_status: CommandTower::Workflows::Messaging::ErrorMapping.http_status_for(result.errors.first)
91
+ )
92
+ end
93
+
94
+ def failed_delivery(outcome)
95
+ error = CommandTower::Errors::Messaging::GalleryTestSendFailedError.new(error_code: outcome.error_code)
96
+ failure(errors: [error], http_status: :bad_gateway)
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -12,9 +12,9 @@ module CommandTower
12
12
  user_id:,
13
13
  notification_type_key:,
14
14
  campaign_identity:,
15
- title:,
16
- body:,
17
15
  platform_enabled_channels:,
16
+ title: nil,
17
+ body: nil,
18
18
  metadata: nil,
19
19
  conversation_key: nil,
20
20
  conversation_title: nil
@@ -10,12 +10,19 @@ module CommandTower
10
10
  case error
11
11
  when CommandTower::Errors::ValidationError,
12
12
  CommandTower::Errors::Messaging::RecipientUnresolvedError,
13
- CommandTower::Errors::Messaging::AcceptRejectedError
13
+ CommandTower::Errors::Messaging::AcceptRejectedError,
14
+ CommandTower::Errors::Messaging::AnnouncementTypeNotSupportedError
14
15
  :unprocessable_entity
15
16
  when CommandTower::Errors::Messaging::IdempotencyConflictError
16
17
  :conflict
17
18
  when CommandTower::Errors::NotFoundError
18
19
  :not_found
20
+ when CommandTower::Errors::Messaging::GalleryTestSendImpersonatingError
21
+ :forbidden
22
+ when CommandTower::Errors::Messaging::GalleryTestSendRateLimitError
23
+ :too_many_requests
24
+ when CommandTower::Errors::Messaging::GalleryTestSendFailedError
25
+ :bad_gateway
19
26
  else
20
27
  :internal_server_error
21
28
  end
@@ -33,9 +33,11 @@ module CommandTower
33
33
  end
34
34
 
35
35
  class ConversationWorkflow < BaseWorkflow
36
- def call(user:, key:, scope:, around: nil, before_id: nil, after_id: nil, limit: nil)
36
+ # `impersonating` comes from the authenticated boundary; nil (unknown) redacts.
37
+ def call(user:, key:, scope:, around: nil, before_id: nil, after_id: nil, limit: nil, impersonating: nil)
37
38
  result = CommandTower::Services::Messaging::Inbox::Conversation.call(
38
39
  user:, key:, scope:, around:, before_id:, after_id:, limit:,
40
+ ui_catalog: CommandTower::Current.ui_catalog, impersonating:,
39
41
  )
40
42
  return result_or_failure(result) unless result.success?
41
43
 
@@ -47,8 +49,11 @@ module CommandTower
47
49
  end
48
50
 
49
51
  class ShowWorkflow < BaseWorkflow
50
- def call(user:, inbox_item_id:)
51
- result = CommandTower::Services::Messaging::Inbox::Show.call(user:, inbox_item_id:)
52
+ # `impersonating` comes from the authenticated boundary; nil (unknown) redacts.
53
+ def call(user:, inbox_item_id:, impersonating: nil)
54
+ result = CommandTower::Services::Messaging::Inbox::Show.call(
55
+ user:, inbox_item_id:, ui_catalog: CommandTower::Current.ui_catalog, impersonating:
56
+ )
52
57
  return result_or_failure(result) unless result.success?
53
58
 
54
59
  success(payload: CommandTower::Serializers::Messaging::Inbox::DetailSerializer.serialize(result.data[:item]), http_status: :ok)
@@ -56,12 +61,32 @@ module CommandTower
56
61
  end
57
62
 
58
63
  class OpenWorkflow < ShowWorkflow
59
- def call(user:, inbox_item_id:)
60
- result = CommandTower::Services::Messaging::Inbox::Open.call(user:, inbox_item_id:)
64
+ def call(user:, inbox_item_id:, impersonating: nil)
65
+ impersonated = CommandTower::Current.impersonation_active == true
66
+ result = CommandTower::Services::Messaging::Inbox::Open.call(
67
+ user:, inbox_item_id:, mark_viewed: !impersonated,
68
+ ui_catalog: CommandTower::Current.ui_catalog, impersonating:
69
+ )
61
70
  return result_or_failure(result) unless result.success?
62
71
 
72
+ audit_first_view(user:, inbox_item_id:, result:) unless impersonated
73
+
63
74
  success(payload: CommandTower::Serializers::Messaging::Inbox::DetailSerializer.serialize(result.data[:item]), http_status: :ok)
64
75
  end
76
+
77
+ private
78
+
79
+ # One event per first view, for every inbox type. Metadata carries ids only.
80
+ def audit_first_view(user:, inbox_item_id:, result:)
81
+ return unless result.data[:newly_viewed]
82
+
83
+ audit(
84
+ :messaging_inbox_item_viewed,
85
+ affected_user: user,
86
+ changes: {},
87
+ metadata: { inbox_item_id:, communication_id: result.data[:communication_id] }
88
+ )
89
+ end
65
90
  end
66
91
 
67
92
  class ArchiveWorkflow < BaseWorkflow
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Workflows
5
+ module Messaging
6
+ module Unsubscribe
7
+ # RFC 8058 one-click unsubscribe: verify the signed token, turn email off for that user and
8
+ # notification type, audit the first opt-out.
9
+ #
10
+ # Authorization sequence: none, by declaration. The caller is a mail client with no session;
11
+ # the signed, purpose-bound, expiring token is the only credential and it names the one user
12
+ # and one type it may change. Every failure is the same 422 body so the endpoint is not an oracle.
13
+ class OneClickWorkflow < CommandTower::Workflows::ApplicationWorkflow
14
+ retry_strategy :none
15
+
16
+ Unsubscribes = CommandTower::Services::Messaging::Unsubscribe
17
+
18
+ def call(token:, client_ip:)
19
+ limited = Unsubscribes::CheckRateLimit.call(token:, client_ip:)
20
+ return failure(errors: limited.errors, http_status: :too_many_requests) if limited.failure?
21
+
22
+ verified = Unsubscribes::VerifyToken.call(token:)
23
+ return failure(errors: verified.errors, http_status: :unprocessable_entity) if verified.failure?
24
+
25
+ opt_out_and_audit(verified.data)
26
+ end
27
+
28
+ private
29
+
30
+ def opt_out_and_audit(identity)
31
+ opt_out = Unsubscribes::OptOutOfEmail.call(**identity.slice(:user_id, :notification_type_key))
32
+ return failure(errors: opt_out.errors, http_status: status_for(opt_out.errors.first)) if opt_out.failure?
33
+
34
+ audit_opt_out(opt_out.data[:user], identity[:notification_type_key]) if opt_out.data[:changed]
35
+ success(payload: { unsubscribed: true }, http_status: :ok)
36
+ end
37
+
38
+ def status_for(error)
39
+ error.is_a?(CommandTower::Errors::ValidationError) ? :unprocessable_entity : :internal_server_error
40
+ end
41
+
42
+ def audit_opt_out(user, notification_type_key)
43
+ audit(
44
+ :messaging_unsubscribed,
45
+ subject: user,
46
+ affected_user: user,
47
+ attribution_mode: :system,
48
+ changes: { email_enabled: { from: true, to: false } },
49
+ metadata: { notification_type_key: }
50
+ )
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Workflows
5
+ module Messaging
6
+ module Unsubscribe
7
+ # GET on the unsubscribe URL (a person clicking the link, or a scanner prefetching it). It
8
+ # never reads or verifies the token and never mutates: it only names where the preferences
9
+ # page is, so the Location carries no token and no user id.
10
+ #
11
+ # Authorization sequence: none, by declaration. The result is the same public URL for everyone.
12
+ class PreferencesRedirectWorkflow < CommandTower::Workflows::ApplicationWorkflow
13
+ retry_strategy :none
14
+
15
+ def call
16
+ base = CommandTower.config.application.composed_url.to_s
17
+ path = CommandTower.config.messaging.compliance.preferences_path.to_s
18
+ success(payload: { location: "#{base}#{path}" }, http_status: :see_other)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
data/config/routes.rb CHANGED
@@ -55,6 +55,12 @@ CommandTower::Engine.routes.draw do
55
55
  end
56
56
  end
57
57
 
58
+ # Unauthenticated one-click unsubscribe (RFC 8058). POST acts; GET only redirects to preferences.
59
+ namespace :messaging do
60
+ get "unsubscribe", to: "unsubscribe#show"
61
+ post "unsubscribe", to: "unsubscribe#create"
62
+ end
63
+
58
64
  get "me", to: "me#show"
59
65
  get "profile", to: "profile#show"
60
66
  namespace :me do
@@ -134,6 +140,9 @@ CommandTower::Engine.routes.draw do
134
140
 
135
141
  namespace :messaging do
136
142
  post "announcements", to: "announcements#create"
143
+ get "gallery", to: "gallery#index"
144
+ get "gallery/:notification_type_key", to: "gallery#show"
145
+ post "gallery/:notification_type_key/test-send", to: "gallery_test_sends#create"
137
146
  end
138
147
  end
139
148
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddMessageDefinitionColumnsToMessagingCommunications < ActiveRecord::Migration[7.2]
4
+ def change
5
+ add_column :messaging_communications, :slots, :json
6
+ add_column :messaging_communications, :render_zone, :string, limit: 64
7
+ add_column :messaging_communications, :metadata_version, :integer
8
+ end
9
+ end
@@ -34,7 +34,7 @@ Lookup: `CommandTower.config.registry.admin_workspace.fetch(:audit)`.
34
34
  | group | required segment token; **open** set |
35
35
  | sort_order | required Integer |
36
36
  | required_entity | required RBAC entity name; must exist in the composed graph after `Authorization.default_defined!` |
37
- | icon | optional segment token or nil |
37
+ | icon | optional lowercase token (letters, digits, `_`, `-`; for example `layout-template`) or nil |
38
38
 
39
39
  | `scope_required` | optional Boolean (default false); when true, tool APIs require host scope transport |
40
40
  | `scope_parameter` | required when `scope_required`; query param key (e.g. `partition`) |
@@ -111,6 +111,7 @@ These routes are **frontend navigation targets**. Shared Admin shell consumes th
111
111
  | `users` | Users | Find and inspect platform user accounts. | `/admin/users` | `operations` | 50 | `admin_users` | `users` |
112
112
  | `audit` | Audit | Browse account and administrative audit history. | `/admin/audit` | `operations` | 100 | `admin_audit_events` | `history` |
113
113
  | `messaging` | Messaging | Manage platform announcements and administrative messaging. | `/admin/messaging` | `messaging` | 200 | `admin_messaging_announcements` | `megaphone` |
114
+ | `messaging_gallery` | Message gallery | Preview every channel for each message type. | `/admin/messaging/gallery` | `messaging` | 210 | `admin_messaging_gallery` | `layout-template` |
114
115
 
115
116
  RBAC CRUD, health, and Pick'em product tools are **not** seeded. Impersonation is a session primitive (`POST /admin/users/:id/impersonation-sessions`), not an Admin Workspace tool.
116
117