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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 975edf68bda972a251cc1049d1c7fe3046f98cc3c2407cd2a9389742bff78f56
4
- data.tar.gz: ad2fa525b22b80ab6a3d6ca061831cf301e59d849367ae3ef017845ce0112944
3
+ metadata.gz: 42e2598ba53291eb5392477e6bd78d1c8129b849f21682b8d3c2575afd98ae8d
4
+ data.tar.gz: 112e103cd27edb7bbcf923161a23760ba34a994aa771b64380bdd08caaad4e10
5
5
  SHA512:
6
- metadata.gz: 4eb44a6051918263a36b5a69fff6d9fa2d63a99e138d47a2c5a376eef6f167aff82d574eba032fa5f29a075482b846f781398afaeafe21359783780f49ab7613
7
- data.tar.gz: '00880332ea7d67fb1b6f6f96f95304335f690df9d609416e0f8ca52338a6993296c442da936b83e270307d557b472da3b67b536c5c9739dc5bbab3d95e3dbcaf'
6
+ metadata.gz: 17df018dc3daf30e315dbe95b80a0115a8171386059ec6ce0feb9bd3c8c10794574e9b14e38c9393e586ea5525e25a5ca8ecefdc8d7e8c06173fbf9aacb9e2b6
7
+ data.tar.gz: c6e9a0567b1f5ba0642590ce2b2b01468ffea8cf79cd67e5bb1c480ba0f68d4768c6a2e198b2770ac2384c9f9504a8736d23a09fb551a02f227a2b5bd4b05f40
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Admin
5
+ module Messaging
6
+ # Admin message gallery: previews every channel of each defined notification type from its
7
+ # declared fixtures. Index and show need the RBAC entity `admin_messaging_gallery`. The test
8
+ # send (slice 13) lives in `GalleryTestSendsController` under its own entity. Responses are
9
+ # `no-store`.
10
+ class GalleryController < CommandTower::Admin::ApplicationController
11
+ prepend_before_action :set_no_store
12
+
13
+ def index
14
+ render_application_result(CommandTower::Workflows::Admin::Messaging::Gallery::IndexWorkflow.call)
15
+ end
16
+
17
+ def show
18
+ render_application_result(
19
+ CommandTower::Workflows::Admin::Messaging::Gallery::ShowWorkflow.call(
20
+ notification_type_key: params[:notification_type_key].to_s,
21
+ fixture: params[:fixture].presence&.to_s,
22
+ zone: params[:zone].presence&.to_s
23
+ )
24
+ )
25
+ end
26
+
27
+ private
28
+
29
+ def set_no_store
30
+ response.set_header("Cache-Control", "no-store")
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Admin
5
+ module Messaging
6
+ # Gallery test send: emails one fixture to the real actor's own address. It has its own
7
+ # controller because the role check requires every entity mapped to a controller to approve
8
+ # each of its actions, so it cannot share `GalleryController` with `admin_messaging_gallery`.
9
+ #
10
+ # Impersonation is refused here with `gallery_test_send_impersonating` (403), ahead of
11
+ # authorization (which would judge the impersonated user), not with the generic admin 418.
12
+ class GalleryTestSendsController < CommandTower::Admin::ApplicationController
13
+ skip_before_action :reject_admin_operations_during_impersonation!
14
+ skip_before_action :authorize_request!
15
+ before_action :refuse_impersonation!
16
+ before_action :authorize_request!
17
+ prepend_before_action :set_no_store
18
+
19
+ def create
20
+ render_application_result(
21
+ CommandTower::Workflows::Admin::Messaging::Gallery::TestSendWorkflow.call(
22
+ actor: current_auth_context.actor_user,
23
+ effective_user: current_user,
24
+ notification_type_key: params[:notification_type_key].to_s,
25
+ fixture: params[:fixture].presence&.to_s,
26
+ zone: params[:zone].presence&.to_s
27
+ )
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def refuse_impersonation!
34
+ return unless impersonating_or_not_actor?
35
+
36
+ render_application_result(
37
+ CommandTower::Workflows::WorkflowResult.failure(
38
+ errors: [CommandTower::Errors::Messaging::GalleryTestSendImpersonatingError.new],
39
+ http_status: :forbidden
40
+ )
41
+ )
42
+ end
43
+
44
+ def impersonating_or_not_actor?
45
+ CommandTower::Current.impersonation_active || current_auth_context.actor_user&.id != current_user.id
46
+ end
47
+
48
+ def set_no_store
49
+ response.set_header("Cache-Control", "no-store")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -8,6 +8,8 @@ module CommandTower
8
8
 
9
9
  before_action :authenticate_request!
10
10
  before_action :authorize_request!
11
+ # Inbox reads can carry per-recipient, impersonation-redacted content: never cache them.
12
+ before_action :set_no_store, only: %i[index show open conversation]
11
13
 
12
14
  def index
13
15
  deserialized = CommandTower::Deserializers::Messaging::Inbox::ListDeserializer.call(params)
@@ -31,15 +33,16 @@ module CommandTower
31
33
  before_id: input.before_id,
32
34
  after_id: input.after_id,
33
35
  limit: input.limit,
36
+ impersonating: impersonating_flag,
34
37
  ))
