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
parent
67ef155c60
commit
90e398e5c9
@ -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);
|
||||
};
|
||||
};
|
@ -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);
|
||||
};
|
||||
|
||||
};
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
@ -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…
Reference in New Issue