mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 03:01:08 +01:00
style
This commit is contained in:
211
src/events.js
211
src/events.js
@@ -9,94 +9,95 @@ var batch = require('./batch');
|
||||
var user = require('./user');
|
||||
var utils = require('./utils');
|
||||
|
||||
(function (events) {
|
||||
events.log = function (data, callback) {
|
||||
callback = callback || function () {};
|
||||
var events = module.exports;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.incrObjectField('global', 'nextEid', next);
|
||||
},
|
||||
function (eid, next) {
|
||||
data.timestamp = Date.now();
|
||||
data.eid = eid;
|
||||
events.log = function (data, callback) {
|
||||
callback = callback || function () {};
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.incrObjectField('global', 'nextEid', next);
|
||||
},
|
||||
function (eid, next) {
|
||||
data.timestamp = Date.now();
|
||||
data.eid = eid;
|
||||
|
||||
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;
|
||||
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] || ''));
|
||||
}
|
||||
});
|
||||
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] || ''));
|
||||
}
|
||||
});
|
||||
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);
|
||||
};
|
||||
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;
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
isAdmin: function (next) {
|
||||
user.isAdministrator(uids, next);
|
||||
},
|
||||
userData: function (next) {
|
||||
user.getUsersFields(uids, ['username', 'userslug', 'picture'], next);
|
||||
},
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
if (!uids.length) {
|
||||
return callback(null, eventsData);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
var map = {};
|
||||
@@ -110,30 +111,30 @@ var utils = require('./utils');
|
||||
event[objectName] = map[event[field]];
|
||||
}
|
||||
});
|
||||
callback(null, eventsData);
|
||||
});
|
||||
}
|
||||
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.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 () {};
|
||||
events.deleteAll = function (callback) {
|
||||
callback = callback || function () {};
|
||||
|
||||
batch.processSortedSet('events:time', function (eids, next) {
|
||||
events.deleteEvents(eids, next);
|
||||
}, { alwaysStartAt: 0 }, callback);
|
||||
};
|
||||
}(module.exports));
|
||||
batch.processSortedSet('events:time', function (eids, next) {
|
||||
events.deleteEvents(eids, next);
|
||||
}, { alwaysStartAt: 0 }, callback);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user