refactor: async/await controllers/user.js

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 5c0266d8c4
commit 3c6c40b177

@ -1,152 +1,110 @@
'use strict'; 'use strict';
var async = require('async'); const path = require('path');
var path = require('path'); const fs = require('fs');
var fs = require('fs'); const winston = require('winston');
var winston = require('winston'); const converter = require('json-2-csv');
var converter = require('json-2-csv'); const archiver = require('archiver');
var archiver = require('archiver'); const util = require('util');
var db = require('../database'); const db = require('../database');
var user = require('../user'); const user = require('../user');
var meta = require('../meta'); const meta = require('../meta');
var posts = require('../posts'); const posts = require('../posts');
var batch = require('../batch'); const batch = require('../batch');
var events = require('../events'); const events = require('../events');
var privileges = require('../privileges'); const privileges = require('../privileges');
var accountHelpers = require('./accounts/helpers'); const accountHelpers = require('./accounts/helpers');
var userController = module.exports; const userController = module.exports;
userController.getCurrentUser = function (req, res, next) { userController.getCurrentUser = async function (req, res) {
if (!req.loggedIn) { if (!req.loggedIn) {
return res.status(401).json('not-authorized'); return res.status(401).json('not-authorized');
} }
async.waterfall([ const userslug = await user.getUserField(req.uid, 'userslug');
function (next) { const userData = await accountHelpers.getUserDataByUserSlug(userslug, req.uid);
user.getUserField(req.uid, 'userslug', next);
},
function (userslug, next) {
accountHelpers.getUserDataByUserSlug(userslug, req.uid, next);
},
function (userData) {
res.json(userData); res.json(userData);
},
], next);
}; };
userController.getUserByUID = async function (req, res, next) {
userController.getUserByUID = function (req, res, next) { await byType('uid', req, res, next);
byType('uid', req, res, next);
}; };
userController.getUserByUsername = function (req, res, next) { userController.getUserByUsername = async function (req, res, next) {
byType('username', req, res, next); await byType('username', req, res, next);
}; };
userController.getUserByEmail = function (req, res, next) { userController.getUserByEmail = async function (req, res, next) {
byType('email', req, res, next); await byType('email', req, res, next);
}; };
function byType(type, req, res, next) { async function byType(type, req, res, next) {
async.waterfall([ const userData = await userController.getUserDataByField(req.uid, type, req.params[type]);
function (next) { if (!userData) {
userController.getUserDataByField(req.uid, type, req.params[type], next);
},
function (data, next) {
if (!data) {
return next(); return next();
} }
res.json(data); res.json(userData);
},
], next);
} }
userController.getUserDataByField = function (callerUid, field, fieldValue, callback) { userController.getUserDataByField = async function (callerUid, field, fieldValue) {
async.waterfall([ let uid = null;
function (next) {
if (field === 'uid') { if (field === 'uid') {
next(null, fieldValue); uid = fieldValue;
} else if (field === 'username') { } else if (field === 'username') {
user.getUidByUsername(fieldValue, next); uid = await user.getUidByUsername(fieldValue);
} else if (field === 'email') { } else if (field === 'email') {
user.getUidByEmail(fieldValue, next); uid = await user.getUidByEmail(fieldValue);
} else {
next(null, null);
} }
},
function (uid, next) {
if (!uid) { if (!uid) {
return next(null, null); return null;
} }
userController.getUserDataByUID(callerUid, uid, next); return await userController.getUserDataByUID(callerUid, uid);
},
], callback);
}; };
userController.getUserDataByUID = function (callerUid, uid, callback) { userController.getUserDataByUID = async function (callerUid, uid) {
if (!parseInt(uid, 10)) { if (!parseInt(uid, 10)) {
return callback(new Error('[[error:no-user]]')); throw new Error('[[error:no-user]]');
} }
async.waterfall([ const canView = await privileges.global.can('view:users', callerUid);
function (next) {
privileges.global.can('view:users', callerUid, next);
},
function (canView, next) {
if (!canView) { if (!canView) {
return next(new Error('[[error:no-privileges]]')); throw new Error('[[error:no-privileges]]');
} }
async.parallel({ const [userData, settings] = await Promise.all([
userData: async.apply(user.getUserData, uid), user.getUserData(uid),
settings: async.apply(user.getSettings, uid), user.getSettings(uid),
}, next); ]);
},
function (results, next) { if (!userData) {
if (!results.userData) { throw new Error('[[error:no-user]]');
return next(new Error('[[error:no-user]]'));
} }
results.userData.email = results.settings.showemail && !meta.config.hideEmail ? results.userData.email : undefined; userData.email = settings.showemail && !meta.config.hideEmail ? userData.email : undefined;
results.userData.fullname = results.settings.showfullname && !meta.config.hideFullname ? results.userData.fullname : undefined; userData.fullname = settings.showfullname && !meta.config.hideFullname ? userData.fullname : undefined;
next(null, results.userData); return userData;
},
], callback);
}; };
userController.exportPosts = function (req, res, next) { const json2csv = util.promisify(function (payload, options, callback) {
async.waterfall([ converter.json2csv(payload, callback, options);
function (next) { });
var payload = [];
batch.processSortedSet('uid:' + res.locals.uid + ':posts', function (pids, next) {
posts.getPostsData(pids, function (err, posts) {
if (err) {
return next(err);
}
userController.exportPosts = async function (req, res) {
var payload = [];
await batch.processSortedSet('uid:' + res.locals.uid + ':posts', async function (pids) {
let postData = await posts.getPostsData(pids);
// Remove empty post references and convert newlines in content // Remove empty post references and convert newlines in content
posts = posts.filter(Boolean).map(function (post) { postData = postData.filter(Boolean).map(function (post) {
post.content = '"' + post.content.replace(/\n/g, '\\n').replace(/"/g, '\\"') + '"'; post.content = '"' + post.content.replace(/\n/g, '\\n').replace(/"/g, '\\"') + '"';
return post; return post;
}); });
payload = payload.concat(postData);
payload = payload.concat(posts);
next();
});
}, function (err) {
next(err, payload);
}); });
}, const csv = await json2csv(payload, {
function (payload, next) {
converter.json2csv(payload, next, {
checkSchemaDifferences: false, checkSchemaDifferences: false,
emptyFieldValue: '', emptyFieldValue: '',
}); });
},
function (csv) {
res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="' + req.params.uid + '_posts.csv"').send(csv); res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="' + req.params.uid + '_posts.csv"').send(csv);
},
], next);
}; };
userController.exportUploads = function (req, res, next) { userController.exportUploads = function (req, res, next) {
@ -230,18 +188,17 @@ userController.exportUploads = function (req, res, next) {
}); });
}; };
userController.exportProfile = function (req, res, next) { userController.exportProfile = async function (req, res) {
const targetUid = res.locals.uid; const targetUid = res.locals.uid;
async.waterfall([ const objects = await db.getObjects(['user:' + targetUid, 'user:' + targetUid + ':settings']);
async.apply(db.getObjects.bind(db), ['user:' + targetUid, 'user:' + targetUid + ':settings']),
function (objects, next) {
Object.assign(objects[0], objects[1]); Object.assign(objects[0], objects[1]);
delete objects[0].password; delete objects[0].password;
converter.json2csv(objects[0], next); const csv = await json2csv(objects[0], {});
},
function (csv) {
res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="' + targetUid + '_profile.csv"').send(csv); res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="' + targetUid + '_profile.csv"').send(csv);
},
], next);
}; };
require('../promisify')(userController, [
'getCurrentUser', 'getUserByUID', 'getUserByUsername', 'getUserByEmail',
'exportPosts', 'exportUploads', 'exportProfile',
]);

Loading…
Cancel
Save