Class: ChatSDK::Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/thread.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, channel_id:, adapter:, chat:) ⇒ Thread

Returns a new instance of Thread.



7
8
9
10
11
12
# File 'lib/chat_sdk/thread.rb', line 7

def initialize(id:, channel_id:, adapter:, chat:)
  @id = id
  @channel_id = channel_id
  @adapter = adapter
  @chat = chat
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/chat_sdk/thread.rb', line 5

def adapter
  @adapter
end

#channel_idObject (readonly)

Returns the value of attribute channel_id.



5
6
7
# File 'lib/chat_sdk/thread.rb', line 5

def channel_id
  @channel_id
end

#chatObject (readonly)

Returns the value of attribute chat.



5
6
7
# File 'lib/chat_sdk/thread.rb', line 5

def chat
  @chat
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/chat_sdk/thread.rb', line 5

def id
  @id
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



92
93
94
# File 'lib/chat_sdk/thread.rb', line 92

def ==(other)
  other.is_a?(ChatSDK::Thread) && id == other.id && channel_id == other.channel_id
end

#delete(message_id) ⇒ Object



52
53
54
# File 'lib/chat_sdk/thread.rb', line 52

def delete(message_id)
  adapter.delete_message(channel_id: channel_id, message_id: message_id)
end

#edit(message_id, content) ⇒ Object



47
48
49
50
# File 'lib/chat_sdk/thread.rb', line 47

def edit(message_id, content)
  message = PostableMessage.from(content)
  adapter.edit_message(channel_id: channel_id, message_id: message_id, message: message)
end

#hashObject



97
98
99
# File 'lib/chat_sdk/thread.rb', line 97

def hash
  [id, channel_id].hash
end

#mention_user(user_id) ⇒ Object



84
85
86
# File 'lib/chat_sdk/thread.rb', line 84

def mention_user(user_id)
  adapter.mention(user_id)
end

#messages(cursor: nil, limit: 50) ⇒ Object



72
73
74
# File 'lib/chat_sdk/thread.rb', line 72

def messages(cursor: nil, limit: 50)
  adapter.fetch_messages(channel_id: channel_id, thread_id: id, cursor: cursor, limit: limit)
end

#open_modal(trigger_id:, modal:) ⇒ Object



88
89
90
# File 'lib/chat_sdk/thread.rb', line 88

def open_modal(trigger_id:, modal:)
  adapter.open_modal(trigger_id: trigger_id, modal: modal)
end

#post(content) ⇒ Object



26
27
28
29
# File 'lib/chat_sdk/thread.rb', line 26

def post(content)
  message = PostableMessage.from(content)
  adapter.post_message(channel_id: channel_id, message: message, thread_id: id)
end

#post_ai_stream(enumerable, placeholder: "Thinking...") ⇒ Object



64
65
66
# File 'lib/chat_sdk/thread.rb', line 64

def post_ai_stream(enumerable, placeholder: "Thinking...")
  ChatSDK::AI::StreamHandler.stream_to_thread(self, enumerable, placeholder: placeholder)
end

#post_ephemeral(content, user_id:) ⇒ Object



31
32
33
34
# File 'lib/chat_sdk/thread.rb', line 31

def post_ephemeral(content, user_id:)
  message = PostableMessage.from(content)
  adapter.post_ephemeral(channel_id: channel_id, user_id: user_id, message: message, thread_id: id)
end

#post_stream(placeholder: nil, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/chat_sdk/thread.rb', line 36

def post_stream(placeholder: nil, &block)
  stream = Streaming::Stream.new(
    adapter: adapter,
    channel_id: channel_id,
    thread_id: id,
    placeholder: placeholder,
    update_interval: chat.config.streaming_update_interval
  )
  stream.run(&block)
end

#react(message_id, emoji) ⇒ Object



56
57
58
# File 'lib/chat_sdk/thread.rb', line 56

def react(message_id, emoji)
  adapter.add_reaction(channel_id: channel_id, message_id: message_id, emoji: emoji)
end

#set_state(value) ⇒ Object



80
81
82
# File 'lib/chat_sdk/thread.rb', line 80

def set_state(value)
  chat.state.set(state_key(:state), value, ttl: 30 * 24 * 3600)
end

#stateObject



76
77
78
# File 'lib/chat_sdk/thread.rb', line 76

def state
  chat.state.get(state_key(:state))
end

#subscribeObject



14
15
16
# File 'lib/chat_sdk/thread.rb', line 14

def subscribe
  chat.state.subscribe(state_key)
end

#subscribed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chat_sdk/thread.rb', line 22

def subscribed?
  chat.state.subscribed?(state_key)
end

#unreact(message_id, emoji) ⇒ Object



60
61
62
# File 'lib/chat_sdk/thread.rb', line 60

def unreact(message_id, emoji)
  adapter.remove_reaction(channel_id: channel_id, message_id: message_id, emoji: emoji)
end

#unsubscribeObject



18
19
20
# File 'lib/chat_sdk/thread.rb', line 18

def unsubscribe
  chat.state.unsubscribe(state_key)
end

#upload(io:, filename:, comment: nil) ⇒ Object



68
69
70
# File 'lib/chat_sdk/thread.rb', line 68

def upload(io:, filename:, comment: nil)
  adapter.upload_file(channel_id: channel_id, io: io, filename: filename, thread_id: id, comment: comment)
end