user js refactor, category fix

if a category that didn't exist was request with category/1231 or
api/category/1231 it was crashing.
v1.18.x
barisusakli 11 years ago
parent 67ef155c60
commit 90e398e5c9

@ -29,8 +29,8 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
return app.alertError(err.message); return app.alertError(err.message);
} }
if (!data || !data.success) { if (!data) {
return app.alertError('There was an error updating your profile! ' + err.message); return app.alertError('There was an error updating your profile!');
} }
app.alertSuccess('Your profile has been updated successfully!'); app.alertSuccess('Your profile has been updated successfully!');

@ -57,32 +57,38 @@ var db = require('./database'),
Categories.getCategoryById = function(cid, start, end, uid, callback) { Categories.getCategoryById = function(cid, start, end, uid, callback) {
if(parseInt(uid, 10)) { CategoryTools.exists(cid, function(err, exists) {
Categories.markAsRead(cid, uid); if(err || !exists) {
} return callback(err || new Error('category-not-found [' + cid + ']'));
async.parallel({
category: function(next) {
Categories.getCategoryData(cid, next);
},
topics: function(next) {
Categories.getCategoryTopics(cid, start, end, uid, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
} }
}, function(err, results) {
if(err) { if(parseInt(uid, 10)) {
return callback(err); Categories.markAsRead(cid, uid);
} }
var category = results.category; async.parallel({
category.topics = results.topics.topics; category: function(next) {
category.nextStart = results.topics.nextStart; Categories.getCategoryData(cid, next);
category.pageCount = results.pageCount; },
category.topic_row_size = 'col-md-9'; topics: function(next) {
Categories.getCategoryTopics(cid, start, end, uid, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
}
}, function(err, results) {
if(err) {
return callback(err);
}
var category = results.category;
category.topics = results.topics.topics;
category.nextStart = results.topics.nextStart;
category.pageCount = results.pageCount;
category.topic_row_size = 'col-md-9';
callback(null, category); callback(null, category);
});
}); });
}; };

@ -89,6 +89,10 @@ categoriesController.get = function(req, res, next) {
end = start + settings.topicsPerPage - 1; end = start + settings.topicsPerPage - 1;
categories.getCategoryById(cid, start, end, uid, function (err, categoryData) { categories.getCategoryById(cid, start, end, uid, function (err, categoryData) {
if (err) {
return next(err);
}
if (categoryData) { if (categoryData) {
if (parseInt(categoryData.disabled, 10) === 1) { if (parseInt(categoryData.disabled, 10) === 1) {
return next(new Error('Category disabled'), null); return next(new Error('Category disabled'), null);

@ -186,10 +186,8 @@ Sockets.logoutUser = function(uid) {
}; };
Sockets.emitUserCount = function() { Sockets.emitUserCount = function() {
db.getObjectField('global', 'userCount', function(err, count) { user.count(function(err, count) {
io.sockets.emit('user.count', err?{message:err.message}:null, { io.sockets.emit('user.count', err ? {message:err.message} : null, count);
count: count
});
}); });
}; };

@ -19,9 +19,14 @@ var bcrypt = require('bcryptjs'),
(function(User) { (function(User) {
User.email = require('./useremail'); User.email = require('./user/email');
User.notifications = require('./usernotifications'); User.notifications = require('./user/notifications');
User.reset = require('./userreset'); User.reset = require('./user/reset');
require('./user/follow')(User);
require('./user/profile')(User);
require('./user/admin')(User);
require('./user/settings')(User);
User.create = function(userData, callback) { User.create = function(userData, callback) {
userData = userData || {}; userData = userData || {};
@ -224,51 +229,7 @@ var bcrypt = require('bcryptjs'),
}); });
}; };
User.getSettings = function(uid, callback) {
function sendDefaultSettings() {
callback(null, {
showemail: false,
usePagination: parseInt(meta.config.usePagination, 10) === 1,
topicsPerPage: parseInt(meta.config.topicsPerPage, 10) || 20,
postsPerPage: parseInt(meta.config.postsPerPage, 10) || 10
});
}
if(!parseInt(uid, 10)) {
return sendDefaultSettings();
}
db.getObject('user:' + uid + ':settings', function(err, settings) {
if(err) {
return callback(err);
}
if(!settings) {
settings = {};
}
settings.showemail = settings.showemail ? parseInt(settings.showemail, 10) !== 0 : false;
settings.usePagination = settings.usePagination ? parseInt(settings.usePagination, 10) === 1 : parseInt(meta.config.usePagination, 10) === 1;
settings.topicsPerPage = settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20;
settings.postsPerPage = settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10;
callback(null, settings);
});
};
User.saveSettings = function(uid, data, callback) {
if(!data.topicsPerPage || !data.postsPerPage || parseInt(data.topicsPerPage, 10) <= 0 || parseInt(data.postsPerPage, 10) <= 0) {
return callback(new Error('Invalid pagination value!'));
}
db.setObject('user:' + uid + ':settings', {
showemail: data.showemail,
usePagination: data.usePagination,
topicsPerPage: data.topicsPerPage,
postsPerPage: data.postsPerPage
}, callback);
};
User.updateLastOnlineTime = function(uid, callback) { User.updateLastOnlineTime = function(uid, callback) {
User.getUserField(uid, 'status', function(err, status) { User.getUserField(uid, 'status', function(err, status) {
@ -286,157 +247,6 @@ var bcrypt = require('bcryptjs'),
}); });
}; };
User.updateProfile = function(uid, data, callback) {
var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];
var returnData = {
success: false
};
function isSignatureValid(next) {
if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) {
next(new Error('Signature can\'t be longer than ' + meta.config.maximumSignatureLength + ' characters!'), false);
} else {
next(null, true);
}
}
function isEmailAvailable(next) {
if (!data.email) {
return next(null, true);
}
User.getUserField(uid, 'email', function(err, email) {
if(email === data.email) {
return next(null, true);
}
User.email.available(data.email, function(err, available) {
if (err) {
return next(err, null);
}
if (!available) {
next(new Error('Email not available!'), false);
} else {
next(null, true);
}
});
});
}
function isUsernameAvailable(next) {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
var userslug = utils.slugify(data.username);
if(userslug === userData.userslug) {
return next(null, true);
}
if(!utils.isUserNameValid(data.username) || !userslug) {
return next(new Error('Invalid Username!'), false);
}
User.exists(userslug, function(err, exists) {
if(err) {
return next(err);
}
if(exists) {
next(new Error('Username not available!'), false);
} else {
next(null, true);
}
});
});
}
async.series([isSignatureValid, isEmailAvailable, isUsernameAvailable], function(err, results) {
if (err) {
return callback(err, returnData);
}
async.each(fields, updateField, function(err) {
if (err) {
return callback(err, returnData);
}
returnData.success = true;
callback(null, returnData);
});
});
function updateField(field, next) {
if (!(data[field] !== undefined && typeof data[field] === 'string')) {
return next();
}
data[field] = data[field].trim();
data[field] = validator.escape(data[field]);
if (field === 'email') {
User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) {
if (err) {
return next(err);
}
if(userData.email === data.email) {
return next();
}
var gravatarpicture = User.createGravatarURLFromEmail(data.email);
User.setUserField(uid, 'gravatarpicture', gravatarpicture);
db.deleteObjectField('email:uid', userData.email);
db.setObjectField('email:uid', data.email, uid);
User.setUserField(uid, 'email', data.email);
if (userData.picture !== userData.uploadedpicture) {
returnData.picture = gravatarpicture;
User.setUserField(uid, 'picture', gravatarpicture);
}
returnData.gravatarpicture = gravatarpicture;
events.logEmailChange(uid, userData.email, data.email);
next();
});
return;
} else if (field === 'username') {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
var userslug = utils.slugify(data.username);
if(data.username !== userData.username) {
User.setUserField(uid, 'username', data.username);
db.deleteObjectField('username:uid', userData.username);
db.setObjectField('username:uid', data.username, uid);
events.logUsernameChange(uid, userData.username, data.username);
}
if(userslug !== userData.userslug) {
User.setUserField(uid, 'userslug', userslug);
db.deleteObjectField('userslug:uid', userData.userslug);
db.setObjectField('userslug:uid', userslug, uid);
returnData.userslug = userslug;
}
next();
});
return;
} else if (field === 'signature') {
data[field] = S(data[field]).stripTags().s;
} else if (field === 'website') {
if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') {
data[field] = 'http://' + data[field];
}
}
User.setUserField(uid, field, data[field]);
next();
}
};
User.isReadyToPost = function(uid, callback) { User.isReadyToPost = function(uid, callback) {
User.getUserField(uid, 'lastposttime', function(err, lastposttime) { User.getUserField(uid, 'lastposttime', function(err, lastposttime) {
if(err) { if(err) {
@ -532,52 +342,43 @@ var bcrypt = require('bcryptjs'),
}; };
User.getUsers = function(set, start, stop, callback) { User.getUsers = function(set, start, stop, callback) {
function loadUserInfo(user, callback) {
db.getSortedSetRevRange(set, start, stop, function(err, uids) { if (!user) {
if (err) { return callback(null, user);
return callback(err);
} }
User.getUsersData(uids, function(err, users) { async.waterfall([
if (err) { function(next) {
return callback(err); User.isAdministrator(user.uid, next);
} },
function(isAdmin, next) {
async.map(users, function(user, next) { user.status = !user.status ? 'online' : '';
if (!user) { user.administrator = isAdmin ? '1':'0';
return next(null, user); if (set === 'users:online') {
return callback(null, user);
} }
db.sortedSetScore('users:online', user.uid, next);
if (!user.status) { },
user.status = 'online'; function(score, next) {
if (!score) {
user.status = 'offline';
} }
next(null, user);
}
], callback);
}
User.isAdministrator(user.uid, function(err, isAdmin) { async.waterfall([
if (err) { function(next) {
return next(err); db.getSortedSetRevRange(set, start, stop, next);
} },
function(uids, next) {
user.administrator = isAdmin ? '1':'0'; User.getUsersData(uids, next);
},
if(set === 'users:online') { function(users, next) {
return next(null, user); async.map(users, loadUserInfo, next);
} }
], callback);
db.sortedSetScore('users:online', user.uid, function(err, score) {
if (err) {
return next(err);
}
if(!score) {
user.status = 'offline';
}
next(null, user);
});
});
}, callback);
});
});
}; };
User.createGravatarURLFromEmail = function(email) { User.createGravatarURLFromEmail = function(email) {
@ -597,36 +398,14 @@ var bcrypt = require('bcryptjs'),
User.hashPassword = function(password, callback) { User.hashPassword = function(password, callback) {
if (!password) { if (!password) {
callback(password); return callback(password);
return;
} }
bcrypt.genSalt(nconf.get('bcrypt_rounds'), function(err, salt) { bcrypt.genSalt(nconf.get('bcrypt_rounds'), function(err, salt) {
bcrypt.hash(password, salt, callback);
});
};
User.getUsersCSV = function(callback) {
var csvContent = '';
db.getObjectValues('username:uid', function(err, uids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
bcrypt.hash(password, salt, callback);
User.getMultipleUserFields(uids, ['email', 'username'], function(err, usersData) {
if (err) {
return callback(err);
}
usersData.forEach(function(user, index) {
if (user) {
csvContent += user.email + ',' + user.username + ',' + uids[index] + '\n';
}
});
callback(null, csvContent);
});
}); });
}; };
@ -688,78 +467,8 @@ var bcrypt = require('bcryptjs'),
User.getPostIds = function(uid, start, stop, callback) { User.getPostIds = function(uid, start, stop, callback) {
db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function(err, pids) { db.getSortedSetRevRange('uid:' + uid + ':posts', start, stop, function(err, pids) {
if(err) { callback(err, Array.isArray(pids) ? pids : []);
return callback(err);
}
if (pids && pids.length) {
callback(null, pids);
} else {
callback(null, []);
}
});
};
User.follow = function(uid, followuid, callback) {
toggleFollow('follow', uid, followuid, callback);
};
User.unfollow = function(uid, unfollowuid, callback) {
toggleFollow('unfollow', uid, unfollowuid, callback);
};
function toggleFollow(type, uid, theiruid, callback) {
var command = type === 'follow' ? 'setAdd' : 'setRemove';
db[command]('following:' + uid, theiruid, function(err) {
if(err) {
return callback(err);
}
db[command]('followers:' + theiruid, uid, callback);
});
}
User.getFollowing = function(uid, callback) {
getFollow('following:' + uid, callback);
};
User.getFollowers = function(uid, callback) {
getFollow('followers:' + uid, callback);
};
function getFollow(set, callback) {
db.getSetMembers(set, function(err, uids) {
if(err) {
return callback(err);
}
User.getUsersData(uids, callback);
}); });
}
User.getFollowingCount = function(uid, callback) {
db.setCount('following:' + uid, callback);
};
User.getFollowerCount = function(uid, callback) {
db.setCount('followers:' + uid, callback);
};
User.getFollowStats = function (uid, callback) {
async.parallel({
followingCount: function(next) {
User.getFollowingCount(uid, next);
},
followerCount : function(next) {
User.getFollowerCount(uid, next);
}
}, callback);
};
User.isFollowing = function(uid, theirid, callback) {
db.isSetMember('following:' + uid, theirid, callback);
}; };
User.exists = function(userslug, callback) { User.exists = function(userslug, callback) {
@ -770,13 +479,7 @@ var bcrypt = require('bcryptjs'),
User.count = function(callback) { User.count = function(callback) {
db.getObjectField('global', 'userCount', function(err, count) { db.getObjectField('global', 'userCount', function(err, count) {
if(err) { callback(err, count ? count : 0);
return callback(err);
}
callback(null, {
count: count ? count : 0
});
}); });
}; };
@ -825,21 +528,5 @@ var bcrypt = require('bcryptjs'),
groups.isMemberByGroupName(uid, 'administrators', callback); groups.isMemberByGroupName(uid, 'administrators', callback);
}; };
User.logIP = function(uid, ip) {
db.sortedSetAdd('uid:' + uid + ':ip', +new Date(), ip || 'Unknown');
};
User.getIPs = function(uid, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':ip', 0, end, function(err, ips) {
if(err) {
return callback(err);
}
callback(null, ips.map(function(ip) {
return {ip:ip};
}));
});
};
}(exports)); }(exports));

@ -0,0 +1,46 @@
'use strict';
var async = require('async'),
db = require('./../database');
module.exports = function(User) {
User.logIP = function(uid, ip) {
db.sortedSetAdd('uid:' + uid + ':ip', Date.now(), ip || 'Unknown');
};
User.getIPs = function(uid, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':ip', 0, end, function(err, ips) {
if(err) {
return callback(err);
}
callback(null, ips.map(function(ip) {
return {ip:ip};
}));
});
};
User.getUsersCSV = function(callback) {
var csvContent = '';
async.waterfall([
function(next) {
db.getObjectValues('username:uid', next);
},
function(uids, next) {
User.getMultipleUserFields(uids, ['uid', 'email', 'username'], next);
},
function(usersData, next) {
usersData.forEach(function(user, index) {
if (user) {
csvContent += user.email + ',' + user.username + ',' + user.uid + '\n';
}
});
next(null, csvContent);
}
], callback);
};
};

@ -5,12 +5,12 @@ var async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
winston = require('winston'), winston = require('winston'),
user = require('./user'), user = require('./../user'),
utils = require('./../public/src/utils'), utils = require('./../../public/src/utils'),
plugins = require('./plugins'), plugins = require('./../plugins'),
db = require('./database'), db = require('./../database'),
meta = require('./meta'), meta = require('./../meta'),
emailer = require('./emailer'); emailer = require('./../emailer');
(function(UserEmail) { (function(UserEmail) {

@ -0,0 +1,68 @@
'use strict';
var async = require('async'),
db = require('./../database');
module.exports = function(User) {
User.follow = function(uid, followuid, callback) {
toggleFollow('follow', uid, followuid, callback);
};
User.unfollow = function(uid, unfollowuid, callback) {
toggleFollow('unfollow', uid, unfollowuid, callback);
};
function toggleFollow(type, uid, theiruid, callback) {
var command = type === 'follow' ? 'setAdd' : 'setRemove';
db[command]('following:' + uid, theiruid, function(err) {
if(err) {
return callback(err);
}
db[command]('followers:' + theiruid, uid, callback);
});
}
User.getFollowing = function(uid, callback) {
getFollow('following:' + uid, callback);
};
User.getFollowers = function(uid, callback) {
getFollow('followers:' + uid, callback);
};
function getFollow(set, callback) {
db.getSetMembers(set, function(err, uids) {
if(err) {
return callback(err);
}
User.getUsersData(uids, callback);
});
}
User.getFollowingCount = function(uid, callback) {
db.setCount('following:' + uid, callback);
};
User.getFollowerCount = function(uid, callback) {
db.setCount('followers:' + uid, callback);
};
User.getFollowStats = function (uid, callback) {
async.parallel({
followingCount: function(next) {
User.getFollowingCount(uid, next);
},
followerCount : function(next) {
User.getFollowerCount(uid, next);
}
}, callback);
};
User.isFollowing = function(uid, theirid, callback) {
db.isSetMember('following:' + uid, theirid, callback);
};
};

@ -5,12 +5,11 @@ var async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
winston = require('winston'), winston = require('winston'),
user = require('./user'), user = require('./../user'),
utils = require('./../public/src/utils'), utils = require('./../../public/src/utils'),
db = require('./database'), db = require('./../database'),
notifications = require('./notifications'), notifications = require('./../notifications'),
topics = require('./topics'), topics = require('./../topics');
websockets = require('./socket.io');
(function(UserNotifications) { (function(UserNotifications) {
@ -161,7 +160,7 @@ var async = require('async'),
}; };
UserNotifications.pushCount = function(uid) { UserNotifications.pushCount = function(uid) {
var websockets = require('./../socket.io');
UserNotifications.getUnreadCount(uid, function(err, count) { UserNotifications.getUnreadCount(uid, function(err, count) {
if (err) { if (err) {
return winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)'); return winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)');

@ -0,0 +1,196 @@
'use strict';
var async = require('async'),
validator = require('validator'),
S = require('string'),
utils = require('./../../public/src/utils'),
meta = require('./../meta'),
events = require('./../events'),
db = require('./../database');
module.exports = function(User) {
User.updateProfile = function(uid, data, callback) {
var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];
function isSignatureValid(next) {
if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) {
next(new Error('Signature can\'t be longer than ' + meta.config.maximumSignatureLength + ' characters!'));
} else {
next();
}
}
function isEmailAvailable(next) {
if (!data.email) {
return next();
}
User.getUserField(uid, 'email', function(err, email) {
if(email === data.email) {
return next();
}
User.email.available(data.email, function(err, available) {
if (err) {
return next(err);
}
next(!available ? new Error('Email not available!') : null);
});
});
}
function isUsernameAvailable(next) {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
var userslug = utils.slugify(data.username);
if(userslug === userData.userslug) {
return next();
}
if(!utils.isUserNameValid(data.username) || !userslug) {
return next(new Error('Invalid Username!'));
}
User.exists(userslug, function(err, exists) {
if(err) {
return next(err);
}
next(exists ? new Error('Username not available!') : null);
});
});
}
async.series([isSignatureValid, isEmailAvailable, isUsernameAvailable], function(err, results) {
if (err) {
return callback(err);
}
async.each(fields, updateField, function(err) {
if (err) {
return callback(err);
}
User.getUserFields(uid, ['userslug', 'picture', 'gravatarpicture'], callback);
});
});
function updateField(field, next) {
if (!(data[field] !== undefined && typeof data[field] === 'string')) {
return next();
}
data[field] = data[field].trim();
data[field] = validator.escape(data[field]);
if (field === 'email') {
return updateEmail(uid, data.email, next);
} else if (field === 'username') {
return updateUsername(uid, data.username, next);
} else if (field === 'signature') {
data[field] = S(data[field]).stripTags().s;
} else if (field === 'website') {
if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') {
data[field] = 'http://' + data[field];
}
}
User.setUserField(uid, field, data[field]);
next();
}
};
function updateEmail(uid, newEmail, callback) {
User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) {
if (err) {
return callback(err);
}
if(userData.email === newEmail) {
return callback();
}
db.deleteObjectField('email:uid', userData.email, function(err) {
if (err) {
return callback(err);
}
events.logEmailChange(uid, userData.email, newEmail);
var gravatarpicture = User.createGravatarURLFromEmail(newEmail);
async.parallel([
function(next) {
User.setUserField(uid, 'gravatarpicture', gravatarpicture, next);
},
function(next) {
db.setObjectField('email:uid', newEmail, uid, next);
},
function(next) {
User.setUserField(uid, 'email', newEmail, next);
},
function(next) {
if (userData.picture !== userData.uploadedpicture) {
User.setUserField(uid, 'picture', gravatarpicture, next);
}
},
], callback);
});
});
}
function updateUsername(uid, newUsername, callback) {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
function update(field, object, value, callback) {
async.parallel([
function(next) {
User.setUserField(uid, field, value, next);
},
function(next) {
db.setObjectField(object, value, uid, next);
}
], callback);
}
if (err) {
return callback(err);
}
var userslug = utils.slugify(newUsername);
async.parallel([
function(next) {
if (newUsername === userData.username) {
return next();
}
db.deleteObjectField('username:uid', userData.username, function(err) {
if (err) {
return next(err);
}
events.logUsernameChange(uid, userData.username, newUsername);
update('username', 'username:uid', newUsername, next);
});
},
function(next) {
if (userslug === userData.userslug) {
return next();
}
db.deleteObjectField('userslug:uid', userData.userslug, function(err) {
if (err) {
return next(err);
}
update('userslug', 'userslug:uid', userslug, next);
});
}
], callback);
});
}
};

@ -4,13 +4,13 @@
var async = require('async'), var async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
user = require('./user'), user = require('./../user'),
utils = require('./../public/src/utils'), utils = require('./../../public/src/utils'),
db = require('./database'), db = require('./../database'),
meta = require('./meta'), meta = require('./../meta'),
events = require('./events'), events = require('./../events'),
emailer = require('./emailer'); emailer = require('./../emailer');
(function(UserReset) { (function(UserReset) {

@ -0,0 +1,54 @@
'use strict';
var meta = require('./../meta'),
db = require('./../database');
module.exports = function(User) {
User.getSettings = function(uid, callback) {
function sendDefaultSettings() {
callback(null, {
showemail: false,
usePagination: parseInt(meta.config.usePagination, 10) === 1,
topicsPerPage: parseInt(meta.config.topicsPerPage, 10) || 20,
postsPerPage: parseInt(meta.config.postsPerPage, 10) || 10
});
}
if(!parseInt(uid, 10)) {
return sendDefaultSettings();
}
db.getObject('user:' + uid + ':settings', function(err, settings) {
if(err) {
return callback(err);
}
if(!settings) {
settings = {};
}
settings.showemail = settings.showemail ? parseInt(settings.showemail, 10) !== 0 : false;
settings.usePagination = settings.usePagination ? parseInt(settings.usePagination, 10) === 1 : parseInt(meta.config.usePagination, 10) === 1;
settings.topicsPerPage = settings.topicsPerPage ? parseInt(settings.topicsPerPage, 10) : parseInt(meta.config.topicsPerPage, 10) || 20;
settings.postsPerPage = settings.postsPerPage ? parseInt(settings.postsPerPage, 10) : parseInt(meta.config.postsPerPage, 10) || 10;
callback(null, settings);
});
};
User.saveSettings = function(uid, data, callback) {
if(!data.topicsPerPage || !data.postsPerPage || parseInt(data.topicsPerPage, 10) <= 0 || parseInt(data.postsPerPage, 10) <= 0) {
return callback(new Error('Invalid pagination value!'));
}
db.setObject('user:' + uid + ':settings', {
showemail: data.showemail,
usePagination: data.usePagination,
topicsPerPage: data.topicsPerPage,
postsPerPage: data.postsPerPage
}, callback);
};
};
Loading…
Cancel
Save