-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
312 lines (232 loc) · 9.31 KB
/
app.js
File metadata and controls
312 lines (232 loc) · 9.31 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* *
* Webex - instance of the webex sdk
* window.webex is initialized in the node module with 'require'
*
*/
const Webex = window.webex;
//globals
/**
* @type RoomObject
* */
let testRoom;
/**
* @type Webex
* */
let webex;
/**
* Set during authentication and
* used when updating test room details.
*
* @type PersonObject
* */
let tokenHolder;
/**
* Listening to messages
* */
let listening = false;
/**
* Creates an instance of webex, with the access token provided,
* however it just creates the object, does not verify if the
* provided token is valid.
* */
function initialize() {
webex = Webex.init({
credentials: {
access_token: document.getElementById('access-token').value
}
});
}
/**
* Handles pushing the Authenticate button.
* */
document.getElementById('authenticate').addEventListener('submit', ev => {
//prevents the page from refreshing automatically after the button is pushed
ev.preventDefault();
//initialize the webex object
initialize();
document.getElementById('authenticate-message').innerHTML = 'authenticating';
document.getElementById('authenticate-message').classList.remove('label-error');
document.getElementById('authenticate-message').classList.add('label', 'label-warning');
//makes a call to /people/me to verify that the passed access-token is valid
//if it is, shows further options, like 'Listen to messages',... are activated
webex.people.get('me').then(person => {
//access token is verified
tokenHolder = person;
document.getElementById('authenticate-message').innerHTML = 'authenticated as '
+ tokenHolder.displayName;
document.getElementById('authenticate-message').classList.remove('label-warning',
'label-error');
document.getElementById('authenticate-message').classList.add('label-success');
//show further options
document.getElementById('option-wrapper').style.display = 'inline';
document.getElementById('message-wrapper').style.display = 'inline';
document.getElementById('authenticate-button').disabled = true;
}).catch(reason => {
//access token expired
document.getElementById('authenticate-message').innerHTML = 'authentication failed';
document.getElementById('authenticate-message').classList.add('label', 'label-error');
});
});
/**
* Handles pushing the Start Listening to Messages button.
* Depending on whether or not the global 'listening' variable is true,
* it calls listenToMessages() or stopListeningToMessages()
*/
document.getElementById('listener-btn').addEventListener('click', event => {
//prevents the page from refreshing automatically after the button is pushed
event.preventDefault();
if (!listening) {
listenToMessages();
} else {
stopListeningToMessages();
}
});
/**
* Handles pushing the Create / Delete Test Room button,
* creates or deletes the room and updates the UI,
* depending on the global 'testRoom' variable
* */
document.getElementById('room-btn').addEventListener('click', event => {
//prevents the page from refreshing automatically after the button is pushed
event.preventDefault();
if (testRoom) {
//we already have a test room, delete it, update UI
clearRoom();
} else {
//no test room yet, create one, update UI
createRoom();
}
});
/**
* Handles pushing the Send Random Message button,
* by posting a random unicode emoji from a message pool.
* */
document.getElementById('messages-btn').addEventListener('click', event => {
//prevents the page from refreshing automatically after the button is pushed
event.preventDefault();
const messages = ['ʕ·͡ᴥ·ʔ',
'(っ◕‿◕)っ',
'(⌐■_■)',
'\\m/_(>_<)_\\m/',
'ᕙ(⇀‸↼)ᕗ',
'[¬º-°]¬'];
//random message from the messages array
const message = messages[Math.floor(Math.random() * messages.length)];
webex.messages.create({
roomId: testRoom.id,
markdown: message
}).then(() => {
console.log('message created successfully')
}).catch(reason => {
//message creation failed for some reason
console.error(reason.message)
});
});
/**
* Starts a websocket, that will listen for incoming messages. The process is
* similar to creating a webhook with 'resource' and 'event', here the resource
* is defined by the webex.messages.listen() function, while the event is
* specified by the webex.messages.on() function.
* */
function listenToMessages() {
document.getElementById('listen-message').innerHTML = 'starting message listener';
document.getElementById('listen-message').classList.add('label-warning');
//creating the websocket listener with 'messages' resource...
webex.messages.listen().then(() => {
//updating UI
document.getElementById('listen-message').innerHTML = 'listening to messages';
document.getElementById('listen-message').classList.remove('label-warning');
document.getElementById('listen-message').classList.add('label-success');
document.getElementById('listener-btn').innerHTML = 'Stop Listening to Messages';
//setting global variable to keep track of the listener
listening = true;
//...and 'created' event
webex.messages.on('created', (event) => {
//message is received in any of the token holders rooms
console.log('message received');
//logging the time of the event and putting it into a user friendly format
const date = new Date(Date.parse(event.data.created));
const dateToDisplay = date.toTimeString().split(' ')[0];
//getting the details of the message sender to update the UI
webex.people.get(event.actorId).then(
sender => {
//getting the details of the room the message was posted in
webex.rooms.get(event.data.roomId).then(
room => {
//update UI
document.getElementById('message-room-title').innerHTML = room.title;
document.getElementById('message-posted').innerHTML = event.data.text;
document.getElementById('message-sent-by').innerHTML = sender.displayName;
document.getElementById('message-sent-at').innerHTML = dateToDisplay;
}
).catch(reason => {
//could not get room details for some reason
console.error(reason.message);
});
}
).catch(reason => {
//could not fetch person details
console.error(reason.message);
});
})
}).catch(error => {
//listener / websocket could not be started
console.error(error.message);
});
}
/**
* Deactivates the listener by disconnecting from the websocket
* and the app does not listen to incoming messages anymore.
* */
function stopListeningToMessages() {
//deactivate websocket and cleans up
//both are required for deactivation
webex.messages.stopListening();
webex.messages.off('created');
//update UI
document.getElementById('listen-message').innerHTML = 'not listening to messages';
document.getElementById('listen-message').classList.remove('label-warning', 'label-success');
document.getElementById('listener-btn').innerHTML = 'Start Listening to Messages';
listening = false;
}
/**
* Creates a test room, so the user can trigger messages from the app.
* */
function createRoom() {
webex.rooms.create({title: 'WebSocket Test Room'}).then(room => {
testRoom = room;
let date = new Date(Date.parse(room.created));
//show test room table
document.getElementById('room-wrapper').style.display = 'inline';
document.getElementById('messages-btn').style.display = 'inline';
//update test room table
document.getElementById('room-table-title').innerHTML = room.title;
document.getElementById('room-table-created-by').innerHTML = tokenHolder.displayName;
document.getElementById('room-table-created-at').innerHTML = date.toTimeString().split(' ')[0];
//change button text
document.getElementById('room-btn').innerHTML = 'Delete Test Room';
}).catch(reason => {
//room could not be created for some reason
console.error(reason.message);
});
}
/**
* Deletes the created test room.
* */
function clearRoom() {
webex.rooms.remove(testRoom).then(() => {
testRoom = undefined;
//hide room table
document.getElementById('room-wrapper').style.display = 'none';
document.getElementById('messages-btn').style.display = 'none';
//clear room table
document.getElementById('room-table-title').innerHTML = '';
document.getElementById('room-table-created-by').innerHTML = '';
document.getElementById('room-table-created-at').innerHTML = '';
//change button text
document.getElementById('room-btn').innerHTML = 'Create Test Room';
}).catch(reason => {
//room could not be deleted for some reason
console.log(reason.message);
});
}