Files
NodeBB/src/events.js

140 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-12-21 19:42:21 -05:00
'use strict';
2013-12-21 19:42:21 -05:00
var fs = require('fs'),
winston = require('winston'),
2013-12-21 19:42:21 -05:00
path = require('path'),
nconf = require('nconf'),
user = require('./user');
(function(events) {
2014-04-02 14:31:26 -04:00
var logFileName = 'logs/events.log';
2013-12-21 19:42:21 -05:00
events.logPasswordChange = function(uid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'changed password');
};
events.logAdminChangeUserPassword = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'changed password of', callback);
};
events.logAdminUserDelete = function(adminUid, theirUid, callback) {
logAdminEvent(adminUid, theirUid, 'deleted', callback);
};
2013-12-21 19:42:21 -05:00
function logAdminEvent(adminUid, theirUid, message, callback) {
2014-02-19 13:30:31 -05:00
user.getMultipleUserFields([adminUid, theirUid], ['username'], function(err, userData) {
if(err) {
return winston.error('Error logging event. ' + err.message);
}
var msg = userData[0].username + '(uid ' + adminUid + ') ' + message + ' ' + userData[1].username + '(uid ' + theirUid + ')';
events.log(msg, callback);
2014-02-19 13:30:31 -05:00
});
}
2013-12-21 19:42:21 -05:00
events.logPasswordReset = function(uid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'reset password');
};
2013-12-21 19:42:21 -05:00
events.logEmailChange = function(uid, oldEmail, newEmail) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid,'changed email from "' + oldEmail + '" to "' + newEmail +'"');
};
2013-12-21 19:42:21 -05:00
events.logUsernameChange = function(uid, oldUsername, newUsername) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid,'changed username from "' + oldUsername + '" to "' + newUsername +'"');
};
2013-12-21 19:42:21 -05:00
events.logAdminLogin = function(uid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'logged into admin panel');
};
2013-12-21 19:42:21 -05:00
events.logPostEdit = function(uid, pid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'edited post (pid ' + pid + ')');
};
2013-12-21 19:42:21 -05:00
events.logPostDelete = function(uid, pid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'deleted post (pid ' + pid + ')');
};
2013-12-21 19:42:21 -05:00
events.logPostRestore = function(uid, pid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'restored post (pid ' + pid + ')');
};
2013-12-21 19:42:21 -05:00
events.logTopicDelete = function(uid, tid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'deleted topic (tid ' + tid + ')');
};
2013-12-21 19:42:21 -05:00
events.logTopicRestore = function(uid, tid) {
2014-09-06 22:28:09 -04:00
events.logWithUser(uid, 'restored topic (tid ' + tid + ')');
};
2013-12-21 19:42:21 -05:00
2014-09-06 22:28:09 -04:00
events.logWithUser = function(uid, string) {
2013-12-21 19:42:21 -05:00
user.getUserField(uid, 'username', function(err, username) {
if(err) {
2014-02-19 13:30:31 -05:00
return winston.error('Error logging event. ' + err.message);
2013-12-21 19:42:21 -05:00
}
2014-02-19 13:30:31 -05:00
var msg = username + '(uid ' + uid + ') ' + string;
2014-02-11 17:44:44 -05:00
events.log(msg);
2014-02-11 14:40:43 -05:00
});
2014-10-08 12:11:06 -04:00
};
2013-12-21 19:42:21 -05:00
events.log = function(msg, callback) {
2014-02-11 14:40:43 -05:00
var logFile = path.join(nconf.get('base_dir'), logFileName);
2013-12-21 19:42:21 -05:00
2014-02-19 13:30:31 -05:00
msg = '[' + new Date().toUTCString() + '] - ' + msg;
2014-02-11 15:13:02 -05:00
fs.appendFile(logFile, msg + '\n', function(err) {
2014-02-11 14:40:43 -05:00
if(err) {
winston.error('Error logging event. ' + err.message);
if (typeof callback === 'function') {
callback(err);
}
2014-02-11 14:40:43 -05:00
return;
}
if (typeof callback === 'function') {
callback();
}
2013-12-21 19:42:21 -05:00
});
};
2013-12-21 19:42:21 -05:00
2014-10-06 18:19:33 -04:00
events.getLog = function(end, len, callback) {
var logFile = path.join(nconf.get('base_dir'), logFileName);
2013-12-21 19:42:21 -05:00
2014-10-06 18:19:33 -04:00
fs.stat(logFile, function(err, stat) {
if (err) {
2014-04-02 14:31:26 -04:00
return callback(null, 'No logs found!');
}
2014-10-06 18:19:33 -04:00
var buffer = '';
var size = stat.size;
if (end === -1) {
end = size;
}
end = parseInt(end, 10);
var start = Math.max(0, end - len);
var rs = fs.createReadStream(logFile, {start: start, end: end});
rs.addListener('data', function(lines) {
buffer += lines.toString();
});
rs.addListener('end', function() {
var firstNewline = buffer.indexOf('\n');
if (firstNewline !== -1) {
buffer = buffer.slice(firstNewline);
buffer = buffer.split('\n').reverse().join('\n');
}
callback(null, {data: buffer, next: end - buffer.length});
2014-10-08 12:11:06 -04:00
});
});
2014-10-06 18:19:33 -04:00
};
2013-12-21 19:42:21 -05:00
2014-04-10 20:31:57 +01:00
}(module.exports));