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

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

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, bus:) ⇒ Accept

Returns a new instance of Accept.



5
6
7
8
# File 'lib/textus/application/writes/accept.rb', line 5

def initialize(ctx:, bus:)
  @ctx = ctx
  @bus = bus
end

Instance Method Details

#call(pending_key) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/textus/application/writes/accept.rb', line 10

def call(pending_key)
  raise ProposalError.new("only human role can accept proposals; got '#{@ctx.role}'") unless @ctx.role == "human"

  env = @ctx.store.get(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"

  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"]
    Composition.writes_put(@ctx).call(target, meta: target_meta, body: target_body)
  when "delete"
    Composition.writes_delete(@ctx).call(target)
  else
    raise ProposalError.new("unknown action: #{action}")
  end

  Composition.writes_delete(@ctx).call(pending_key)

  @bus.publish(:accepted,
               store: @ctx.with_role(@ctx.role),
               key: pending_key,
               target_key: target,
               correlation_id: @ctx.correlation_id)

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