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,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
class MessageDefinition
|
|
6
|
+
# Base class for a definition's metadata schema. Hosts subclass it with
|
|
7
|
+
# `attribute` and ActiveModel validations:
|
|
8
|
+
#
|
|
9
|
+
# class Metadata < CommandTower::Messaging::MessageDefinition::Metadata
|
|
10
|
+
# attribute :class_name, :string
|
|
11
|
+
# validates :class_name, presence: true
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# Metadata arrives as a string-keyed Hash. `schema_errors` returns attribute names only,
|
|
15
|
+
# never values, so callers can log them safely.
|
|
16
|
+
class Metadata
|
|
17
|
+
include ActiveModel::API
|
|
18
|
+
include ActiveModel::Attributes
|
|
19
|
+
|
|
20
|
+
# Builds an instance from a Hash. Keys the schema does not declare are ignored, so a
|
|
21
|
+
# newer producer does not break an older schema.
|
|
22
|
+
def self.from_hash(hash)
|
|
23
|
+
source = (hash || {}).to_h.transform_keys(&:to_s)
|
|
24
|
+
new(source.slice(*attribute_names))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [Array<String>] names of attributes that fail validation (no values)
|
|
28
|
+
def self.schema_errors(hash)
|
|
29
|
+
instance = from_hash(hash)
|
|
30
|
+
instance.valid? ? [] : instance.errors.attribute_names.map(&:to_s).uniq
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
# Base class for host-defined messages. A definition declares its metadata contract with
|
|
6
|
+
# class macros and describes its content with overridable instance methods. Registration
|
|
7
|
+
# (`Declaration.build(..., message: Klass)`) lists the class; this class holds no state per
|
|
8
|
+
# message and its methods are pure (no DB, no network).
|
|
9
|
+
#
|
|
10
|
+
# class ClassCanceled < CommandTower::Messaging::MessageDefinition
|
|
11
|
+
# metadata_version 1
|
|
12
|
+
# metadata_schema Metadata
|
|
13
|
+
# default_zone "America/Los_Angeles"
|
|
14
|
+
# required_fact :class_name, :text
|
|
15
|
+
# required_fact :starts_at, :time, zone: "studio_zone"
|
|
16
|
+
# fixture :default, metadata: { "class_name" => "Arms & Abs", ... }
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# The `:basic` defaults for `facts`, `inbox`, `email`, `slots` and `headline` are the
|
|
20
|
+
# behaviour a definition gets when it does not override a method. `inbox` and `email`
|
|
21
|
+
# defaults ask the composer builder for its basic layout (`d.basic`).
|
|
22
|
+
class MessageDefinition
|
|
23
|
+
# One declared required fact. `matcher` is :text, :time or a host matcher class.
|
|
24
|
+
# `zone` is a metadata key, :user, or nil.
|
|
25
|
+
RequiredFact = Data.define(:key, :matcher, :zone, :optional)
|
|
26
|
+
|
|
27
|
+
MATCHER_SYMBOLS = %i[text time].freeze
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
# Macro/reader. With an argument it sets, without it reads. Subclasses inherit.
|
|
31
|
+
def metadata_version(value = nil)
|
|
32
|
+
return inherited_setting(:@metadata_version) if value.nil?
|
|
33
|
+
|
|
34
|
+
@metadata_version = value
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def metadata_schema(klass = nil)
|
|
38
|
+
return inherited_setting(:@metadata_schema) || Metadata if klass.nil?
|
|
39
|
+
|
|
40
|
+
@metadata_schema = klass
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def default_zone(zone = nil)
|
|
44
|
+
return inherited_setting(:@default_zone) if zone.nil?
|
|
45
|
+
|
|
46
|
+
@default_zone = zone.to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def promotional(value = nil)
|
|
50
|
+
return inherited_setting(:@promotional) == true if value.nil?
|
|
51
|
+
|
|
52
|
+
@promotional = value ? true : false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def promotional?
|
|
56
|
+
promotional
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# UI validation profile of the definition's documents. `:message` requires exactly one
|
|
60
|
+
# `facts` node; a definition with no fact block (`AnnouncementDefinition`) uses `:default`.
|
|
61
|
+
def document_profile(value = nil)
|
|
62
|
+
return inherited_setting(:@document_profile) || :message if value.nil?
|
|
63
|
+
|
|
64
|
+
@document_profile = value
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def required_fact(key, matcher, zone: nil, optional: false)
|
|
68
|
+
unless MATCHER_SYMBOLS.include?(matcher) || matcher.is_a?(Class)
|
|
69
|
+
raise ArgumentError,
|
|
70
|
+
"required_fact matcher must be :text, :time or a matcher class (got #{matcher.inspect})"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
fact = RequiredFact.new(key: key.to_s, matcher:, zone: normalize_zone(zone),
|
|
74
|
+
optional: optional ? true : false)
|
|
75
|
+
@required_facts = own_required_facts + [fact]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @return [Array<RequiredFact>] inherited facts first, then this class's own
|
|
79
|
+
def required_facts
|
|
80
|
+
inherited = superclass.respond_to?(:required_facts) ? superclass.required_facts : []
|
|
81
|
+
(inherited + own_required_facts).freeze
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def fixture(name, metadata:)
|
|
85
|
+
@fixtures = own_fixtures.merge(name.to_s => deep_freeze(metadata.to_h.transform_keys(&:to_s)))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @return [Hash{String => Hash}] fixture name => string-keyed metadata, declaration order
|
|
89
|
+
def fixtures
|
|
90
|
+
inherited = superclass.respond_to?(:fixtures) ? superclass.fixtures : {}
|
|
91
|
+
inherited.merge(own_fixtures).freeze
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
def inherited_setting(ivar)
|
|
97
|
+
return instance_variable_get(ivar) if instance_variable_defined?(ivar)
|
|
98
|
+
|
|
99
|
+
superclass.respond_to?(:metadata_version, true) ? superclass.send(:inherited_setting, ivar) : nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def normalize_zone(zone)
|
|
103
|
+
return nil if zone.nil?
|
|
104
|
+
|
|
105
|
+
zone == :user ? :user : zone.to_s
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def own_required_facts
|
|
109
|
+
@required_facts || []
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def own_fixtures
|
|
113
|
+
@fixtures || {}
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def deep_freeze(value)
|
|
117
|
+
case value
|
|
118
|
+
when Hash then value.each_value { |v| deep_freeze(v) }
|
|
119
|
+
when Array then value.each { |v| deep_freeze(v) }
|
|
120
|
+
end
|
|
121
|
+
value.freeze
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Fact block shared by every channel's document. Default: no fact block.
|
|
126
|
+
def facts(_factory, **)
|
|
127
|
+
nil
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def inbox(builder, **)
|
|
131
|
+
builder.basic
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def email(builder, **)
|
|
135
|
+
builder.basic
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Headline slot. Default reads the metadata `headline` key.
|
|
139
|
+
def headline(metadata:)
|
|
140
|
+
metadata["headline"]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Channel slots as a Hash keyed by slot name (headline, subject, push_body, sms_body,
|
|
144
|
+
# pushover_title, pushover_message). Overrides also receive `t:`, the zone-aware time
|
|
145
|
+
# formatter callable.
|
|
146
|
+
def slots(metadata:, **)
|
|
147
|
+
{ headline: headline(metadata:) }
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -28,7 +28,13 @@ module CommandTower
|
|
|
28
28
|
:sound,
|
|
29
29
|
:deep_link_required,
|
|
30
30
|
:destination_locks,
|
|
31
|
+
:message,
|
|
31
32
|
) do
|
|
33
|
+
# `message` is optional so direct `Declaration.new` callers predating it keep working.
|
|
34
|
+
def initialize(message: nil, **attributes)
|
|
35
|
+
super
|
|
36
|
+
end
|
|
37
|
+
|
|
32
38
|
def self.build(
|
|
33
39
|
key:,
|
|
34
40
|
allowed_channels:,
|
|
@@ -53,7 +59,8 @@ module CommandTower
|
|
|
53
59
|
urgency: nil,
|
|
54
60
|
sound: nil,
|
|
55
61
|
deep_link_required: false,
|
|
56
|
-
destination_locks: {}
|
|
62
|
+
destination_locks: {},
|
|
63
|
+
message: nil
|
|
57
64
|
)
|
|
58
65
|
new(
|
|
59
66
|
key: key.nil? ? nil : key.to_s,
|
|
@@ -96,6 +103,10 @@ module CommandTower
|
|
|
96
103
|
# so Produce can reject a missing metadata deep_link.
|
|
97
104
|
deep_link_required: !!deep_link_required,
|
|
98
105
|
destination_locks: freeze_destination_locks(destination_locks),
|
|
106
|
+
# String class name of a `MessageDefinition` subclass. Stored as a name, never
|
|
107
|
+
# constantized here, so host registration order cannot depend on autoload order;
|
|
108
|
+
# `Registry#seal` constantizes and validates it.
|
|
109
|
+
message: message.nil? ? nil : message.to_s,
|
|
99
110
|
).freeze
|
|
100
111
|
end
|
|
101
112
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module NotificationTypes
|
|
6
|
+
# Seal-time checks for declarations that name a `MessageDefinition` (`message:`) or still use
|
|
7
|
+
# `inbox_document_composer`. Runs only from `Registry#seal`, so the class name is constantized
|
|
8
|
+
# after host autoloading is ready and never while hosts are still registering. Errors carry
|
|
9
|
+
# the type key, class names and slot names only; never metadata values.
|
|
10
|
+
class MessageDefinitionChecks
|
|
11
|
+
# Slot each channel needs before its content can be delivered. `inbox` renders from the
|
|
12
|
+
# inbox document, not from a slot.
|
|
13
|
+
CHANNEL_SLOTS = {
|
|
14
|
+
"email" => :subject,
|
|
15
|
+
"push" => :push_body,
|
|
16
|
+
"sms" => :sms_body,
|
|
17
|
+
"pushover" => :pushover_message
|
|
18
|
+
}.freeze
|
|
19
|
+
|
|
20
|
+
TYPE_TEMPLATE_ROOT = "command_tower/messaging/rendering"
|
|
21
|
+
TYPE_KEY_FORMAT = /\A[a-z0-9_]+\z/
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# Raises InvalidDeclarationError for the first invalid declaration, then logs one
|
|
25
|
+
# deprecation line for every type still on `inbox_document_composer`.
|
|
26
|
+
def validate!(declarations)
|
|
27
|
+
declarations.each do |declaration|
|
|
28
|
+
found = errors(declaration)
|
|
29
|
+
raise InvalidDeclarationError, "#{declaration.key}: #{found.join('; ')}" if found.any?
|
|
30
|
+
end
|
|
31
|
+
log_composer_deprecation(declarations)
|
|
32
|
+
Rendering::UnsubscribeHeaders.warn_when_unconfigured(declarations.select(&:message))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @return [Array<String>]
|
|
36
|
+
def errors(declaration)
|
|
37
|
+
new(declaration).errors
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def log_composer_deprecation(declarations)
|
|
43
|
+
keys = declarations.select(&:inbox_document_composer).map(&:key)
|
|
44
|
+
return if keys.empty?
|
|
45
|
+
|
|
46
|
+
Rails.logger.warn(
|
|
47
|
+
"[command_tower] DEPRECATION: inbox_document_composer is deprecated in favor of message: " \
|
|
48
|
+
"(types: #{keys.join(', ')})"
|
|
49
|
+
)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def initialize(declaration)
|
|
54
|
+
@declaration = declaration
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def errors
|
|
58
|
+
return [] if @declaration.message.nil?
|
|
59
|
+
return ["message: and inbox_document_composer are mutually exclusive"] if @declaration.inbox_document_composer
|
|
60
|
+
return ["message #{@declaration.message.inspect} must name a MessageDefinition subclass"] unless definition
|
|
61
|
+
|
|
62
|
+
per_type_template_errors + zone_errors + version_errors + promotional_errors + fixture_errors
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def definition
|
|
68
|
+
return @definition if defined?(@definition)
|
|
69
|
+
|
|
70
|
+
klass = @declaration.message.safe_constantize
|
|
71
|
+
@definition = klass if klass.is_a?(Class) && klass < MessageDefinition
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def per_type_template_errors
|
|
75
|
+
key = @declaration.key.to_s
|
|
76
|
+
return [] unless key.match?(TYPE_KEY_FORMAT)
|
|
77
|
+
|
|
78
|
+
dirs = [Rails.root, CommandTower::Engine.root].map { |root| root.join("app/views", TYPE_TEMPLATE_ROOT, key) }
|
|
79
|
+
return [] unless dirs.any? { |dir| Dir.exist?(dir) && Dir.children(dir).any? }
|
|
80
|
+
|
|
81
|
+
["message definition types must not have per-type ERB under #{TYPE_TEMPLATE_ROOT}/#{key}"]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def zone_errors
|
|
85
|
+
zone = definition.default_zone
|
|
86
|
+
return [] if zone.nil? || TZInfo::Timezone.all_identifiers.include?(zone)
|
|
87
|
+
|
|
88
|
+
["default_zone #{zone.inspect} is not a TZInfo identifier"]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def version_errors
|
|
92
|
+
version = definition.metadata_version
|
|
93
|
+
return [] if version.is_a?(Integer) && version.positive?
|
|
94
|
+
|
|
95
|
+
["metadata_version must be a positive Integer"]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def promotional_errors
|
|
99
|
+
return [] unless definition.promotional?
|
|
100
|
+
return [] if CommandTower.config.messaging.compliance.postal_address.present?
|
|
101
|
+
|
|
102
|
+
["promotional messages require config.messaging.compliance.postal_address"]
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def fixture_errors
|
|
106
|
+
first = definition.fixtures.values.first
|
|
107
|
+
return ["at least one fixture is required"] if first.nil?
|
|
108
|
+
|
|
109
|
+
slot_errors(first)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The first fixture is the representative one: it must produce a headline and a slot for
|
|
113
|
+
# every allowed channel.
|
|
114
|
+
def slot_errors(metadata)
|
|
115
|
+
slots = build_slots(metadata)
|
|
116
|
+
return ["first fixture slots could not be built (#{slots})"] if slots.is_a?(String)
|
|
117
|
+
|
|
118
|
+
required = [:headline] + @declaration.allowed_channels.filter_map { |channel| CHANNEL_SLOTS[channel] }
|
|
119
|
+
missing = required.select { |slot| slots[slot].to_s.strip.empty? }
|
|
120
|
+
missing.empty? ? [] : ["first fixture slots are missing: #{missing.join(', ')}"]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @return [Hash{Symbol => String}, String] the slots, or the error class name when the
|
|
124
|
+
# definition's `slots` raised
|
|
125
|
+
def build_slots(metadata)
|
|
126
|
+
Definitions::DocumentBuilder.slots(definition: definition.new, context: context(metadata))
|
|
127
|
+
rescue StandardError => e
|
|
128
|
+
e.class.name
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def context(metadata)
|
|
132
|
+
Definitions::RenderContext.new(
|
|
133
|
+
notification_type_key: @declaration.key,
|
|
134
|
+
metadata:,
|
|
135
|
+
metadata_version: definition.metadata_version,
|
|
136
|
+
slots: nil,
|
|
137
|
+
render_zone: definition.default_zone.presence || CommandTower.config.ui.default_zone
|
|
138
|
+
)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -96,6 +96,8 @@ module CommandTower
|
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
validate_home_deep_link_configuration!
|
|
99
|
+
# Constantizes `message:` class names, so it runs here (seal) and never at register.
|
|
100
|
+
MessageDefinitionChecks.validate!(@declarations.values)
|
|
99
101
|
end
|
|
100
102
|
|
|
101
103
|
# Sound ids are checked at seal, not register, so host initializer order cannot matter.
|
|
@@ -65,16 +65,57 @@ module CommandTower
|
|
|
65
65
|
private
|
|
66
66
|
|
|
67
67
|
def render_email(address)
|
|
68
|
+
definition = email_definition
|
|
69
|
+
return render_generic_email(address) unless definition
|
|
70
|
+
|
|
71
|
+
render_defined_email(address, definition)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def render_generic_email(address, subject: @communication.title.to_s)
|
|
68
75
|
RenderedPayload.build(
|
|
69
76
|
recipient_address: address,
|
|
70
|
-
subject
|
|
77
|
+
subject:,
|
|
71
78
|
text_body: render_template("email.text.erb"),
|
|
72
79
|
html_body: render_template("email.html.erb"),
|
|
73
80
|
)
|
|
74
81
|
end
|
|
75
82
|
|
|
83
|
+
# Defined types: subject from the stored `subject` slot, document through the email
|
|
84
|
+
# composer, then the CT fallback document, then the generic ERBs (design D20).
|
|
85
|
+
def render_defined_email(address, definition)
|
|
86
|
+
subject = slot("subject") || @communication.title.to_s
|
|
87
|
+
output = DefinedEmailRenderer.render(communication: @communication, definition:)
|
|
88
|
+
RenderedPayload.build(
|
|
89
|
+
recipient_address: address, subject:, text_body: output.text, html_body: output.html,
|
|
90
|
+
headers: UnsubscribeHeaders.call(
|
|
91
|
+
declaration: NotificationTypes.lookup(@communication.notification_type_key),
|
|
92
|
+
user_id: @communication.user_id
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
rescue StandardError => error
|
|
96
|
+
log_erb_fallback(error)
|
|
97
|
+
render_generic_email(address, subject:)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The declared MessageDefinition class for a defined communication, else nil.
|
|
101
|
+
def email_definition
|
|
102
|
+
return nil unless @communication.message_definition_backed?
|
|
103
|
+
|
|
104
|
+
name = NotificationTypes.lookup(@communication.notification_type_key).message
|
|
105
|
+
name.presence&.constantize
|
|
106
|
+
rescue StandardError
|
|
107
|
+
nil
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def log_erb_fallback(error)
|
|
111
|
+
Rails.logger.warn(
|
|
112
|
+
"[command_tower] email_erb_fallback communication_id=#{@communication.id} " \
|
|
113
|
+
"notification_type_key=#{@communication.notification_type_key} error_class=#{error.class.name}"
|
|
114
|
+
)
|
|
115
|
+
end
|
|
116
|
+
|
|
76
117
|
def render_sms(address)
|
|
77
|
-
body = render_template("sms.text.erb").to_s.strip
|
|
118
|
+
body = slot("sms_body") ? with_deep_link(slot("sms_body")) : render_template("sms.text.erb").to_s.strip
|
|
78
119
|
raise ArgumentError, "body is required" if body.empty?
|
|
79
120
|
|
|
80
121
|
RenderedSmsPayload.build(
|
|
@@ -84,8 +125,13 @@ module CommandTower
|
|
|
84
125
|
end
|
|
85
126
|
|
|
86
127
|
def render_pushover(address)
|
|
87
|
-
|
|
88
|
-
|
|
128
|
+
if slot("pushover_message")
|
|
129
|
+
title = slot("pushover_title") || slot("headline") || notification_title
|
|
130
|
+
message = with_deep_link(slot("pushover_message"))
|
|
131
|
+
else
|
|
132
|
+
title = notification_title
|
|
133
|
+
message = render_template("pushover.text.erb").to_s.strip
|
|
134
|
+
end
|
|
89
135
|
raise ArgumentError, "message is required" if message.empty?
|
|
90
136
|
|
|
91
137
|
RenderedPushoverPayload.build(
|
|
@@ -96,8 +142,13 @@ module CommandTower
|
|
|
96
142
|
end
|
|
97
143
|
|
|
98
144
|
def render_push(address)
|
|
99
|
-
|
|
100
|
-
|
|
145
|
+
if slot("headline")
|
|
146
|
+
title = slot("headline")
|
|
147
|
+
body = slot("push_body") || title
|
|
148
|
+
else
|
|
149
|
+
title = notification_title
|
|
150
|
+
body = render_template("push.text.erb").to_s.strip
|
|
151
|
+
end
|
|
101
152
|
raise ArgumentError, "body is required" if body.empty?
|
|
102
153
|
|
|
103
154
|
deep_link = resolve_push_deep_link
|
|
@@ -115,6 +166,22 @@ module CommandTower
|
|
|
115
166
|
)
|
|
116
167
|
end
|
|
117
168
|
|
|
169
|
+
# Stored slot text for a MessageDefinition-backed communication (D24); nil for undefined
|
|
170
|
+
# types and blank slots, which keep the generic ERBs. Slots are stored at Accept, never re-run.
|
|
171
|
+
def slot(name)
|
|
172
|
+
return nil unless @communication.message_definition_backed?
|
|
173
|
+
|
|
174
|
+
stored = @communication.slots
|
|
175
|
+
return nil unless stored.respond_to?(:to_h)
|
|
176
|
+
|
|
177
|
+
stored.to_h.transform_keys(&:to_s)[name].to_s.strip.presence
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def with_deep_link(text)
|
|
181
|
+
link = deep_link_from_metadata
|
|
182
|
+
link ? "#{text} #{link}" : text
|
|
183
|
+
end
|
|
184
|
+
|
|
118
185
|
def notification_title
|
|
119
186
|
title = @communication.title.to_s.strip
|
|
120
187
|
return title unless title.empty?
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "erb"
|
|
4
|
+
|
|
5
|
+
module CommandTower
|
|
6
|
+
module Messaging
|
|
7
|
+
module Rendering
|
|
8
|
+
# The compliance footer of a defined-type email (design D21, ux states E-01, E-03, E-06),
|
|
9
|
+
# in the composed and the fallback email alike. Three copies: mandatory ("about your
|
|
10
|
+
# account"), promotional (adds the postal address) and transactional. The preferences link
|
|
11
|
+
# is `composed_url + preferences_path`, else `composed_url`. The footer never carries a
|
|
12
|
+
# token and is identical whether or not the one-click headers are present.
|
|
13
|
+
class ComplianceFooter
|
|
14
|
+
Output = Data.define(:html, :text)
|
|
15
|
+
|
|
16
|
+
PREFERENCES_LABEL = "Manage email preferences"
|
|
17
|
+
PROMOTIONAL_LABEL = "Unsubscribe or manage email preferences"
|
|
18
|
+
|
|
19
|
+
# @return [Output]
|
|
20
|
+
def self.call(declaration:, definition:)
|
|
21
|
+
new(declaration:, definition:).call
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(declaration:, definition:)
|
|
25
|
+
@declaration = declaration
|
|
26
|
+
@definition = definition
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def call
|
|
30
|
+
lines = [copy]
|
|
31
|
+
lines << [postal_address, nil] if promotional?
|
|
32
|
+
Output.new(html: html_for(lines), text: text_for(lines))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
attr_reader :declaration, :definition
|
|
38
|
+
|
|
39
|
+
def promotional?
|
|
40
|
+
!declaration.mandatory && definition.promotional?
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [Array(String, String)] the sentence and the link label
|
|
44
|
+
def copy
|
|
45
|
+
app = CommandTower.app_name_for_comms
|
|
46
|
+
return ["#{app} sends this email about your account.", PREFERENCES_LABEL] if declaration.mandatory
|
|
47
|
+
|
|
48
|
+
because = "You're getting this email because"
|
|
49
|
+
if promotional?
|
|
50
|
+
["#{because} #{declaration.label.to_s.downcase} are turned on for your #{app} account.", PROMOTIONAL_LABEL]
|
|
51
|
+
else
|
|
52
|
+
["#{because} #{declaration.label} emails are turned on for your #{app} account.", PREFERENCES_LABEL]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def postal_address
|
|
57
|
+
CommandTower.config.messaging.compliance.postal_address.to_s
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def preferences_url
|
|
61
|
+
"#{CommandTower.config.application.composed_url}#{CommandTower.config.messaging.compliance.preferences_path}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Trusted, escaped HTML: every value goes through html_escape.
|
|
65
|
+
def html_for(lines)
|
|
66
|
+
lines.map do |text, label|
|
|
67
|
+
escaped = ERB::Util.html_escape(text)
|
|
68
|
+
next escaped unless label
|
|
69
|
+
|
|
70
|
+
%(#{escaped} <a href="#{ERB::Util.html_escape(preferences_url)}">#{ERB::Util.html_escape(label)}</a>)
|
|
71
|
+
end.join("<br>\n")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def text_for(lines)
|
|
75
|
+
lines.map { |text, label| label ? "#{text} #{label}: #{preferences_url}" : text }.join("\n")
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|