random user avatar on signup

This commit is contained in:
arnaucode
2017-07-10 19:11:58 +02:00
parent 8ff1e6151c
commit 4c1ddb6243
2 changed files with 79 additions and 23 deletions

View File

@@ -13,6 +13,8 @@ exports.getAllTravels = function(req, res) {
.sort('date') .sort('date')
.limit(pageSize) .limit(pageSize)
.skip(pageSize * Number(req.query.page)) .skip(pageSize * Number(req.query.page))
.lean()
.populate('user', 'username avatar')
.exec(function (err, travels) { .exec(function (err, travels) {
if (err) return res.send(500, err.message); if (err) return res.send(500, err.message);
res.status(200).jsonp(travels); res.status(200).jsonp(travels);

View File

@@ -17,14 +17,61 @@ var crypto = require('crypto');
var request = require('request'); var request = require('request');
function getRand(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
function getAvatar(n){
switch (n) {
case 1:
avatar = "img/avatars/racoon.png";
break;
case 2:
avatar = "img/avatars/duck.png";
break;
case 3:
avatar = "img/avatars/clown-fish.png";
break;
case 4:
avatar = "img/avatars/tiger.png";
break;
case 5:
avatar = "img/avatars/sloth.png";
break;
case 6:
avatar = "img/avatars/penguin.png";
break;
case 7:
avatar = "img/avatars/owl.png";
break;
case 8:
avatar = "img/avatars/chameleon.png";
break;
case 9:
avatar = "img/avatars/siberian-husky.png";
break;
case 10:
avatar = "img/avatars/toucan.png";
break;
default:
avatar = "img/avatars/racoon.png";
}
return avatar;
}
//POST - Insert a new User in the DB //POST - Insert a new User in the DB
exports.signup = function(req, res) { exports.signup = function(req, res) {
//get random avatar
var r = getRand(1, 10);
randAvatar = getAvatar(r);
var user = new userModel({ var user = new userModel({
username: req.body.username, username: req.body.username,
password: crypto.createHash('sha256').update(req.body.password).digest('base64'), password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
description: req.body.description, description: req.body.description,
avatar: req.body.avatar, avatar: randAvatar,
email: req.body.email, email: req.body.email,
phone: req.body.phone, phone: req.body.phone,
telegram: req.body.telegram telegram: req.body.telegram
@@ -237,10 +284,13 @@ exports.getNotifications = function(req, res) {
}); });
} else if (notifications) { } else if (notifications) {
//here, maybe in the future is better delete the viewed notifications //here, maybe in the future is better delete the viewed notifications
notificationModel.update( notificationModel.update({
{state: "pendent"}, state: "pendent"
{state: "viewed"}, }, {
{multi: true}, state: "viewed"
}, {
multi: true
},
function(err) { function(err) {
if (err) { if (err) {
console.log(err); console.log(err);
@@ -252,9 +302,11 @@ exports.getNotifications = function(req, res) {
}); });
//now, clean notifications count from user //now, clean notifications count from user
userModel.update( userModel.update({
{'token': req.headers['x-access-token']}, 'token': req.headers['x-access-token']
{notifications: []}, }, {
notifications: []
},
function(err) { function(err) {
if (err) { if (err) {
console.log(err); console.log(err);
@@ -270,6 +322,7 @@ function getRandomInt(min, max) {
max = Math.floor(max); max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
} }
function postImage(req, res, filename, fileImg) { function postImage(req, res, filename, fileImg) {
url = "http://127.0.0.1:3050/image"; url = "http://127.0.0.1:3050/image";
var importFile = function(fileImg) { var importFile = function(fileImg) {
@@ -288,6 +341,7 @@ function postImage(req, res, filename, fileImg) {
} }
importFile(fileImg); importFile(fileImg);
} }
function updateUserWithNewImages(req, res, imgUrl) { function updateUserWithNewImages(req, res, imgUrl) {
//adding random number to the url, to force ionic reload the image //adding random number to the url, to force ionic reload the image
req.body.avatar = imgUrl + "?" + getRandomInt(1, 9999); req.body.avatar = imgUrl + "?" + getRandomInt(1, 9999);