35
38
  end
36
39
 
37
40
  def show
38
- run_item_workflow(CommandTower::Workflows::Messaging::Inbox::ShowWorkflow)
41
+ run_item_workflow(CommandTower::Workflows::Messaging::Inbox::ShowWorkflow, impersonating: impersonating_flag)
39
42
  end
40
43
 
41
44
  def open
42
- run_item_workflow(CommandTower::Workflows::Messaging::Inbox::OpenWorkflow)
45
+ run_item_workflow(CommandTower::Workflows::Messaging::Inbox::OpenWorkflow, impersonating: impersonating_flag)
43
46
  end
44
47
 
45
48
  def archive
@@ -76,11 +79,23 @@ module CommandTower
76
79
 
77
80
  private
78
81
 
79
- def run_item_workflow(workflow)
82
+ def run_item_workflow(workflow, **extra)
80
83
  deserialized = CommandTower::Deserializers::Messaging::Inbox::ShowDeserializer.call(params)
81
84
  return render_deserializer_errors unless deserialized.success?
82
85
 
83
- render_application_result(workflow.call(user: current_user, inbox_item_id: deserialized.input.inbox_item_id))
86
+ render_application_result(workflow.call(user: current_user, inbox_item_id: deserialized.input.inbox_item_id, **extra))
87
+ end
88
+
89
+ def set_no_store
90
+ response.headers["Cache-Control"] = "no-store"
91
+ end
92
+
93
+ # From the authentication result: true while impersonating, false for a normal session,
94
+ # nil when there is no auth context (unknown, which redacts).
95
+ def impersonating_flag
96
+ return nil if current_auth_context.nil?
97
+
98
+ CommandTower::Current.impersonation_active == true
84
99
  end
85
100
 
86
101
  def run_bulk_workflow(workflow)
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Messaging
5
+ # One-click unsubscribe (RFC 8058). Unauthenticated by design: the caller is a mail client.
6
+ #
7
+ # - It never authenticates and never reads the session or cookies. `ActionController::API` has no
8
+ # cookie CSRF protection, and `authenticate_user!` (the only CSRF check) is not called, so a
9
+ # cookie-bearing request is treated like any other.
10
+ # - The only credential is the signed token in the query string (`token` is a filtered parameter).
11
+ # - Responses are `no-store` with `Referrer-Policy: no-referrer`, so the token is not cached or
12
+ # leaked in a Referer header.
13
+ # - Rate limits key mainly on the token digest. The per-IP limit uses `request.remote_ip`, which
14
+ # follows `X-Forwarded-For` only behind the host's `trusted_proxies`.
15
+ # - `show` (GET) is a documented transport non-envelope: a 303 redirect to the preferences page,
16
+ # since a person's browser follows it. It never mutates.
17
+ class UnsubscribeController < CommandTower::ApplicationController
18
+ include CommandTower::Api::ApplicationResponseRenderer
19
+
20
+ before_action :set_privacy_headers
21
+
22
+ def show
23
+ result = CommandTower::Workflows::Messaging::Unsubscribe::PreferencesRedirectWorkflow.call
24
+ redirect_to result.payload[:location], status: :see_other, allow_other_host: true
25
+ end
26
+
27
+ def create
28
+ result = CommandTower::Workflows::Messaging::Unsubscribe::OneClickWorkflow.call(
29
+ token: params[:token].to_s,
30
+ client_ip: CommandTower::Services::Auth::ClientIpResolver.call(request:)
31
+ )
32
+ render_application_result(result)
33
+ end
34
+
35
+ private
36
+
37
+ def set_privacy_headers
38
+ response.set_header("Referrer-Policy", "no-referrer")
39
+ response.set_header("Cache-Control", "no-store")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -5,6 +5,8 @@ module CommandTower
5
5
  module HttpBoundary
