command_tower 0.23.0 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/command_tower/admin/home_notices_controller.rb +39 -0
- data/app/controllers/command_tower/me/home_notices_controller.rb +79 -0
- data/app/deserializers/command_tower/deserializers/admin/home_notices/control_deserializer.rb +40 -0
- data/app/deserializers/command_tower/deserializers/home_notices/notice_id_deserializer.rb +42 -0
- data/app/errors/command_tower/errors/home_notices/linked_message_unavailable_error.rb +23 -0
- data/app/errors/command_tower/errors/home_notices/rate_limit_error.rb +25 -0
- data/app/errors/command_tower/errors/home_notices/unavailable_error.rb +22 -0
- data/app/jobs/command_tower/home_notices/prune_ended_job.rb +14 -0
- data/app/models/command_tower/home_notice.rb +41 -0
- data/app/models/command_tower/home_notice_control.rb +12 -0
- data/app/serializers/command_tower/serializers/home_notices/control_serializer.rb +14 -0
- data/app/serializers/command_tower/serializers/home_notices/dismissal_serializer.rb +19 -0
- data/app/serializers/command_tower/serializers/home_notices/mutation_serializer.rb +16 -0
- data/app/serializers/command_tower/serializers/home_notices/notice_serializer.rb +35 -0
- data/app/serializers/command_tower/serializers/home_notices/selection_serializer.rb +21 -0
- data/app/services/command_tower/home_notices/campaign.rb +28 -0
- data/app/services/command_tower/home_notices/campaigns/update_app.rb +77 -0
- data/app/services/command_tower/home_notices/catalog/validator.rb +92 -0
- data/app/services/command_tower/home_notices/catalog.rb +92 -0
- data/app/services/command_tower/home_notices/context.rb +10 -0
- data/app/services/command_tower/home_notices/delivery.rb +43 -0
- data/app/services/command_tower/home_notices/notice.rb +88 -0
- data/app/services/command_tower/home_notices/notice_record.rb +28 -0
- data/app/services/command_tower/messaging/contract/inbox.rb +29 -0
- data/app/services/command_tower/messaging/contract/requests/document_for_communication.rb +24 -0
- data/app/services/command_tower/messaging/contract/results/inbox_document.rb +17 -0
- data/app/services/command_tower/services/auth/purge_user_associated_data.rb +6 -0
- data/app/services/command_tower/services/home_notices/acknowledge.rb +149 -0
- data/app/services/command_tower/services/home_notices/check_mutation_eligibility.rb +84 -0
- data/app/services/command_tower/services/home_notices/check_rate_limit.rb +52 -0
- data/app/services/command_tower/services/home_notices/compose.rb +113 -0
- data/app/services/command_tower/services/home_notices/deliver.rb +111 -0
- data/app/services/command_tower/services/home_notices/dismiss.rb +147 -0
- data/app/services/command_tower/services/home_notices/load_candidates.rb +85 -0
- data/app/services/command_tower/services/home_notices/pick_notices.rb +139 -0
- data/app/services/command_tower/services/home_notices/prune_ended.rb +93 -0
- data/app/services/command_tower/services/home_notices/read_controls.rb +31 -0
- data/app/services/command_tower/services/home_notices/record_impression.rb +154 -0
- data/app/services/command_tower/services/home_notices/resolve_notice.rb +69 -0
- data/app/services/command_tower/services/home_notices/select.rb +136 -0
- data/app/services/command_tower/services/home_notices/update_control.rb +56 -0
- data/app/workflows/command_tower/workflows/admin/home_notices/update_control_workflow.rb +64 -0
- data/app/workflows/command_tower/workflows/home_notices/acknowledge_workflow.rb +140 -0
- data/app/workflows/command_tower/workflows/home_notices/dismiss_workflow.rb +107 -0
- data/app/workflows/command_tower/workflows/home_notices/prune_ended_workflow.rb +25 -0
- data/app/workflows/command_tower/workflows/home_notices/record_impression_workflow.rb +92 -0
- data/app/workflows/command_tower/workflows/home_notices/show_workflow.rb +44 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20261124200001_create_command_tower_home_notices.rb +43 -0
- data/db/migrate/20261124200002_create_command_tower_home_notice_controls.rb +12 -0
- data/docs/api_reference.md +108 -1
- data/docs/audit.md +12 -0
- data/docs/bootstrap/08-initializer.md +15 -0
- data/docs/bootstrap/09-rbac.md +6 -0
- data/docs/bootstrap/13-optional.md +12 -0
- data/docs/extending.md +2 -0
- data/docs/home_notices.md +236 -0
- data/docs/messaging_integration_guide.md +4 -0
- data/docs/models.md +4 -0
- data/docs/principal_capabilities.md +1 -0
- data/docs/ui.md +13 -0
- data/docs/upgrades/0.24.0.md +50 -0
- data/lib/command_tower/authorization/default.yml +10 -0
- data/lib/command_tower/configuration/config.rb +6 -0
- data/lib/command_tower/configuration/home_notices/config.rb +67 -0
- data/lib/command_tower/configuration/registry/audit/config.rb +61 -0
- data/lib/command_tower/configuration/registry/principal_capabilities/config.rb +1 -1
- data/lib/command_tower/engine.rb +1 -0
- data/lib/command_tower/install/baseline.rb +2 -0
- data/lib/command_tower/ui/document.rb +26 -5
- data/lib/command_tower/version.rb +1 -1
- data/lib/tasks/home_notices.rake +11 -0
- metadata +53 -2
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module HomeNotices
|
|
5
|
+
# Boot-time registry of host notices. `load!` constantizes `config.home_notices.notices`, checks
|
|
6
|
+
# every class (see `Catalog::Validator`) and raises `Catalog::InvalidNoticeError` on the first
|
|
7
|
+
# problem, in every environment. Runs from the engine's `to_prepare` only when
|
|
8
|
+
# `config.home_notices.enabled`.
|
|
9
|
+
class Catalog
|
|
10
|
+
class InvalidNoticeError < StandardError; end
|
|
11
|
+
|
|
12
|
+
# Platform notices. Re-loading a list that already holds them (the registered `notices`)
|
|
13
|
+
# does not register them twice.
|
|
14
|
+
BUILTIN = [Campaigns::UpdateApp].freeze
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# @param class_names [Array<String>] defaults to `config.home_notices.notices`
|
|
18
|
+
# @return [Array<Class>] the validated notice classes
|
|
19
|
+
def load!(class_names = CommandTower.config.home_notices.notices)
|
|
20
|
+
classes = validated_classes(class_names)
|
|
21
|
+
validate_unique_keys!(classes)
|
|
22
|
+
|
|
23
|
+
@notices = classes.freeze
|
|
24
|
+
@campaigns_by_key = classes.select { |klass| klass < Campaign }.index_by(&:key).freeze
|
|
25
|
+
@notices
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def reset!
|
|
29
|
+
@notices = nil
|
|
30
|
+
@campaigns_by_key = nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def notices
|
|
34
|
+
@notices || []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Deliveries are not indexed: they are found through their persisted rows.
|
|
38
|
+
def campaign(key)
|
|
39
|
+
(@campaigns_by_key || {})[key.to_s]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def find(key)
|
|
43
|
+
notices.find { |klass| klass.key == key.to_s }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def registered?(key)
|
|
47
|
+
!find(key).nil?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def validate!(klass, builtin: false)
|
|
51
|
+
errors = Validator.errors_for(klass, builtin:)
|
|
52
|
+
return klass if errors.empty?
|
|
53
|
+
|
|
54
|
+
raise InvalidNoticeError, "#{klass.name || klass.inspect}: #{errors.join('; ')}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
# The host classes, then the platform notices that are switched on, each checked.
|
|
60
|
+
def validated_classes(class_names)
|
|
61
|
+
host = Array(class_names).map { |name| constantize!(name) } - BUILTIN
|
|
62
|
+
host.each { |klass| validate!(klass) }
|
|
63
|
+
builtin = builtin_classes
|
|
64
|
+
builtin.each { |klass| validate!(klass, builtin: true) }
|
|
65
|
+
host + builtin
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Platform notices, which alone may use the reserved `ct.` key prefix.
|
|
69
|
+
def builtin_classes
|
|
70
|
+
return [] unless CommandTower.config.home_notices.update_app_campaign
|
|
71
|
+
|
|
72
|
+
BUILTIN
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def constantize!(name)
|
|
76
|
+
return name if name.is_a?(Class)
|
|
77
|
+
|
|
78
|
+
name.to_s.constantize
|
|
79
|
+
rescue NameError
|
|
80
|
+
raise InvalidNoticeError, "config.home_notices.notices names an unknown class: #{name}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def validate_unique_keys!(classes)
|
|
84
|
+
duplicate = classes.group_by(&:key).find { |_key, group| group.size > 1 }
|
|
85
|
+
return if duplicate.nil?
|
|
86
|
+
|
|
87
|
+
raise InvalidNoticeError, "duplicate notice key: #{duplicate.first}"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module HomeNotices
|
|
5
|
+
# Per-request facts passed to `show?`, `compose` and `period_key`.
|
|
6
|
+
Context = Data.define(
|
|
7
|
+
:now, :time_zone, :app_version, :platform, :ui_catalog, :client_compatibility, :impersonating
|
|
8
|
+
)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module HomeNotices
|
|
5
|
+
# A notice a host workflow pushes to one user (for example "class canceled") through
|
|
6
|
+
# `Deliver`. Adds the composer macro.
|
|
7
|
+
class Delivery < Notice
|
|
8
|
+
COMPOSERS = %i[message].freeze
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
def composer(value = nil)
|
|
12
|
+
return inherited_setting(:@composer) if value.nil?
|
|
13
|
+
|
|
14
|
+
unless COMPOSERS.include?(value)
|
|
15
|
+
raise ArgumentError, "composer must be one of #{COMPOSERS.inspect} (got #{value.inspect})"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
@composer = value
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Default for a linked delivery (`composer :message`): shown while the user has not viewed the
|
|
23
|
+
# linked inbox item. False when it is viewed (true) or unavailable (nil: missing, deleted,
|
|
24
|
+
# foreign or not inbox-backed). Hosts may override and call `super`. An unlinked delivery
|
|
25
|
+
# has no default meaning, so it is shown; the catalog requires it to override `show?`.
|
|
26
|
+
def show?(user:, context:, notice:) # rubocop:disable Lint/UnusedMethodArgument
|
|
27
|
+
return true unless self.class.composer == :message
|
|
28
|
+
return false if notice&.communication_id.nil?
|
|
29
|
+
|
|
30
|
+
unviewed?(user, notice.communication_id)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def unviewed?(user, communication_id)
|
|
36
|
+
request = CommandTower::Messaging::Contract::Requests::ViewedForCommunication.build(
|
|
37
|
+
communication_id:, recipient_id: user.id
|
|
38
|
+
)
|
|
39
|
+
CommandTower::Messaging::Contract::Inbox.viewed_for_communication?(request) == false
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module HomeNotices
|
|
5
|
+
# Abstract base class for host-defined home notices. A notice declares its contract with class
|
|
6
|
+
# macros and describes its behaviour with the overridable instance methods `show?` and `compose`.
|
|
7
|
+
# Hosts subclass `Campaign` or `Delivery`, never `Notice` directly. Pacing, windows, selection and
|
|
8
|
+
# audit are platform services; this class holds no state per user and its methods are pure.
|
|
9
|
+
#
|
|
10
|
+
# class MakeYourPicks < CommandTower::HomeNotices::Campaign
|
|
11
|
+
# key "pickem.make_your_picks"
|
|
12
|
+
# severity :dismissible
|
|
13
|
+
# priority 50
|
|
14
|
+
# impressions max: 4, cooldown: 12.hours
|
|
15
|
+
# end
|
|
16
|
+
class Notice
|
|
17
|
+
SEVERITIES = %i[blocking dismissible].freeze
|
|
18
|
+
Window = Data.define(:starts_at, :ends_at)
|
|
19
|
+
|
|
20
|
+
class << self
|
|
21
|
+
# Macro/reader. With an argument it sets, without it reads. Subclasses inherit.
|
|
22
|
+
def key(value = nil)
|
|
23
|
+
return inherited_setting(:@key) if value.nil?
|
|
24
|
+
|
|
25
|
+
@key = value.to_s
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def severity(value = nil)
|
|
29
|
+
return inherited_setting(:@severity) if value.nil?
|
|
30
|
+
|
|
31
|
+
unless SEVERITIES.include?(value)
|
|
32
|
+
raise ArgumentError, "severity must be one of #{SEVERITIES.inspect} (got #{value.inspect})"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
@severity = value
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def priority(value = nil)
|
|
39
|
+
return inherited_setting(:@priority) if value.nil?
|
|
40
|
+
|
|
41
|
+
raise ArgumentError, "priority must be an Integer (got #{value.inspect})" unless value.is_a?(Integer)
|
|
42
|
+
|
|
43
|
+
@priority = value
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def blocking_reason(value = nil)
|
|
47
|
+
return inherited_setting(:@blocking_reason) if value.nil?
|
|
48
|
+
|
|
49
|
+
@blocking_reason = value.to_s
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Absolute UTC window. Either bound may be nil (open ended).
|
|
53
|
+
def window(starts_at: nil, ends_at: nil)
|
|
54
|
+
return inherited_setting(:@window) if starts_at.nil? && ends_at.nil?
|
|
55
|
+
|
|
56
|
+
@window = Window.new(starts_at:, ends_at:)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def blocking?
|
|
60
|
+
severity == :blocking
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def dismissible?
|
|
64
|
+
severity == :dismissible
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def inherited_setting(ivar)
|
|
70
|
+
return instance_variable_get(ivar) if instance_variable_defined?(ivar)
|
|
71
|
+
|
|
72
|
+
superclass.respond_to?(:key, true) ? superclass.send(:inherited_setting, ivar) : nil
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Whether the notice applies to the user right now. No network I/O. `notice` is a read-only
|
|
77
|
+
# NoticeRecord, or nil for an unmaterialized campaign.
|
|
78
|
+
def show?(user:, context:, notice:) # rubocop:disable Lint/UnusedMethodArgument
|
|
79
|
+
true
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Builds the notice content on the document builder.
|
|
83
|
+
def compose(_builder, user:, context:, notice:) # rubocop:disable Lint/UnusedMethodArgument
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module HomeNotices
|
|
5
|
+
# Read-only snapshot of a persisted `HomeNotice` row, handed to `show?` and `compose` so host
|
|
6
|
+
# code cannot mutate platform state.
|
|
7
|
+
NoticeRecord = Data.define(
|
|
8
|
+
:public_id, :kind, :key, :campaign_key, :period_key, :identity, :communication_id,
|
|
9
|
+
:starts_at, :ends_at, :metadata, :impression_count, :first_shown_at, :last_shown_at,
|
|
10
|
+
:acknowledged_at, :dismissed_until
|
|
11
|
+
) do
|
|
12
|
+
# @param record [CommandTower::HomeNotice]
|
|
13
|
+
def self.from(record)
|
|
14
|
+
new(**members.index_with { |member| record.public_send(member) }).then do |snapshot|
|
|
15
|
+
snapshot.with(metadata: deep_freeze(snapshot.metadata))
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.deep_freeze(value)
|
|
20
|
+
case value
|
|
21
|
+
when Hash then value.each_value { |v| deep_freeze(v) }
|
|
22
|
+
when Array then value.each { |v| deep_freeze(v) }
|
|
23
|
+
end
|
|
24
|
+
value.freeze
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -31,8 +31,37 @@ module CommandTower
|
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# Read-only, no audit. Returns Results::InboxDocument carrying the catalog 2
|
|
35
|
+
# UI::Document, or nil when the communication is missing, not the recipient's,
|
|
36
|
+
# deleted or archived, not inbox-backed, or its type has no MessageDefinition.
|
|
37
|
+
def document_for_communication(request)
|
|
38
|
+
guarded("inbox.document_for_communication", request, Requests::DocumentForCommunication) do
|
|
39
|
+
item = CommandTower::Messaging::Inbox::Scope.find_for_recipient_communication(
|
|
40
|
+
recipient_id: request.recipient_id,
|
|
41
|
+
communication_id: request.communication_id
|
|
42
|
+
)
|
|
43
|
+
item && inbox_document(item, request)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
34
47
|
private
|
|
35
48
|
|
|
49
|
+
def inbox_document(item, request)
|
|
50
|
+
return nil if item.deleted? || item.archived?
|
|
51
|
+
|
|
52
|
+
communication = item.communication
|
|
53
|
+
return nil unless communication.message_definition_backed?
|
|
54
|
+
|
|
55
|
+
document = CommandTower::Messaging::Rendering::InboxDocumentRenderer.render(
|
|
56
|
+
communication:, recipient_id: request.recipient_id, ui_catalog: 2, impersonating: request.impersonating
|
|
57
|
+
)
|
|
58
|
+
return nil unless document.is_a?(CommandTower::UI::Document)
|
|
59
|
+
|
|
60
|
+
Results::InboxDocument.new(
|
|
61
|
+
communication_id: communication.id, inbox_item_id: item.id, document:
|
|
62
|
+
).freeze
|
|
63
|
+
end
|
|
64
|
+
|
|
36
65
|
def guarded(operation, request, expected_class, &)
|
|
37
66
|
ensure_request!(request, expected_class)
|
|
38
67
|
validate!(request)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Contract
|
|
6
|
+
module Requests
|
|
7
|
+
# impersonating follows the renderer: anything but exactly false redacts `code` values.
|
|
8
|
+
DocumentForCommunication = Data.define(
|
|
9
|
+
:communication_id,
|
|
10
|
+
:recipient_id,
|
|
11
|
+
:impersonating
|
|
12
|
+
) do
|
|
13
|
+
def self.build(communication_id:, recipient_id:, impersonating: nil)
|
|
14
|
+
new(
|
|
15
|
+
communication_id:,
|
|
16
|
+
recipient_id:,
|
|
17
|
+
impersonating:
|
|
18
|
+
).freeze
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Messaging
|
|
5
|
+
module Contract
|
|
6
|
+
module Results
|
|
7
|
+
# The catalog 2 document (a CommandTower::UI::Document, already redacted per the
|
|
8
|
+
# request's impersonating flag) of a definition-backed communication's inbox item.
|
|
9
|
+
InboxDocument = Data.define(
|
|
10
|
+
:communication_id,
|
|
11
|
+
:inbox_item_id,
|
|
12
|
+
:document
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -10,11 +10,17 @@ module CommandTower
|
|
|
10
10
|
purge_user_secrets!
|
|
11
11
|
purge_messaging_inbox!
|
|
12
12
|
purge_messaging_endpoints!
|
|
13
|
+
purge_home_notices!
|
|
13
14
|
revoke_impersonation_sessions!
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
private
|
|
17
18
|
|
|
19
|
+
# Per-user notice state only. Audit events (home_notice_acknowledged) are a separate record and stay.
|
|
20
|
+
def purge_home_notices!
|
|
21
|
+
CommandTower::HomeNotice.where(user_id: user.id).delete_all
|
|
22
|
+
end
|
|
23
|
+
|
|
18
24
|
def purge_user_secrets!
|
|
19
25
|
UserSecret.where(user_id: user.id).delete_all
|
|
20
26
|
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# Acknowledges one resolved notice for its user (design D3, D13, §3.4).
|
|
7
|
+
#
|
|
8
|
+
# Inputs: `user`, `notice_class`, `row` and `period_key` (from `ResolveNotice`; the row is nil
|
|
9
|
+
# for a campaign with no row in the current period yet) and `request_context` (a
|
|
10
|
+
# HomeNotices::Context; `now` is the clock). Eligibility is the caller's job
|
|
11
|
+
# (`CheckMutationEligibility`). It never runs `show?`: a user who saw a notice can always clear
|
|
12
|
+
# it, even when `show?` is now false or raises.
|
|
13
|
+
#
|
|
14
|
+
# The decision to audit comes from an atomic write, never from a read before it, so concurrent
|
|
15
|
+
# requests acknowledge and audit exactly once. The write and the audit share one transaction:
|
|
16
|
+
# - existing row: `acknowledged_at` is set only `WHERE acknowledged_at IS NULL`. Exactly one row
|
|
17
|
+
# changed means this request acknowledged it and `home_notice_acknowledged` is audited. No row
|
|
18
|
+
# changed means it was already acknowledged: an idempotent success with no audit.
|
|
19
|
+
# - campaign with no row: `insert!` creates the row already acknowledged. On `RecordNotUnique`
|
|
20
|
+
# a concurrent request created it first, and this one continues as the existing-row case.
|
|
21
|
+
#
|
|
22
|
+
# A blocking notice also stores the content hash of the document it showed (HMAC from
|
|
23
|
+
# `Compose`) in `acknowledged_content_hash`. The hash is composed before the transaction opens
|
|
24
|
+
# (compose runs host code) and is nil when the document cannot be composed. It is never put
|
|
25
|
+
# in audit metadata (design D4).
|
|
26
|
+
#
|
|
27
|
+
# Nothing is written while impersonating or when impersonation is unknown (`impersonating` is
|
|
28
|
+
# not `false`): the result fails with `ForbiddenError`.
|
|
29
|
+
#
|
|
30
|
+
# Success data is `{ acknowledged: true, recorded:, public_id: }`. `recorded` is true only when
|
|
31
|
+
# this request made the change (and audited it).
|
|
32
|
+
class Acknowledge < CommandTower::Services::ApplicationService
|
|
33
|
+
validate :user, is_a: User, required: true
|
|
34
|
+
validate :notice_class, is_a: Class, required: true
|
|
35
|
+
validate :row, is_a: [CommandTower::HomeNotice, NilClass], required: false
|
|
36
|
+
validate :period_key, is_a: [String, NilClass], required: false
|
|
37
|
+
validate :request_context, is_a: CommandTower::HomeNotices::Context, required: true
|
|
38
|
+
|
|
39
|
+
def call
|
|
40
|
+
return forbidden unless request_context.impersonating == false
|
|
41
|
+
return not_found if row.nil? && !campaign?
|
|
42
|
+
|
|
43
|
+
hash = content_hash
|
|
44
|
+
transaction { acknowledge(hash) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def now
|
|
50
|
+
request_context.now
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def campaign?
|
|
54
|
+
notice_class < CommandTower::HomeNotices::Campaign
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def stored_period_key
|
|
58
|
+
CommandTower::HomeNotice.stored_period_key(period_key)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Only a blocking acknowledgement keeps a hash.
|
|
62
|
+
def content_hash
|
|
63
|
+
return nil unless notice_class.blocking?
|
|
64
|
+
|
|
65
|
+
snapshot = row && CommandTower::HomeNotices::NoticeRecord.from(row)
|
|
66
|
+
CommandTower::Services::HomeNotices::Compose.call(
|
|
67
|
+
notice_class:, user:, request_context:, record: snapshot
|
|
68
|
+
).data[:content_hash]
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def acknowledge(hash)
|
|
72
|
+
return acknowledge_existing(row.id, row.public_id, hash) if row
|
|
73
|
+
|
|
74
|
+
public_id = insert_campaign_row(hash)
|
|
75
|
+
return acknowledged!(public_id) if public_id
|
|
76
|
+
|
|
77
|
+
id, existing_public_id = campaign_scope.lock.pick(:id, :public_id)
|
|
78
|
+
acknowledge_existing(id, existing_public_id, hash)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def campaign_scope
|
|
82
|
+
CommandTower::HomeNotice.where(
|
|
83
|
+
user_id: user.id, campaign_key: notice_class.key, period_key: stored_period_key
|
|
84
|
+
)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The new row's public id, or nil when the row already exists.
|
|
88
|
+
def insert_campaign_row(hash)
|
|
89
|
+
attributes = new_campaign_attributes(hash)
|
|
90
|
+
CommandTower::HomeNotice.transaction(requires_new: true) do
|
|
91
|
+
CommandTower::HomeNotice.insert!(attributes) # rubocop:disable Rails/SkipsModelValidations
|
|
92
|
+
end
|
|
93
|
+
attributes[:public_id]
|
|
94
|
+
rescue ActiveRecord::RecordNotUnique
|
|
95
|
+
nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def new_campaign_attributes(hash)
|
|
99
|
+
{
|
|
100
|
+
public_id: SecureRandom.alphanumeric(32), user_id: user.id, kind: "campaign", key: notice_class.key,
|
|
101
|
+
campaign_key: notice_class.key, period_key: stored_period_key,
|
|
102
|
+
acknowledged_at: now, acknowledged_content_hash: hash
|
|
103
|
+
}
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def acknowledge_existing(id, public_id, hash)
|
|
107
|
+
changed = CommandTower::HomeNotice
|
|
108
|
+
.where(id:, acknowledged_at: nil)
|
|
109
|
+
.update_all(acknowledged_at: now, acknowledged_content_hash: hash, updated_at: now) # rubocop:disable Rails/SkipsModelValidations
|
|
110
|
+
return acknowledged!(public_id) if changed == 1
|
|
111
|
+
|
|
112
|
+
finish(recorded: false, public_id:)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def acknowledged!(public_id)
|
|
116
|
+
audit(
|
|
117
|
+
:home_notice_acknowledged,
|
|
118
|
+
subject: user, affected_user: user, changes: {}, metadata: audit_metadata(public_id)
|
|
119
|
+
)
|
|
120
|
+
finish(recorded: true, public_id:)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Ids only (design §3.5): never the content hash.
|
|
124
|
+
def audit_metadata(public_id)
|
|
125
|
+
{
|
|
126
|
+
notice_id: public_id,
|
|
127
|
+
key: notice_class.key,
|
|
128
|
+
kind: campaign? ? "campaign" : "delivery",
|
|
129
|
+
severity: notice_class.severity.to_s
|
|
130
|
+
}
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def finish(recorded:, public_id:)
|
|
134
|
+
context.acknowledged = true
|
|
135
|
+
context.recorded = recorded
|
|
136
|
+
context.public_id = public_id
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def forbidden
|
|
140
|
+
context.fail!(application_error: CommandTower::Errors::ForbiddenError.new)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def not_found
|
|
144
|
+
context.fail!(application_error: CommandTower::Errors::NotFoundError.new)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# Re-checks a resolved notice before a mutation (design §3.4 "Mutation re-checks"). It never
|
|
7
|
+
# runs `show?`: a user who saw a notice can always clear it.
|
|
8
|
+
#
|
|
9
|
+
# Inputs: `notice_class` and `row` (from `ResolveNotice`; the row is nil for a campaign with no
|
|
10
|
+
# row yet) and `request_context` (a HomeNotices::Context; `now` is the clock). Ownership is
|
|
11
|
+
# settled by `ResolveNotice`, which only finds the user's own rows.
|
|
12
|
+
#
|
|
13
|
+
# Checks, in order:
|
|
14
|
+
# - registration: the class is registered, and its kind matches the row (a delivery needs a row)
|
|
15
|
+
# - kill switch: an absent control row means enabled
|
|
16
|
+
# - window: the class `window` and the row's own `starts_at` / `ends_at`
|
|
17
|
+
# (`starts_at` inclusive, `ends_at` exclusive)
|
|
18
|
+
# - pacing state, only with `for_impression: true`: not acknowledged and not dismissed into the
|
|
19
|
+
# future. Period max and item cooldown are not re-checked: `Select` applied them when it
|
|
20
|
+
# served the notice, and a repeat impression inside the cooldown is an idempotent
|
|
21
|
+
# `recorded: false`, not a 404.
|
|
22
|
+
#
|
|
23
|
+
# Success data is `{}`. An ineligible notice fails with `NotFoundError`. When the controls
|
|
24
|
+
# cannot be read the result fails with the `InternalError` from `ReadControls` (the caller maps
|
|
25
|
+
# it to 503 `home_notices_unavailable`).
|
|
26
|
+
class CheckMutationEligibility < CommandTower::Services::ApplicationService
|
|
27
|
+
validate :notice_class, is_a: [Class, NilClass], required: false
|
|
28
|
+
validate :row, is_a: [CommandTower::HomeNotice, NilClass], required: false
|
|
29
|
+
validate :request_context, is_a: CommandTower::HomeNotices::Context, required: true
|
|
30
|
+
validate :for_impression, is_one: [true, false], default: false
|
|
31
|
+
|
|
32
|
+
def call
|
|
33
|
+
return not_found unless registered?
|
|
34
|
+
|
|
35
|
+
check_controls
|
|
36
|
+
not_found unless inside_window? && (!for_impression || open_state?)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def now
|
|
42
|
+
request_context.now
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def registered?
|
|
46
|
+
return false if notice_class.nil? || !CommandTower::HomeNotices::Catalog.registered?(notice_class.key)
|
|
47
|
+
|
|
48
|
+
notice_class < CommandTower::HomeNotices::Delivery ? !row.nil? : campaign_row_matches?
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def campaign_row_matches?
|
|
52
|
+
row.nil? || row.campaign_key == notice_class.key
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def check_controls
|
|
56
|
+
result = ReadControls.call(keys: [notice_class.key])
|
|
57
|
+
return not_found if result.success? && result.data[:disabled_keys].include?(notice_class.key)
|
|
58
|
+
return if result.success?
|
|
59
|
+
|
|
60
|
+
context.fail!(application_error: result.errors.first)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def inside_window?
|
|
64
|
+
window = notice_class.window
|
|
65
|
+
return false unless window.nil? || inside?(window.starts_at, window.ends_at)
|
|
66
|
+
|
|
67
|
+
row.nil? || inside?(row.starts_at, row.ends_at)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def inside?(starts_at, ends_at)
|
|
71
|
+
(starts_at.nil? || starts_at <= now) && (ends_at.nil? || ends_at > now)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def open_state?
|
|
75
|
+
row.nil? || (row.acknowledged_at.nil? && (row.dismissed_until.nil? || row.dismissed_until <= now))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def not_found
|
|
79
|
+
context.fail!(application_error: CommandTower::Errors::NotFoundError.new)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module CommandTower
|
|
4
|
+
module Services
|
|
5
|
+
module HomeNotices
|
|
6
|
+
# Rate limits Home Notices requests per user in a 60 second fixed bucket (design D5):
|
|
7
|
+
# - `bucket: :get` (default): `GET /me/home-notices`, `config.home_notices.rate_limit_per_minute`
|
|
8
|
+
# - `bucket: :mutation`: impression, acknowledge and dismiss share one bucket,
|
|
9
|
+
# `config.home_notices.mutation_rate_limit_per_minute`
|
|
10
|
+
#
|
|
11
|
+
# Fails open when Redis is unavailable: an outage must not hide must-see notices. The failure
|
|
12
|
+
# publishes `command_tower.home_notices.rate_limiter_unavailable` (user id only).
|
|
13
|
+
# Over the limit, the result fails with `Errors::HomeNotices::RateLimitError` carrying the
|
|
14
|
+
# seconds until the bucket resets.
|
|
15
|
+
class CheckRateLimit < CommandTower::Services::ApplicationService
|
|
16
|
+
WINDOW_SECONDS = 60
|
|
17
|
+
|
|
18
|
+
validate :user, is_a: User, required: true
|
|
19
|
+
validate :bucket, is_one: %i[get mutation], default: :get
|
|
20
|
+
|
|
21
|
+
def call
|
|
22
|
+
result = CommandTower::Services::RateLimits::Check.call(key:, ttl_seconds: WINDOW_SECONDS)
|
|
23
|
+
return report_unavailable(result) if result.failure?
|
|
24
|
+
return if result.data[:count] <= limit
|
|
25
|
+
|
|
26
|
+
ttl = result.data[:ttl]
|
|
27
|
+
context.fail!(application_error: CommandTower::Errors::HomeNotices::RateLimitError.new(
|
|
28
|
+
retry_after_seconds: ttl.positive? ? ttl : nil
|
|
29
|
+
))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def limit
|
|
35
|
+
config = CommandTower.config.home_notices
|
|
36
|
+
bucket == :mutation ? config.mutation_rate_limit_per_minute : config.rate_limit_per_minute
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def key
|
|
40
|
+
"home-notices:rate:#{bucket}:#{user.id}:#{Time.now.to_i / WINDOW_SECONDS}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def report_unavailable(result)
|
|
44
|
+
error = result.errors.first
|
|
45
|
+
CommandTower::Events.publish(category: :home_notices, name: "rate_limiter_unavailable",
|
|
46
|
+
payload: { user_id: user.id, error_class: (error&.cause || error).class.name,
|
|
47
|
+
log_level: :error })
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|