Class: Textus::Application::Writes::Accept

Inherits:
Object
  • Object
show all
Includes:
AuthorityGate
Defined in:
lib/textus/application/writes/accept.rb

Instance Method Summary collapse

Methods included from AuthorityGate

#assert_accept_authority!

Constructor Details

#initialize(ctx:, manifest:, file_store:, schemas:, envelope_io:, bus:, authorizer:, hook_context:) ⇒ Accept

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
15
16
17
18
# File 'lib/textus/application/writes/accept.rb', line 9

def initialize(ctx:, manifest:, file_store:, schemas:, envelope_io:, bus:, authorizer:, hook_context:) # rubocop:disable Metrics/ParameterLists
  @ctx          = ctx
  @manifest     = manifest
  @file_store   = file_store
  @schemas      = schemas
  @envelope_io  = envelope_io
  @bus          = bus
  @authorizer   = authorizer
  @hook_context = hook_context
end

Instance Method Details

#call(pending_key) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/textus/application/writes/accept.rb', line 20

def call(pending_key)
  assert_accept_authority!("accept")

  env = Textus::Application::Reads::Get.new(
    ctx: @ctx, manifest: @manifest, file_store: @file_store,
  ).call(pending_key)
  proposal = env.meta["proposal"] or raise ProposalError.new("entry has no proposal block: #{pending_key}")
  target = proposal["target_key"] or raise ProposalError.new("proposal missing target_key")
  action = proposal["action"] || "put"

  evaluate_promotion!(env, target)

  case action
  when "put"
    # Nested proposal "frontmatter" — the meta to write to the accepted
    # target. Not related to the removed intake-handler legacy bridge.
    target_meta = env.meta["frontmatter"] || {}
    target_body = env.body
    put_op.call(target, meta: target_meta, body: target_body)
  when "delete"
    delete_op.call(target)
  else
    raise ProposalError.new("unknown action: #{action}")
  end

  delete_op.call(pending_key)

  @bus.publish(:proposal_accepted,
               ctx: @hook_context,
               key: pending_key,
               target_key: target)

  { "protocol" => PROTOCOL, "accepted" => pending_key, "target_key" => target, "action" => action }
end