mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	feat: async/await refactor
controllers/accounts
This commit is contained in:
		| @@ -1,212 +1,156 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| var async = require('async'); | ||||
| const user = require('../../user'); | ||||
| const meta = require('../../meta'); | ||||
| const plugins = require('../../plugins'); | ||||
| const helpers = require('../helpers'); | ||||
| const groups = require('../../groups'); | ||||
| const accountHelpers = require('./helpers'); | ||||
| const privileges = require('../../privileges'); | ||||
| const file = require('../../file'); | ||||
|  | ||||
| var db = require('../../database'); | ||||
| var user = require('../../user'); | ||||
| var meta = require('../../meta'); | ||||
| var plugins = require('../../plugins'); | ||||
| var helpers = require('../helpers'); | ||||
| var groups = require('../../groups'); | ||||
| var accountHelpers = require('./helpers'); | ||||
| var privileges = require('../../privileges'); | ||||
| var file = require('../../file'); | ||||
| const editController = module.exports; | ||||
|  | ||||
| var editController = module.exports; | ||||
| editController.get = async function (req, res, next) { | ||||
| 	const [userData, canUseSignature] = await Promise.all([ | ||||
| 		accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid), | ||||
| 		privileges.global.can('signature', req.uid), | ||||
| 	]); | ||||
| 	if (!userData) { | ||||
| 		return next(); | ||||
| 	} | ||||
| 	userData.maximumSignatureLength = meta.config.maximumSignatureLength; | ||||
| 	userData.maximumAboutMeLength = meta.config.maximumAboutMeLength; | ||||
| 	userData.maximumProfileImageSize = meta.config.maximumProfileImageSize; | ||||
| 	userData.allowProfilePicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:profile-picture']; | ||||
| 	userData.allowCoverPicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:cover-picture']; | ||||
| 	userData.allowProfileImageUploads = meta.config.allowProfileImageUploads; | ||||
| 	userData.allowMultipleBadges = meta.config.allowMultipleBadges === 1; | ||||
| 	userData.allowAccountDelete = meta.config.allowAccountDelete === 1; | ||||
| 	userData.allowWebsite = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:website']; | ||||
| 	userData.allowAboutMe = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:aboutme']; | ||||
| 	userData.allowSignature = canUseSignature && (!userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:signature']); | ||||
| 	userData.profileImageDimension = meta.config.profileImageDimension; | ||||
| 	userData.defaultAvatar = user.getDefaultAvatar(); | ||||
|  | ||||
| editController.get = function (req, res, callback) { | ||||
| 	async.waterfall([ | ||||
| 		function (next) { | ||||
| 			async.parallel({ | ||||
| 				userData: function (next) { | ||||
| 					accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next); | ||||
| 				}, | ||||
| 				canUseSignature: function (next) { | ||||
| 					privileges.global.can('signature', req.uid, next); | ||||
| 				}, | ||||
| 			}, next); | ||||
| 	userData.groups = userData.groups.filter(g => g && g.userTitleEnabled && !groups.isPrivilegeGroup(g.name) && g.name !== 'registered-users'); | ||||
|  | ||||
| 	if (!userData.allowMultipleBadges) { | ||||
| 		userData.groupTitle = userData.groupTitleArray[0]; | ||||
| 	} | ||||
| 	userData.groups.forEach(function (group) { | ||||
| 		group.selected = userData.groupTitleArray.includes(group.name); | ||||
| 	}); | ||||
|  | ||||
| 	userData.title = '[[pages:account/edit, ' + userData.username + ']]'; | ||||
| 	userData.breadcrumbs = helpers.buildBreadcrumbs([ | ||||
| 		{ | ||||
| 			text: userData.username, | ||||
| 			url: '/user/' + userData.userslug, | ||||
| 		}, | ||||
| 		function (results, next) { | ||||
| 			var userData = results.userData; | ||||
| 			if (!userData) { | ||||
| 				return callback(); | ||||
| 			} | ||||
| 			userData.maximumSignatureLength = meta.config.maximumSignatureLength; | ||||
| 			userData.maximumAboutMeLength = meta.config.maximumAboutMeLength; | ||||
| 			userData.maximumProfileImageSize = meta.config.maximumProfileImageSize; | ||||
| 			userData.allowProfilePicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:profile-picture']; | ||||
| 			userData.allowCoverPicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:cover-picture']; | ||||
| 			userData.allowProfileImageUploads = meta.config.allowProfileImageUploads; | ||||
| 			userData.allowMultipleBadges = meta.config.allowMultipleBadges === 1; | ||||
| 			userData.allowAccountDelete = meta.config.allowAccountDelete === 1; | ||||
| 			userData.allowWebsite = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:website']; | ||||
| 			userData.allowAboutMe = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:aboutme']; | ||||
| 			userData.allowSignature = results.canUseSignature && (!userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:signature']); | ||||
| 			userData.profileImageDimension = meta.config.profileImageDimension; | ||||
| 			userData.defaultAvatar = user.getDefaultAvatar(); | ||||
|  | ||||
| 			userData.groups = userData.groups.filter(function (group) { | ||||
| 				return group && group.userTitleEnabled && !groups.isPrivilegeGroup(group.name) && group.name !== 'registered-users'; | ||||
| 			}); | ||||
|  | ||||
| 			if (!userData.allowMultipleBadges) { | ||||
| 				userData.groupTitle = userData.groupTitleArray[0]; | ||||
| 			} | ||||
| 			userData.groups.forEach(function (group) { | ||||
| 				group.selected = userData.groupTitleArray.includes(group.name); | ||||
| 			}); | ||||
|  | ||||
| 			userData.title = '[[pages:account/edit, ' + userData.username + ']]'; | ||||
| 			userData.breadcrumbs = helpers.buildBreadcrumbs([ | ||||
| 				{ | ||||
| 					text: userData.username, | ||||
| 					url: '/user/' + userData.userslug, | ||||
| 				}, | ||||
| 				{ | ||||
| 					text: '[[user:edit]]', | ||||
| 				}, | ||||
| 			]); | ||||
| 			userData.editButtons = []; | ||||
|  | ||||
| 			plugins.fireHook('filter:user.account.edit', userData, next); | ||||
| 		{ | ||||
| 			text: '[[user:edit]]', | ||||
| 		}, | ||||
| 		function (userData) { | ||||
| 			res.render('account/edit', userData); | ||||
| 		}, | ||||
| 	], callback); | ||||
| 	]); | ||||
| 	userData.editButtons = []; | ||||
|  | ||||
| 	const result = await plugins.fireHook('filter:user.account.edit', userData); | ||||
| 	res.render('account/edit', result); | ||||
| }; | ||||
|  | ||||
| editController.password = function (req, res, next) { | ||||
| 	renderRoute('password', req, res, next); | ||||
| editController.password = async function (req, res, next) { | ||||
| 	await renderRoute('password', req, res, next); | ||||
| }; | ||||
|  | ||||
| editController.username = function (req, res, next) { | ||||
| 	renderRoute('username', req, res, next); | ||||
| editController.username = async function (req, res, next) { | ||||
| 	await renderRoute('username', req, res, next); | ||||
| }; | ||||
|  | ||||
| editController.email = function (req, res, next) { | ||||
| 	renderRoute('email', req, res, next); | ||||
| editController.email = async function (req, res, next) { | ||||
| 	await renderRoute('email', req, res, next); | ||||
| }; | ||||
|  | ||||
| function renderRoute(name, req, res, next) { | ||||
| 	async.waterfall([ | ||||
| 		function (next) { | ||||
| 			getUserData(req, next, next); | ||||
| async function renderRoute(name, req, res, next) { | ||||
| 	const userData = await getUserData(req, next); | ||||
| 	if (!userData) { | ||||
| 		return next(); | ||||
| 	} | ||||
| 	if (meta.config[name + ':disableEdit'] && !userData.isAdmin) { | ||||
| 		return helpers.notAllowed(req, res); | ||||
| 	} | ||||
|  | ||||
| 	if (name === 'password') { | ||||
| 		userData.minimumPasswordLength = meta.config.minimumPasswordLength; | ||||
| 		userData.minimumPasswordStrength = meta.config.minimumPasswordStrength; | ||||
| 	} | ||||
|  | ||||
| 	userData.title = '[[pages:account/edit/' + name + ', ' + userData.username + ']]'; | ||||
| 	userData.breadcrumbs = helpers.buildBreadcrumbs([ | ||||
| 		{ | ||||
| 			text: userData.username, | ||||
| 			url: '/user/' + userData.userslug, | ||||
| 		}, | ||||
| 		function (userData) { | ||||
| 			if (!userData) { | ||||
| 				return next(); | ||||
| 			} | ||||
|  | ||||
| 			if (meta.config[name + ':disableEdit'] && !userData.isAdmin) { | ||||
| 				return helpers.notAllowed(req, res); | ||||
| 			} | ||||
|  | ||||
| 			if (name === 'password') { | ||||
| 				userData.minimumPasswordLength = meta.config.minimumPasswordLength; | ||||
| 				userData.minimumPasswordStrength = meta.config.minimumPasswordStrength; | ||||
| 			} | ||||
|  | ||||
| 			userData.title = '[[pages:account/edit/' + name + ', ' + userData.username + ']]'; | ||||
| 			userData.breadcrumbs = helpers.buildBreadcrumbs([ | ||||
| 				{ | ||||
| 					text: userData.username, | ||||
| 					url: '/user/' + userData.userslug, | ||||
| 				}, | ||||
| 				{ | ||||
| 					text: '[[user:edit]]', | ||||
| 					url: '/user/' + userData.userslug + '/edit', | ||||
| 				}, | ||||
| 				{ | ||||
| 					text: '[[user:' + name + ']]', | ||||
| 				}, | ||||
| 			]); | ||||
|  | ||||
| 			res.render('account/edit/' + name, userData); | ||||
| 		{ | ||||
| 			text: '[[user:edit]]', | ||||
| 			url: '/user/' + userData.userslug + '/edit', | ||||
| 		}, | ||||
| 	], next); | ||||
| 		{ | ||||
| 			text: '[[user:' + name + ']]', | ||||
| 		}, | ||||
| 	]); | ||||
|  | ||||
| 	res.render('account/edit/' + name, userData); | ||||
| } | ||||
|  | ||||
| function getUserData(req, next, callback) { | ||||
| 	var userData; | ||||
| 	async.waterfall([ | ||||
| 		function (next) { | ||||
| 			accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next); | ||||
| 		}, | ||||
| 		function (data, next) { | ||||
| 			userData = data; | ||||
| 			if (!userData) { | ||||
| 				return callback(null, null); | ||||
| 			} | ||||
| 			db.getObjectField('user:' + userData.uid, 'password', next); | ||||
| 		}, | ||||
| 		function (password, next) { | ||||
| 			userData.hasPassword = !!password; | ||||
| 			next(null, userData); | ||||
| 		}, | ||||
| 	], callback); | ||||
| async function getUserData(req) { | ||||
| 	const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid); | ||||
| 	if (!userData) { | ||||
| 		return null; | ||||
| 	} | ||||
|  | ||||
| 	userData.hasPassword = await user.hasPassword(userData.uid); | ||||
| 	return userData; | ||||
| } | ||||
|  | ||||
| editController.uploadPicture = function (req, res, next) { | ||||
| 	var userPhoto = req.files.files[0]; | ||||
|  | ||||
| 	var updateUid; | ||||
|  | ||||
| 	async.waterfall([ | ||||
| 		function (next) { | ||||
| 			user.getUidByUserslug(req.params.userslug, next); | ||||
| 		}, | ||||
| 		function (uid, next) { | ||||
| 			updateUid = uid; | ||||
|  | ||||
| 			privileges.users.canEdit(req.uid, uid, next); | ||||
| 		}, | ||||
| 		function (isAllowed, next) { | ||||
| 			if (!isAllowed) { | ||||
| 				return helpers.notAllowed(req, res); | ||||
| 			} | ||||
| 			user.checkMinReputation(req.uid, updateUid, 'min:rep:profile-picture', next); | ||||
| 		}, | ||||
| 		function (next) { | ||||
| 			user.uploadCroppedPicture({ | ||||
| 				uid: updateUid, | ||||
| 				file: userPhoto, | ||||
| 			}, next); | ||||
| 		}, | ||||
| 	], function (err, image) { | ||||
| 		file.delete(userPhoto.path); | ||||
| 		if (err) { | ||||
| 			return next(err); | ||||
| editController.uploadPicture = async function (req, res, next) { | ||||
| 	const userPhoto = req.files.files[0]; | ||||
| 	try { | ||||
| 		const updateUid = await user.getUidByUserslug(req.params.userslug); | ||||
| 		const isAllowed = await privileges.users.canEdit(req.uid, updateUid); | ||||
| 		if (!isAllowed) { | ||||
| 			return helpers.notAllowed(req, res); | ||||
| 		} | ||||
|  | ||||
| 		await user.checkMinReputation(req.uid, updateUid, 'min:rep:profile-picture'); | ||||
| 		const image = await user.uploadCroppedPicture({ | ||||
| 			uid: updateUid, | ||||
| 			file: userPhoto, | ||||
| 		}); | ||||
| 		res.json([{ | ||||
| 			name: userPhoto.name, | ||||
| 			url: image.url, | ||||
| 		}]); | ||||
| 	}); | ||||
| 	} catch (err) { | ||||
| 		next(err); | ||||
| 	} finally { | ||||
| 		file.delete(userPhoto.path); | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| editController.uploadCoverPicture = function (req, res, next) { | ||||
| editController.uploadCoverPicture = async function (req, res, next) { | ||||
| 	var params = JSON.parse(req.body.params); | ||||
| 	var coverPhoto = req.files.files[0]; | ||||
|  | ||||
| 	async.waterfall([ | ||||
| 		function (next) { | ||||
| 			user.checkMinReputation(req.uid, params.uid, 'min:rep:cover-picture', next); | ||||
| 		}, | ||||
| 		function (next) { | ||||
| 			user.updateCoverPicture({ | ||||
| 				file: coverPhoto, | ||||
| 				uid: params.uid, | ||||
| 			}, next); | ||||
| 		}, | ||||
| 	], function (err, image) { | ||||
| 		file.delete(coverPhoto.path); | ||||
| 		if (err) { | ||||
| 			return next(err); | ||||
| 		} | ||||
| 	try { | ||||
| 		await user.checkMinReputation(req.uid, params.uid, 'min:rep:cover-picture'); | ||||
| 		const image = await user.updateCoverPicture({ | ||||
| 			file: coverPhoto, | ||||
| 			uid: params.uid, | ||||
| 		}); | ||||
| 		res.json([{ | ||||
| 			url: image.url, | ||||
| 		}]); | ||||
| 	}); | ||||
| 	} catch (err) { | ||||
| 		next(err); | ||||
| 	} finally { | ||||
| 		file.delete(coverPhoto.path); | ||||
| 	} | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user