6
6
  extend ActiveSupport::Concern
7
7
 
8
+ CLIENT_TIME_ZONE_MAX_LENGTH = 64
9
+
8
10
  included do
9
11
  around_action :command_tower_http_execution
10
12
  end
@@ -19,13 +21,46 @@ module CommandTower
19
21
  correlation_id: request_id,
20
22
  remote_ip: request.remote_ip,
21
23
  user_agent: request.user_agent,
22
- ui_catalog: CommandTower::UI::Catalog.from_header(request.headers["X-UI-Catalog"])
24
+ ui_catalog: CommandTower::UI::Catalog.from_header(request.headers["X-UI-Catalog"]),
25
+ client_time_zone: parse_client_time_zone(request.headers["X-Time-Zone"])
23
26
  ) do
24
27
  yield
25
28
  record_impersonation_activity_if_needed
29
+ record_client_time_zone_if_needed
26
30
  end
27
31
  end
28
32
 
33
+ # A TZInfo identifier of at most 64 characters, else nil (the header is ignored).
34
+ def parse_client_time_zone(raw)
35
+ zone = raw.to_s.strip
36
+ return nil if zone.empty? || zone.length > CLIENT_TIME_ZONE_MAX_LENGTH
37
+
38
+ zone if TZInfo::Timezone.all_identifiers.include?(zone)
39
+ end
40
+
41
+ # Request bookkeeping that runs outside a workflow, like impersonation activity above: it is
42
+ # a throttled side write on the authenticated user, not a business operation, and it must
43
+ # never change the response.
44
+ def record_client_time_zone_if_needed
45
+ zone = CommandTower::Current.client_time_zone
46
+ return if zone.blank? || !response.status.to_i.between?(200, 299)
47
+
48
+ user = time_zone_recordable_user
49
+ CommandTower::Services::Users::RecordLastKnownTimeZone.call(user:, zone:) if user
50
+ rescue StandardError
51
+ nil
52
+ end
53
+
54
+ # The real authenticated actor, taken from the authentication result (there is no
55
+ # Current.user). Impersonated requests return nil so an admin's zone never lands on the target.
56
+ def time_zone_recordable_user
57
+ return if CommandTower::Current.impersonation_active
58
+
59
+ auth_context = try(:current_auth_context)
60
+ user = auth_context&.user
61
+ user if user && auth_context.actor_user&.id == user.id
62
+ end
63
+
29
64
  def record_impersonation_activity_if_needed
30
65
  return unless CommandTower::Current.impersonation_active
