mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 00:10:25 +01:00
moved user routes into routers/user.js
This commit is contained in:
2
app.js
2
app.js
@@ -35,4 +35,6 @@ global.configuration = {};
|
|||||||
|
|
||||||
setup_categories();
|
setup_categories();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}(global.configuration));
|
}(global.configuration));
|
||||||
@@ -45,7 +45,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
|
|
||||||
<form id="uploadForm" action="/pictureupload" method="post" enctype="multipart/form-data">
|
<form id="uploadForm" action="/users/uploadpicture" method="post" enctype="multipart/form-data">
|
||||||
<input id="userPhotoInput" type="file" name="userPhoto" >
|
<input id="userPhotoInput" type="file" name="userPhoto" >
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
@@ -212,7 +212,7 @@ $(document).ready(function() {
|
|||||||
type: type
|
type: type
|
||||||
};
|
};
|
||||||
|
|
||||||
$.post('/changeuserpicture',
|
$.post('/users/changepicture',
|
||||||
userData,
|
userData,
|
||||||
function(data) {
|
function(data) {
|
||||||
socket.emit('api:updateHeader', { fields: ['username', 'picture'] });
|
socket.emit('api:updateHeader', { fields: ['username', 'picture'] });
|
||||||
@@ -233,7 +233,7 @@ $(document).ready(function() {
|
|||||||
location:$('#inputLocation').val()
|
location:$('#inputLocation').val()
|
||||||
};
|
};
|
||||||
|
|
||||||
$.post('/edituser',
|
$.post('/users/doedit',
|
||||||
userData,
|
userData,
|
||||||
function(data) {
|
function(data) {
|
||||||
|
|
||||||
@@ -242,6 +242,7 @@ $(document).ready(function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function updateImages() {
|
function updateImages() {
|
||||||
|
var currentPicture = $('#user-current-picture').attr('src');
|
||||||
|
|
||||||
if(gravatarPicture) {
|
if(gravatarPicture) {
|
||||||
$('#user-gravatar-picture').attr('src', gravatarPicture);
|
$('#user-gravatar-picture').attr('src', gravatarPicture);
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
|
||||||
|
|
||||||
var user = require('./../user.js');
|
var user = require('./../user.js'),
|
||||||
|
fs = require('fs'),
|
||||||
|
utils = require('./../utils.js'),
|
||||||
|
config = require('../../config.js');
|
||||||
|
|
||||||
|
|
||||||
(function(User) {
|
(function(User) {
|
||||||
@@ -55,40 +58,186 @@ var user = require('./../user.js');
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
app.get('/users/:username/edit', function(req, res){
|
||||||
|
|
||||||
|
if(!req.user)
|
||||||
|
return res.redirect('/403');
|
||||||
|
|
||||||
|
user.getUserField(req.user.uid, 'username', function(username) {
|
||||||
|
|
||||||
|
if(req.params.username && username === req.params.username)
|
||||||
|
res.send(templates['header'] + app.create_route('users/'+req.params.username+'/edit','accountedit') + templates['footer']);
|
||||||
|
else
|
||||||
|
return res.redirect('/403');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/users/doedit', function(req, res){
|
||||||
|
|
||||||
|
if(!req.user)
|
||||||
|
return res.redirect('/403');
|
||||||
|
|
||||||
|
if(req.user.uid != req.body.uid)
|
||||||
|
return res.redirect('/');
|
||||||
|
|
||||||
|
user.updateProfile(req.user.uid, req.body);
|
||||||
|
|
||||||
|
res.redirect('/');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/users/uploadpicture', function(req, res) {
|
||||||
|
|
||||||
|
if(!req.user)
|
||||||
|
return res.redirect('/403');
|
||||||
|
|
||||||
|
if(req.files.userPhoto.size > 131072) {
|
||||||
|
res.send({
|
||||||
|
error: 'Images must be smaller than 128kb!'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) {
|
||||||
|
|
||||||
|
if(!oldpicture) {
|
||||||
|
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var index = oldpicture.lastIndexOf('/');
|
||||||
|
var filename = oldpicture.substr(index + 1);
|
||||||
|
|
||||||
|
var absolutePath = global.configuration['ROOT_DIRECTORY'] + config.upload_path + filename;
|
||||||
|
|
||||||
|
fs.unlink(absolutePath, function(err) {
|
||||||
|
if(err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
function uploadUserPicture(uid, filename, tempPath, res) {
|
||||||
|
|
||||||
|
if(!filename){
|
||||||
|
res.send({
|
||||||
|
error: 'Error uploading file! Error : Invalid file name!'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
filename = uid + '-' + filename;
|
||||||
|
var uploadPath = config.upload_path + filename;
|
||||||
|
|
||||||
|
console.log('trying to upload to : '+ global.configuration['ROOT_DIRECTORY'] + uploadPath);
|
||||||
|
|
||||||
|
fs.rename(
|
||||||
|
tempPath,
|
||||||
|
global.configuration['ROOT_DIRECTORY'] + uploadPath,
|
||||||
|
function(error) {
|
||||||
|
if(error) {
|
||||||
|
console.log(error);
|
||||||
|
res.send({
|
||||||
|
error: 'Error uploading file!'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var imageUrl = config.upload_url + filename;
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
path: imageUrl
|
||||||
|
});
|
||||||
|
|
||||||
|
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
||||||
|
user.setUserField(uid, 'picture', imageUrl);
|
||||||
|
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
app.post('/users/changepicture', function(req, res){
|
||||||
|
if(!req.user)
|
||||||
|
return res.redirect('/403');
|
||||||
|
|
||||||
|
if(req.user.uid != req.body.uid)
|
||||||
|
return res.redirect('/');
|
||||||
|
|
||||||
|
var type = req.body.type;
|
||||||
|
if(type == 'gravatar') {
|
||||||
|
user.getUserField(req.user.uid, 'gravatarpicture', function(gravatar){
|
||||||
|
user.setUserField(req.user.uid, 'picture', gravatar);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if(type == 'uploaded') {
|
||||||
|
user.getUserField(req.user.uid, 'uploadedpicture', function(uploadedpicture){
|
||||||
|
user.setUserField(req.user.uid, 'picture', uploadedpicture);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
res.send({});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
function api_method(req, res) {
|
function api_method(req, res) {
|
||||||
switch(req.params.method) {
|
|
||||||
case 'users' :
|
var callerUID = req.user?req.user.uid : 0;
|
||||||
if (req.params.tab == 'search') {
|
|
||||||
res.send(JSON.stringify({search_display: 'block', users: []}))
|
if (!req.params.section && !req.params.username) {
|
||||||
} else {
|
|
||||||
user.getUserList(function(data){
|
user.getUserList(function(data){
|
||||||
res.send(JSON.stringify({search_display: 'none', users:data}));
|
|
||||||
|
res.send(JSON.stringify({users:data}));
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
else if (String(req.params.section).toLowerCase() === 'edit') {
|
||||||
break;
|
getUserDataByUserName(req.params.username, callerUID, function(userData) {
|
||||||
case 'categories':
|
res.send(JSON.stringify(userData));
|
||||||
if (req.params.tab == 'disabled') {
|
});
|
||||||
res.send(JSON.stringify({categories: []}));
|
|
||||||
} else {
|
} else {
|
||||||
categories.get(function(data) {
|
getUserDataByUserName(req.params.username, callerUID, function(userData) {
|
||||||
res.send(JSON.stringify(data));
|
res.send(JSON.stringify(userData));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case 'topics' :
|
|
||||||
topics.get(function(data) {
|
|
||||||
res.send(JSON.stringify(data));
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
res.send('{}');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
app.get('/api/admin/:method/:tab?*', api_method);
|
}
|
||||||
app.get('/api/admin/:method*', api_method);*/
|
|
||||||
|
app.get('/api/users/:username?/:section?', api_method);
|
||||||
|
|
||||||
|
function getUserDataByUserName(username, callerUID, callback) {
|
||||||
|
|
||||||
|
user.get_uid_by_username(username, function(uid) {
|
||||||
|
|
||||||
|
user.getUserData(uid, function(data) {
|
||||||
|
if(data) {
|
||||||
|
data.joindate = utils.relativeTime(data.joindate);
|
||||||
|
|
||||||
|
if(!data.birthday)
|
||||||
|
data.age = '';
|
||||||
|
else
|
||||||
|
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
|
||||||
|
|
||||||
|
data.uid = uid;
|
||||||
|
data.yourid = callerUID;
|
||||||
|
data.theirid = uid;
|
||||||
|
|
||||||
|
callback(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
callback({});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
190
src/webserver.js
190
src/webserver.js
@@ -101,6 +101,7 @@ var express = require('express'),
|
|||||||
|
|
||||||
// These functions are called via ajax once the initial page is loaded to populate templates with data
|
// These functions are called via ajax once the initial page is loaded to populate templates with data
|
||||||
function api_method(req, res) {
|
function api_method(req, res) {
|
||||||
|
|
||||||
switch(req.params.method) {
|
switch(req.params.method) {
|
||||||
case 'home' :
|
case 'home' :
|
||||||
categories.get(function(data) {
|
categories.get(function(data) {
|
||||||
@@ -176,23 +177,6 @@ var express = require('express'),
|
|||||||
res.send(JSON.stringify(data));
|
res.send(JSON.stringify(data));
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case 'users' :
|
|
||||||
if (!req.params.section && !req.params.id) {
|
|
||||||
get_users_fn(req, res, function(userData) {
|
|
||||||
res.send(JSON.stringify(userData));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (String(req.params.section).toLowerCase() === 'edit') {
|
|
||||||
get_account_fn(req, res, function(userData) {
|
|
||||||
res.send(JSON.stringify(userData));
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
get_account_fn(req, res, function(userData) {
|
|
||||||
res.send(JSON.stringify(userData));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'confirm':
|
case 'confirm':
|
||||||
user.email.confirm(req.params.id, function(data) {
|
user.email.confirm(req.params.id, function(data) {
|
||||||
if (data.status === 'ok') {
|
if (data.status === 'ok') {
|
||||||
@@ -222,183 +206,11 @@ var express = require('express'),
|
|||||||
app.get('/api/:method/:id/:section?', api_method);
|
app.get('/api/:method/:id/:section?', api_method);
|
||||||
app.get('/api/:method/:id*', api_method);
|
app.get('/api/:method/:id*', api_method);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO move user related logic into another file vvvvvvvvvvvvvvvvvvvv
|
|
||||||
|
|
||||||
app.post('/pictureupload', function(req, res) {
|
|
||||||
|
|
||||||
if(!req.user)
|
|
||||||
return res.redirect('/403');
|
|
||||||
|
|
||||||
if(req.files.userPhoto.size > 131072) {
|
|
||||||
res.send({
|
|
||||||
error: 'Images must be smaller than 128kb!'
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) {
|
|
||||||
|
|
||||||
if(!oldpicture) {
|
|
||||||
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var index = oldpicture.lastIndexOf('/');
|
|
||||||
var filename = oldpicture.substr(index+1);
|
|
||||||
|
|
||||||
var absolutePath = global.configuration['ROOT_DIRECTORY'] + config.upload_path + filename;
|
|
||||||
|
|
||||||
fs.unlink(absolutePath, function(err) {
|
|
||||||
if(err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res);
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
function uploadUserPicture(uid, filename, tempPath, res) {
|
|
||||||
|
|
||||||
if(!filename){
|
|
||||||
res.send({
|
|
||||||
error: 'Error uploading file! Error : Invalid file name!'
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
filename = uid + '-' + filename;
|
|
||||||
var uploadPath = config.upload_path + filename;
|
|
||||||
|
|
||||||
console.log('trying to upload to : '+ global.configuration['ROOT_DIRECTORY'] + uploadPath);
|
|
||||||
|
|
||||||
fs.rename(
|
|
||||||
tempPath,
|
|
||||||
global.configuration['ROOT_DIRECTORY'] + uploadPath,
|
|
||||||
function(error) {
|
|
||||||
if(error) {
|
|
||||||
console.log(error);
|
|
||||||
res.send({
|
|
||||||
error: 'Error uploading file!'
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var imageUrl = config.upload_url + filename;
|
|
||||||
|
|
||||||
res.send({
|
|
||||||
path: imageUrl
|
|
||||||
});
|
|
||||||
|
|
||||||
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
|
||||||
user.setUserField(uid, 'picture', imageUrl);
|
|
||||||
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
app.post('/changeuserpicture', function(req, res){
|
|
||||||
if(!req.user)
|
|
||||||
return res.redirect('/403');
|
|
||||||
|
|
||||||
if(req.user.uid != req.body.uid)
|
|
||||||
return res.redirect('/');
|
|
||||||
|
|
||||||
var type = req.body.type;
|
|
||||||
if(type == 'gravatar') {
|
|
||||||
user.getUserField(req.user.uid, 'gravatarpicture', function(gravatar){
|
|
||||||
user.setUserField(req.user.uid, 'picture', gravatar);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if(type == 'uploaded') {
|
|
||||||
user.getUserField(req.user.uid, 'uploadedpicture', function(uploadedpicture){
|
|
||||||
user.setUserField(req.user.uid, 'picture', uploadedpicture);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
res.send({});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
app.post('/edituser', function(req, res){
|
|
||||||
|
|
||||||
if(!req.user)
|
|
||||||
return res.redirect('/403');
|
|
||||||
|
|
||||||
if(req.user.uid != req.body.uid)
|
|
||||||
return res.redirect('/');
|
|
||||||
|
|
||||||
user.updateProfile(req.user.uid, req.body);
|
|
||||||
|
|
||||||
res.redirect('/');
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
//to baris, move this into account.js or sth later - just moved this out here for you to utilize client side tpl parsing
|
|
||||||
//I didn't want to change too much so you should probably sort out the params etc
|
|
||||||
function get_account_fn(req, res, callback) {
|
|
||||||
|
|
||||||
var username = req.params.id;
|
|
||||||
console.log("derp");
|
|
||||||
user.get_uid_by_username(username, function(uid) {
|
|
||||||
|
|
||||||
user.getUserData(uid, function(data) {
|
|
||||||
if(data)
|
|
||||||
{
|
|
||||||
data.joindate = utils.relativeTime(data.joindate);
|
|
||||||
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
|
|
||||||
console.log(data.age);
|
|
||||||
if(data.age === null)
|
|
||||||
data.age = 0;
|
|
||||||
data.uid = uid;
|
|
||||||
|
|
||||||
data.yourid = (req.user)?req.user.uid : 0;
|
|
||||||
data.theirid = uid;
|
|
||||||
|
|
||||||
callback(data);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
callback({user:{}});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_users_fn(req, res, callback) {
|
|
||||||
user.getUserList(function(data){
|
|
||||||
callback({users:data});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
app.get('/users/:uid/edit', function(req, res){
|
|
||||||
|
|
||||||
if(!req.user)
|
|
||||||
return res.redirect('/403');
|
|
||||||
|
|
||||||
user.getUserField(req.user.uid, 'username', function(username) {
|
|
||||||
|
|
||||||
if(req.params.uid && username === req.params.uid)
|
|
||||||
res.send(templates['header'] + app.create_route('users/'+req.params.uid+'/edit','accountedit') + templates['footer']);
|
|
||||||
else
|
|
||||||
return res.redirect('/403');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
app.get('/test', function(req, res) {
|
app.get('/test', function(req, res) {
|
||||||
posts.getRawContent(11, function(post) {
|
posts.getRawContent(11, function(post) {
|
||||||
res.send(JSON.stringify(post));
|
res.send(JSON.stringify(post));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO move user related logic into another file ^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
}(WebServer));
|
}(WebServer));
|
||||||
|
|
||||||
server.listen(config.port);
|
server.listen(config.port);
|
||||||
|
|||||||
Reference in New Issue
Block a user