Class: CodexNotify::HookThreadPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/codex_notify/hook_thread_publisher.rb

Constant Summary collapse

STALE_THREAD_ERROR_CODES =
%w[thread_not_found message_not_found invalid_ts].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, store:, outbox: nil, channel: nil) ⇒ HookThreadPublisher

Returns a new instance of HookThreadPublisher.



11
12
13
14
15
# File 'lib/codex_notify/hook_thread_publisher.rb', line 11

def initialize(client:, store:, outbox: nil, channel: nil)
  @client = client
  @store = store
  @durable = DurableSlackPublisher.new(client:, store:, outbox:, channel:) if outbox
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



17
18
19
# File 'lib/codex_notify/hook_thread_publisher.rb', line 17

def client
  @client
end

Instance Method Details

#drainObject



74
75
76
# File 'lib/codex_notify/hook_thread_publisher.rb', line 74

def drain
  @durable&.drain
end

#ensure_thread(session_id:, root_message:) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/codex_notify/hook_thread_publisher.rb', line 19

def ensure_thread(session_id:, root_message:)
  return @durable.ensure_thread(key: session_id, root_message:) if @durable

  thread_ts = thread_for(session_id)
  return thread_ts if thread_ts

  create_thread(session_id:, root_message:)
end

#publish_reply(session_id:, message:, recovery_root_message:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/codex_notify/hook_thread_publisher.rb', line 45

def publish_reply(session_id:, message:, recovery_root_message:)
  if @durable
    return @durable.publish_reply(key: session_id, message:, recovery_root_message:)
  end

  thread_ts = thread_for(session_id)
  return unless thread_ts

  begin
    post_message(message, thread_ts:)
    thread_ts
  rescue SlackClient::Error => e
    raise unless stale_thread_error?(e)

    reset_thread(session_id:)
    recovered_thread_ts = create_thread(session_id:, root_message: recovery_root_message)
    return unless recovered_thread_ts

    post_message(message, thread_ts: recovered_thread_ts)
    recovered_thread_ts
  end
end

#publish_root_or_reply(session_id:, message:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/codex_notify/hook_thread_publisher.rb', line 28

def publish_root_or_reply(session_id:, message:)
  return @durable.publish_root_or_reply(key: session_id, message:) if @durable

  thread_ts = thread_for(session_id)
  return create_thread(session_id:, root_message: message) unless thread_ts

  begin
    post_message(message, thread_ts:)
    thread_ts
  rescue SlackClient::Error => e
    raise unless stale_thread_error?(e)

    reset_thread(session_id:)
    create_thread(session_id:, root_message: message)
  end
end

#reset_thread(session_id:) ⇒ Object



68
69
70
71
72
# File 'lib/codex_notify/hook_thread_publisher.rb', line 68

def reset_thread(session_id:)
  return @durable.reset(key: session_id) if @durable

  @store.clear_thread(session_id)
end