Class: SelfSDK::Chat::Message
- Inherits:
-
Object
- Object
- SelfSDK::Chat::Message
- Defined in:
- lib/chat/message.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#from ⇒ Object
Returns the value of attribute from.
-
#gid ⇒ Object
Returns the value of attribute gid.
-
#objects ⇒ Object
Returns the value of attribute objects.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#recipients ⇒ Object
Returns the value of attribute recipients.
Instance Method Summary collapse
-
#delete! ⇒ Object
delete! deletes the current message from the conversation.
-
#edit(body) ⇒ Object
edit changes the current message body for all participants.
-
#initialize(chat, recipients, payload, auth_token, self_url) ⇒ Message
constructor
A new instance of Message.
-
#mark_as_delivered ⇒ Object
mark_as_delivered marks the current message as delivered if it comes from another recipient.
-
#mark_as_read ⇒ Object
mark_as_read marks the current message as read if it comes from another recipient.
-
#message(body, opts = {}) ⇒ Object
message sends a new message to the same conversation as the current message.
-
#respond(body) ⇒ Object
respond sends a direct response to the current message.
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
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/chat/message.rb', line 8 def body @body end |
#from ⇒ Object
Returns the value of attribute from.
8 9 10 |
# File 'lib/chat/message.rb', line 8 def from @from end |
#gid ⇒ Object
Returns the value of attribute gid.
8 9 10 |
# File 'lib/chat/message.rb', line 8 def gid @gid end |
#objects ⇒ Object
Returns the value of attribute objects.
8 9 10 |
# File 'lib/chat/message.rb', line 8 def objects @objects end |
#payload ⇒ Object
Returns the value of attribute payload.
8 9 10 |
# File 'lib/chat/message.rb', line 8 def payload @payload end |
#recipients ⇒ Object
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.
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_delivered ⇒ Object
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_read ⇒ Object
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.
89 90 91 92 93 94 95 96 97 |
# File 'lib/chat/message.rb', line 89 def (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.(to, body, opts) end |
#respond(body) ⇒ Object
respond sends a direct response to the current message.
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.(to, body, opts) end |