Files
NodeBB/src/events.js

138 lines
3.3 KiB
JavaScript
Raw Normal View History

2013-12-21 19:42:21 -05:00
'use strict';
2013-12-21 19:42:21 -05:00
2016-09-27 14:23:48 +03:00
var async = require('async');
var validator = require('validator');
2016-10-18 22:00:38 -07:00
2016-09-27 14:23:48 +03:00
var db = require('./database');
var batch = require('./batch');
var user = require('./user');
var utils = require('../public/src/utils');
2013-12-21 19:42:21 -05:00
(function (events) {
events.log = function (data, callback) {
callback = callback || function () {};
2015-02-01 19:11:58 -05:00
async.waterfall([
function (next) {
2015-02-01 19:11:58 -05:00
db.incrObjectField('global', 'nextEid', next);
},
function (eid, next) {
2015-02-01 19:11:58 -05:00
data.timestamp = Date.now();
data.eid = eid;
async.parallel([
function (next) {
2015-02-01 19:11:58 -05:00
db.sortedSetAdd('events:time', data.timestamp, eid, next);
},
function (next) {
2015-02-01 19:11:58 -05:00
db.setObject('event:' + eid, data, next);
}
], next);
2014-02-19 13:30:31 -05:00
}
], function (err, result) {
2015-02-01 19:11:58 -05:00
callback(err);
2014-02-19 13:30:31 -05:00
});
2014-10-10 15:44:02 -04:00
};
events.getEvents = function (start, stop, callback) {
2015-02-01 19:11:58 -05:00
async.waterfall([
function (next) {
2015-02-01 19:11:58 -05:00
db.getSortedSetRevRange('events:time', start, stop, next);
},
function (eids, next) {
var keys = eids.map(function (eid) {
2015-02-01 19:11:58 -05:00
return 'event:' + eid;
});
db.getObjects(keys, next);
},
function (eventsData, next) {
2016-10-23 19:16:19 +03:00
eventsData = eventsData.filter(Boolean);
2016-08-22 14:38:04 +03:00
addUserData(eventsData, 'uid', 'user', next);
},
function (eventsData, next) {
2016-08-22 14:38:04 +03:00
addUserData(eventsData, 'targetUid', 'targetUser', next);
},
function (eventsData, next) {
2016-10-23 19:16:19 +03:00
eventsData.forEach(function (event) {
Object.keys(event).forEach(function (key) {
2016-09-27 14:23:48 +03:00
if (typeof event[key] === 'string') {
2016-10-18 22:00:38 -07:00
event[key] = validator.escape(String(event[key] || ''));
}
2016-09-27 14:23:48 +03:00
});
2015-02-01 19:11:58 -05:00
var e = utils.merge(event);
2016-08-22 14:38:04 +03:00
e.eid = e.uid = e.type = e.ip = e.user = undefined;
2015-02-01 19:11:58 -05:00
event.jsonString = JSON.stringify(e, null, 4);
event.timestampISO = new Date(parseInt(event.timestamp, 10)).toUTCString();
});
2016-08-22 14:38:04 +03:00
next(null, eventsData);
2013-12-21 19:42:21 -05:00
}
2015-02-01 19:11:58 -05:00
], callback);
2014-10-08 12:11:06 -04:00
};
2013-12-21 19:42:21 -05:00
2015-02-01 19:11:58 -05:00
function addUserData(eventsData, field, objectName, callback) {
var uids = eventsData.map(function (event) {
2015-02-01 19:11:58 -05:00
return event && event[field];
}).filter(function (uid, index, array) {
2015-02-01 19:11:58 -05:00
return uid && array.indexOf(uid) === index;
2013-12-21 19:42:21 -05:00
});
2015-02-01 19:11:58 -05:00
if (!uids.length) {
return callback(null, eventsData);
}
2013-12-21 19:42:21 -05:00
2015-02-01 19:11:58 -05:00
async.parallel({
isAdmin: function (next) {
2015-02-01 19:11:58 -05:00
user.isAdministrator(uids, next);
},
userData: function (next) {
2015-09-25 17:38:58 -04:00
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
}
}, function (err, results) {
2015-02-01 19:11:58 -05:00
if (err) {
return callback(err);
2014-10-06 18:19:33 -04:00
}
2015-02-01 19:11:58 -05:00
var userData = results.userData;
2014-10-06 18:19:33 -04:00
2015-02-01 19:11:58 -05:00
var map = {};
userData.forEach(function (user, index) {
2015-02-01 19:11:58 -05:00
user.isAdmin = results.isAdmin[index];
map[user.uid] = user;
2014-10-06 18:19:33 -04:00
});
2016-10-23 19:16:19 +03:00
eventsData.forEach(function (event) {
2015-02-01 19:11:58 -05:00
if (map[event[field]]) {
event[objectName] = map[event[field]];
2014-10-06 18:19:33 -04:00
}
2014-10-08 12:11:06 -04:00
});
2015-02-01 19:11:58 -05:00
callback(null, eventsData);
});
2015-02-01 19:11:58 -05:00
}
events.deleteEvents = function (eids, callback) {
callback = callback || function () {};
2015-02-01 19:11:58 -05:00
async.parallel([
function (next) {
var keys = eids.map(function (eid) {
2015-02-01 19:11:58 -05:00
return 'event:' + eid;
});
db.deleteAll(keys, next);
},
function (next) {
2015-02-01 19:11:58 -05:00
db.sortedSetRemove('events:time', eids, next);
}
], callback);
};
events.deleteAll = function (callback) {
callback = callback || function () {};
2014-10-06 18:19:33 -04:00
batch.processSortedSet('events:time', function (eids, next) {
2016-09-27 14:24:14 +03:00
events.deleteEvents(eids, next);
2015-02-01 19:11:58 -05:00
}, {alwaysStartAt: 0}, callback);
};
2013-12-21 19:42:21 -05:00
2015-02-01 19:11:58 -05:00
2014-04-10 20:31:57 +01:00
}(module.exports));