Sha256: c44903fcc632cbb0d8d9877087c297920fb03c7356416133feaef64f09adba5c

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module Talkbird
  module Entity
    # SendBird channel
    class Channel

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

      class << self

        def find(from, to)
          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
            }
          )

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

        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

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

      end

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

      def id
        @data[:channel_url]
      end

      def update(message)
        body = {
          user_id: message.sender.id,
          message: message.body,
          message_type: 'MESG',
        }

        result = Client.request(
          :post,
          "group_channels/#{id}/messages",
          body: body
        )

        if result.is_a?(Result::Success)
          Entity::Message.build(result.body)
        else
          false
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
talkbird-0.0.2 lib/talkbird/entity/channel.rb