-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
admin.js
70 lines (63 loc) · 1.62 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
var async = require('async');
var winston = require('winston');
var validator = require('validator');
var db = require('../database');
var plugins = require('../plugins');
module.exports = function (User) {
User.logIP = function (uid, ip, callback) {
var now = Date.now();
async.waterfall([
function (next) {
db.sortedSetAdd('uid:' + uid + ':ip', now, ip || 'Unknown', next);
},
function (next) {
if (ip) {
db.sortedSetAdd('ip:' + ip + ':uid', now, uid, next);
} else {
next();
}
},
], callback);
};
User.getIPs = function (uid, stop, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':ip', 0, stop, next);
},
function (ips, next) {
ips = ips.map(function (ip) {
return validator.escape(String(ip));
});
next(null, ips);
},
], callback);
};
User.getUsersCSV = function (callback) {
winston.verbose('[user/getUsersCSV] Compiling User CSV data');
var csvContent = '';
var uids;
async.waterfall([
function (next) {
db.getSortedSetRangeWithScores('username:uid', 0, -1, next);
},
function (users, next) {
uids = users.map(function (user) {
return user.score;
});
plugins.fireHook('filter:user.csvFields', { fields: ['uid', 'email', 'username'] }, next);
},
function (data, next) {
User.getUsersFields(uids, data.fields, next);
},
function (usersData, next) {
usersData.forEach(function (user) {
if (user) {
csvContent += user.email + ',' + user.username + ',' + user.uid + '\n';
}
});
next(null, csvContent);
},
], callback);
};
};