2014-03-12 20:53:42 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2015-12-11 15:17:05 +02:00
|
|
|
var async = require('async');
|
|
|
|
|
var db = require('../database');
|
2016-07-04 17:49:02 +03:00
|
|
|
var posts = require('../posts');
|
2015-12-11 15:17:05 +02:00
|
|
|
var plugins = require('../plugins');
|
2014-03-12 20:53:42 -04:00
|
|
|
|
|
|
|
|
module.exports = function(User) {
|
|
|
|
|
|
|
|
|
|
User.logIP = function(uid, ip) {
|
2014-12-02 12:38:53 -05:00
|
|
|
var now = Date.now();
|
|
|
|
|
db.sortedSetAdd('uid:' + uid + ':ip', now, ip || 'Unknown');
|
|
|
|
|
if (ip) {
|
|
|
|
|
db.sortedSetAdd('ip:' + ip + ':uid', now, uid);
|
|
|
|
|
}
|
2014-03-12 20:53:42 -04:00
|
|
|
};
|
|
|
|
|
|
2015-03-31 23:40:58 -04:00
|
|
|
User.getIPs = function(uid, stop, callback) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':ip', 0, stop, function(err, ips) {
|
2015-12-11 15:17:05 +02:00
|
|
|
if (err) {
|
2014-03-12 20:53:42 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, ips.map(function(ip) {
|
|
|
|
|
return {ip:ip};
|
|
|
|
|
}));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.getUsersCSV = function(callback) {
|
|
|
|
|
var csvContent = '';
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
2015-12-11 15:17:05 +02:00
|
|
|
function (next) {
|
2015-05-15 14:46:37 -04:00
|
|
|
db.getSortedSetRangeWithScores('username:uid', 0, -1, next);
|
2014-03-12 20:53:42 -04:00
|
|
|
},
|
2015-12-11 15:17:05 +02:00
|
|
|
function (users, next) {
|
2015-05-15 14:46:37 -04:00
|
|
|
var uids = users.map(function(user) {
|
|
|
|
|
return user.score;
|
|
|
|
|
});
|
2015-09-25 17:38:58 -04:00
|
|
|
User.getUsersFields(uids, ['uid', 'email', 'username'], next);
|
2014-03-12 20:53:42 -04:00
|
|
|
},
|
2015-12-11 15:17:05 +02:00
|
|
|
function (usersData, next) {
|
|
|
|
|
usersData.forEach(function(user) {
|
2014-03-12 20:53:42 -04:00
|
|
|
if (user) {
|
|
|
|
|
csvContent += user.email + ',' + user.username + ',' + user.uid + '\n';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
next(null, csvContent);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
2014-03-12 21:57:25 -04:00
|
|
|
|
2016-06-28 16:30:39 -04:00
|
|
|
User.ban = function(uid, until, callback) {
|
|
|
|
|
// "until" (optional) is unix timestamp in milliseconds
|
|
|
|
|
if (!callback && typeof until === 'function') {
|
|
|
|
|
callback = until;
|
|
|
|
|
until = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
until = parseInt(until, 10);
|
|
|
|
|
if (isNaN(until)) {
|
|
|
|
|
return callback(new Error('[[error:ban-expiry-missing]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tasks = [
|
|
|
|
|
async.apply(User.setUserField, uid, 'banned', 1),
|
|
|
|
|
async.apply(db.sortedSetAdd, 'users:banned', Date.now(), uid),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (until > 0 && Date.now() < until) {
|
|
|
|
|
tasks.push(async.apply(db.sortedSetAdd, 'users:banned:expire', until, uid));
|
2016-06-29 12:07:23 -04:00
|
|
|
} else {
|
|
|
|
|
until = 0;
|
2016-06-28 16:30:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.series(tasks, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
2014-10-03 14:14:41 -04:00
|
|
|
}
|
2016-06-28 16:30:39 -04:00
|
|
|
|
2016-06-29 12:07:23 -04:00
|
|
|
plugins.fireHook('action:user.banned', {
|
|
|
|
|
uid: uid,
|
|
|
|
|
until: until > 0 ? until : undefined
|
|
|
|
|
});
|
2016-06-28 16:30:39 -04:00
|
|
|
callback();
|
|
|
|
|
});
|
2014-03-12 21:57:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
User.unban = function(uid, callback) {
|
2015-12-11 15:17:05 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
User.setUserField(uid, 'banned', 0, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
2016-07-05 11:11:22 -04:00
|
|
|
db.sortedSetsRemove(['users:banned', 'users:banned:expire'], uid, next);
|
2015-12-11 15:17:05 +02:00
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('action:user.unbanned', {uid: uid});
|
|
|
|
|
next();
|
2014-10-03 14:14:41 -04:00
|
|
|
}
|
2015-12-11 15:17:05 +02:00
|
|
|
], callback);
|
2014-03-12 21:57:25 -04:00
|
|
|
};
|
2014-12-21 16:29:32 -05:00
|
|
|
|
|
|
|
|
User.resetFlags = function(uids, callback) {
|
|
|
|
|
if (!Array.isArray(uids) || !uids.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2016-07-04 17:49:02 +03:00
|
|
|
|
|
|
|
|
async.eachSeries(uids, function(uid, next) {
|
|
|
|
|
posts.dismissUserFlags(uid, next);
|
|
|
|
|
}, callback);
|
2014-12-21 16:29:32 -05:00
|
|
|
};
|
2014-03-14 19:07:50 -04:00
|
|
|
};
|