parent
a3541d887b
commit
3cc7ec63e8
@ -1,212 +1,156 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
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');
|
||||
|
||||
var editController = module.exports;
|
||||
|
||||
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);
|
||||
},
|
||||
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);
|
||||
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');
|
||||
|
||||
const 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();
|
||||
|
||||
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 (userData) {
|
||||
res.render('account/edit', userData);
|
||||
{
|
||||
text: '[[user:edit]]',
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
]);
|
||||
userData.editButtons = [];
|
||||
|
||||
editController.password = function (req, res, next) {
|
||||
renderRoute('password', req, res, next);
|
||||
const result = await plugins.fireHook('filter:user.account.edit', userData);
|
||||
res.render('account/edit', result);
|
||||
};
|
||||
|
||||
editController.username = function (req, res, next) {
|
||||
renderRoute('username', req, res, next);
|
||||
editController.password = async function (req, res, next) {
|
||||
await renderRoute('password', req, res, next);
|
||||
};
|
||||
|
||||
editController.email = function (req, res, next) {
|
||||
renderRoute('email', req, res, next);
|
||||
editController.username = async function (req, res, next) {
|
||||
await renderRoute('username', req, res, next);
|
||||
};
|
||||
|
||||
function renderRoute(name, req, res, next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
getUserData(req, next, next);
|
||||
},
|
||||
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);
|
||||
},
|
||||
], next);
|
||||
}
|
||||
editController.email = async function (req, res, next) {
|
||||
await renderRoute('email', req, res, next);
|
||||
};
|
||||
|
||||
function getUserData(req, next, callback) {
|
||||
var userData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, 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 (data, next) {
|
||||
userData = data;
|
||||
if (!userData) {
|
||||
return callback(null, null);
|
||||
}
|
||||
db.getObjectField('user:' + userData.uid, 'password', next);
|
||||
{
|
||||
text: '[[user:edit]]',
|
||||
url: '/user/' + userData.userslug + '/edit',
|
||||
},
|
||||
function (password, next) {
|
||||
userData.hasPassword = !!password;
|
||||
next(null, userData);
|
||||
{
|
||||
text: '[[user:' + name + ']]',
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
]);
|
||||
|
||||
editController.uploadPicture = function (req, res, next) {
|
||||
var userPhoto = req.files.files[0];
|
||||
res.render('account/edit/' + name, userData);
|
||||
}
|
||||
|
||||
var updateUid;
|
||||
async function getUserData(req) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.getUidByUserslug(req.params.userslug, next);
|
||||
},
|
||||
function (uid, next) {
|
||||
updateUid = uid;
|
||||
userData.hasPassword = await user.hasPassword(userData.uid);
|
||||
return userData;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
@ -1,80 +1,59 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
const util = require('util');
|
||||
|
||||
var db = require('../../database');
|
||||
var user = require('../../user');
|
||||
var helpers = require('../helpers');
|
||||
var accountHelpers = require('./helpers');
|
||||
const db = require('../../database');
|
||||
const user = require('../../user');
|
||||
const helpers = require('../helpers');
|
||||
const accountHelpers = require('./helpers');
|
||||
|
||||
var sessionController = module.exports;
|
||||
const sessionController = module.exports;
|
||||
|
||||
sessionController.get = function (req, res, callback) {
|
||||
var userData;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (_userData, next) {
|
||||
userData = _userData;
|
||||
if (!userData) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
user.auth.getSessions(userData.uid, req.sessionID, next);
|
||||
},
|
||||
function (sessions) {
|
||||
userData.sessions = sessions;
|
||||
sessionController.get = async function (req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return next();
|
||||
}
|
||||
|
||||
userData.title = '[[pages:account/sessions]]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[pages:account/sessions]]' }]);
|
||||
userData.sessions = await user.auth.getSessions(userData.uid, req.sessionID);
|
||||
userData.title = '[[pages:account/sessions]]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[pages:account/sessions]]' }]);
|
||||
|
||||
res.render('account/sessions', userData);
|
||||
},
|
||||
], callback);
|
||||
res.render('account/sessions', userData);
|
||||
};
|
||||
|
||||
sessionController.revoke = function (req, res, next) {
|
||||
const getSessionAsync = util.promisify(function (sid, callback) {
|
||||
db.sessionStore.get(sid, (err, sessionObj) => callback(err, sessionObj || null));
|
||||
});
|
||||
|
||||
sessionController.revoke = async function (req, res, next) {
|
||||
if (!req.params.hasOwnProperty('uuid')) {
|
||||
return next();
|
||||
}
|
||||
|
||||
var _id;
|
||||
var uid = res.locals.uid;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (!uid) {
|
||||
return next(new Error('[[error:no-session-found]]'));
|
||||
}
|
||||
db.getSortedSetRange('uid:' + uid + ':sessions', 0, -1, next);
|
||||
},
|
||||
function (sids, done) {
|
||||
async.eachSeries(sids, function (sid, next) {
|
||||
db.sessionStore.get(sid, function (err, sessionObj) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid === req.params.uuid) {
|
||||
_id = sid;
|
||||
done();
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
if (!_id) {
|
||||
return next(new Error('[[error:no-session-found]]'));
|
||||
try {
|
||||
const uid = await user.getUidByUserslug(req.params.userslug);
|
||||
if (!uid) {
|
||||
throw new Error('[[error:no-session-found]]');
|
||||
}
|
||||
const sids = await db.getSortedSetRange('uid:' + uid + ':sessions', 0, -1);
|
||||
let _id;
|
||||
for (const sid of sids) {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const sessionObj = await getSessionAsync(sid);
|
||||
if (sessionObj && sessionObj.meta && sessionObj.meta.uuid === req.params.uuid) {
|
||||
_id = sid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
user.auth.revokeSession(_id, uid, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
return res.status(500).send(err.message);
|
||||
if (!_id) {
|
||||
throw new Error('[[error:no-session-found]]');
|
||||
}
|
||||
return res.sendStatus(200);
|
||||
});
|
||||
|
||||
await user.auth.revokeSession(_id, uid);
|
||||
} catch (err) {
|
||||
return res.status(500).send(err.message);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
};
|
||||
|
@ -1,356 +1,306 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var winston = require('winston');
|
||||
var _ = require('lodash');
|
||||
var jwt = require('jsonwebtoken');
|
||||
|
||||
var user = require('../../user');
|
||||
var languages = require('../../languages');
|
||||
var meta = require('../../meta');
|
||||
var plugins = require('../../plugins');
|
||||
var privileges = require('../../privileges');
|
||||
var categories = require('../../categories');
|
||||
var notifications = require('../../notifications');
|
||||
var db = require('../../database');
|
||||
var helpers = require('../helpers');
|
||||
var accountHelpers = require('./helpers');
|
||||
|
||||
var settingsController = module.exports;
|
||||
|
||||
settingsController.get = function (req, res, callback) {
|
||||
var userData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (_userData, next) {
|
||||
userData = _userData;
|
||||
if (!userData) {
|
||||
return callback();
|
||||
}
|
||||
async.parallel({
|
||||
settings: function (next) {
|
||||
user.getSettings(userData.uid, next);
|
||||
},
|
||||
languages: function (next) {
|
||||
languages.list(next);
|
||||
},
|
||||
soundsMapping: function (next) {
|
||||
meta.sounds.getUserSoundMap(userData.uid, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
userData.settings = results.settings;
|
||||
userData.languages = results.languages;
|
||||
if (userData.isAdmin && userData.isSelf) {
|
||||
userData.acpLanguages = _.cloneDeep(results.languages);
|
||||
}
|
||||
|
||||
var types = [
|
||||
'notification',
|
||||
'chat-incoming',
|
||||
'chat-outgoing',
|
||||
];
|
||||
var aliases = {
|
||||
notification: 'notificationSound',
|
||||
'chat-incoming': 'incomingChatSound',
|
||||
'chat-outgoing': 'outgoingChatSound',
|
||||
};
|
||||
const nconf = require('nconf');
|
||||
const winston = require('winston');
|
||||
const _ = require('lodash');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const util = require('util');
|
||||
|
||||
const user = require('../../user');
|
||||
const languages = require('../../languages');
|
||||
const meta = require('../../meta');
|
||||
const plugins = require('../../plugins');
|
||||
const privileges = require('../../privileges');
|
||||
const categories = require('../../categories');
|
||||
const notifications = require('../../notifications');
|
||||
const db = require('../../database');
|
||||
const helpers = require('../helpers');
|
||||
const accountHelpers = require('./helpers');
|
||||
|
||||
const settingsController = module.exports;
|
||||
|
||||
settingsController.get = async function (req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return next();
|
||||
}
|
||||
const [settings, languagesData, soundsMapping] = await Promise.all([
|
||||
user.getSettings(userData.uid),
|
||||
languages.list(),
|
||||
meta.sounds.getUserSoundMap(userData.uid),
|
||||
|
||||
types.forEach(function (type) {
|
||||
var soundpacks = plugins.soundpacks.map(function (pack) {
|
||||
var sounds = Object.keys(pack.sounds).map(function (soundName) {
|
||||
var value = pack.name + ' | ' + soundName;
|
||||
return {
|
||||
name: soundName,
|
||||
value: value,
|
||||
selected: value === results.soundsMapping[type],
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
name: pack.name,
|
||||
sounds: sounds,
|
||||
};
|
||||
});
|
||||
|
||||
userData[type + '-sound'] = soundpacks;
|
||||
// fallback
|
||||
userData[aliases[type]] = soundpacks.concat.apply([], soundpacks.map(function (pack) {
|
||||
return pack.sounds.map(function (sound) {
|
||||
return {
|
||||
name: sound.value,
|
||||
selected: sound.selected,
|
||||
};
|
||||
});
|
||||
}));
|
||||
});
|
||||
]);
|
||||
|
||||
plugins.fireHook('filter:user.customSettings', { settings: results.settings, customSettings: [], uid: req.uid }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
userData.customSettings = data.customSettings;
|
||||
async.parallel({
|
||||
notificationSettings: function (next) {
|
||||
getNotificationSettings(userData, next);
|
||||
},
|
||||
routes: function (next) {
|
||||
getHomePageRoutes(userData, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results) {
|
||||
userData.homePageRoutes = results.routes;
|
||||
userData.notificationSettings = results.notificationSettings;
|
||||
userData.disableEmailSubscriptions = meta.config.disableEmailSubscriptions;
|
||||
|
||||
userData.dailyDigestFreqOptions = [
|
||||
{ value: 'off', name: '[[user:digest_off]]', selected: userData.settings.dailyDigestFreq === 'off' },
|
||||
{ value: 'day', name: '[[user:digest_daily]]', selected: userData.settings.dailyDigestFreq === 'day' },
|
||||
{ value: 'week', name: '[[user:digest_weekly]]', selected: userData.settings.dailyDigestFreq === 'week' },
|
||||
{ value: 'month', name: '[[user:digest_monthly]]', selected: userData.settings.dailyDigestFreq === 'month' },
|
||||
];
|
||||
|
||||
userData.bootswatchSkinOptions = [
|
||||
{ name: 'Default', value: '' },
|
||||
{ name: 'Cerulean', value: 'cerulean' },
|
||||
{ name: 'Cosmo', value: 'cosmo' },
|
||||
{ name: 'Cyborg', value: 'cyborg' },
|
||||
{ name: 'Darkly', value: 'darkly' },
|
||||
{ name: 'Flatly', value: 'flatly' },
|
||||
{ name: 'Journal', value: 'journal' },
|
||||
{ name: 'Lumen', value: 'lumen' },
|
||||
{ name: 'Paper', value: 'paper' },
|
||||
{ name: 'Readable', value: 'readable' },
|
||||
{ name: 'Sandstone', value: 'sandstone' },
|
||||
{ name: 'Simplex', value: 'simplex' },
|
||||
{ name: 'Slate', value: 'slate' },
|
||||
{ name: 'Spacelab', value: 'spacelab' },
|
||||
{ name: 'Superhero', value: 'superhero' },
|
||||
{ name: 'United', value: 'united' },
|
||||
{ name: 'Yeti', value: 'yeti' },
|
||||
];
|
||||
|
||||
userData.bootswatchSkinOptions.forEach(function (skin) {
|
||||
skin.selected = skin.value === userData.settings.bootswatchSkin;
|
||||
});
|
||||
userData.settings = settings;
|
||||
userData.languages = languagesData;
|
||||
if (userData.isAdmin && userData.isSelf) {
|
||||
userData.acpLanguages = _.cloneDeep(languagesData);
|
||||
}
|
||||
|
||||
userData.languages.forEach(function (language) {
|
||||
language.selected = language.code === userData.settings.userLang;
|
||||
});
|
||||
addSoundSettings(userData, soundsMapping);
|
||||
|
||||
if (userData.isAdmin && userData.isSelf) {
|
||||
userData.acpLanguages.forEach(function (language) {
|
||||
language.selected = language.code === userData.settings.acpLang;
|
||||
});
|
||||
}
|
||||
|
||||
var notifFreqOptions = [
|
||||
'all',
|
||||
'first',
|
||||
'everyTen',
|
||||
'threshold',
|
||||
'logarithmic',
|
||||
'disabled',
|
||||
];
|
||||
|
||||
userData.upvoteNotifFreq = notifFreqOptions.map(function (name) {
|
||||
return {
|
||||
name: name,
|
||||
selected: name === userData.settings.upvoteNotifFreq,
|
||||
};
|
||||
});
|
||||
const data = await plugins.fireHook('filter:user.customSettings', {
|
||||
settings: settings,
|
||||
customSettings: [],
|
||||
uid: req.uid,
|
||||
});
|
||||
|
||||
userData.categoryWatchState = { [userData.settings.categoryWatchState]: true };
|
||||
const [notificationSettings, routes] = await Promise.all([
|
||||
getNotificationSettings(userData),
|
||||
getHomePageRoutes(userData),
|
||||
]);
|
||||
|
||||
userData.customSettings = data.customSettings;
|
||||
userData.homePageRoutes = routes;
|
||||
userData.notificationSettings = notificationSettings;
|
||||
userData.disableEmailSubscriptions = meta.config.disableEmailSubscriptions;
|
||||
|
||||
userData.dailyDigestFreqOptions = [
|
||||
{ value: 'off', name: '[[user:digest_off]]', selected: userData.settings.dailyDigestFreq === 'off' },
|
||||
{ value: 'day', name: '[[user:digest_daily]]', selected: userData.settings.dailyDigestFreq === 'day' },
|
||||
{ value: 'week', name: '[[user:digest_weekly]]', selected: userData.settings.dailyDigestFreq === 'week' },
|
||||
{ value: 'month', name: '[[user:digest_monthly]]', selected: userData.settings.dailyDigestFreq === 'month' },
|
||||
];
|
||||
|
||||
userData.bootswatchSkinOptions = [
|
||||
{ name: 'Default', value: '' },
|
||||
{ name: 'Cerulean', value: 'cerulean' },
|
||||
{ name: 'Cosmo', value: 'cosmo' },
|
||||
{ name: 'Cyborg', value: 'cyborg' },
|
||||
{ name: 'Darkly', value: 'darkly' },
|
||||
{ name: 'Flatly', value: 'flatly' },
|
||||
{ name: 'Journal', value: 'journal' },
|
||||
{ name: 'Lumen', value: 'lumen' },
|
||||
{ name: 'Paper', value: 'paper' },
|
||||
{ name: 'Readable', value: 'readable' },
|
||||
{ name: 'Sandstone', value: 'sandstone' },
|
||||
{ name: 'Simplex', value: 'simplex' },
|
||||
{ name: 'Slate', value: 'slate' },
|
||||
{ name: 'Spacelab', value: 'spacelab' },
|
||||
{ name: 'Superhero', value: 'superhero' },
|
||||
{ name: 'United', value: 'united' },
|
||||
{ name: 'Yeti', value: 'yeti' },
|
||||
];
|
||||
|
||||
userData.bootswatchSkinOptions.forEach(function (skin) {
|
||||
skin.selected = skin.value === userData.settings.bootswatchSkin;
|
||||
});
|
||||
|
||||
userData.disableCustomUserSkins = meta.config.disableCustomUserSkins;
|
||||
userData.languages.forEach(function (language) {
|
||||
language.selected = language.code === userData.settings.userLang;
|
||||
});
|
||||
|
||||
userData.allowUserHomePage = meta.config.allowUserHomePage;
|
||||
if (userData.isAdmin && userData.isSelf) {
|
||||
userData.acpLanguages.forEach(function (language) {
|
||||
language.selected = language.code === userData.settings.acpLang;
|
||||
});
|
||||
}
|
||||
|
||||
userData.hideFullname = meta.config.hideFullname;
|
||||
userData.hideEmail = meta.config.hideEmail;
|
||||
const notifFreqOptions = [
|
||||
'all',
|
||||
'first',
|
||||
'everyTen',
|
||||
'threshold',
|
||||
'logarithmic',
|
||||
'disabled',
|
||||
];
|
||||
|
||||
userData.inTopicSearchAvailable = plugins.hasListeners('filter:topic.search');
|
||||
userData.upvoteNotifFreq = notifFreqOptions.map(name => ({ name: name, selected: name === userData.settings.upvoteNotifFreq }));
|
||||
|
||||
userData.maxTopicsPerPage = meta.config.maxTopicsPerPage;
|
||||
userData.maxPostsPerPage = meta.config.maxPostsPerPage;
|
||||
userData.categoryWatchState = { [userData.settings.categoryWatchState]: true };
|
||||
|
||||
userData.title = '[[pages:account/settings]]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:settings]]' }]);
|
||||
userData.disableCustomUserSkins = meta.config.disableCustomUserSkins;
|
||||
|
||||
res.render('account/settings', userData);
|
||||
},
|
||||
], callback);
|
||||
userData.allowUserHomePage = meta.config.allowUserHomePage;
|
||||
|
||||
userData.hideFullname = meta.config.hideFullname;
|
||||
userData.hideEmail = meta.config.hideEmail;
|
||||
|
||||
userData.inTopicSearchAvailable = plugins.hasListeners('filter:topic.search');
|
||||
|
||||
userData.maxTopicsPerPage = meta.config.maxTopicsPerPage;
|
||||
userData.maxPostsPerPage = meta.config.maxPostsPerPage;
|
||||
|
||||
userData.title = '[[pages:account/settings]]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[user:settings]]' }]);
|
||||
|
||||
res.render('account/settings', userData);
|
||||
};
|
||||
|
||||
settingsController.unsubscribe = function (req, res) {
|
||||
function addSoundSettings(userData, soundsMapping) {
|
||||
const types = [
|
||||
'notification',
|
||||
'chat-incoming',
|
||||
'chat-outgoing',
|
||||
];
|
||||
const aliases = {
|
||||
notification: 'notificationSound',
|
||||
'chat-incoming': 'incomingChatSound',
|
||||
'chat-outgoing': 'outgoingChatSound',
|
||||
};
|
||||
|
||||
types.forEach(function (type) {
|
||||
const soundpacks = plugins.soundpacks.map(function (pack) {
|
||||
const sounds = Object.keys(pack.sounds).map(function (soundName) {
|
||||
const value = pack.name + ' | ' + soundName;
|
||||
return {
|
||||
name: soundName,
|
||||
value: value,
|
||||
selected: value === soundsMapping[type],
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
name: pack.name,
|
||||
sounds: sounds,
|
||||
};
|
||||
});
|
||||
|
||||
userData[type + '-sound'] = soundpacks;
|
||||
// fallback
|
||||
userData[aliases[type]] = soundpacks.concat.apply([], soundpacks.map(function (pack) {
|
||||
return pack.sounds.map(function (sound) {
|
||||
return {
|
||||
name: sound.value,
|
||||
selected: sound.selected,
|
||||
};
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
const jwtVerifyAsync = util.promisify(function (token, callback) {
|
||||
jwt.verify(token, nconf.get('secret'), (err, payload) => callback(err, payload));
|
||||
});
|
||||
|
||||
settingsController.unsubscribe = async function (req, res) {
|
||||
if (!req.params.token) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
|
||||
jwt.verify(req.params.token, nconf.get('secret'), function (err, payload) {
|
||||
if (err) {
|
||||
return res.sendStatus(403);
|
||||
let payload;
|
||||
try {
|
||||
payload = await jwtVerifyAsync(req.params.token);
|
||||
if (!payload || (payload.template !== 'notification' && payload.template !== 'digest')) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
|
||||
switch (payload.template) {
|
||||
case 'digest':
|
||||
async.parallel([
|
||||
async.apply(user.setSetting, payload.uid, 'dailyDigestFreq', 'off'),
|
||||
async.apply(user.updateDigestSetting, payload.uid, 'off'),
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error('[settings/unsubscribe] One-click unsubscribe failed with error: ' + err.message);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
|
||||
return res.sendStatus(200);
|
||||
});
|
||||
break;
|
||||
case 'notification':
|
||||
async.waterfall([
|
||||
async.apply(db.getObjectField, 'user:' + payload.uid + ':settings', 'notificationType_' + payload.type),
|
||||
(current, next) => {
|
||||
user.setSetting(payload.uid, 'notificationType_' + payload.type, (current === 'notificationemail' ? 'notification' : 'none'), next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error('[settings/unsubscribe] One-click unsubscribe failed with error: ' + err.message);
|
||||
return res.sendStatus(500);
|
||||
}
|
||||
|
||||
return res.sendStatus(200);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
res.sendStatus(404);
|
||||
break;
|
||||
} catch (err) {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
try {
|
||||
if (payload.template === 'digest') {
|
||||
await Promise.all([
|
||||
user.setSetting(payload.uid, 'dailyDigestFreq', 'off'),
|
||||
user.updateDigestSetting(payload.uid, 'off'),
|
||||
]);
|
||||
} else if (payload.template === 'notification') {
|
||||
const current = await db.getObjectField('user:' + payload.uid + ':settings', 'notificationType_' + payload.type);
|
||||
await user.setSetting(payload.uid, 'notificationType_' + payload.type, (current === 'notificationemail' ? 'notification' : 'none'));
|
||||
}
|
||||
});
|
||||
res.sendStatus(200);
|
||||
} catch (err) {
|
||||
winston.error('[settings/unsubscribe] One-click unsubscribe failed with error: ' + err.message);
|
||||
res.sendStatus(500);
|
||||
}
|
||||
};
|
||||
|
||||
function getNotificationSettings(userData, callback) {
|
||||
var privilegedTypes = [];
|
||||
async function getNotificationSettings(userData) {
|
||||
const privilegedTypes = [];
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.getPrivileges(userData.uid, next);
|
||||
},
|
||||
function (privileges, next) {
|
||||
if (privileges.isAdmin) {
|
||||
privilegedTypes.push('notificationType_new-register');
|
||||
}
|
||||
if (privileges.isAdmin || privileges.isGlobalMod || privileges.isModeratorOfAnyCategory) {
|
||||
privilegedTypes.push('notificationType_post-queue', 'notificationType_new-post-flag');
|
||||
}
|
||||
if (privileges.isAdmin || privileges.isGlobalMod) {
|
||||
privilegedTypes.push('notificationType_new-user-flag');
|
||||
}
|
||||
plugins.fireHook('filter:user.notificationTypes', {
|
||||
types: notifications.baseTypes.slice(),
|
||||
privilegedTypes: privilegedTypes,
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
function modifyType(type) {
|
||||
var setting = userData.settings[type];
|
||||
const privileges = await user.getPrivileges(userData.uid);
|
||||
if (privileges.isAdmin) {
|
||||
privilegedTypes.push('notificationType_new-register');
|
||||
}
|
||||
if (privileges.isAdmin || privileges.isGlobalMod || privileges.isModeratorOfAnyCategory) {
|
||||
privilegedTypes.push('notificationType_post-queue', 'notificationType_new-post-flag');
|
||||
}
|
||||
if (privileges.isAdmin || privileges.isGlobalMod) {
|
||||
privilegedTypes.push('notificationType_new-user-flag');
|
||||
}
|
||||
const results = await plugins.fireHook('filter:user.notificationTypes', {
|
||||
types: notifications.baseTypes.slice(),
|
||||
privilegedTypes: privilegedTypes,
|
||||
});
|
||||
|
||||
return {
|
||||
name: type,
|
||||
label: '[[notifications:' + type + ']]',
|
||||
none: setting === 'none',
|
||||
notification: setting === 'notification',
|
||||
email: setting === 'email',
|
||||
notificationemail: setting === 'notificationemail',
|
||||
};
|
||||
}
|
||||
function modifyType(type) {
|
||||
const setting = userData.settings[type];
|
||||
return {
|
||||
name: type,
|
||||
label: '[[notifications:' + type + ']]',
|
||||
none: setting === 'none',
|
||||
notification: setting === 'notification',
|
||||
email: setting === 'email',
|
||||
notificationemail: setting === 'notificationemail',
|
||||
};
|
||||
}
|
||||
|
||||
if (meta.config.disableChat) {
|
||||
results.types = results.types.filter(type => type !== 'notificationType_new-chat');
|
||||
}
|
||||
if (meta.config.disableChat) {
|
||||
results.types = results.types.filter(type => type !== 'notificationType_new-chat');
|
||||
}
|
||||
|
||||
var notificationSettings = results.types.map(modifyType).concat(results.privilegedTypes.map(modifyType));
|
||||
next(null, notificationSettings);
|
||||
},
|
||||
], callback);
|
||||
return results.types.map(modifyType).concat(results.privilegedTypes.map(modifyType));
|
||||
}
|
||||
|
||||
function getHomePageRoutes(userData, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRange('cid:0:children', 0, -1, next);
|
||||
async function getHomePageRoutes(userData) {
|
||||
let cids = await categories.getAllCidsFromSet('cid:0:children');
|
||||
cids = await privileges.categories.filterCids('find', cids, userData.uid);
|
||||
let categoryData = await categories.getCategoriesFields(cids, ['name', 'slug']);
|
||||
|
||||
categoryData = categoryData.map(function (category) {
|
||||
return {
|
||||
route: 'category/' + category.slug,
|
||||
name: 'Category: ' + category.name,
|
||||
};
|
||||
});
|
||||
|
||||
const data = await plugins.fireHook('filter:homepage.get', { routes: [
|
||||
{
|
||||
route: 'categories',
|
||||
name: 'Categories',
|
||||
},
|
||||
function (cids, next) {
|
||||
privileges.categories.filterCids('find', cids, 0, next);
|
||||
{
|
||||
route: 'unread',
|
||||
name: 'Unread',
|
||||
},
|
||||
function (cids, next) {
|
||||
categories.getCategoriesFields(cids, ['name', 'slug'], next);
|
||||
{
|
||||
route: 'recent',
|
||||
name: 'Recent',
|
||||
},
|
||||
function (categoryData, next) {
|
||||
categoryData = categoryData.map(function (category) {
|
||||
return {
|
||||
route: 'category/' + category.slug,
|
||||
name: 'Category: ' + category.name,
|
||||
};
|
||||
});
|
||||
|
||||
categoryData = categoryData || [];
|
||||
|
||||
plugins.fireHook('filter:homepage.get', { routes: [
|
||||
{
|
||||
route: 'categories',
|
||||
name: 'Categories',
|
||||
},
|
||||
{
|
||||
route: 'unread',
|
||||
name: 'Unread',
|
||||
},
|
||||
{
|
||||
route: 'recent',
|
||||
name: 'Recent',
|
||||
},
|
||||
{
|
||||
route: 'top',
|
||||
name: 'Top',
|
||||
},
|
||||
{
|
||||
route: 'popular',
|
||||
name: 'Popular',
|
||||
},
|
||||
].concat(categoryData, [
|
||||
{
|
||||
route: 'custom',
|
||||
name: 'Custom',
|
||||
},
|
||||
]) }, next);
|
||||
{
|
||||
route: 'top',
|
||||
name: 'Top',
|
||||
},
|
||||
function (data, next) {
|
||||
// Set selected for each route
|
||||
var customIdx;
|
||||
var hasSelected = false;
|
||||
data.routes = data.routes.map(function (route, idx) {
|
||||
if (route.route === userData.settings.homePageRoute) {
|
||||
route.selected = true;
|
||||
hasSelected = true;
|
||||
} else {
|
||||
route.selected = false;
|
||||
}
|
||||
|
||||
if (route.route === 'custom') {
|
||||
customIdx = idx;
|
||||
}
|
||||
|
||||
return route;
|
||||
});
|
||||
{
|
||||
route: 'popular',
|
||||
name: 'Popular',
|
||||
},
|
||||
].concat(categoryData, [
|
||||
{
|
||||
route: 'custom',
|
||||
name: 'Custom',
|
||||
},
|
||||
]) });
|
||||
|
||||
// Set selected for each route
|
||||
var customIdx;
|
||||
var hasSelected = false;
|
||||
data.routes = data.routes.map(function (route, idx) {
|
||||
if (route.route === userData.settings.homePageRoute) {
|
||||
route.selected = true;
|
||||
hasSelected = true;
|
||||
} else {
|
||||
route.selected = false;
|
||||
}
|
||||
|
||||
if (!hasSelected && customIdx && userData.settings.homePageRoute !== 'none') {
|
||||
data.routes[customIdx].selected = true;
|
||||
}
|
||||
if (route.route === 'custom') {
|
||||
customIdx = idx;
|
||||
}
|
||||
|
||||
next(null, data.routes);
|
||||
},
|
||||
], callback);
|
||||
return route;
|
||||
});
|
||||
|
||||
if (!hasSelected && customIdx && userData.settings.homePageRoute !== 'none') {
|
||||
data.routes[customIdx].selected = true;
|
||||
}
|
||||
|
||||
return data.routes;
|
||||
}
|
||||
|
@ -1,57 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var db = require('../../database');
|
||||
var helpers = require('../helpers');
|
||||
var meta = require('../../meta');
|
||||
var pagination = require('../../pagination');
|
||||
var accountHelpers = require('./helpers');
|
||||
|
||||
var uploadsController = module.exports;
|
||||
|
||||
uploadsController.get = function (req, res, callback) {
|
||||
var userData;
|
||||
|
||||
var page = Math.max(1, parseInt(req.query.page, 10) || 1);
|
||||
var itemsPerPage = 25;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
|
||||
},
|
||||
function (_userData, next) {
|
||||
userData = _userData;
|
||||
if (!userData) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
var start = (page - 1) * itemsPerPage;
|
||||
var stop = start + itemsPerPage - 1;
|
||||
async.parallel({
|
||||
itemCount: function (next) {
|
||||
db.sortedSetCard('uid:' + userData.uid + ':uploads', next);
|
||||
},
|
||||
uploadNames: function (next) {
|
||||
db.getSortedSetRevRange('uid:' + userData.uid + ':uploads', start, stop, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results) {
|
||||
userData.uploads = results.uploadNames.map(function (uploadName) {
|
||||
return {
|
||||
name: uploadName,
|
||||
url: nconf.get('upload_url') + uploadName,
|
||||
};
|
||||
});
|
||||
var pageCount = Math.ceil(results.itemCount / itemsPerPage);
|
||||
userData.pagination = pagination.create(page, pageCount, req.query);
|
||||
userData.privateUploads = meta.config.privateUploads === 1;
|
||||
userData.title = '[[pages:account/uploads, ' + userData.username + ']]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:uploads]]' }]);
|
||||
res.render('account/uploads', userData);
|
||||
},
|
||||
], callback);
|
||||
const nconf = require('nconf');
|
||||
|
||||
const db = require('../../database');
|
||||
const helpers = require('../helpers');
|
||||
const meta = require('../../meta');
|
||||
const pagination = require('../../pagination');
|
||||
const accountHelpers = require('./helpers');
|
||||
|
||||
const uploadsController = module.exports;
|
||||
|
||||
uploadsController.get = async function (req, res, next) {
|
||||
const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid);
|
||||
if (!userData) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const page = Math.max(1, parseInt(req.query.page, 10) || 1);
|
||||
const itemsPerPage = 25;
|
||||
const start = (page - 1) * itemsPerPage;
|
||||
const stop = start + itemsPerPage - 1;
|
||||
const [itemCount, uploadNames] = await Promise.all([
|
||||
db.sortedSetCard('uid:' + userData.uid + ':uploads'),
|
||||
db.getSortedSetRevRange('uid:' + userData.uid + ':uploads', start, stop),
|
||||
]);
|
||||
|
||||
userData.uploads = uploadNames.map(function (uploadName) {
|
||||
return {
|
||||
name: uploadName,
|
||||
url: nconf.get('upload_url') + uploadName,
|
||||
};
|
||||
});
|
||||
const pageCount = Math.ceil(itemCount / itemsPerPage);
|
||||
userData.pagination = pagination.create(page, pageCount, req.query);
|
||||
userData.privateUploads = meta.config.privateUploads === 1;
|
||||
userData.title = '[[pages:account/uploads, ' + userData.username + ']]';
|
||||
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username, url: '/user/' + userData.userslug }, { text: '[[global:uploads]]' }]);
|
||||
res.render('account/uploads', userData);
|
||||
};
|
||||
|
Loading…
Reference in New Issue