2014-03-12 20:53:42 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2020-11-27 15:38:22 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const path = require('path');
|
2019-10-03 23:31:42 -04:00
|
|
|
const winston = require('winston');
|
|
|
|
|
const validator = require('validator');
|
2017-11-30 14:24:13 -05:00
|
|
|
|
2020-11-27 15:38:22 -05:00
|
|
|
const { baseDir } = require('../constants').paths;
|
2019-10-03 23:31:42 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const plugins = require('../plugins');
|
2020-03-05 11:03:49 -05:00
|
|
|
const batch = require('../batch');
|
2014-03-12 20:53:42 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (User) {
|
2019-07-09 22:23:10 -04:00
|
|
|
User.logIP = async function (uid, ip) {
|
2018-12-14 00:09:15 -05:00
|
|
|
if (!(parseInt(uid, 10) > 0)) {
|
2019-07-09 22:23:10 -04:00
|
|
|
return;
|
2018-12-14 00:09:15 -05:00
|
|
|
}
|
2019-10-03 23:31:42 -04:00
|
|
|
const now = Date.now();
|
2019-06-24 21:36:20 -04:00
|
|
|
const bulk = [
|
2021-02-03 23:59:08 -07:00
|
|
|
[`uid:${uid}:ip`, now, ip || 'Unknown'],
|
2019-06-24 21:36:20 -04:00
|
|
|
];
|
|
|
|
|
if (ip) {
|
2021-02-03 23:59:08 -07:00
|
|
|
bulk.push([`ip:${ip}:uid`, now, uid]);
|
2019-06-24 21:36:20 -04:00
|
|
|
}
|
2019-07-09 22:23:10 -04:00
|
|
|
await db.sortedSetAddBulk(bulk);
|
2014-03-12 20:53:42 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 22:23:10 -04:00
|
|
|
User.getIPs = async function (uid, stop) {
|
2021-02-03 23:59:08 -07:00
|
|
|
const ips = await db.getSortedSetRevRange(`uid:${uid}:ip`, 0, stop);
|
2019-07-09 22:23:10 -04:00
|
|
|
return ips.map(ip => validator.escape(String(ip)));
|
2014-03-12 20:53:42 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 22:23:10 -04:00
|
|
|
User.getUsersCSV = async function () {
|
2016-11-23 17:15:31 +03:00
|
|
|
winston.verbose('[user/getUsersCSV] Compiling User CSV data');
|
2020-03-05 11:03:49 -05:00
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
const data = await plugins.hooks.fire('filter:user.csvFields', { fields: ['uid', 'email', 'username'] });
|
2021-02-03 23:59:08 -07:00
|
|
|
let csvContent = `${data.fields.join(',')}\n`;
|
2020-03-05 11:03:49 -05:00
|
|
|
await batch.processSortedSet('users:joindate', async (uids) => {
|
|
|
|
|
const usersData = await User.getUsersFields(uids, data.fields);
|
|
|
|
|
csvContent += usersData.reduce((memo, user) => {
|
2021-02-03 23:59:08 -07:00
|
|
|
memo += `${data.fields.map(field => user[field]).join(',')}\n`;
|
2020-03-05 11:03:49 -05:00
|
|
|
return memo;
|
|
|
|
|
}, '');
|
|
|
|
|
}, {});
|
|
|
|
|
|
2019-07-09 22:23:10 -04:00
|
|
|
return csvContent;
|
2014-03-12 20:53:42 -04:00
|
|
|
};
|
2020-11-27 15:38:22 -05:00
|
|
|
|
|
|
|
|
User.exportUsersCSV = async function () {
|
|
|
|
|
winston.verbose('[user/exportUsersCSV] Exporting User CSV data');
|
|
|
|
|
|
|
|
|
|
const data = await plugins.hooks.fire('filter:user.csvFields', { fields: ['email', 'username', 'uid'] });
|
|
|
|
|
const fd = await fs.promises.open(
|
|
|
|
|
path.join(baseDir, 'build/export', 'users.csv'),
|
|
|
|
|
'w'
|
|
|
|
|
);
|
2021-06-08 11:50:52 -04:00
|
|
|
fs.promises.appendFile(fd, `${data.fields.join(',')},ip\n`);
|
2020-11-27 15:38:22 -05:00
|
|
|
await batch.processSortedSet('users:joindate', async (uids) => {
|
|
|
|
|
const usersData = await User.getUsersFields(uids, data.fields.slice());
|
2021-06-08 11:50:52 -04:00
|
|
|
const ips = await Promise.all(uids.map(uid => db.getSortedSetRevRange(`uid:${uid}:ip`, 0, -1)));
|
2020-11-27 15:38:22 -05:00
|
|
|
let line = '';
|
2021-06-08 11:50:52 -04:00
|
|
|
usersData.forEach((user, index) => {
|
|
|
|
|
const userIPs = ips[index] ? ips[index].join(',') : '';
|
|
|
|
|
line += `${data.fields.map(field => user[field]).join(',')},"${userIPs}"\n`;
|
2020-11-27 15:38:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await fs.promises.appendFile(fd, line);
|
|
|
|
|
}, {
|
|
|
|
|
batch: 5000,
|
|
|
|
|
interval: 250,
|
|
|
|
|
});
|
|
|
|
|
await fd.close();
|
|
|
|
|
};
|
2014-03-14 19:07:50 -04:00
|
|
|
};
|