-
Notifications
You must be signed in to change notification settings - Fork 0
/
jivelib.js
97 lines (67 loc) · 1.91 KB
/
jivelib.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
'use strict';
var q = require('q');
var request = require('request');
var qs = require('querystring');
var config = require('./config');
var auth = {
'user': config.username,
'pass': config.password
};
function getActivity(afterDate) {
var defer = q.defer();
var params = {
fields: 'content'
};
if (afterDate) {
params.after = afterDate;
}
var url = config.jiveUrl + '/api/core/v3/activities?' + qs.stringify(params);
request.get({url: url, auth: auth}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var escaped = body.replace(/^throw [^;]*;/, '');
defer.resolve(JSON.parse(escaped));
return;
}
defer.reject(response.statusCode + ' ' + error);
});
return defer.promise;
};
function typeOfContent(content) {
if (/.*<\/a> liked <a.*/.test(content)) {
return 'LIKE';
}
if (/.*<\/a> joined <a.*/.test(content)) {
return 'JOIN';
}
if (/.*<\/a> created <a.*/.test(content)) {
return 'CREATE';
}
if (/.*<\/a> replied to <a.*/.test(content)) {
return 'REPLY';
}
if (/.*<\/a> modified <a.*/.test(content)) {
return 'MODIFY';
}
if (/.*<\/a> commented on <a.*/.test(content)) {
return 'COMMENT';
}
if (/.*<\/a> created $/.test(content)) {
return 'ACCOUNT';
}
return 'UNKNOWN';
};
var lastActivityDate = null;
exports.getLastActivity = function () {
return getActivity(lastActivityDate).then(function (result) {
if (result.list.length) {
var activity = result.list[0];
lastActivityDate = activity.updated || activity.published;
return typeOfContent(activity.content);
} else {
return 'NOTHING';
}
}, function (error) {
console.log('error ' + error);
return error;
});
};