Class: Talkbird::Entity::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/talkbird/entity/channel.rb

Overview

SendBird channel

Constant Summary collapse

DEFAULTS =
{
  distinct: true,
  is_ephemeral: true
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Channel

Returns a new instance of Channel.



77
78
79
# File 'lib/talkbird/entity/channel.rb', line 77

def initialize(data = {})
  @data = data
end

Class Method Details

.build(payload) ⇒ Channel, Boolean

Build a channel based on the current payload.

Returns:



48
49
50
# File 'lib/talkbird/entity/channel.rb', line 48

def build(payload)
  !payload.empty? && Channel.new(payload)
end

.create(from, to, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/talkbird/entity/channel.rb', line 52

def create(from, to, opts = {})
  body = DEFAULTS.merge(opts)

  body[:channel_url] = opts.fetch(:id) { SecureRandom.uuid }
  body[:user_ids] = [from, to]

  result = Client.request(
    :post,
    'group_channels',
    body: body
  )

  if result.is_a?(Result::Success)
    Channel.new(result.body)
  else
    false
  end
end

.find(from, to) ⇒ Object



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
41
42
43
# File 'lib/talkbird/entity/channel.rb', line 15

def find(from, to)
  # The only way to find a conversation with another person is to search
  # through all the conversations with some rather strict parameters
  # and pick the best match.
  #
  # In this case, the order is based on the latest last message as it
  # makes more sense to have the
  result = Client.request(
    :get,
    "users/#{from}/my_group_channels",
    params: {
      members_exactly_in: to,
      order: 'latest_last_message',
      distinct_mode: 'distinct',
      public_mode: 'private',
      show_member: true,
      limit: 1
    }
  )
  channels = result.body[:channels] || []

  # Since this is the result of a search, the response can be an empty
  # array, in which case the result should also be false.
  if result.is_a?(Result::Success) && !channels.empty?
    Channel.build(channels.first)
  else
    false
  end
end

.find_or_create(from, to) ⇒ Object



71
72
73
# File 'lib/talkbird/entity/channel.rb', line 71

def find_or_create(from, to)
  Channel.find(from, to) || Channel.create(from, to)
end

Instance Method Details

#idObject



81
82
83
# File 'lib/talkbird/entity/channel.rb', line 81

def id
  @data[:channel_url]
end

#membersObject



89
90
91
# File 'lib/talkbird/entity/channel.rb', line 89

def members
  @data[:members].map { |hsh| Entity::User.new(hsh) }
end

#nameObject



85
86
87
# File 'lib/talkbird/entity/channel.rb', line 85

def name
  @data[:name]
end

#to_sObject



101
102
103
# File 'lib/talkbird/entity/channel.rb', line 101

def to_s
  "#<Talkbird::Entity::Channel id=#{id} name=#{name} members=#{members}>"
end

#update(message) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/talkbird/entity/channel.rb', line 93

def update(message)
  Client.request(
    :post,
    "group_channels/#{id}/messages",
    body: message.to_h
  )
end