Class: Layer::Conversation

Overview

Note:

This is available in both the REST and the Platform API. Please refer to the respective documentation to see which methods are available.

Examples:

Creating a new conversation

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

Finding an existing conversation

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

Updating an existing conversation

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

See Also:

Instance Attribute Summary

Attributes inherited from Resource

#attributes, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations::Find::ClassMethods

find

Methods included from Operations::Paginate::ClassMethods

all

Methods included from Operations::Create::ClassMethods

create

Methods included from Operations::Patch

#attributes=, #save

Methods included from Operations::Find

#reload

Methods inherited from Resource

class_name, from_response, #id, #initialize, #respond_to_missing?, url, #url

Constructor Details

This class inherits a constructor from Layer::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Layer::Resource

Class Method Details

.delete(id, options = {}, client = self.client) ⇒ Object

Deletes the resource with the given id

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

Parameters:

  • id (String)

    the resource’s id

  • options (Hash) (defaults to: {})

    the options for the delete request (REST API only: ‘leave: true/false`, `mode: all_participants/my_devices`)

  • client (Layer::Client) (defaults to: self.client)

    the client to use to make this request

Raises:



37
38
39
40
41
# 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) ⇒ Object

Destroys the resource with the given id

In the REST API, this deletes the conversation for everyone

Parameters:

  • id (String)

    the resource’s id

  • client (Layer::Client) (defaults to: self.client)

    the client to use to make this request

Raises:



50
51
52
# File 'lib/layer/conversation.rb', line 50

def self.destroy(id, client = self.client)
  delete(id, { mode: :all_participants }, client)
end

Instance Method Details

#contentsLayer::RelationProxy

Note:

This is only available via the Platform API.

Allows creating and finding of the conversation’s rich content

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

#created_atTime

Returns the time the conversation was created at

Returns:

  • (Time)

    the time the conversation was created at



109
110
111
# File 'lib/layer/conversation.rb', line 109

def created_at
  Time.parse(attributes['created_at'])
end

#delete(options = {}) ⇒ Object

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

Parameters:

  • options (Hash) (defaults to: {})

    the options for the delete request (REST API only: ‘leave: true/false`, `mode: all_participants/my_devices`)

Raises:



124
125
126
127
# File 'lib/layer/conversation.rb', line 124

def delete(options = {})
  options = { mode: :my_devices }.merge(options)
  client.delete(url, {}, { params: options })
end

#destroyObject

Destroys the conversation, removing it for everyone

Raises:



116
117
118
# File 'lib/layer/conversation.rb', line 116

def destroy
  delete(mode: :all_participants)
end

#distinct?Boolean

Whether the conversation is distinct

Returns:

  • (Boolean)

    whether the conversation is distinct



102
103
104
# File 'lib/layer/conversation.rb', line 102

def distinct?
  attributes['distinct']
end

#messagesLayer::RelationProxy

Note:

This is available in both the REST and the Platform API. Please refer to the respective documentation to see which methods are available.

Returns the conversations messages

Returns:

See Also:



58
59
60
# File 'lib/layer/conversation.rb', line 58

def messages
  RelationProxy.new(self, Message, [Operations::Create, Operations::Paginate, Operations::Find, Operations::Delete, Operations::Destroy])
end

#metadataLayer::Patch::Hash

Returns the conversations metadata

Returns:



86
87
88
89
# File 'lib/layer/conversation.rb', line 86

def 
  attributes['metadata'] ||= {}
  attributes['metadata']
end

#participantsLayer::Patch::Array

Returns the conversations metadata

Returns:



94
95
96
97
# File 'lib/layer/conversation.rb', line 94

def participants
  attributes['participants'] ||= []
  attributes['participants']
end