-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer-messaging.js
256 lines (199 loc) · 5.67 KB
/
layer-messaging.js
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/** @module LayerMessaging */
LayerMessaging = (function($, window) {
/**
* APP_ID given by Layer.com. Used to authenticate any user.
*
* @var {string} APP_ID
*/
var APP_ID = null;
/**
* Session token used to sign all requests to Layer.com, gets populated after
* authentication.
*
* @var {string} session_token
*/
var session_token = null;
/**
* Layer.com API host.
*
* @var {string} server_url
*/
var server_url = 'https://api.layer.com';
/**
* Self-hosted service provider that generates Identity Tokens to be sent to
* Layer in exchange for a session token.
*
* @var {string} identity_provider
*/
var identity_provider = null;
/**
* Layer.com endpoint to generate a new nonce at the start of the
* authentication handshake.
*
* @var {string} nonces_endpoint
*/
var nonces_endpoint = server_url + '/nonces';
/**
* Layer.com endpoint to generate a token to be used with every request in
* this session.
*
* @var {string} sessions_endpoint
*/
var sessions_endpoint = server_url + '/sessions';
/**
* Object that holds endpoints for the different Layer.com resources
* including nonces, conversations, and messages. Populated after handshake
*
* @var {object} links
*/
var links = null;
var ws;
/*
* Public functions
*/
var init = function(app_id, ISP, token) {
APP_ID = app_id;
identity_provider = ISP;
if (token)
session_token = token;
};
/*
* Public functions - Authentication
*/
var authenticate = function(callback) {
if (session_token)
{
callback(session_token);
return;
}
post(nonces_endpoint, {}, getIdentityToken(function(identity_service_reply) {
var token = identity_service_reply.response.token;
createSession(token, callback);
}));
};
/*
* Public functions - Conversations
*/
var listConversations = function(callback) {
if (!session_token)
throw new Error('Authentication required before sending messages.');
get(server_url + '/conversations', function(data, textStatus, jqXHR) {
var count = jqXHR.getResponseHeader('Layer-Count');
callback(data);
});
};
var getConversation = function(uuid, callback) {
uuid = beforeChecks(uuid);
get(links.conversations + '/' + uuid, function(data) {
console.log(data);
if (callback)
callback(data);
});
};
var createConversation = function(uuid, user_id, message, callback) {
var data = JSON.stringify({participants: [user_id], distinct: true, metadata: {}});
post(links.conversations, data, function(conversation_creation) {
sendMessage(conversation_creation.id, user_id, message, function(message_addition) {
messageHandler = sendMessage;
callback(message_addition);
});
});
};
var messageHandler = createConversation;
/*
* Public functions - Messages
*/
var getAllMessages = function(uuid, callback) {
uuid = beforeChecks(uuid);
get(links.conversations + '/' + uuid + '/messages', function(data) {
if (callback)
callback(data);
});
};
var sendMessage = function(uuid, user_id, message, callback) {
uuid = beforeChecks(uuid);
var data = JSON.stringify({parts: [{mime_type: 'text/plain', body: message}]});
post(links.conversations + '/' + uuid + '/messages', data, function(data) {
callback(data);
});
};
/*
* Private functions
*/
var getIdentityToken = function(callback) {
return function(layer_response) {
var nonce_data = {nonce: layer_response.nonce};
$.post(identity_provider, nonce_data, callback);
};
};
var createSession = function(token, callback) {
var data = JSON.stringify({app_id: APP_ID, identity_token: token});
post(sessions_endpoint, data, function(sessions_reply, status, jqXHR) {
// Set state variables
session_token = sessions_reply.session_token;
links = parseLinkHeader(jqXHR.getResponseHeader('link'));
callback(session_token);
});
};
var beforeChecks = function(uuid) {
if (!session_token)
throw new Error('Authentication required before sending messages.');
if (!uuid)
throw new Error('Provided conversation UUID is empty.');
return extractUUID(uuid);
};
var extractUUID = function(uuid) {
if (uuid.indexOf('layer://') != -1) {
var parts = uuid.split('/');
uuid = parts[parts.length - 1];
}
return uuid;
};
var errorHandler = function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
};
var request = function(type, endpoint, data, callback) {
var headers = {
Accept: 'application/vnd.layer+json; version=1.0',
"Content-Type": 'application/json'
};
if (session_token)
headers.Authorization = 'Layer session-token="' + session_token + '"';
$.ajax(endpoint, {
type: type,
data: data,
headers: headers,
timeout: 800,
success: callback,
error: errorHandler
});
};
var get = function(endpoint, data, callback) {
if (typeof data == 'function') {
callback = data;
data = {};
}
request('GET', endpoint, data, callback);
};
var post = function(endpoint, data, callback) {
request('POST', endpoint, data, callback);
};
/*
* Setup
*/
if (window.conversation_uuid)
messageHandler = sendMessage;
/**
* Public API
* @exports LayerMessaging
*/
return {
init: init,
authenticate: authenticate,
listConversations: listConversations,
getAllMessages: getAllMessages,
messageHandler: messageHandler,
createConversation: createConversation,
ws: ws
};
})(jQuery, window);