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

129 lines
2.9 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var async = require('async'),
11 years ago
db = require('./database'),
batch = require('./batch'),
user = require('./user'),
utils = require('../public/src/utils');
11 years ago
(function(events) {
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);
11 years ago
}
], function(err, result) {
callback(err);
11 years ago
});
10 years ago
};
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.forEach(function(event) {
var e = utils.merge(event);
e.eid = e.uid = e.type = e.ip = undefined;
event.jsonString = JSON.stringify(e, null, 4);
event.timestampISO = new Date(parseInt(event.timestamp, 10)).toUTCString();
});
addUserData(eventsData, 'uid', 'user', next);
},
function(eventsData, next) {
addUserData(eventsData, 'targetUid', 'targetUser', next);
11 years ago
}
], callback);
10 years ago
};
11 years ago
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;
11 years ago
});
if (!uids.length) {
return callback(null, eventsData);
}
11 years ago
async.parallel({
isAdmin: function(next) {
user.isAdministrator(uids, next);
},
userData: function(next) {
user.getMultipleUserFields(uids, ['username', 'userslug', 'picture'], next);
}
}, function(err, results) {
if (err) {
return callback(err);
10 years ago
}
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
});
callback(null, eventsData);
});
}
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() {};
10 years ago
batch.processSortedSet('events:time', function(eids, next) {
events.deleteEvents(eids, callback);
}, {alwaysStartAt: 0}, callback);
};
11 years ago
}(module.exports));