forked from aaronmars/martian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
492 lines (468 loc) · 24.8 KB
/
events.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { Plug } from '/mindtouch-http.js/plug.js';
import { Settings } from './lib/settings.js';
import { utility } from './lib/utility.js';
import { modelParser } from './lib/modelParser.js';
import { apiErrorModel } from './models/apiError.model.js';
import { userActivityModel } from './models/userActivity.model.js';
import { pageHistoryModel } from './models/pageHistory.model.js';
import { pageHistoryDetailModel } from './models/pageHistoryDetail.model.js';
import { reportLogsModel } from './models/reportLogs.model.js';
import { logUrlModel } from './models/logUrl.model.js';
const _errorParser = modelParser.createParser(apiErrorModel);
/**
* A class for fetching and managing events.
*/
export class Events {
/**
* Construct a new Events object.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(settings = new Settings()) {
this._plug = new Plug(settings.host, settings.plugConfig).at('@api', 'deki', 'events');
}
/**
* Get the available drafts history logs.
* @returns {Promise.<reportLogsModel>} - A Promise that, when resolved, yields a {@link reportLogsModel} containing the available logs for drafts history.
*/
getSiteDraftsHistoryLogs() {
return this._plug.at('draft-hierarchy', 'logs').get()
.then((r) => r.json()).then(modelParser.createParser(reportLogsModel));
}
/**
* Get the draft history log url.
* @param {String} logName - Name of log to retrive URL from.
* @returns {Promise.<logUrlModel>} - A Promise that, when resolved, yields a {@link logUrlModel} containing log url.
*/
getSiteDraftsHistoryLogUrl(logName) {
if(!logName) {
return Promise.reject(new Error('Attempting to get log url without required name'));
}
return this._plug.at('draft-hierarchy', 'logs', logName, 'url').get()
.then((r) => r.json()).then(modelParser.createParser(logUrlModel));
}
/**
* Get the drafts history
* @param {Object} [options] - An object that directs the history fetching.
* @param {String|Number} [options.pageId=home] - The page in the site hierarchy to return the history.
* @param {Number} [options.limit=25] - The maximum number results to retrieve. Regardless of what is passed in, no more than 1000 results will be returned.
* @param {String} [options.upTo] - The history event ID to start fetching at.
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel|Error>} - A Promise that will be resolved with the page history data, or rejected with an error specifying the reason for rejection.
*/
getSiteDraftsHistory(options = {}) {
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number less than or equal to 1000.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
if(options.upTo) {
if(typeof options.upTo !== 'string') {
return Promise.reject(new Error('The `upTo` parameter must be a string.'));
}
params.upto = options.upTo;
}
return this._plug.at('draft-hierarchy', utility.getResourceId(options.pageId, 'home')).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get the detail of a site history event
* @param {String} detailId - The GUID specifying the detail to fetch.
* @param {Object} [options] - Information about the detail to fetch
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel|Error>} - A Promise that will be resolved with the page history data, or rejected with an error specifying the reason for rejection.
*/
getSiteDraftsHistoryDetail(detailId, options = {}) {
if(!detailId || typeof detailId !== 'string') {
return Promise.reject(new Error('The detail ID must be specified, and it must be a string.'));
}
const params = {};
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` option must be an array'));
}
params.include = options.include.join(',');
}
return this._plug.at('draft-hierarchy', 'details', options.detailId).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get draft history summary.
* @param {Number|String} [pageId=home] - The page ID or path.
* @param {Object} [options] - An object that directs the history fetching.
* @param {Number} [options.limit=25] - The maximum number results to retrieve. Regardless of what is passed in, no more than 1000 results will be returned.
* @param {String} [options.upTo] - The history event ID to start fetching at.
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryModel} that contains the listing of the page events.
*/
getDraftHistory(pageId = 'home', options = {}) {
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number.'));
}
params.limit = options.limit;
}
if(options.upTo) {
if(typeof options.upTo !== 'string') {
return Promise.reject(new Error('The `upTo` parameter must be a string.'));
}
params.upto = options.upTo;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.at('draft', utility.getResourceId(pageId, 'home')).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get draft history detail.
* @param {Number|String} pageId = 'home' - The page ID or path.
* @param {String} detailId - The detail ID.
* @param {Object} [options] - Options to direct the fetching of the detail.
* @param {Array} [options.include] - An array of strings identifying elements to expand in the result. Valid identifiers are: 'page', 'user', 'file', and 'request'.
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryModel} that contains the listing of the page events.
*/
getDraftHistoryDetail(pageId, detailId, options = {}) {
if(!pageId) {
return Promise.reject(new Error('The page ID is required to fetch a draft history detail.'));
}
if(!detailId) {
return Promise.reject(new Error('The detail ID is required to fetch a draft history detail.'));
}
const params = {};
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.at('draft', utility.getResourceId(pageId, 'home'), detailId).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryDetailModel));
}
/**
* Get the history summary for a Learning Path
* @param {String} learningPathId - The string identifier of the learning path.
* @param {Object} [options] - Options to direct the fetching of the learning path history.
* @param {Number} [options.limit=25] - The maximum number of results to fetch.
* @param {String} [options.upTo] - The GUID identifier to use for paging.
* @param {Array} [options.include] - An array of strings identifying elements to expand in the result. Valid identifiers are: 'page', 'user', 'file', and 'request'.
* @returns {Promise.<Object|Error>} - A Promise that will be resolved with the learning path history data, or rejected with an error specifying the reason for rejection.
*/
getLearningPathHistory(learningPathId, options = {}) {
if(!learningPathId || typeof learningPathId !== 'string') {
return Promise.reject(new Error('The learning path ID must be supplied, and must be a string'));
}
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number.'));
}
params.limit = options.limit;
}
if(options.upTo) {
if(typeof options.upTo !== 'string') {
return Promise.reject(new Error('The `upTo` parameter must be a string.'));
}
params.upto = options.upTo;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.at('learningpath', utility.getResourceId(learningPathId)).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get the available site history logs.
* @param {String} No params necessary.
* @returns {Promise.<reportLogsModel>} - A Promise that, when resolved, yields a {@link reportLogsModel} containing the available logs for site history.
*/
getSiteHistoryLogs() {
return this._plug.at('page-hierarchy', 'logs').get()
.then((r) => r.json()).then(modelParser.createParser(reportLogsModel));
}
/**
* Get the site history log url.
* @param {String} logName - Name of log to retrive URL from.
* @returns {Promise.<logUrlModel>} - A Promise that, when resolved, yields a {@link logUrlModel} containing log url.
*/
getSiteHistoryLogUrl(logName) {
if(!logName) {
return Promise.reject(new Error('Attempting to get log url without required name'));
}
return this._plug.at('page-hierarchy', 'logs', logName, 'url').get()
.then((r) => r.json()).then(modelParser.createParser(logUrlModel));
}
/**
* Get site history summary
* @param {Object} [options] - An object that directs the history fetching.
* @param {String|Number} [options.pageId=home] - The page in the site hierarchy to return the history.
* @param {Number} [options.limit=25] - The maximum number results to retrieve. Regardless of what is passed in, no more than 1000 results will be returned.
* @param {String} [options.upTo] - The history event ID to start fetching at.
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel|Error>} - A Promise that will be resolved with the site history data, or rejected with an error specifying the reason for rejection.
*/
getSiteHistory(options = {}) {
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number less than or equal to 1000.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
if(options.upTo) {
if(typeof options.upTo !== 'string') {
return Promise.reject(new Error('The `upTo` parameter must be a string.'));
}
params.upto = options.upTo;
}
return this._plug.at('page-hierarchy', utility.getResourceId(options.pageId, 'home')).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get the detail of a site history event
* @param {String} detailId - The GUID specifying the detail to fetch.
* @param {Object} [options] - Information about the detail to fetch
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel|Error>} - A Promise that will be resolved with the site history detail data, or rejected with an error specifying the reason for rejection.
*/
getSiteHistoryDetail(detailId, options = {}) {
if(!detailId || typeof detailId !== 'string') {
return Promise.reject(new Error('The detail ID must be specified, and it must be a string.'));
}
const params = {};
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` option must be an array'));
}
params.include = options.include.join(',');
}
return this._plug.at('page-hierarchy', 'details', detailId).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Notify the system that a page was viewed by a user
* @param {String|Number} pageId - The numeric ID or path of the page to log a view event for.
* @param {Object} [eventData] - Specific data about the search that was performed.
* @returns {Promise.<pageHistoryModel|Error>} - A Promise that will be resolved, or rejected with an error specifying the reason for rejection.
*/
logPageView(pageId, eventData = {}) {
return this._plug.at('page-view', utility.getResourceId(pageId, 'home')).post(JSON.stringify(eventData), utility.jsonRequestType);
}
/**
* Get page history summary.
* @param {Number|String} [pageId=home] - The page ID or path.
* @param {Object} [options] - An object that directs the history fetching.
* @param {Number} [options.limit=25] - The maximum number results to retrieve. Regardless of what is passed in, no more than 1000 results will be returned.
* @param {String} [options.upTo] - The history event ID to start fetching at.
* @param {Array} [options.include] - An array of entity details to include. Valid entries are 'page', 'user', 'group', 'file', and 'request'
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryModel} that contains the listing of the page events.
*/
getPageHistory(pageId = 'home', options = {}) {
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number less than or equal to 1000.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
if(options.upTo) {
if(typeof options.upTo !== 'string') {
return Promise.reject(new Error('The `upTo` parameter must be a string.'));
}
params.upto = options.upTo;
}
return this._plug.at('page', utility.getResourceId(pageId, 'home')).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Get page history detail.
* @param {Number|String} pageId = 'home' - The page ID or path.
* @param {String} detailId - The detail ID.
* @param {Object} [options] - Options to direct the fetching of the detail.
* @param {Array} [options.include] - An array of strings identifying elements to expand in the result. Valid identifiers are: 'page', 'user', 'file', and 'request'.
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryDetailModel} that contains the listing of the page events.
*/
getPageHistoryDetail(pageId, detailId, options = {}) {
if(!pageId) {
return Promise.reject(new Error('The page ID is required to fetch a page history detail.'));
}
if(!detailId) {
return Promise.reject(new Error('The detail ID is required to fetch a page history detail.'));
}
const params = {};
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.at('page', utility.getResourceId(pageId, 'home'), detailId).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryDetailModel));
}
/**
* Log a search event that is performed by a specific user.
* @param {Number|String} [userId=current] - The user's numeric ID or username.
* @param {Object} [eventData] - Specific data about the search that was performed.
* @returns {Promise} - A Promise that, when resolved, indicates a successful posting of the search event.
*/
logSearch(userId, eventData) {
return this._plug.at('search', utility.getResourceId(userId, 'current')).post(JSON.stringify(eventData), utility.jsonRequestType);
}
/**
* Get the available user activity logs.
* @returns {Promise.<reportLogsModel>} - A Promise that, when resolved, yields a {@link reportLogsModel} containing the available logs for user activity.
*/
getUserActivityLogs() {
return this._plug.at('support-agent', 'logs').get()
.then((r) => r.json()).then(modelParser.createParser(reportLogsModel));
}
/**
* Get the user activity log url.
* @param {String} logName - Name of log to retrive URL from.
* @returns {Promise.<logUrlModel>} - A Promise that, when resolved, yields a {@link logUrlModel} containing log url.
*/
getUserActivityLogUrl(logName) {
if(!logName) {
return Promise.reject(new Error('Attempting to get log url without required name'));
}
return this._plug.at('support-agent', 'logs', logName, 'url').get()
.then((r) => r.json()).then(modelParser.createParser(logUrlModel));
}
/**
* Get the user activity.
* @param {Number|String} userActivityToken - A token that identifies the user from an user activity perspective. It can be the user's numeric ID, username, or another system-defined token.
* @param {Object} [options] - Additional information to direct the activity fetching.
* @param {Number} [options.limit=10] - The maximum number results to retrieve.
* @param {Array} [options.include] - An array of strings identifying elements to expand in the result. Valid identifiers are: 'user', 'page', and 'request'.
* @param {String|Date} [options.upTo] - The marker used to paginate.
* @returns {Promise.<userActivityModel>} - A Promise that, when resolved, yields a {@link userActivityModel} containing the user's activity events.
*/
getUserActivity(userActivityToken, options = {}) {
if(!userActivityToken) {
return Promise.reject(new Error('The user activity token must be supplied'));
}
let token;
try {
token = utility.getNormalizedUserActivityToken(userActivityToken);
} catch(e) {
return Promise.reject(e);
}
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
if(options.upTo) {
if(typeof options.upTo !== 'string' && !(options.upTo instanceof Date)) {
return Promise.reject(new Error('The `upTo` parameter must be a string or a Date.'));
}
if(options.upTo instanceof Date) {
params.upto = utility.getApiDateString(options.upTo);
} else {
params.upto = options.upTo;
}
}
return this._plug.at('support-agent', token).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(userActivityModel));
}
/**
* Get the user's history events.
* @param {Number|String} [userId=current] - The user's numeric ID or username.
* @param {Object} [options] - Additional options to direct the history fetching.
* @param {Number} [options.limit=10] - The maximum number results that we want to retrieve.
* @param {Array} [options.include] - An array of elements you'd like to expand. If specified, valid entries are 'user', 'page', and 'request'.
* @param {String|Date} [options.upTo] - The marker used to paginate.
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryModel} that contains the listing of the user's events.
*/
getUserHistory(userId = 'current', options = {}) {
const params = {};
if(options.limit) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be a number.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
if(options.upTo) {
if(typeof options.upTo !== 'string' && !(options.upTo instanceof Date)) {
return Promise.reject(new Error('The `upTo` parameter must be a string or a Date.'));
}
if(options.upTo instanceof Date) {
params.upto = utility.getApiDateString(options.upTo);
} else {
params.upto = options.upTo;
}
}
return this._plug.at('user-page', utility.getResourceId(userId, 'current')).withParams(params).get()
.catch((e) => Promise.reject(_errorParser(e)))
.then((r) => r.json())
.then(modelParser.createParser(pageHistoryModel));
}
/**
* Get the details of a specific user event.
* @param {String} detailId - The detail ID of the event.
* @param {Object} [options] - Information to direct the detail to fetch.
* @param {Array} [options.include] - An array of strings identifying elements to expand in the result. Valid identifiers are: 'page', 'user', 'file', and 'request'.
* @returns {Promise.<pageHistoryModel>} - A Promise that, when resolved, yields a {@link pageHistoryModel} that contains the event information.
*/
getUserHistoryDetail(detailId, options = {}) {
if(!detailId) {
return Promise.reject(new Error('The detail ID must be supplied'));
}
const params = {};
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.at('user-page', 'current', detailId).withParams(params).get()
.then((r) => r.json()).then(modelParser.createParser(pageHistoryModel));
}
/**
* Log a web widget impression event. This request will fail if not called from a MindTouch web widget.
* @returns {Promise} - A Promise that, when resolved, contains the status of the web widget impression request.
*/
logWebWidgetImpression() {
return this._plug.at('web-widget-impression').post();
}
}