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.
- checksums.yaml +4 -4
- data/app/controllers/command_tower/admin/messaging/gallery_controller.rb +35 -0
- data/app/controllers/command_tower/admin/messaging/gallery_test_sends_controller.rb +54 -0
- data/app/controllers/command_tower/me/inbox_controller.rb +19 -4
- data/app/controllers/command_tower/messaging/unsubscribe_controller.rb +43 -0
- data/app/controllers/concerns/command_tower/execution/http_boundary.rb +36 -1
- data/app/errors/command_tower/errors/messaging/announcement_type_not_supported_error.rb +23 -0
- data/app/errors/command_tower/errors/messaging/gallery_test_send_failed_error.rb +26 -0
- data/app/errors/command_tower/errors/messaging/gallery_test_send_impersonating_error.rb +23 -0
- data/app/errors/command_tower/errors/messaging/gallery_test_send_rate_limit_error.rb +25 -0
- data/app/errors/command_tower/errors/messaging/unsubscribe_rate_limit_error.rb +25 -0
- data/app/jobs/command_tower/messaging/communications/produce_recipient_job.rb +2 -2
- data/app/mailers/command_tower/messaging/channel_mailer.rb +2 -0
- data/app/models/command_tower/messaging/communication.rb +10 -0
- data/app/serializers/command_tower/serializers/admin/messaging/gallery_serializer.rb +67 -0
- data/app/services/command_tower/email_theme/resolver.rb +13 -12
- data/app/services/command_tower/messaging/accept/coordinator.rb +57 -4
- data/app/services/command_tower/messaging/accept/persister.rb +3 -0
- data/app/services/command_tower/messaging/accept/request_normalizer.rb +9 -4
- data/app/services/command_tower/messaging/accept/resolve_communication_slots.rb +114 -0
- data/app/services/command_tower/messaging/announcement_definition.rb +88 -0
- data/app/services/command_tower/messaging/contract/inbox.rb +65 -0
- data/app/services/command_tower/messaging/contract/mappers/communication_mapper.rb +9 -0
- data/app/services/command_tower/messaging/contract/requests/mark_viewed_for_communication.rb +21 -0
- data/app/services/command_tower/messaging/contract/requests/viewed_for_communication.rb +21 -0
- data/app/services/command_tower/messaging/contract/results/communication_result.rb +3 -0
- data/app/services/command_tower/messaging/contract/results/inbox_view_mark.rb +18 -0
- data/app/services/command_tower/messaging/definitions/document_builder.rb +157 -0
- data/app/services/command_tower/messaging/definitions/render_context.rb +18 -0
- data/app/services/command_tower/messaging/inbox/item_result.rb +5 -1
- data/app/services/command_tower/messaging/inbox/mutator.rb +17 -0
- data/app/services/command_tower/messaging/inbox/reader.rb +12 -0
- data/app/services/command_tower/messaging/inbox/scope.rb +8 -0
- data/app/services/command_tower/messaging/inbox.rb +8 -0
- data/app/services/command_tower/messaging/message_definition/metadata.rb +35 -0
- data/app/services/command_tower/messaging/message_definition.rb +151 -0
- data/app/services/command_tower/messaging/notification_types/declaration.rb +12 -1
- data/app/services/command_tower/messaging/notification_types/message_definition_checks.rb +143 -0
- data/app/services/command_tower/messaging/notification_types/registry.rb +2 -0
- data/app/services/command_tower/messaging/rendering/channel_renderer.rb +73 -6
- data/app/services/command_tower/messaging/rendering/compliance_footer.rb +80 -0
- data/app/services/command_tower/messaging/rendering/defined_email_renderer.rb +141 -0
- data/app/services/command_tower/messaging/rendering/inbox_document_renderer.rb +79 -4
- data/app/services/command_tower/messaging/rendering/rendered_payload.rb +13 -1
- data/app/services/command_tower/messaging/rendering/unsubscribe_headers.rb +51 -0
- data/app/services/command_tower/messaging/unsubscribe/token_verifier.rb +26 -0
- data/app/services/command_tower/services/messaging/announcements/resolve_produce_arguments.rb +74 -0
- data/app/services/command_tower/services/messaging/communications/explicit_copy_rule.rb +50 -0
- data/app/services/command_tower/services/messaging/communications/produce.rb +18 -4
- data/app/services/command_tower/services/messaging/communications/produce_many.rb +20 -2
- data/app/services/command_tower/services/messaging/gallery/channel_failed.rb +31 -0
- data/app/services/command_tower/services/messaging/gallery/check_test_send_rate_limit.rb +47 -0
- data/app/services/command_tower/services/messaging/gallery/list_gallery_types.rb +44 -0
- data/app/services/command_tower/services/messaging/gallery/preview_builder.rb +122 -0
- data/app/services/command_tower/services/messaging/gallery/preview_channels.rb +51 -0
- data/app/services/command_tower/services/messaging/gallery/preview_checks.rb +85 -0
- data/app/services/command_tower/services/messaging/gallery/preview_email.rb +56 -0
- data/app/services/command_tower/services/messaging/gallery/render_gallery_preview.rb +85 -0
- data/app/services/command_tower/services/messaging/gallery/test_send.rb +55 -0
- data/app/services/command_tower/services/messaging/inbox.rb +36 -8
- data/app/services/command_tower/services/messaging/unsubscribe/check_rate_limit.rb +57 -0
- data/app/services/command_tower/services/messaging/unsubscribe/issue_token.rb +25 -0
- data/app/services/command_tower/services/messaging/unsubscribe/opt_out_of_email.rb +65 -0
- data/app/services/command_tower/services/messaging/unsubscribe/verify_token.rb +42 -0
- data/app/services/command_tower/services/users/record_last_known_time_zone.rb +40 -0
- data/app/workflows/command_tower/workflows/admin/messaging/create_announcement_workflow.rb +16 -3
- data/app/workflows/command_tower/workflows/admin/messaging/gallery/index_workflow.rb +29 -0
- data/app/workflows/command_tower/workflows/admin/messaging/gallery/show_workflow.rb +40 -0
- data/app/workflows/command_tower/workflows/admin/messaging/gallery/test_send_workflow.rb +103 -0
- data/app/workflows/command_tower/workflows/messaging/communications/produce_recipient_workflow.rb +2 -2
- data/app/workflows/command_tower/workflows/messaging/error_mapping.rb +8 -1
- data/app/workflows/command_tower/workflows/messaging/inbox.rb +30 -5
- data/app/workflows/command_tower/workflows/messaging/unsubscribe/one_click_workflow.rb +56 -0
- data/app/workflows/command_tower/workflows/messaging/unsubscribe/preferences_redirect_workflow.rb +24 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20261124000001_add_message_definition_columns_to_messaging_communications.rb +9 -0
- data/docs/admin_workspace.md +2 -1
- data/docs/api_reference.md +78 -20
- data/docs/audit.md +9 -0
- data/docs/authentication_authorization_guide.md +1 -1
- data/docs/bootstrap/09-rbac.md +2 -0
- data/docs/bootstrap/11-auth-client-path.md +1 -1
- data/docs/cookie_authentication_guide.md +2 -0
- data/docs/extending.md +4 -0
- data/docs/messaging_integration_guide.md +40 -15
- data/docs/principal_capabilities.md +2 -0
- data/docs/ui.md +64 -2
- data/docs/upgrades/0.23.0.md +64 -0
- data/docs/upgrades/README.md +1 -0
- data/lib/command_tower/authorization/default.yml +8 -0
- data/lib/command_tower/configuration/messaging/compliance.rb +47 -0
- data/lib/command_tower/configuration/messaging/config.rb +25 -0
- data/lib/command_tower/configuration/registry/admin_workspace/config.rb +9 -0
- data/lib/command_tower/configuration/registry/admin_workspace/tool_definition.rb +10 -1
- data/lib/command_tower/configuration/registry/audit/config.rb +38 -0
- data/lib/command_tower/configuration/registry/principal_capabilities/config.rb +2 -0
- data/lib/command_tower/current.rb +1 -0
- data/lib/command_tower/email_theme/default_ui_tokens.json +18 -0
- data/lib/command_tower/email_theme/palette.rb +75 -0
- data/lib/command_tower/email_theme/token_file.rb +81 -0
- data/lib/command_tower/engine.rb +7 -0
- data/lib/command_tower/execution.rb +2 -1
- data/lib/command_tower/install/baseline.rb +1 -0
- data/lib/command_tower/rspec/message_definition.rb +125 -0
- data/lib/command_tower/ui/document.rb +18 -4
- data/lib/command_tower/ui/email/renderer/text_nodes.rb +2 -1
- data/lib/command_tower/ui/email/renderer.rb +17 -5
- data/lib/command_tower/ui/email/text_part.rb +53 -0
- data/lib/command_tower/ui/fragments/markdown.rb +121 -0
- data/lib/command_tower/ui/markdown_subset/chunker.rb +91 -0
- data/lib/command_tower/ui/markdown_subset/inline.rb +86 -0
- data/lib/command_tower/ui/markdown_subset/segment.rb +15 -0
- data/lib/command_tower/ui/markdown_subset/walker.rb +129 -0
- data/lib/command_tower/ui/nodes/instant.rb +3 -0
- data/lib/command_tower/ui/redaction.rb +44 -0
- data/lib/command_tower/ui/required_facts/matchers/text.rb +22 -0
- data/lib/command_tower/ui/required_facts/matchers/time.rb +37 -0
- data/lib/command_tower/ui/required_facts/verdict.rb +54 -0
- data/lib/command_tower/ui/text_derivation.rb +18 -17
- data/lib/command_tower/ui/time_formatter.rb +5 -3
- data/lib/command_tower/ui.rb +6 -0
- data/lib/command_tower/version.rb +1 -1
- data/lib/command_tower.rb +2 -0
- metadata +78 -2
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Rendering
|
|
6
|
+
# Internal Messaging::Rendering collaborator for `ChannelRenderer`. Renders the email HTML
|
|
7
|
+
# and text parts of a MessageDefinition-backed communication (design D20).
|
|
8
|
+
#
|
|
9
|
+
# 1. The definition's email composer (or `:basic`) through `UI::Email::Renderer` with the
|
|
10
|
+
# resolved palette and `user_zone: render_zone`.
|
|
11
|
+
# 2. When that raises (a composer error, a missing fact block, which the `:message` profile
|
|
12
|
+
# rejects, or a `code` node, which the renderer rejects): a CT-built fallback document
|
|
13
|
+
# (headline, the fact block's derived text when the facts composer still works, else the
|
|
14
|
+
# communication `body`, then a CTA from the deep link, design G2) through the same
|
|
15
|
+
# renderer. One `email_document_fallback` log line carries ids and the error class only.
|
|
16
|
+
#
|
|
17
|
+
# When the fallback also raises, the error propagates and `ChannelRenderer` uses the generic
|
|
18
|
+
# ERB templates.
|
|
19
|
+
class DefinedEmailRenderer
|
|
20
|
+
# @param footer_notice [String, nil] a plain line placed above the compliance footer (the
|
|
21
|
+
# gallery test send uses it to mark the message as a test)
|
|
22
|
+
def self.render(communication:, definition:, footer_notice: nil)
|
|
23
|
+
new(communication:, definition:, footer_notice:).render
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def initialize(communication:, definition:, footer_notice: nil)
|
|
27
|
+
@communication = communication
|
|
28
|
+
@definition = definition
|
|
29
|
+
@footer_notice = footer_notice
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @return [CommandTower::UI::Email::Renderer::Output]
|
|
33
|
+
def render
|
|
34
|
+
present(composed_document)
|
|
35
|
+
rescue StandardError => e
|
|
36
|
+
log_fallback(e)
|
|
37
|
+
present(fallback_document)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
attr_reader :communication, :definition
|
|
43
|
+
|
|
44
|
+
# The `:message` profile requires a fact block, so a definition without one raises here.
|
|
45
|
+
def composed_document
|
|
46
|
+
builder.email(definition: definition.new, context:)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def fact_document
|
|
50
|
+
return @fact_document if defined?(@fact_document)
|
|
51
|
+
|
|
52
|
+
@fact_document = builder.facts(definition: definition.new, context:)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def fallback_document
|
|
56
|
+
text = fact_text.presence || communication.body.to_s.strip
|
|
57
|
+
builder.email(definition: fallback_definition(text).new, context:, profile: :default)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# A definition built from the communication alone: headline, `text` lines in place of the
|
|
61
|
+
# fact block, and the `:basic` button. It does not call the host definition.
|
|
62
|
+
def fallback_definition(text)
|
|
63
|
+
title = communication.title.to_s.strip
|
|
64
|
+
Class.new(CommandTower::Messaging::MessageDefinition) do
|
|
65
|
+
define_method(:facts) { |factory, **| text.each_line(chomp: true) { |line| factory.text(line) } }
|
|
66
|
+
define_method(:headline) { |metadata:| metadata["headline"].presence || title }
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Derived text of the fact block, or "" when the facts composer fails or yields nothing.
|
|
71
|
+
def fact_text
|
|
72
|
+
document = fact_document
|
|
73
|
+
document ? CommandTower::UI::TextDerivation.call(document.root, user_zone: zone) : ""
|
|
74
|
+
rescue StandardError
|
|
75
|
+
""
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# The compliance footer is added here so the composed and the fallback email carry it alike.
|
|
79
|
+
def present(document)
|
|
80
|
+
footer = footer_with_notice
|
|
81
|
+
output = CommandTower::UI::Email::Renderer.call(
|
|
82
|
+
document,
|
|
83
|
+
palette: CommandTower::EmailTheme::Palette.resolve,
|
|
84
|
+
origin: CommandTower.config.application.composed_url,
|
|
85
|
+
user_zone: zone,
|
|
86
|
+
app_name: CommandTower.app_name_for_comms,
|
|
87
|
+
footer_html: footer.html
|
|
88
|
+
)
|
|
89
|
+
CommandTower::UI::Email::Renderer::Output.new(html: output.html, text: "#{output.text}\n\n#{footer.text}")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The compliance footer, with the optional notice line above it.
|
|
93
|
+
def footer_with_notice
|
|
94
|
+
footer = compliance_footer
|
|
95
|
+
return footer if @footer_notice.blank?
|
|
96
|
+
|
|
97
|
+
ComplianceFooter::Output.new(
|
|
98
|
+
html: "#{ERB::Util.html_escape(@footer_notice)}<br>\n#{footer.html}",
|
|
99
|
+
text: "#{@footer_notice}\n#{footer.text}"
|
|
100
|
+
)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def compliance_footer
|
|
104
|
+
declaration = NotificationTypes.lookup(communication.notification_type_key)
|
|
105
|
+
ComplianceFooter.call(declaration:, definition:)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def builder
|
|
109
|
+
CommandTower::Messaging::Definitions::DocumentBuilder
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def zone
|
|
113
|
+
communication.render_zone.presence || CommandTower.config.ui.default_zone
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def context
|
|
117
|
+
@context ||= CommandTower::Messaging::Definitions::RenderContext.new(
|
|
118
|
+
notification_type_key: communication.notification_type_key,
|
|
119
|
+
metadata: string_metadata,
|
|
120
|
+
metadata_version: communication.metadata_version || definition.metadata_version,
|
|
121
|
+
slots: communication.slots,
|
|
122
|
+
render_zone: communication.render_zone
|
|
123
|
+
)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def string_metadata
|
|
127
|
+
stored = communication.metadata
|
|
128
|
+
(stored.is_a?(Hash) ? stored : {}).transform_keys(&:to_s)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Ids and the error class only: metadata values and error messages can quote user data.
|
|
132
|
+
def log_fallback(error)
|
|
133
|
+
Rails.logger.warn(
|
|
134
|
+
"[command_tower] email_document_fallback communication_id=#{communication.id} " \
|
|
135
|
+
"notification_type_key=#{communication.notification_type_key} error_class=#{error.class.name}"
|
|
136
|
+
)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
@@ -52,21 +52,36 @@ module CommandTower
|
|
|
52
52
|
UNSAFE_SCHEMES = %w[javascript data vbscript].freeze
|
|
53
53
|
DEFAULT_CTA_LABEL = "Open"
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
# Types registered with a `message:` MessageDefinition render through it: the composed
|
|
56
|
+
# `UI::Document` (profile `:message`) is redacted when `impersonating` is not exactly
|
|
57
|
+
# false, then returned as is for `ui_catalog >= 2` or mapped to `inbox_document_v1` for
|
|
58
|
+
# catalog 1. Every other type renders the generic/composer path, identical for all
|
|
59
|
+
# catalogs. In production a failing definition falls back to the `:basic` document and
|
|
60
|
+
# logs ids only; elsewhere it raises so the definition bug is visible.
|
|
61
|
+
def self.render(communication:, recipient_id:, ui_catalog: 1, impersonating: false)
|
|
62
|
+
new(communication:, recipient_id:, ui_catalog:, impersonating:).render
|
|
57
63
|
end
|
|
58
64
|
|
|
59
65
|
def self.compose_shell(communication)
|
|
60
66
|
new(communication:, recipient_id: nil).compose_shell
|
|
61
67
|
end
|
|
62
68
|
|
|
63
|
-
def initialize(communication:, recipient_id:)
|
|
69
|
+
def initialize(communication:, recipient_id:, ui_catalog: 1, impersonating: false)
|
|
64
70
|
@communication = communication
|
|
65
71
|
@recipient_id = recipient_id
|
|
72
|
+
@ui_catalog = ui_catalog
|
|
73
|
+
@impersonating = impersonating
|
|
66
74
|
@presentation_attempted = false
|
|
67
75
|
end
|
|
68
76
|
|
|
69
77
|
def render
|
|
78
|
+
definition = message_definition
|
|
79
|
+
return render_defined(definition) if definition
|
|
80
|
+
|
|
81
|
+
render_undefined
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def render_undefined
|
|
70
85
|
document = compose_shell
|
|
71
86
|
snapshot = stored_presentation_snapshot
|
|
72
87
|
return resolve_presentation_blocks(document) if snapshot.nil?
|
|
@@ -81,7 +96,67 @@ module CommandTower
|
|
|
81
96
|
|
|
82
97
|
private
|
|
83
98
|
|
|
84
|
-
attr_reader :communication, :recipient_id
|
|
99
|
+
attr_reader :communication, :recipient_id, :ui_catalog, :impersonating
|
|
100
|
+
|
|
101
|
+
# The declared MessageDefinition class, or nil for an unregistered or undefined type.
|
|
102
|
+
def message_definition
|
|
103
|
+
name = CommandTower::Messaging::NotificationTypes.lookup(communication.notification_type_key).message
|
|
104
|
+
name.presence&.constantize
|
|
105
|
+
rescue StandardError
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def render_defined(definition)
|
|
110
|
+
document = defined_document(definition)
|
|
111
|
+
return document unless document.is_a?(CommandTower::UI::Document)
|
|
112
|
+
|
|
113
|
+
document = CommandTower::UI::Redaction.call(document, impersonating:)
|
|
114
|
+
ui_catalog.to_i >= 2 ? document : CommandTower::UI::LegacyMapping.call(document)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def defined_document(definition)
|
|
118
|
+
composed_inbox(definition)
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
raise unless Rails.env.production?
|
|
121
|
+
|
|
122
|
+
log_fallback(e)
|
|
123
|
+
basic_fallback(definition)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def basic_fallback(definition)
|
|
127
|
+
basic = Class.new(definition) do
|
|
128
|
+
def inbox(builder, **)
|
|
129
|
+
builder.basic
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
composed_inbox(basic)
|
|
133
|
+
rescue StandardError
|
|
134
|
+
build_generic
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def composed_inbox(definition)
|
|
138
|
+
CommandTower::Messaging::Definitions::DocumentBuilder.inbox(
|
|
139
|
+
definition: definition.new, context: render_context(definition), profile: definition.document_profile
|
|
140
|
+
)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def render_context(definition)
|
|
144
|
+
CommandTower::Messaging::Definitions::RenderContext.new(
|
|
145
|
+
notification_type_key: communication.notification_type_key,
|
|
146
|
+
metadata: (communication.metadata.is_a?(Hash) ? communication.metadata : {}).transform_keys(&:to_s),
|
|
147
|
+
metadata_version: communication.metadata_version || definition.metadata_version,
|
|
148
|
+
slots: communication.slots,
|
|
149
|
+
render_zone: communication.render_zone
|
|
150
|
+
)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Ids and the error class only: metadata values and error messages can quote user data.
|
|
154
|
+
def log_fallback(error)
|
|
155
|
+
Rails.logger.warn(
|
|
156
|
+
"[command_tower] inbox_document_fallback communication_id=#{communication.id} " \
|
|
157
|
+
"notification_type_key=#{communication.notification_type_key} error_class=#{error.class.name}"
|
|
158
|
+
)
|
|
159
|
+
end
|
|
85
160
|
|
|
86
161
|
def build_from_type_composer
|
|
87
162
|
declaration = CommandTower::Messaging::NotificationTypes.lookup(communication.notification_type_key)
|
|
@@ -8,8 +8,19 @@ module CommandTower
|
|
|
8
8
|
:subject,
|
|
9
9
|
:text_body,
|
|
10
10
|
:html_body,
|
|
11
|
+
:headers,
|
|
11
12
|
) do
|
|
12
|
-
|
|
13
|
+
# `headers` is optional (extra RFC 8058 mail headers, String => String) so direct callers
|
|
14
|
+
# predating it keep working.
|
|
15
|
+
def initialize(headers: {}.freeze, **attributes)
|
|
16
|
+
unless headers.is_a?(Hash) && headers.all? { |k, v| k.is_a?(String) && v.is_a?(String) }
|
|
17
|
+
raise ArgumentError, "headers must be a Hash of String to String"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
super(headers: headers.dup.freeze, **attributes)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.build(recipient_address:, subject:, text_body:, html_body:, headers: {})
|
|
13
24
|
values = [recipient_address, subject, text_body, html_body]
|
|
14
25
|
raise ArgumentError, "arbitrary hashes are not accepted" if values.any? { |v| v.is_a?(Hash) }
|
|
15
26
|
raise ArgumentError, "ActiveRecord objects are not accepted" if values.any? { |v| active_record_like?(v) }
|
|
@@ -25,6 +36,7 @@ module CommandTower
|
|
|
25
36
|
subject:,
|
|
26
37
|
text_body:,
|
|
27
38
|
html_body:,
|
|
39
|
+
headers:,
|
|
28
40
|
).freeze
|
|
29
41
|
end
|
|
30
42
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi"
|
|
4
|
+
|
|
5
|
+
module CommandTower
|
|
6
|
+
module Messaging
|
|
7
|
+
module Rendering
|
|
8
|
+
# RFC 8058 one-click headers for a defined-type email (design D21). Present only when the type
|
|
9
|
+
# is not mandatory, is user configurable, allows email and `compliance.api_base_url` is set;
|
|
10
|
+
# otherwise empty. The token is the only secret-bearing part and is never logged.
|
|
11
|
+
module UnsubscribeHeaders
|
|
12
|
+
POST_VALUE = "List-Unsubscribe=One-Click"
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
# @return [Boolean]
|
|
16
|
+
def eligible?(declaration)
|
|
17
|
+
!declaration.mandatory && declaration.user_configurable && declaration.allowed_channels.include?("email")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# One boot warning (type keys only) when eligible types exist but `compliance.api_base_url`
|
|
21
|
+
# is unset, so their one-click headers are omitted.
|
|
22
|
+
def warn_when_unconfigured(declarations)
|
|
23
|
+
return if CommandTower.config.messaging.compliance.api_base_url.present?
|
|
24
|
+
|
|
25
|
+
keys = declarations.select { |declaration| eligible?(declaration) }.map(&:key)
|
|
26
|
+
return if keys.empty?
|
|
27
|
+
|
|
28
|
+
Rails.logger.warn(
|
|
29
|
+
"[command_tower] one-click unsubscribe headers omitted: config.messaging.compliance.api_base_url " \
|
|
30
|
+
"is not set (types: #{keys.join(', ')})"
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Hash{String => String}]
|
|
35
|
+
def call(declaration:, user_id:)
|
|
36
|
+
base = CommandTower.config.messaging.compliance.api_base_url
|
|
37
|
+
return {} if base.blank? || !eligible?(declaration)
|
|
38
|
+
|
|
39
|
+
result = CommandTower::Services::Messaging::Unsubscribe::IssueToken.call(
|
|
40
|
+
user_id:, notification_type_key: declaration.key
|
|
41
|
+
)
|
|
42
|
+
return {} unless result.success?
|
|
43
|
+
|
|
44
|
+
url = "#{base}/messaging/unsubscribe?token=#{CGI.escape(result.data[:token])}"
|
|
45
|
+
{ "List-Unsubscribe" => "<#{url}>", "List-Unsubscribe-Post" => POST_VALUE }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Unsubscribe
|
|
6
|
+
# The signing boundary for one-click unsubscribe tokens (design D21). The key is derived from
|
|
7
|
+
# `secret_key_base` through `Rails.application.message_verifier` unless
|
|
8
|
+
# `config.messaging.compliance.unsubscribe_secret` overrides it. The token is signed, not
|
|
9
|
+
# encrypted, and is bound to a purpose so no other signed value verifies as an unsubscribe token.
|
|
10
|
+
module TokenVerifier
|
|
11
|
+
VERIFIER_NAME = "command_tower/messaging/unsubscribe"
|
|
12
|
+
PURPOSE = "unsubscribe"
|
|
13
|
+
EXPIRES_IN = 90.days
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
def verifier
|
|
17
|
+
secret = CommandTower.config.messaging.compliance.unsubscribe_secret.presence
|
|
18
|
+
return Rails.application.message_verifier(VERIFIER_NAME) unless secret
|
|
19
|
+
|
|
20
|
+
ActiveSupport::MessageVerifier.new(secret, serializer: JSON, url_safe: true)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Messaging
|
|
6
|
+
module Announcements
|
|
7
|
+
# Decides how an admin announcement is handed to the Produce family (design D26).
|
|
8
|
+
#
|
|
9
|
+
# - Undefined or unregistered type: title, body and metadata pass through unchanged.
|
|
10
|
+
# - Type defined by an `AnnouncementDefinition` (or a subclass): the admin title and body
|
|
11
|
+
# become metadata (`title`, `body_markdown`) and no title or body is produced. Metadata that
|
|
12
|
+
# fails the definition's schema is a `ValidationError` naming attributes only.
|
|
13
|
+
# - Any other defined type: fails with `AnnouncementTypeNotSupportedError` (HTTP 422).
|
|
14
|
+
#
|
|
15
|
+
# Outputs: `title`, `body`, `metadata`.
|
|
16
|
+
class ResolveProduceArguments < CommandTower::Services::ApplicationService
|
|
17
|
+
validate :notification_type_key, is_a: String, required: true
|
|
18
|
+
validate :title, is_a: String, required: false
|
|
19
|
+
validate :body, is_a: String, required: false
|
|
20
|
+
validate :metadata, is_a: Hash, required: false
|
|
21
|
+
|
|
22
|
+
def call
|
|
23
|
+
definition = definition_class
|
|
24
|
+
return pass_through if definition.nil?
|
|
25
|
+
|
|
26
|
+
unless definition <= CommandTower::Messaging::AnnouncementDefinition
|
|
27
|
+
context.fail!(application_error: CommandTower::Errors::Messaging::AnnouncementTypeNotSupportedError.new)
|
|
28
|
+
return
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
map_into_metadata(definition)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def definition_class
|
|
37
|
+
return nil unless CommandTower::Messaging::NotificationTypes.registered?(notification_type_key)
|
|
38
|
+
|
|
39
|
+
klass = CommandTower::Messaging::NotificationTypes.lookup(notification_type_key).message&.safe_constantize
|
|
40
|
+
klass if klass.is_a?(Class) && klass < CommandTower::Messaging::MessageDefinition
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def pass_through
|
|
44
|
+
context.title = title
|
|
45
|
+
context.body = body
|
|
46
|
+
context.metadata = metadata
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def map_into_metadata(definition)
|
|
50
|
+
mapped = mapped_metadata
|
|
51
|
+
invalid = definition.metadata_schema.schema_errors(mapped)
|
|
52
|
+
return fail_invalid(invalid) if invalid.any?
|
|
53
|
+
|
|
54
|
+
context.title = nil
|
|
55
|
+
context.body = nil
|
|
56
|
+
context.metadata = mapped
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def mapped_metadata
|
|
60
|
+
(metadata || {}).to_h.transform_keys(&:to_s).merge("title" => title, "body_markdown" => body)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def fail_invalid(invalid)
|
|
64
|
+
context.fail!(
|
|
65
|
+
application_error: CommandTower::Errors::ValidationError.new(
|
|
66
|
+
details: { messaging: "announcement is invalid (attributes: #{invalid.join(', ')})" }
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Messaging
|
|
6
|
+
module Communications
|
|
7
|
+
# D17 for the Produce family: a MessageDefinition-backed type derives title and body, so
|
|
8
|
+
# explicit copy is rejected in strict mode (dev/test) and dropped with an ids-only log
|
|
9
|
+
# otherwise. Undefined types are untouched.
|
|
10
|
+
module ExplicitCopyRule
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def defined_type?(notification_type_key)
|
|
14
|
+
return false unless CommandTower::Messaging::NotificationTypes.registered?(notification_type_key)
|
|
15
|
+
|
|
16
|
+
message = CommandTower::Messaging::NotificationTypes.lookup(notification_type_key).message
|
|
17
|
+
klass = message&.safe_constantize
|
|
18
|
+
klass.is_a?(Class) && klass < CommandTower::Messaging::MessageDefinition
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def explicit_copy?(title, body)
|
|
22
|
+
[title, body].any? { |value| !value.nil? && !(value.respond_to?(:strip) && value.strip.empty?) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def violation?(notification_type_key:, title:, body:)
|
|
26
|
+
explicit_copy?(title, body) && defined_type?(notification_type_key)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def strict?
|
|
30
|
+
CommandTower.config.messaging.strict_definitions
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def failure
|
|
34
|
+
CommandTower::Errors::ValidationError.new(
|
|
35
|
+
details: { messaging: "title and body must not be supplied for a defined type" }
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def log_ignored(notification_type_key:, **ids)
|
|
40
|
+
CommandTower::Messaging::Contract::Observability::Publisher.warn(
|
|
41
|
+
event: "messaging.produce.produce_explicit_copy_ignored",
|
|
42
|
+
notification_type_key:,
|
|
43
|
+
**ids
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -11,8 +11,9 @@ module CommandTower
|
|
|
11
11
|
validate :user, is_a: User, required: true
|
|
12
12
|
validate :notification_type_key, is_a: String, required: true
|
|
13
13
|
validate :host_event_identity, is_a: String, required: true
|
|
14
|
-
|
|
15
|
-
validate :
|
|
14
|
+
# Required for undefined types (Accept enforces it); defined types derive them (D17).
|
|
15
|
+
validate :title, is_a: String, required: false
|
|
16
|
+
validate :body, is_a: String, required: false
|
|
16
17
|
validate :metadata, is_a: Hash, required: false
|
|
17
18
|
validate :platform_enabled_channels, is_a: Array, required: true
|
|
18
19
|
validate :conversation_key, is_a: String, required: false
|
|
@@ -34,12 +35,25 @@ module CommandTower
|
|
|
34
35
|
return
|
|
35
36
|
end
|
|
36
37
|
|
|
38
|
+
accepted_title = title
|
|
39
|
+
accepted_body = body
|
|
40
|
+
if ExplicitCopyRule.violation?(notification_type_key:, title:, body:)
|
|
41
|
+
if ExplicitCopyRule.strict?
|
|
42
|
+
context.fail!(application_error: ExplicitCopyRule.failure)
|
|
43
|
+
return
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
ExplicitCopyRule.log_ignored(notification_type_key:, host_event_identity:, recipient_id: user.id)
|
|
47
|
+
accepted_title = nil
|
|
48
|
+
accepted_body = nil
|
|
49
|
+
end
|
|
50
|
+
|
|
37
51
|
accept_result = CommandTower::Messaging.accept(
|
|
38
52
|
recipient_id: recipient_result.data[:recipient_id],
|
|
39
53
|
notification_type_key:,
|
|
40
54
|
host_event_identity:,
|
|
41
|
-
title
|
|
42
|
-
body
|
|
55
|
+
title: accepted_title,
|
|
56
|
+
body: accepted_body,
|
|
43
57
|
metadata:,
|
|
44
58
|
preference_state: nil,
|
|
45
59
|
platform_enabled_channels:,
|
|
@@ -14,8 +14,9 @@ module CommandTower
|
|
|
14
14
|
validate :user_ids, is_a: Array, required: true
|
|
15
15
|
validate :notification_type_key, is_a: String, required: true
|
|
16
16
|
validate :campaign_identity, is_a: String, required: true
|
|
17
|
-
|
|
18
|
-
validate :
|
|
17
|
+
# Required for undefined types (Accept enforces it); defined types derive them (D17).
|
|
18
|
+
validate :title, is_a: String, required: false
|
|
19
|
+
validate :body, is_a: String, required: false
|
|
19
20
|
validate :platform_enabled_channels, is_a: Array, required: true
|
|
20
21
|
validate :metadata, is_a: Hash, required: false
|
|
21
22
|
validate :conversation_key, is_a: String, required: false
|
|
@@ -25,6 +26,7 @@ module CommandTower
|
|
|
25
26
|
def call
|
|
26
27
|
ids = normalize_user_ids
|
|
27
28
|
mode = normalized_execution_mode
|
|
29
|
+
return unless explicit_copy_allowed?
|
|
28
30
|
|
|
29
31
|
if mode == :sync && ids.size > SYNC_MAX
|
|
30
32
|
context.fail!(
|
|
@@ -46,6 +48,22 @@ module CommandTower
|
|
|
46
48
|
|
|
47
49
|
private
|
|
48
50
|
|
|
51
|
+
# D17, checked once for the whole fan-out: strict mode fails the call; otherwise the copy
|
|
52
|
+
# is dropped (one ids-only log) so no recipient carries it.
|
|
53
|
+
def explicit_copy_allowed?
|
|
54
|
+
return true unless ExplicitCopyRule.violation?(notification_type_key:, title:, body:)
|
|
55
|
+
|
|
56
|
+
if ExplicitCopyRule.strict?
|
|
57
|
+
context.fail!(application_error: ExplicitCopyRule.failure)
|
|
58
|
+
return false
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
ExplicitCopyRule.log_ignored(notification_type_key:, campaign_identity:)
|
|
62
|
+
context.title = nil
|
|
63
|
+
context.body = nil
|
|
64
|
+
true
|
|
65
|
+
end
|
|
66
|
+
|
|
49
67
|
def normalize_user_ids
|
|
50
68
|
Array(user_ids).map { |id| Integer(id) }
|
|
51
69
|
rescue ArgumentError, TypeError
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Messaging
|
|
6
|
+
module Gallery
|
|
7
|
+
# Raised when one channel's composer or renderer fails while building a gallery preview.
|
|
8
|
+
# Carries the channel and the error class only; the original message can hold metadata
|
|
9
|
+
# values, so it is dropped.
|
|
10
|
+
class ChannelFailed < StandardError
|
|
11
|
+
# Runs the block, turning any StandardError into a ChannelFailed for `channel`.
|
|
12
|
+
def self.guard(channel)
|
|
13
|
+
yield
|
|
14
|
+
rescue ChannelFailed
|
|
15
|
+
raise
|
|
16
|
+
rescue StandardError => e
|
|
17
|
+
raise new(channel, e.class.name)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
attr_reader :channel, :error_class
|
|
21
|
+
|
|
22
|
+
def initialize(channel, error_class)
|
|
23
|
+
@channel = channel
|
|
24
|
+
@error_class = error_class
|
|
25
|
+
super("channel #{channel} failed")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module Messaging
|
|
6
|
+
module Gallery
|
|
7
|
+
# Caps the gallery test send per real actor: 5 an hour and 20 a day. Each call counts one
|
|
8
|
+
# attempt in both windows. A Redis error fails closed (the underlying InternalError).
|
|
9
|
+
class CheckTestSendRateLimit < CommandTower::Services::ApplicationService
|
|
10
|
+
WINDOWS = [
|
|
11
|
+
{ name: "hour", limit: 5, ttl_seconds: 3600 },
|
|
12
|
+
{ name: "day", limit: 20, ttl_seconds: 86_400 }
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
validate :actor_id, is_a: Integer, required: true
|
|
16
|
+
|
|
17
|
+
def call
|
|
18
|
+
WINDOWS.each { |window| enforce(window) }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def enforce(window)
|
|
24
|
+
result = count_attempt(window)
|
|
25
|
+
context.fail!(application_error: result.errors.first) if result.failure?
|
|
26
|
+
return if result.data[:count] <= window[:limit]
|
|
27
|
+
|
|
28
|
+
context.fail!(application_error: rate_limited(result.data[:ttl]))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def count_attempt(window)
|
|
32
|
+
CommandTower::Services::RateLimits::Check.call(
|
|
33
|
+
key: "admin:messaging:gallery_test_send:#{window[:name]}:actor:#{actor_id}",
|
|
34
|
+
ttl_seconds: window[:ttl_seconds]
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def rate_limited(ttl)
|
|
39
|
+
CommandTower::Errors::Messaging::GalleryTestSendRateLimitError.new(
|
|
40
|
+
retry_after_seconds: ttl.positive? ? ttl : nil
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|