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.
-
#generate_connection_deep_link(callback, opts = {}) ⇒ Object
Generates a connection request in form of deep link.
-
#generate_connection_qr(opts = {}) ⇒ Object
Generates a connection request in form of QR.
-
#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_connection(&block) ⇒ Object
Subscribes to a connection response.
- #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.
16 17 18 19 20 21 22 |
# File 'lib/services/chat.rb', line 16 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.
14 15 16 |
# File 'lib/services/chat.rb', line 14 def app_id @app_id end |
Instance Method Details
#delete(recipients, cids, gid = nil) ⇒ Object
Sends a message to delete a specific message.
97 98 99 100 101 102 |
# File 'lib/services/chat.rb', line 97 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.
65 66 67 |
# File 'lib/services/chat.rb', line 65 def delivered(recipients, cids, gid = nil) confirm('delivered', recipients, cids, gid) end |
#edit(recipients, cid, body, gid = nil) ⇒ Object
Modifies a previously sent message
85 86 87 88 89 90 |
# File 'lib/services/chat.rb', line 85 def edit(recipients, cid, body, gid = nil) send(recipients, typ: "chat.message.edit", cid: cid, msg: body, gid: gid) end |
#generate_connection_deep_link(callback, opts = {}) ⇒ Object
Generates a connection request in form of deep link
182 183 184 185 186 187 188 189 190 |
# File 'lib/services/chat.rb', line 182 def generate_connection_deep_link(callback, opts = {}) req = SelfSDK::Messages::ConnectionRequest.new(@messaging) req.populate(@jwt.id, opts) body = @jwt.prepare(req.body) body = @jwt.encode(body) env = @messaging.client.client.env @jwt.build_dynamic_link(body, env, callback) end |
#generate_connection_qr(opts = {}) ⇒ Object
Generates a connection request in form of QR
@opts opts [Integer] :exp_timeout timeout in seconds to expire the request.
170 171 172 173 174 175 176 |
# File 'lib/services/chat.rb', line 170 def generate_connection_qr(opts = {}) req = SelfSDK::Messages::ConnectionRequest.new(@messaging) req.populate(@jwt.id, opts) body = @jwt.prepare(req.body) ::RQRCode::QRCode.new(body, level: 'l') end |
#invite(gid, name, members, opts = {}) ⇒ Object
Invite sends group invitation to a list of members.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/services/chat.rb', line 128 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
149 150 151 152 153 154 155 156 157 |
# File 'lib/services/chat.rb', line 149 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
163 164 165 |
# File 'lib/services/chat.rb', line 163 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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/services/chat.rb', line 28 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 payload[:cid] = opts[:cid] if opts.key? :cid payload[:data] = opts[:data] if opts.key? :data m = SelfSDK::Chat::Message.new(self, recipients, payload, @jwt.auth_token, @self_url) _req = send(m.recipients, m.payload) m end |
#on_connection(&block) ⇒ Object
Subscribes to a connection response
@yield [request] Invokes the block with a connection response message.
195 196 197 198 199 |
# File 'lib/services/chat.rb', line 195 def on_connection(&block) @messaging.subscribe :connection_response do |msg| call(block, msg) end end |
#on_invite(&block) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/services/chat.rb', line 104 def on_invite(&block) @messaging.subscribe :chat_invite do |msg| g = SelfSDK::Chat::Group.new(self, msg.payload) call(block, g) end end |
#on_join(&block) ⇒ Object
111 112 113 114 115 |
# File 'lib/services/chat.rb', line 111 def on_join(&block) @messaging.subscribe :chat_join do |msg| call(block, {iss: msg.payload[:iss], gid: msg.payload[:gid]}) end end |
#on_leave(&block) ⇒ Object
117 118 119 120 121 |
# File 'lib/services/chat.rb', line 117 def on_leave(&block) @messaging.subscribe :chat_remove do |msg| call(block, {iss: msg.payload[:iss], gid: msg.payload[:gid]}) end end |
#on_message(opts = {}, &block) ⇒ Object
Subscribes to an incoming chat message
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/services/chat.rb', line 48 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 call(block, 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.
75 76 77 |
# File 'lib/services/chat.rb', line 75 def read(recipients, cids, gid = nil) confirm('read', recipients, cids, gid) end |