You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/events.js

158 lines
3.8 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var async = require('async');
var validator = require('validator');
var winston = require('winston');
var db = require('./database');
var batch = require('./batch');
var user = require('./user');
var utils = require('./utils');
11 years ago
8 years ago
var events = module.exports;
events.log = function (data, callback) {
callback = callback || function () {};
async.waterfall([
function (next) {
db.incrObjectField('global', 'nextEid', next);
},
function (eid, next) {
data.timestamp = Date.now();
data.eid = eid;
async.parallel([
function (next) {
db.sortedSetAdd('events:time', data.timestamp, eid, next);
},
function (next) {
db.setObject('event:' + eid, data, next);
},
], next);
},
], function (err) {
callback(err);
});
};
events.getEvents = function (start, stop, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRange('events:time', start, stop, next);
},
function (eids, next) {
var keys = eids.map(function (eid) {
return 'event:' + eid;
});
db.getObjects(keys, next);
},
function (eventsData, next) {
eventsData = eventsData.filter(Boolean);
addUserData(eventsData, 'uid', 'user', next);
},
function (eventsData, next) {
addUserData(eventsData, 'targetUid', 'targetUser', next);
},
function (eventsData, next) {
eventsData.forEach(function (event) {
Object.keys(event).forEach(function (key) {
if (typeof event[key] === 'string') {
event[key] = validator.escape(String(event[key] || ''));
}
});
8 years ago
var e = utils.merge(event);
e.eid = undefined;
e.uid = undefined;
e.type = undefined;
e.ip = undefined;
e.user = undefined;
event.jsonString = JSON.stringify(e, null, 4);
event.timestampISO = new Date(parseInt(event.timestamp, 10)).toUTCString();
});
next(null, eventsData);
},
], callback);
};
function addUserData(eventsData, field, objectName, callback) {
var uids = eventsData.map(function (event) {
return event && event[field];
}).filter(function (uid, index, array) {
return uid && array.indexOf(uid) === index;
});
if (!uids.length) {
return callback(null, eventsData);
}
10 years ago
8 years ago
async.waterfall([
function (next) {
async.parallel({
isAdmin: function (next) {
user.isAdministrator(uids, next);
},
userData: function (next) {
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
},
}, next);
},
function (results, next) {
var userData = results.userData;
10 years ago
var map = {};
userData.forEach(function (user, index) {
user.isAdmin = results.isAdmin[index];
map[user.uid] = user;
10 years ago
});
eventsData.forEach(function (event) {
if (map[event[field]]) {
event[objectName] = map[event[field]];
10 years ago
}
10 years ago
});
8 years ago
next(null, eventsData);
},
], callback);
}
events.deleteEvents = function (eids, callback) {
callback = callback || function () {};
async.parallel([
function (next) {
var keys = eids.map(function (eid) {
return 'event:' + eid;
});
db.deleteAll(keys, next);
},
function (next) {
db.sortedSetRemove('events:time', eids, next);
},
], callback);
};
events.deleteAll = function (callback) {
callback = callback || function () {};
batch.processSortedSet('events:time', function (eids, next) {
events.deleteEvents(eids, next);
}, { alwaysStartAt: 0 }, callback);
};
8 years ago
events.output = function () {
process.stdout.write('\nDisplaying last ten administrative events...\n'.bold);
events.getEvents(0, 9, function (err, events) {
if (err) {
winston.error('Error fetching events', err);
throw err;
8 years ago
}
events.forEach(function (event) {
process.stdout.write(' * ' + String(event.timestampISO).green + ' ' + String(event.type).yellow + (event.text ? ' ' + event.text : '') + ' (uid: '.reset + (event.uid ? event.uid : 0) + ')\n');
});
process.exit(0);
});
};