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
# 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
  @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

#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

Instance Method Details

#delete!Object

delete! deletes the current message from the conversation.



37
38
39
# File 'lib/chat/message.rb', line 37

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.



44
45
46
47
48
49
# File 'lib/chat/message.rb', line 44

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.



53
54
55
56
57
# File 'lib/chat/message.rb', line 53

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.



61
62
63
64
65
# File 'lib/chat/message.rb', line 61

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



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

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) ⇒ Object

respond sends a direct response to the current message.

Parameters:

  • body (string)

    the new message body.

Returns:

  • ChatMessage



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

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