refactor: allow user.hidePrivateData() to handle an array of `userData`

isekai-main
Julian Lam 3 years ago
parent 6fac823e64
commit 3f5ae893c6

@ -145,25 +145,33 @@ module.exports = function (User) {
return await User.getUsersFields(uids, []);
};
User.hidePrivateData = async function (userData, callerUID) {
const _userData = { ...userData };
User.hidePrivateData = async function (users, callerUID) {
if (!Array.isArray(users)) {
users = [users];
}
const isSelf = parseInt(callerUID, 10) === parseInt(_userData.uid, 10);
const [userSettings, isAdmin, isGlobalModerator] = await Promise.all([
User.getSettings(_userData.uid),
User.getMultipleUserSettings(users.map(user => user.uid)),
User.isAdministrator(callerUID),
User.isGlobalModerator(callerUID),
]);
const privilegedOrSelf = isAdmin || isGlobalModerator || isSelf;
if (!privilegedOrSelf && (!userSettings.showemail || meta.config.hideEmail)) {
_userData.email = '';
}
if (!privilegedOrSelf && (!userSettings.showfullname || meta.config.hideFullname)) {
_userData.fullname = '';
}
users = await Promise.all(users.map(async (userData, idx) => {
const _userData = { ...userData };
const isSelf = parseInt(callerUID, 10) === parseInt(_userData.uid, 10);
const privilegedOrSelf = isAdmin || isGlobalModerator || isSelf;
if (!privilegedOrSelf && (!userSettings[idx].showemail || meta.config.hideEmail)) {
_userData.email = '';
}
if (!privilegedOrSelf && (!userSettings[idx].showfullname || meta.config.hideFullname)) {
_userData.fullname = '';
}
return _userData;
}));
return _userData;
return users.length === 1 ? users.pop() : users;
};
async function modifyUserData(users, requestedFields, fieldsToRemove) {

Loading…
Cancel
Save