31
66
  return unless CommandTower::Current.impersonation_activity_recorded
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Errors
5
+ module Messaging
6
+ # The admin announcement endpoint targeted a type whose MessageDefinition is not an
7
+ # AnnouncementDefinition (design D26).
8
+ class AnnouncementTypeNotSupportedError < CommandTower::Errors::ApplicationError
9
+ def code
10
+ "announcement_type_not_supported"
11
+ end
12
+
13
+ def message
14
+ "This notification type cannot be used for announcements"
15
+ end
16
+
17
+ def log_level
18
+ :warn
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Errors
5
+ module Messaging
6
+ # The email adapter did not accept the test message. Carries the adapter's error code only.
7
+ class GalleryTestSendFailedError < CommandTower::Errors::ApplicationError
8
+ def initialize(error_code: nil)
9
+ super(details: error_code ? { error_code: error_code } : nil)
10
+ end
11
+
12
+ def code
13
+ "gallery_test_send_failed"
14
+ end
15
+
16
+ def message
17
+ "The test message could not be sent."
18
+ end
19
+
20
+ def log_level
21
+ :warn
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Errors
5
+ module Messaging
6
+ # The gallery test send goes to the real actor's own address, so it is refused while
7
+ # impersonating or whenever the actor is not the effective user.
8
+ class GalleryTestSendImpersonatingError < CommandTower::Errors::ApplicationError
9
+ def code
10
+ "gallery_test_send_impersonating"
11
+ end
12
+
13
+ def message
14
+ "The gallery test send 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,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Errors
5
+ module Messaging
6
+ class GalleryTestSendRateLimitError < CommandTower::Errors::ApplicationError
7
+ def initialize(retry_after_seconds: nil)
8
+ super(details: retry_after_seconds ? { retry_after_seconds: retry_after_seconds } : nil)
9
+ end
10
+
11
+ def code
12
+ "gallery_test_send_rate_limited"
13
+ end
14
+
15
+ def message
16
+ "Too many gallery test sends. Please try again later."
17
+ end
18
+
19
+ def log_level
20
+ :warn
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Errors
5
+ module Messaging
6
+ class UnsubscribeRateLimitError < CommandTower::Errors::ApplicationError
7
+ def initialize(retry_after_seconds: nil)
8
+ super(details: retry_after_seconds ? { retry_after_seconds: retry_after_seconds } : nil)
9
+ end
10
+
11
+ def code
12
+ "unsubscribe_rate_limited"
13
+ end
14
+
15
+ def message
16
+ "Too many unsubscribe requests"
17
+ end
18
+
19
+ def log_level
20
+ :warn
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -12,8 +12,8 @@ module CommandTower
12
12
  user_id:,
13
13
  notification_type_key: attrs.fetch("notification_type_key"),
14
14
  campaign_identity: attrs.fetch("campaign_identity"),
15
- title: attrs.fetch("title"),
16
- body: attrs.fetch("body"),
15
+ title: attrs["title"],
16
+ body: attrs["body"],
17
17
  platform_enabled_channels: Array(attrs["platform_enabled_channels"]),
18
18
  metadata: attrs["metadata"],
19
19
  conversation_key: attrs["conversation_key"],
@@ -10,6 +10,8 @@ module CommandTower
10
10
 
11
11
  from_address = smtp_from_address
12
12
 
13
+ headers(rendered.headers)
14
+
13
15
  mail(
14
16
  to: rendered.recipient_address,
15
17
  from: from_address,
@@ -40,6 +40,10 @@ module CommandTower
40
40
  # writes one JSON document on MySQL and on MariaDB.
41
41
  attribute :inbox_presentation_snapshot, :json
42
42
 
43
+ # Definition-backed slot values. Same MariaDB JSON caster rationale as
44
+ # the snapshot above. NULL for types without a MessageDefinition.
45
+ attribute :slots, :json
46
+
43
47
  CONVERSATION_KEY_FORMAT = /\A[A-Za-z0-9][A-Za-z0-9._:-]{0,127}\z/
44
48
 
45
49
  validates :notification_type_key, :title, :body, presence: true
@@ -61,6 +65,12 @@ module CommandTower
61
65
  )
62
66
  end
63
67
 
68
+ # Reads the stored row only, never the registry: a type can lose its
69
+ # definition after the row was written.
70
+ def message_definition_backed?
71
+ !metadata_version.nil?
72
+ end
73
+
64
74
  def handoff_terminal?
65
75
  HANDOFF_TERMINAL_STATUSES.include?(execution_handoff_status)
