Class: SelfSDK::Services::Chat
- Inherits:
-
Object
- Object
- SelfSDK::Services::Chat
- Defined in:
- lib/services/chat.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
Instance Method Summary collapse
-
#delete(recipients, cids, gid = nil) ⇒ Object
Sends a message to delete a specific message.
-
#delivered(recipients, cids, gid = nil) ⇒ Object
Sends a message to confirm a list of messages (identified by it’s cids) have been delivered.
-
#edit(recipients, cid, body, gid = nil) ⇒ Object
Modifies a previously sent message.
-
#initialize(messaging, client) ⇒ Chat
constructor
A new instance of Chat.
-
#invite(gid, name, members, opts = {}) ⇒ Object
Invite sends group invitation to a list of members.
-
#join(gid, members) ⇒ Object
Join a group.
-
#leave(gid, members) ⇒ Object
Leaves a group.
-
#message(recipients, body, opts = {}) ⇒ Object
Sends a message to a list of recipients.
- #on_invite(&block) ⇒ Object
- #on_join(&block) ⇒ Object
- #on_leave(&block) ⇒ Object
-
#on_message(opts = {}, &block) ⇒ Object
Subscribes to an incoming chat message.
-
#read(recipients, cids, gid = nil) ⇒ Object
Sends a message to confirm a list of messages (identified by it’s cids) have been read.
Constructor Details
#initialize(messaging, client) ⇒ Chat
Returns a new instance of Chat.
14 15 16 17 18 19 20 |
# File 'lib/services/chat.rb', line 14 def initialize(messaging, client) @messaging = messaging @client = client @app_id = @messaging.client.client.jwt.id @jwt = @messaging.client.client.jwt @self_url = @messaging.client.client.self_url end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
12 13 14 |
# File 'lib/services/chat.rb', line 12 def app_id @app_id end |
Instance Method Details
#delete(recipients, cids, gid = nil) ⇒ Object
Sends a message to delete a specific message.
93 94 95 96 97 98 |
# File 'lib/services/chat.rb', line 93 def delete(recipients, cids, gid = nil) cids = [cids] if cids.is_a? String send(recipients, typ: "chat.message.delete", cids: cids, gid: gid) end |
#delivered(recipients, cids, gid = nil) ⇒ Object
Sends a message to confirm a list of messages (identified by it’s cids) have been delivered.
61 62 63 |
# File 'lib/services/chat.rb', line 61 def delivered(recipients, cids, gid = nil) confirm('delivered', recipients, cids, gid) end |
#edit(recipients, cid, body, gid = nil) ⇒ Object
Modifies a previously sent message
81 82 83 84 85 86 |
# File 'lib/services/chat.rb', line 81 def edit(recipients, cid, body, gid = nil) send(recipients, typ: "chat.message.edit", cid: cid, msg: body, gid: gid) end |
#invite(gid, name, members, opts = {}) ⇒ Object
Invite sends group invitation to a list of members.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/services/chat.rb', line 124 def invite(gid, name, members, opts = {}) payload = { typ: "chat.invite", gid: gid, name: name, members: members } if opts.key? :data obj = SelfSDK::Chat::FileObject.new(@jwt.auth_token, @self_url) obj_payload = obj.build_from_data("", opts[:data], opts[:mime]).to_payload obj_payload.delete(:name) payload.merge! obj_payload end @messaging.send(members, payload) SelfSDK::Chat::Group.new(self, payload) end |
#join(gid, members) ⇒ Object
Join a group
145 146 147 148 149 150 151 152 153 |
# File 'lib/services/chat.rb', line 145 def join(gid, members) # Allow incoming connections from the given members # Create missing sessions with group members. create_missing_sessions(members) # Send joining confirmation. send(members, typ: 'chat.join', gid: gid, aud: gid) end |
#leave(gid, members) ⇒ Object
Leaves a group
159 160 161 |
# File 'lib/services/chat.rb', line 159 def leave(gid, members) send(members, typ: "chat.remove", gid: gid ) end |
#message(recipients, body, opts = {}) ⇒ Object
Sends a message to a list of recipients.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/services/chat.rb', line 26 def (recipients, body, opts = {}) payload = { typ: "chat.message", msg: body, } payload[:jti] = opts[:jti] if opts.key? :jti payload[:aud] = opts[:gid] if opts.key? :gid payload[:gid] = opts[:gid] if opts.key? :gid payload[:rid] = opts[:rid] if opts.key? :rid payload[:objects] = opts[:objects] if opts.key? :objects m = SelfSDK::Chat::Message.new(self, recipients, payload, @jwt.auth_token, @self_url) _req = send(m.recipients, m.payload) m end |
#on_invite(&block) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/services/chat.rb', line 100 def on_invite(&block) @messaging.subscribe :chat_invite do |msg| g = SelfSDK::Chat::Group.new(self, msg.payload) block.call(g) end end |
#on_join(&block) ⇒ Object
107 108 109 110 111 |
# File 'lib/services/chat.rb', line 107 def on_join(&block) @messaging.subscribe :chat_join do |msg| block.call(iss: msg.payload[:iss], gid: msg.payload[:gid]) end end |
#on_leave(&block) ⇒ Object
113 114 115 116 117 |
# File 'lib/services/chat.rb', line 113 def on_leave(&block) @messaging.subscribe :chat_remove do |msg| block.call(iss: msg.payload[:iss], gid: msg.payload[:gid]) end end |
#on_message(opts = {}, &block) ⇒ Object
Subscribes to an incoming chat message
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/services/chat.rb', line 44 def (opts = {}, &block) @messaging.subscribe :chat_message do |msg| cm = SelfSDK::Chat::Message.new(self, msg.payload[:aud], msg.payload, @jwt.auth_token, @self_url) cm.mark_as_delivered unless opts[:mark_as_delivered] == false cm.mark_as_read if opts[:mark_as_read] == true block.call(cm) end end |
#read(recipients, cids, gid = nil) ⇒ Object
Sends a message to confirm a list of messages (identified by it’s cids) have been read.
71 72 73 |
# File 'lib/services/chat.rb', line 71 def read(recipients, cids, gid = nil) confirm('read', recipients, cids, gid) end |