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.



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.

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.



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

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.



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

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.



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.

Parameters:

  • gid (string)

    group id.

  • name (string)

    name of the group.

  • members (array)

    list of group 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

Parameters:

  • gid (string)

    group id.

  • members (array)

    list of group members.



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

Parameters:

  • gid (string)

    group id.



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.

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
44
45
# 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
  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 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

    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.

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.



75
76
77
# File 'lib/services/chat.rb', line 75

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