class Layer::Conversation

@example Creating a new conversation

conversation = Layer::Conversation.create({ participants: ['1', '2'] })

@example Finding an existing conversation

conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')

@example Updating an existing conversation

conversation = Layer::Conversation.find('CONVERSATION_ID_HERE')
conversation.participants << 3
conversation.metadata[:foo] = 'bar'
conversation.save

@see developer.layer.com/docs/platform#retrieving-conversations Layer Platform API Documentation about conversations @see developer.layer.com/docs/client/rest#conversations Layer REST API Documentation about conversations @!macro various-apis

Public Class Methods

delete(id, options = {}, client = self.client) click to toggle source

Deletes the resource with the given id

In the REST API, this deletes the conversation for the current user only

@param id [String] the resource's id @param options [Hash] the options for the delete request (REST API only: `leave: true/false`, `mode: all_participants/my_devices`) @param client [Layer::Client] the client to use to make this request @raise [Layer::Exceptions::Exception] a subclass of Layer::Exceptions::Exception describing the error

# File lib/layer/conversation.rb, line 37
def self.delete(id, options = {}, client = self.client)
  id = Layer::Client.normalize_id(id)
  options = { mode: :my_devices }.merge(options)
  client.delete("#{url}/#{id}", {}, { params: options })
end
destroy(id, client = self.client) click to toggle source

Destroys the resource with the given id

In the REST API, this deletes the conversation for everyone

@param id [String] the resource's id @param client [Layer::Client] the client to use to make this request @raise [Layer::Exceptions::Exception] a subclass of Layer::Exceptions::Exception describing the error

# File lib/layer/conversation.rb, line 50
def self.destroy(id, client = self.client)
  delete(id, { mode: :all_participants }, client)
end

Public Instance Methods

contents() click to toggle source

Allows creating and finding of the conversation's rich content

@return [Layer::RelationProxy] the conversation's rich content @!macro platform-api

# File lib/layer/conversation.rb, line 66
def contents
  RelationProxy.new(self, Content, [Operations::Create, Operations::Find]) do
    def create(mime_type, file, client = self.client)
      response = client.post(url, {}, {
        'Upload-Content-Type' => mime_type,
        'Upload-Content-Length' => file.size
      })

      attributes = response.merge('size' => file.size, 'mime_type' => mime_type)

      Content.from_response(attributes, client).tap do |content|
        content.upload(file)
      end
    end
  end
end
create(mime_type, file, client = self.client) click to toggle source
# File lib/layer/conversation.rb, line 68
def create(mime_type, file, client = self.client)
  response = client.post(url, {}, {
    'Upload-Content-Type' => mime_type,
    'Upload-Content-Length' => file.size
  })

  attributes = response.merge('size' => file.size, 'mime_type' => mime_type)

  Content.from_response(attributes, client).tap do |content|
    content.upload(file)
  end
end
created_at() click to toggle source

Returns the time the conversation was created at

@return [Time] the time the conversation was created at

# File lib/layer/conversation.rb, line 109
def created_at
  Time.parse(attributes['created_at'])
end
delete(options = {}) click to toggle source

Deletes the conversation, removing it from the user's devices by default

@param options [Hash] the options for the delete request (REST API only: `leave: true/false`, `mode: all_participants/my_devices`) @raise [Layer::Exceptions::Exception] a subclass of Layer::Exceptions::Exception describing the error

# File lib/layer/conversation.rb, line 124
def delete(options = {})
  options = { mode: :my_devices }.merge(options)
  client.delete(url, {}, { params: options })
end
destroy() click to toggle source

Destroys the conversation, removing it for everyone

@raise [Layer::Exceptions::Exception] a subclass of Layer::Exceptions::Exception describing the error

# File lib/layer/conversation.rb, line 116
def destroy
  delete(mode: :all_participants)
end
distinct?() click to toggle source

Whether the conversation is distinct

@return [Boolean] whether the conversation is distinct

# File lib/layer/conversation.rb, line 102
def distinct?
  attributes['distinct']
end
messages() click to toggle source

Returns the conversations messages

@return [Layer::RelationProxy] the conversation's messages @!macro various-apis

# File lib/layer/conversation.rb, line 58
def messages
  RelationProxy.new(self, Message, [Operations::Create, Operations::Paginate, Operations::Find, Operations::Delete, Operations::Destroy])
end
metadata() click to toggle source

Returns the conversations metadata

@return [Layer::Patch::Hash] the metadata hash

# File lib/layer/conversation.rb, line 86
def metadata
  attributes['metadata'] ||= {}
  attributes['metadata']
end
participants() click to toggle source

Returns the conversations metadata

@return [Layer::Patch::Array] the participants array

# File lib/layer/conversation.rb, line 94
def participants
  attributes['participants'] ||= []
  attributes['participants']
end