Class: SelfSDK::Services::Chat

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject

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.

Parameters:

  • recipient (string)

    the recipient of the message

  • cid (string)

    message cid to be marked as read.

  • gid (string) (defaults to: nil)

    group id where the conversation ids are referenced.



95
96
97
98
99
100
# File 'lib/services/chat.rb', line 95

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.

Parameters:

  • recipients (array)

    list of recipients to send the message to.

  • cids (array)

    list of message cids to be marked as delivered.

  • gid (string) (defaults to: nil)

    group id where the conversation ids are referenced.



63
64
65
# File 'lib/services/chat.rb', line 63

def delivered(recipients, cids, gid = nil)
  confirm('delivered', recipients, cids, gid)
end

#edit(recipients, cid, body, gid = nil) ⇒ Object

Modifies a previously sent message

Parameters:

  • recipients (array)

    list of recipients to send the message to.

  • cids (array)

    list of message cids to be marked as read.

  • body (string)

    the new body to replace the previous one.

  • gid (string) (defaults to: nil)

    group id where the conversation ids are referenced.



83
84
85
86
87
88
# File 'lib/services/chat.rb', line 83

def edit(recipients, cid, body, gid = nil)
  send(recipients, typ: "chat.message.edit",
                   cid: cid,
                   msg: body,
                   gid: gid)
end

Generates a connection request in form of deep link

Parameters:

  • callback (String)

    the url you’ll be redirected if the app is not installed. @opts opts [Integer] :exp_timeout timeout in seconds to expire the request.



180
181
182
183
184
185
186
187
188
# File 'lib/services/chat.rb', line 180

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.


168
169
170
171
172
173
174
# File 'lib/services/chat.rb', line 168

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.

Parameters:

  • gid (string)

    group id.

  • name (string)

    name of the group.

  • members (array)

    list of group members.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/services/chat.rb', line 126

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

Parameters:

  • gid (string)

    group id.

  • members (array)

    list of group members.



147
148
149
150
151
152
153
154
155
# File 'lib/services/chat.rb', line 147

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

Parameters:

  • gid (string)

    group id.



161
162
163
# File 'lib/services/chat.rb', line 161

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.

Parameters:

  • recipients (array)

    list of recipients to send the message to.

  • body (string)

    the message content to be sent



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/services/chat.rb', line 28

def message(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_connection(&block) ⇒ Object

Subscribes to a connection response

@yield [request] Invokes the block with a connection response message.


193
194
195
196
197
# File 'lib/services/chat.rb', line 193

def on_connection(&block)
  @messaging.subscribe :connection_response do |msg|
    block.call(msg)
  end
end

#on_invite(&block) ⇒ Object



102
103
104
105
106
107
# File 'lib/services/chat.rb', line 102

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



109
110
111
112
113
# File 'lib/services/chat.rb', line 109

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



115
116
117
118
119
# File 'lib/services/chat.rb', line 115

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



46
47
48
49
50
51
52
53
54
55
# File 'lib/services/chat.rb', line 46

def on_message(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.

Parameters:

  • recipients (array)

    list of recipients to send the message to.

  • cids (array)

    list of message cids to be marked as read.

  • gid (string) (defaults to: nil)

    group id where the conversation ids are referenced.



73
74
75
# File 'lib/services/chat.rb', line 73

def read(recipients, cids, gid = nil)
  confirm('read', recipients, cids, gid)
end