66
76
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CommandTower
4
+ module Serializers
5
+ module Admin
6
+ module Messaging
7
+ # Wire form of the admin message gallery (design 3.5). Keys are camelCase; channels the type
8
+ # does not allow stay `null`.
9
+ module GallerySerializer
10
+ module_function
11
+
12
+ def serialize_list(types)
13
+ types.map { |type| list_item(type) }
14
+ end
15
+
16
+ def list_item(type)
17
+ {
18
+ "notificationTypeKey" => type[:notification_type_key], "label" => type[:label],
19
+ "definition" => type[:definition], "allowedChannels" => type[:allowed_channels],
20
+ "promotional" => type[:promotional],
21
+ "fixtures" => type[:fixtures].map do |fixture|
22
+ { "name" => fixture[:name], "metadataVersion" => fixture[:metadata_version] }
23
+ end
24
+ }
25
+ end
26
+
27
+ def serialize_preview(preview)
28
+ {
29
+ "notificationTypeKey" => preview[:notification_type_key],
30
+ "fixture" => preview[:fixture],
31
+ "metadataVersion" => preview[:metadata_version],
32
+ "renderZone" => preview[:render_zone],
33
+ "slots" => slots(preview[:slots]),
34
+ "channels" => channels(preview[:channels]),
35
+ "facts" => { "app" => preview.dig(:facts, :app), "emailHtml" => preview.dig(:facts, :email_html) },
36
+ "checks" => preview[:checks].map { |check| check.transform_keys { |key| camelize(key) } }
37
+ }
38
+ end
39
+
40
+ def slots(slots)
41
+ {
42
+ "headline" => slots[:headline], "subject" => slots[:subject], "pushBody" => slots[:push_body],
43
+ "smsBody" => slots[:sms_body], "pushoverTitle" => slots[:pushover_title],
44
+ "pushoverMessage" => slots[:pushover_message]
45
+ }
46
+ end
47
+
48
+ def channels(channels)
49
+ wire = %i[push sms pushover inbox].to_h { |name| [name.to_s, channels[name]&.transform_keys(&:to_s)] }
50
+ wire.merge("email" => email(channels[:email]))
51
+ end
52
+
53
+ def email(email)
54
+ return nil unless email
55
+
56
+ { "subject" => email[:subject], "html" => email[:html], "text" => email[:text],
57
+ "usedFallback" => email[:used_fallback] }
58
+ end
59
+
60
+ def camelize(key)
61
+ key.to_s.camelize(:lower)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -25,20 +25,21 @@ module CommandTower
25
25
  end
26
26
 
27
27
  def resolve
28
- theme = CommandTower.config.email_theme
29
- {
30
- canvas_background: theme.canvas_background,
31
- surface_background: theme.surface_background,
32
- surface_border: theme.surface_border,
33
- primary_text: theme.primary_text,
34
- body_text: theme.body_text,
35
- muted_text: theme.muted_text,
36
- accent: theme.accent,
37
- text_on_accent: theme.text_on_accent,
38
- primary_action: theme.primary_action,
28
+ colors.merge(
39
29
  product_name: CommandTower.app_name_for_comms,
40
30
  product_url: CommandTower.config.application.composed_url
41
- }.freeze
31
+ ).freeze
32
+ end
33
+
34
+ private
35
+
36
+ # Derived from the host token file only when it exists (design D19);
37
+ # otherwise exactly today's config.email_theme values.
38
+ def colors
39
+ return Palette.legacy_keys_from(Palette.resolve) if TokenFile.host_file?
40
+
41
+ theme = CommandTower.config.email_theme
42
+ Palette::LEGACY_TO_PALETTE.keys.index_with { |key| theme.public_send(key) }
42
43
  end
43
44
  end
44
45
  end
@@ -52,7 +52,8 @@ module CommandTower
52
52
  }
53
53
 
54
54
  OperationLogger.around(request:) do
55
- validate_request!
55
+ definition = definition_class
56
+ validate_request!(defined_type: !definition.nil?)
56
57
  fingerprint = RequestNormalizer.fingerprint(
57
58
  title: @title,
58
59
  body: @body,
@@ -60,6 +61,7 @@ module CommandTower
60
61
  preference_state: @preference_state,
61
62
  platform_enabled_channels: @platform_enabled_channels,
62
63
  message_overrides: @message_overrides,
64
+ metadata_version: definition&.metadata_version,
63
65
  )
64
66
 
65
67
  existing = find_by_namespace
@@ -67,6 +69,7 @@ module CommandTower
67
69
  return resolve_existing(existing, fingerprint)
68
70
  end
69
71
 
72
+ request = with_definition_fields(request, definition) if definition
70
73
  plan = plan!
71
74
  persist_new!(request:, plan:, fingerprint:)
72
75
  end
@@ -74,13 +77,63 @@ module CommandTower
74
77
 
75
78
  private
76
79
 
