Class: SelfSDK::Chat::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/chat/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat, recipients, payload, auth_token, self_url) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chat/message.rb', line 10

def initialize(chat, recipients, payload, auth_token, self_url)
  @chat = chat
  @recipients = recipients
  @recipients = [@recipients] if @recipients.is_a? String
  @gid = payload[:gid] if payload.key? :gid
  @rid = payload[:rid] if payload.key? :rid
  @cid = payload[:cid] if payload.key? :cid
  @payload = payload
  @payload[:jti] = SecureRandom.uuid unless @payload.include?(:jti)
  @body = @payload[:msg]
  @from = @payload[:iss]
  return unless @payload.key?(:objects)

  @objects = []
  @payload[:objects].each do |o|
    @objects << if o.key? :link
                  SelfSDK::Chat::FileObject.new(auth_token, self_url).build_from_object(o)
                else
                  SelfSDK::Chat::FileObject.new(auth_token, self_url).build_from_data(o[:name], o[:data], o[:mime])
                end
  end
  @payload[:objects] = []
  @payload[:objects] = payload[:raw_objects] if payload[:raw_objects]
  @objects.each do |o|
    @payload[:objects] << o.to_payload
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/chat/message.rb', line 8

def body
  @body
end

#cidObject

Returns the value of attribute cid.



8
9
10
# File 'lib/chat/message.rb', line 8

def cid
  @cid
end

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/chat/message.rb', line 8

def from
  @from
end

#gidObject

Returns the value of attribute gid.



8
9
10
# File 'lib/chat/message.rb', line 8

def gid
  @gid
end

#objectsObject

Returns the value of attribute objects.



8
9
10
# File 'lib/chat/message.rb', line 8

def objects
  @objects
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/chat/message.rb', line 8

def payload
  @payload
end

#recipientsObject

Returns the value of attribute recipients.



8
9
10
# File 'lib/chat/message.rb', line 8

def recipients
  @recipients
end

#ridObject

Returns the value of attribute rid.



8
9
10
# File 'lib/chat/message.rb', line 8

def rid
  @rid
end

Instance Method Details

#delete!Object

delete! deletes the current message from the conversation.



39
40
41
# File 'lib/chat/message.rb', line 39

def delete!
  @chat.delete(@recipients, @payload[:jti], @payload[:gid])
end

#edit(body) ⇒ Object

edit changes the current message body for all participants.

Parameters:

  • body (string)

    the new message body.



46
47
48
49
50
51
# File 'lib/chat/message.rb', line 46

def edit(body)
  return if @recipients == [@chat.app_id]

  @body = body
  @chat.edit(@recipients, @payload[:jti], body, @payload[:gid])
end

#mark_as_deliveredObject

mark_as_delivered marks the current message as delivered if it comes from another recipient.



55
56
57
58
59
# File 'lib/chat/message.rb', line 55

def mark_as_delivered
  return if @recipients != [@chat.app_id]

  @chat.delivered(@payload[:iss], @payload[:jti], @payload[:gid])
end

#mark_as_readObject

mark_as_read marks the current message as read if it comes from another recipient.



63
64
65
66
67
# File 'lib/chat/message.rb', line 63

def mark_as_read
  return if @recipients != [@chat.app_id]

  @chat.read(@payload[:iss], @payload[:jti], @payload[:gid])
end

#message(body, opts = {}) ⇒ Object

message sends a new message to the same conversation as the current message.

Parameters:

  • body (string)

    the new message body.

Returns:

  • ChatMessage



90
91
92
93
94
95
96
97
98
# File 'lib/chat/message.rb', line 90

def message(body, opts = {})
  opts[:aud] = @payload[:gid] if @payload.key? :gid
  opts[:gid] = @payload[:gid] if @payload.key? :gid

  to = opts[:recipients] if opts.key? :recipients
  to = [@payload[:iss]] if @recipients == [@chat.app_id]

  @chat.message(to, body, opts)
end

#respond(body, opts = {}) ⇒ Object

respond sends a direct response to the current message.

Parameters:

  • body (string)

    the new message body.

Returns:

  • ChatMessage



74
75
76
77
78
79
80
81
82
83
# File 'lib/chat/message.rb', line 74

def respond(body, opts = {})
  opts[:aud] = @payload[:gid] if @payload.key? :gid
  opts[:gid] = @payload[:gid] if @payload.key? :gid
  opts[:rid] = @payload[:jti]

  to = @recipients
  to = [@payload[:iss]] if @recipients == [@chat.app_id]

  @chat.message(to, body, opts)
end