77
- def validate_request!
80
+ # The MessageDefinition class for a registered, definition-backed type; nil for undefined
81
+ # and unregistered types (the planner reports the latter).
82
+ def definition_class
83
+ return nil unless NotificationTypes.registered?(@notification_type_key)
84
+
85
+ message = NotificationTypes.lookup(@notification_type_key).message
86
+ klass = message&.safe_constantize
87
+ klass if klass.is_a?(Class) && klass < MessageDefinition
88
+ end
89
+
90
+ # Stored title/body/slots/zone/version come from the definition. Metadata violations fail
91
+ # the request in strict mode and are logged and degraded otherwise.
92
+ def with_definition_fields(request, definition)
93
+ resolution = ResolveCommunicationSlots.call(
94
+ notification_type_key: @notification_type_key,
95
+ definition:,
96
+ metadata: @metadata,
97
+ recipient_id: @recipient_id,
98
+ )
99
+ handle_resolution_failure!(resolution) if resolution.failure?
100
+
101
+ request.merge(
102
+ title: resolution.title,
103
+ body: resolution.body,
104
+ slots: resolution.slots,
105
+ render_zone: resolution.render_zone,
106
+ metadata_version: resolution.metadata_version,
107
+ )
108
+ end
109
+
110
+ def handle_resolution_failure!(resolution)
111
+ unless resolution.degradable? && !CommandTower.config.messaging.strict_definitions
112
+ raise ValidationError, resolution.failure_message
113
+ end
114
+
115
+ Contract::Observability::Publisher.warn(
116
+ event: "messaging.accept.metadata_schema_violation",
117
+ notification_type_key: @notification_type_key,
118
+ recipient_id: @recipient_id,
119
+ attributes: resolution.schema_errors,
120
+ )
121
+ end
122
+
123
+ def validate_request!(defined_type: false)
78
124
  errors = []
79
125
  errors << "recipient_id is required" if blank?(@recipient_id)
80
126
  errors << "notification_type_key is required" if blank?(@notification_type_key)
81
127
  errors << "host_event_identity is required" if blank?(@host_event_identity)
82
- errors << "title is required" if blank?(@title)
83
- errors << "body is required" if blank?(@body)
128
+ if defined_type
129
+ # Defined types derive title and body from the definition; explicit copy is rejected in
130
+ # every environment (D17). Producers decide whether to fail or drop it before accepting.
131
+ errors << "title must not be supplied for a defined type" unless blank?(@title)
132
+ errors << "body must not be supplied for a defined type" unless blank?(@body)
133
+ else
134
+ errors << "title is required" if blank?(@title)
135
+ errors << "body is required" if blank?(@body)
136
+ end
84
137
  if @platform_enabled_channels.nil?
85
138
  errors << "platform_enabled_channels is required"
86
139
  end
@@ -28,6 +28,9 @@ module CommandTower
28
28
  title: @request.fetch(:title),
29
29
  body: @request.fetch(:body),
30
30
  metadata: freeze_metadata(@request[:metadata]),
31
+ slots: @request[:slots],
32
+ render_zone: @request[:render_zone],
33
+ metadata_version: @request[:metadata_version],
31
34
  conversation_key: @request[:conversation_key],
32
35
  conversation_title: @request[:conversation_title],
33
36
  )
@@ -21,8 +21,10 @@ module CommandTower
21
21
  metadata:,
22
22
  preference_state:,
23
23
  platform_enabled_channels:,
24
- message_overrides:
24
+ message_overrides:,
25
+ metadata_version: nil
25
26
  )
27
+ @metadata_version = metadata_version
26
28
  @title = title
27
29
  @body = body
28
30
  @metadata = metadata
@@ -41,15 +43,18 @@ module CommandTower
41
43
 
42
44
  private
43
45
 
46
+ # Defined types (a non-nil metadata_version) derive title and body from the definition, so
47
+ # they are not part of the basis; the version is, so a changed definition conflicts.
44
48
  def canonical_payload
45
- {
46
- "title" => @title.to_s,
47
- "body" => @body.to_s,
49
+ shared = {
48
50
  "metadata" => normalize_value(@metadata),
49
51
  "preference_state" => normalize_value(@preference_state),
50
52
  "platform_enabled_channels" => normalize_channel_list(@platform_enabled_channels),
51
53
  "message_overrides" => normalize_overrides(@message_overrides),
52
54
  }
55
+ return { "title" => @title.to_s, "body" => @body.to_s }.merge(shared) if @metadata_version.nil?
56
+
57
+ shared.merge("metadata_version" => @metadata_version)
53
58
  end
54
59
 
55
60
  def normalize_overrides(raw)