Baris Usakli 12 years ago
commit fbfdf561fc

@ -13,7 +13,7 @@
"socket.io": "~0.9.16", "socket.io": "~0.9.16",
"redis": "0.8.3", "redis": "0.8.3",
"express": "3.2.0", "express": "3.2.0",
"express-namespace": "0.1.1", "express-namespace": "~0.1.1",
"emailjs": "0.3.4", "emailjs": "0.3.4",
"cookie": "0.0.6", "cookie": "0.0.6",
"connect-redis": "1.4.5", "connect-redis": "1.4.5",

@ -158,7 +158,7 @@ var ajaxify = {};
var scripts = [], var scripts = [],
script, script,
children_nodes = body_el.childNodes, children_nodes = $(body_el).children(),
child, child,
i; i;

@ -1,4 +1,4 @@
(function(module) { (function (module) {
var config = {}, var config = {},
templates, templates,
@ -12,11 +12,11 @@
fs = require('fs'); fs = require('fs');
} catch (e) {} } catch (e) {}
templates.force_refresh = function(tpl) { templates.force_refresh = function (tpl) {
return !!config.force_refresh[tpl]; return !!config.force_refresh[tpl];
} }
templates.get_custom_map = function(tpl) { templates.get_custom_map = function (tpl) {
if (config['custom_mapping'] && tpl) { if (config['custom_mapping'] && tpl) {
for (var pattern in config['custom_mapping']) { for (var pattern in config['custom_mapping']) {
if (tpl.match(pattern)) { if (tpl.match(pattern)) {
@ -28,11 +28,11 @@
return false; return false;
} }
templates.is_available = function(tpl) { templates.is_available = function (tpl) {
return jQuery.inArray(tpl, available_templates) !== -1; return jQuery.inArray(tpl, available_templates) !== -1;
}; };
templates.ready = function(callback) { templates.ready = function (callback) {
if (callback == null) { if (callback == null) {
if (this.ready_callback) { if (this.ready_callback) {
this.ready_callback(); this.ready_callback();
@ -48,7 +48,7 @@
} }
}; };
templates.prepare = function(raw_tpl, data) { templates.prepare = function (raw_tpl, data) {
var template = {}; var template = {};
template.html = raw_tpl; template.html = raw_tpl;
template.parse = parse; template.parse = parse;
@ -62,10 +62,10 @@
var loaded = templatesToLoad.length; var loaded = templatesToLoad.length;
for (var t in templatesToLoad) { for (var t in templatesToLoad) {
(function(file) { (function (file) {
fs.readFile(__dirname + '/../templates/' + file + '.tpl', function(err, html) { fs.readFile(__dirname + '/../templates/' + file + '.tpl', function (err, html) {
var template = function() { var template = function () {
this.toString = function() { this.toString = function () {
return this.html; return this.html;
}; };
} }
@ -84,7 +84,7 @@
} }
function loadClient() { function loadClient() {
jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function(config_data, templates_data) { jQuery.when(jQuery.getJSON(RELATIVE_PATH + '/templates/config.json'), jQuery.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
config = config_data[0]; config = config_data[0];
available_templates = templates_data[0]; available_templates = templates_data[0];
templates.ready(); templates.ready();
@ -96,11 +96,11 @@
} }
templates.init = function(templates_to_load) { templates.init = function (templates_to_load) {
loadTemplates(templates_to_load || []); loadTemplates(templates_to_load || []);
} }
templates.getTemplateNameFromUrl = function(url) { templates.getTemplateNameFromUrl = function (url) {
var parts = url.split('?')[0].split('/'); var parts = url.split('?')[0].split('/');
for (var i = 0; i < parts.length; ++i) { for (var i = 0; i < parts.length; ++i) {
@ -112,7 +112,7 @@
} }
templates.load_template = function(callback, url, template) { templates.load_template = function (callback, url, template) {
var location = document.location || window.location, var location = document.location || window.location,
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''); rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : '');
@ -129,13 +129,13 @@
var template_data = null; var template_data = null;
(function() { (function () {
var timestamp = new Date().getTime(); //debug var timestamp = new Date().getTime(); //debug
if (!templates[tpl_url]) { if (!templates[tpl_url]) {
jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function(html) { jQuery.get(RELATIVE_PATH + '/templates/' + tpl_url + '.tpl?v=' + timestamp, function (html) {
var template = function() { var template = function () {
this.toString = function() { this.toString = function () {
return this.html; return this.html;
}; };
} }
@ -154,9 +154,9 @@
}()); }());
(function() { (function () {
jQuery.get(API_URL + api_url, function(data) { jQuery.get(API_URL + api_url, function (data) {
if (!data) { if (!data) {
ajaxify.go('404'); ajaxify.go('404');
@ -165,7 +165,7 @@
template_data = data; template_data = data;
parse_template(); parse_template();
}).fail(function(data) { }).fail(function (data) {
template_data = {}; template_data = {};
parse_template(); parse_template();
}); });
@ -182,20 +182,20 @@
document.getElementById('content').innerHTML = templates[tpl_url].parse(template_data); document.getElementById('content').innerHTML = templates[tpl_url].parse(template_data);
jQuery('#content [template-variable]').each(function(index, element) { jQuery('#content [template-variable]').each(function (index, element) {
var value = null; var value = null;
switch (element.getAttribute('template-type')) { switch (element.getAttribute('template-type')) {
case 'boolean': case 'boolean':
value = (element.value === 'true' || element.value === '1') ? true : false; value = (element.value === 'true' || element.value === '1') ? true : false;
break; break;
case 'int': // Intentional fall-through case 'int': // Intentional fall-through
case 'integer': case 'integer':
value = parseInt(element.value); value = parseInt(element.value);
break; break;
default: default:
value = element.value; value = element.value;
break; break;
} }
templates.set(element.getAttribute('template-variable'), value); templates.set(element.getAttribute('template-variable'), value);
@ -208,20 +208,20 @@
} }
templates.flush = function() { templates.flush = function () {
parsed_variables = {}; parsed_variables = {};
} }
templates.get = function(key) { templates.get = function (key) {
return parsed_variables[key]; return parsed_variables[key];
} }
templates.set = function(key, value) { templates.set = function (key, value) {
parsed_variables[key] = value; parsed_variables[key] = value;
} }
//modified from https://github.com/psychobunny/dcp.templates //modified from https://github.com/psychobunny/dcp.templates
var parse = function(data) { var parse = function (data) {
var self = this; var self = this;
function replace(key, value, template) { function replace(key, value, template) {
@ -230,7 +230,7 @@
} }
function makeRegex(block) { function makeRegex(block) {
return new RegExp("<!-- BEGIN " + block + " -->[^]*<!-- END " + block + " -->", 'g'); return new RegExp("<!-- BEGIN " + block + " -->[\\s\\S]*<!-- END " + block + " -->", 'g');
} }
function getBlock(regex, block, template) { function getBlock(regex, block, template) {
@ -299,7 +299,7 @@
} }
if (namespace) { if (namespace) {
var regex = new RegExp("{" + namespace + "[^]*?}", 'g'); var regex = new RegExp("{" + namespace + "[\\s\\S]*?}", 'g');
template = template.replace(regex, ''); template = template.replace(regex, '');
} }

@ -15,7 +15,7 @@
<div class="alert alert-warning"> <div class="alert alert-warning">
<p> <p>
<strong>Interesed in writing plugins for NodeBB?</strong> <strong>Interested in writing plugins for NodeBB?</strong>
</p> </p>
<p> <p>
Full documentation regarding plugin authoring can be found in the <a target="_blank" href="https://github.com/designcreateplay/NodeBB/wiki/Writing-Plugins-for-NodeBB">NodeBB Wiki</a>. Full documentation regarding plugin authoring can be found in the <a target="_blank" href="https://github.com/designcreateplay/NodeBB/wiki/Writing-Plugins-for-NodeBB">NodeBB Wiki</a>.

@ -1,40 +1,40 @@
{ {
"custom_mapping": { "custom_mapping": {
"admin/testing/categories[^]*": "admin/testing/categories", "admin/testing/categories.*": "admin/testing/categories",
"admin/topics[^]*": "admin/topics", "admin/topics.*": "admin/topics",
"admin/categories[^]*": "admin/categories", "admin/categories.*": "admin/categories",
"admin/users[^]*": "admin/users", "admin/users.*": "admin/users",
"admin/redis[^]*": "admin/redis", "admin/redis.*": "admin/redis",
"admin/index[^]*": "admin/index", "admin/index.*": "admin/index",
"admin/themes[^]*": "admin/themes", "admin/themes.*": "admin/themes",
"admin/plugins[^]*": "admin/plugins", "admin/plugins.*": "admin/plugins",
"^admin/settings[^]*": "admin/settings", "^admin/settings.*": "admin/settings",
"admin/twitter[^]*": "admin/twitter", "admin/twitter.*": "admin/twitter",
"admin/facebook[^]*": "admin/facebook", "admin/facebook.*": "admin/facebook",
"admin/gplus[^]*": "admin/gplus", "admin/gplus.*": "admin/gplus",
"admin/motd/?$": "admin/motd", "admin/motd/?$": "admin/motd",
"admin/groups/?$": "admin/groups", "admin/groups/?$": "admin/groups",
"install/?$": "install/mail", "install/?$": "install/mail",
"install/mail/?": "install/mail", "install/mail/?": "install/mail",
"install/social/?": "install/social", "install/social/?": "install/social",
"install/privileges/?": "install/privileges", "install/privileges/?": "install/privileges",
"users/sort-posts": "users", "users/sort-posts": "users",
"users/latest": "users", "users/latest": "users",
"users/sort-reputation": "users", "users/sort-reputation": "users",
"users/search": "users", "users/search": "users",
"user[^]*edit": "accountedit", "user.*edit": "accountedit",
"user[^]*following": "following", "user.*following": "following",
"user[^]*followers": "followers", "user.*followers": "followers",
"user[^]*settings": "accountsettings", "user.*settings": "accountsettings",
"user[^]*favourites": "favourites", "user.*favourites": "favourites",
"user/[^]*": "account", "user/.*": "account",
"recent": "recent", "recent": "recent",
"unread": "unread", "unread": "unread",
"popular": "category", "popular": "category",
"active": "category", "active": "category",
"search": "search", "search": "search",
"reset/[^]*": "reset_code", "reset/.*": "reset_code",
"reset": "reset" "reset": "reset"
}, },

@ -8,24 +8,24 @@ var user = require('./../user.js'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'); nconf = require('nconf');
(function(Admin) { (function (Admin) {
Admin.isAdmin = function(req, res, next) { Admin.isAdmin = function (req, res, next) {
user.isAdministrator((req.user && req.user.uid) ? req.user.uid : 0, function(isAdmin) { user.isAdministrator((req.user && req.user.uid) ? req.user.uid : 0, function (isAdmin) {
if (!isAdmin) res.redirect('/403'); if (!isAdmin) res.redirect('/403');
else next(); else next();
}); });
} }
Admin.build_header = function(res) { Admin.build_header = function (res) {
return templates['admin/header'].parse({ return templates['admin/header'].parse({
csrf: res.locals.csrf_token, csrf: res.locals.csrf_token,
relative_path: nconf.get('relative_path') relative_path: nconf.get('relative_path')
}); });
} }
Admin.create_routes = function(app) { Admin.create_routes = function (app) {
(function() { (function () {
var routes = [ var routes = [
'categories/active', 'categories/disabled', 'users', 'topics', 'settings', 'themes', 'categories/active', 'categories/disabled', 'users', 'topics', 'settings', 'themes',
'twitter', 'facebook', 'gplus', 'redis', 'motd', 'groups', 'twitter', 'facebook', 'gplus', 'redis', 'motd', 'groups',
@ -34,8 +34,8 @@ var user = require('./../user.js'),
]; ];
for (var i = 0, ii = routes.length; i < ii; i++) { for (var i = 0, ii = routes.length; i < ii; i++) {
(function(route) { (function (route) {
app.get('/admin/' + route, Admin.isAdmin, function(req, res) { app.get('/admin/' + route, Admin.isAdmin, function (req, res) {
res.send(Admin.build_header(res) + app.create_route('admin/' + route) + templates['admin/footer']); res.send(Admin.build_header(res) + app.create_route('admin/' + route) + templates['admin/footer']);
}); });
}(routes[i])); }(routes[i]));
@ -44,8 +44,8 @@ var user = require('./../user.js'),
var unit_tests = ['categories']; var unit_tests = ['categories'];
for (var i = 0, ii = unit_tests.length; i < ii; i++) { for (var i = 0, ii = unit_tests.length; i < ii; i++) {
(function(route) { (function (route) {
app.get('/admin/testing/' + route, Admin.isAdmin, function(req, res) { app.get('/admin/testing/' + route, Admin.isAdmin, function (req, res) {
res.send(Admin.build_header(res) + app.create_route('admin/testing/' + route) + templates['admin/footer']); res.send(Admin.build_header(res) + app.create_route('admin/testing/' + route) + templates['admin/footer']);
}); });
}(unit_tests[i])); }(unit_tests[i]));
@ -53,174 +53,178 @@ var user = require('./../user.js'),
}()); }());
app.get('/admin', Admin.isAdmin, function(req, res) { app.namespace('/admin', function () {
res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); app.get('/', Admin.isAdmin, function (req, res) {
}); res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']);
});
app.get('/admin/index', Admin.isAdmin, function(req, res) { app.get('/index', Admin.isAdmin, function (req, res) {
res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']); res.send(Admin.build_header(res) + app.create_route('admin/index') + templates['admin/footer']);
});
app.get('/api/admin/index', function(req, res) {
res.json({
version: pkg.version
}); });
}); });
app.get('/api/admin/users/search', function(req, res) { app.namespace('/api/admin', function () {
res.json({ app.get('/index', function (req, res) {
search_display: 'block',
loadmore_display: 'none',
users: []
});
});
app.get('/api/admin/users/latest', function(req, res) {
user.getUsers('users:joindate', 0, 49, function(err, data) {
res.json({ res.json({
search_display: 'none', version: pkg.version
loadmore_display: 'block',
users: data,
yourid: req.user.uid
}); });
}); });
});
app.get('/api/admin/users/sort-posts', function(req, res) { app.get('/users/search', function (req, res) {
user.getUsers('users:postcount', 0, 49, function(err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'block',
loadmore_display: 'block', loadmore_display: 'none',
users: data, users: []
yourid: req.user.uid
}); });
}); });
});
app.get('/api/admin/users/sort-reputation', function(req, res) { app.get('/users/latest', function (req, res) {
user.getUsers('users:reputation', 0, 49, function(err, data) { user.getUsers('users:joindate', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
users: data, users: data,
yourid: req.user.uid yourid: req.user.uid
});
}); });
}); });
});
app.get('/api/admin/users', function(req, res) { app.get('/users/sort-posts', function (req, res) {
user.getUsers('users:joindate', 0, 49, function(err, data) { user.getUsers('users:postcount', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
users: data, loadmore_display: 'block',
yourid: req.user.uid users: data,
yourid: req.user.uid
});
}); });
}); });
});
app.get('/api/admin/categories', function(req, res) { app.get('/users/sort-reputation', function (req, res) {
categories.getAllCategories(function(data) { user.getUsers('users:reputation', 0, 49, function (err, data) {
res.json(data); res.json({
}); search_display: 'none',
}); loadmore_display: 'block',
users: data,
app.get('/api/admin/categories/active', function(req, res) { yourid: req.user.uid
categories.getAllCategories(function(data) { });
data.categories = data.categories.filter(function(category) {
return (!category.disabled || category.disabled === "0");
});
res.json(data);
});
});
app.get('/api/admin/categories/disabled', function(req, res) {
categories.getAllCategories(function(data) {
data.categories = data.categories.filter(function(category) {
return category.disabled === "1";
});
res.json(data);
});
});
app.get('/api/admin/topics', function(req, res) {
topics.getAllTopics(10, null, function(topics) {
res.json({
topics: topics
}); });
}); });
});
app.get('/api/admin/redis', function(req, res) { app.get('/users', function (req, res) {
RDB.info(function(err, data) { user.getUsers('users:joindate', 0, 49, function (err, data) {
data = data.split("\r\n"); res.json({
var finalData = {}; search_display: 'none',
users: data,
yourid: req.user.uid
});
});
});
for (var i in data) { app.get('/categories', function (req, res) {
categories.getAllCategories(function (data) {
res.json(data);
});
});
if (data[i].indexOf(':') == -1 || !data[i]) app.get('/categories/active', function (req, res) {
continue; categories.getAllCategories(function (data) {
data.categories = data.categories.filter(function (category) {
return (!category.disabled || category.disabled === "0");
});
res.json(data);
});
});
try { app.get('/categories/disabled', function (req, res) {
data[i] = data[i].replace(/:/, "\":\""); categories.getAllCategories(function (data) {
var json = "{\"" + data[i] + "\"}"; data.categories = data.categories.filter(function (category) {
return category.disabled === "1";
});
res.json(data);
});
});
var jsonObject = JSON.parse(json); app.get('/topics', function (req, res) {
for (var key in jsonObject) { topics.getAllTopics(10, null, function (topics) {
finalData[key] = jsonObject[key]; res.json({
topics: topics
});
});
});
app.get('/redis', function (req, res) {
RDB.info(function (err, data) {
data = data.split("\r\n");
var finalData = {};
for (var i in data) {
if (data[i].indexOf(':') == -1 || !data[i])
continue;
try {
data[i] = data[i].replace(/:/, "\":\"");
var json = "{\"" + data[i] + "\"}";
var jsonObject = JSON.parse(json);
for (var key in jsonObject) {
finalData[key] = jsonObject[key];
}
} catch (err) {
winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err);
} }
} catch (err) {
winston.warn('can\'t parse redis status variable, ignoring', i, data[i], err);
} }
}
res.json(finalData); res.json(finalData);
});
});
app.get('/api/admin/plugins', function(req, res) {
plugins.showInstalled(function(err, plugins) {
if (err || !Array.isArray(plugins)) plugins = [];
res.json(200, {
plugins: plugins
}); });
}); });
});
app.get('/api/admin/settings', function(req, res) { app.get('/plugins', function (req, res) {
res.json(200, {}); plugins.showInstalled(function (err, plugins) {
}); if (err || !Array.isArray(plugins)) plugins = [];
app.get('/api/admin/motd', function(req, res) { res.json(200, {
res.json(200, {}); plugins: plugins
}); });
});
});
app.get('/api/admin/themes', function(req, res) { app.get('/settings', function (req, res) {
res.json(200, {}); res.json(200, {});
}); });
app.get('/api/admin/twitter', function(req, res) { app.get('/motd', function (req, res) {
res.json(200, {}); res.json(200, {});
}); });
app.get('/api/admin/facebook', function(req, res) { app.get('/themes', function (req, res) {
res.json(200, {}); res.json(200, {});
}); });
app.get('/api/admin/gplus', function(req, res) { app.get('/twitter', function (req, res) {
res.json(200, {}); res.json(200, {});
}); });
app.get('/api/admin/testing/categories', function(req, res) { app.get('/facebook', function (req, res) {
res.json(200, {}); res.json(200, {});
}); });
app.get('/api/admin/groups', function(req, res) { app.get('/gplus', function (req, res) {
Groups.list({ res.json(200, {});
expand: true });
}, function(err, groups) {
res.json(200, { app.get('/testing/categories', function (req, res) {
groups: groups res.json(200, {});
});
app.get('/groups', function (req, res) {
Groups.list({
expand: true
}, function (err, groups) {
res.json(200, {
groups: groups
});
}); });
}); });
}); });

@ -9,257 +9,259 @@ var user = require('./../user.js'),
nconf = require('nconf'); nconf = require('nconf');
(function(Api) { (function (Api) {
Api.create_routes = function(app) { Api.create_routes = function (app) {
app.get('/api/get_templates_listing', function(req, res) { app.namespace('/api', function () {
utils.walk(path.join(__dirname, '../../', 'public/templates'), function(err, data) { app.get('/get_templates_listing', function (req, res) {
res.json(data); utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) {
}); res.json(data);
});
app.get('/api/config', function(req, res, next) {
var config = require('../../public/config.json');
config['postDelay'] = meta.config['postDelay'];
config['minimumTitleLength'] = meta.config['minimumTitleLength'];
config['minimumPostLength'] = meta.config['minimumPostLength'];
config['imgurClientIDSet'] = !! meta.config['imgurClientID'];
config['minimumUsernameLength'] = meta.config['minimumUsernameLength'];
config['maximumUsernameLength'] = meta.config['maximumUsernameLength'];
config['minimumPasswordLength'] = meta.config['minimumPasswordLength'];
res.json(200, config);
});
app.get('/api/home', function(req, res) {
var uid = (req.user) ? req.user.uid : 0;
categories.getAllCategories(function(data) {
data.categories = data.categories.filter(function(category) {
return (!category.disabled || category.disabled === "0");
}); });
});
function iterator(category, callback) { app.get('/config', function (req, res, next) {
categories.getRecentReplies(category.cid, 2, function(posts) { var config = require('../../public/config.json');
category["posts"] = posts;
category["post_count"] = posts.length > 2 ? 2 : posts.length; config.postDelay = meta.config.postDelay;
callback(null); config.minimumTitleLength = meta.config.minimumTitleLength;
config.minimumPostLength = meta.config.minimumPostLength;
config.imgurClientIDSet = !! meta.config.imgurClientID;
config.minimumUsernameLength = meta.config.minimumUsernameLength;
config.maximumUsernameLength = meta.config.maximumUsernameLength;
config.minimumPasswordLength = meta.config.minimumPasswordLength;
res.json(200, config);
});
app.get('/home', function (req, res) {
var uid = (req.user) ? req.user.uid : 0;
categories.getAllCategories(function (data) {
data.categories = data.categories.filter(function (category) {
return (!category.disabled || category.disabled === "0");
}); });
function iterator(category, callback) {
categories.getRecentReplies(category.cid, 2, function (posts) {
category.posts = posts;
category.post_count = posts.length > 2 ? 2 : posts.length;
callback(null);
});
}
require('async').each(data.categories, iterator, function (err) {
data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none';
data.motd = require('marked')(meta.config.motd || "# NodeBB <span>v " + pkg.version + "</span>\nWelcome to NodeBB, the discussion platform of the future.\n\n<div class='btn-group'><a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-default btn-lg\"><i class=\"icon-comment\"></i><span class='hidden-mobile'>&nbsp;Get NodeBB</span></a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-default btn-lg\"><i class=\"icon-github-alt\"></i><span class='hidden-mobile'>&nbsp;Fork us on Github</span></a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-default btn-lg\"><i class=\"icon-twitter\"></i><span class='hidden-mobile'>&nbsp;@dcplabs</span></a></div>");
res.json(data);
});
}, uid);
});
app.get('/login', function (req, res) {
var data = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length;
if (num_strategies == 0) {
data = {
'login_window:spansize': 'col-md-12',
'alternate_logins:display': 'none'
};
} else {
data = {
'login_window:spansize': 'col-md-6',
'alternate_logins:display': 'block'
}
for (var i = 0, ii = num_strategies; i < ii; i++) {
data[login_strategies[i] + ':display'] = 'active';
}
} }
require('async').each(data.categories, iterator, function(err) { data.token = res.locals.csrf_token;
data.motd_class = (meta.config.show_motd === '1' || meta.config.show_motd === undefined) ? '' : 'none';
data.motd = require('marked')(meta.config.motd || "# NodeBB <span>v " + pkg.version + "</span>\nWelcome to NodeBB, the discussion platform of the future.\n\n<div class='btn-group'><a target=\"_blank\" href=\"http://www.nodebb.org\" class=\"btn btn-default btn-lg\"><i class=\"icon-comment\"></i><span class='hidden-mobile'>&nbsp;Get NodeBB</span></a> <a target=\"_blank\" href=\"https://github.com/designcreateplay/NodeBB\" class=\"btn btn-default btn-lg\"><i class=\"icon-github-alt\"></i><span class='hidden-mobile'>&nbsp;Fork us on Github</span></a> <a target=\"_blank\" href=\"https://twitter.com/dcplabs\" class=\"btn btn-default btn-lg\"><i class=\"icon-twitter\"></i><span class='hidden-mobile'>&nbsp;@dcplabs</span></a></div>"); res.json(data);
});
app.get('/register', function (req, res) {
var data = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length;
if (num_strategies == 0) {
data = {
'register_window:spansize': 'col-md-12',
'alternate_logins:display': 'none'
};
} else {
data = {
'register_window:spansize': 'col-md-6',
'alternate_logins:display': 'block'
}
for (var i = 0, ii = num_strategies; i < ii; i++) {
data[login_strategies[i] + ':display'] = 'active';
}
}
data.token = res.locals.csrf_token;
data.minimumUsernameLength = meta.config['minimumUsernameLength'];
data.maximumUsernameLength = meta.config['maximumUsernameLength'];
data.minimumPasswordLength = meta.config['minimumPasswordLength'];
res.json(data);
});
app.get('/topic/:id/:slug?', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
topics.getTopicWithPosts(req.params.id, uid, 0, 10, function (err, data) {
if (!err) {
if (data.deleted === '1' && data.expose_tools === 0) {
return res.json(404, {});
}
res.json(data);
} else next();
});
});
app.get('/category/:id/:slug?', function (req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
categories.getCategoryById(req.params.id, uid, function (err, data) {
if (!err)
res.json(data);
else
next();
}, req.params.id, uid);
});
app.get('/recent', function (req, res) {
var uid = (req.user) ? req.user.uid : 0;
topics.getLatestTopics(uid, 0, 19, function (data) {
res.json(data); res.json(data);
}); });
});
}, uid); app.get('/unread', function (req, res) {
}); var uid = (req.user) ? req.user.uid : 0;
topics.getUnreadTopics(uid, 0, 19, function (data) {
res.json(data);
});
});
app.get('/api/login', function(req, res) { app.get('/unread/total', function (req, res) {
var data = {}, var uid = (req.user) ? req.user.uid : 0;
login_strategies = auth.get_login_strategies(), topics.getTotalUnread(uid, function (data) {
num_strategies = login_strategies.length; res.json(data);
});
});
if (num_strategies == 0) { app.get('/confirm/:id', function (req, res) {
data = { user.email.confirm(req.params.id, function (data) {
'login_window:spansize': 'col-md-12', if (data.status === 'ok') {
'alternate_logins:display': 'none' res.json({
}; 'alert-class': 'alert-success',
} else { title: 'Email Confirmed',
data = { text: 'Thank you for vaidating your email. Your account is now fully activated.'
'login_window:spansize': 'col-md-6', });
'alternate_logins:display': 'block' } else {
} res.json({
for (var i = 0, ii = num_strategies; i < ii; i++) { 'alert-class': 'alert-error',
data[login_strategies[i] + ':display'] = 'active'; title: 'An error occurred...',
} text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'
} });
data.token = res.locals.csrf_token;
res.json(data);
});
app.get('/api/register', function(req, res) {
var data = {},
login_strategies = auth.get_login_strategies(),
num_strategies = login_strategies.length;
if (num_strategies == 0) {
data = {
'register_window:spansize': 'col-md-12',
'alternate_logins:display': 'none'
};
} else {
data = {
'register_window:spansize': 'col-md-6',
'alternate_logins:display': 'block'
}
for (var i = 0, ii = num_strategies; i < ii; i++) {
data[login_strategies[i] + ':display'] = 'active';
}
}
data.token = res.locals.csrf_token;
data.minimumUsernameLength = meta.config['minimumUsernameLength'];
data.maximumUsernameLength = meta.config['maximumUsernameLength'];
data.minimumPasswordLength = meta.config['minimumPasswordLength'];
res.json(data);
});
app.get('/api/topic/:id/:slug?', function(req, res, next) {
var uid = (req.user) ? req.user.uid : 0;
topics.getTopicWithPosts(req.params.id, uid, 0, 10, function(err, data) {
if (!err) {
if (data.deleted === '1' && data.expose_tools === 0) {
return res.json(404, {});
} }
res.json(data); });
} else next();
}); });
});
app.get('/api/category/:id/:slug?', function(req, res, next) { app.get('/outgoing', function (req, res) {
var uid = (req.user) ? req.user.uid : 0; var url = req.query.url;
categories.getCategoryById(req.params.id, uid, function(err, data) {
if (!err)
res.json(data);
else
next();
}, req.params.id, uid);
});
app.get('/api/recent', function(req, res) { if (url) {
var uid = (req.user) ? req.user.uid : 0;
topics.getLatestTopics(uid, 0, 19, function(data) {
res.json(data);
});
});
app.get('/api/unread', function(req, res) {
var uid = (req.user) ? req.user.uid : 0;
topics.getUnreadTopics(uid, 0, 19, function(data) {
res.json(data);
});
});
app.get('/api/unread/total', function(req, res) {
var uid = (req.user) ? req.user.uid : 0;
topics.getTotalUnread(uid, function(data) {
res.json(data);
});
});
app.get('/api/confirm/:id', function(req, res) {
user.email.confirm(req.params.id, function(data) {
if (data.status === 'ok') {
res.json({ res.json({
'alert-class': 'alert-success', url: url,
title: 'Email Confirmed', home: nconf.get('url')
text: 'Thank you for vaidating your email. Your account is now fully activated.'
}); });
} else { } else {
res.json({ res.status(404);
'alert-class': 'alert-error', res.redirect(nconf.get('relative_path') + '/404');
title: 'An error occurred...',
text: 'There was a problem validating your email address. Perhaps the code was invalid or has expired.'
});
} }
}); });
});
app.get('/api/outgoing', function(req, res) { app.get('/search', function (req, res) {
var url = req.query.url; return res.json({
show_no_topics: 'hide',
if (url) { show_no_posts: 'hide',
res.json({ show_results: 'hide',
url: url, search_query: '',
home: nconf.get('url') posts: [],
topics: []
}); });
} else {
res.status(404);
res.redirect(nconf.get('relative_path') + '/404');
}
});
app.get('/api/search', function(req, res) {
return res.json({
show_no_topics: 'hide',
show_no_posts: 'hide',
show_results: 'hide',
search_query: '',
posts: [],
topics: []
}); });
});
app.get('/api/search/:term', function(req, res, next) { app.get('/search/:term', function (req, res, next) {
var reds = require('reds'); var reds = require('reds');
var postSearch = reds.createSearch('nodebbpostsearch'); var postSearch = reds.createSearch('nodebbpostsearch');
var topicSearch = reds.createSearch('nodebbtopicsearch'); var topicSearch = reds.createSearch('nodebbtopicsearch');
function search(searchObj, callback) { function search(searchObj, callback) {
searchObj searchObj
.query(query = req.params.term).type('or') .query(query = req.params.term).type('or')
.end(callback); .end(callback);
} }
function searchPosts(callback) { function searchPosts(callback) {
search(postSearch, function(err, pids) { search(postSearch, function (err, pids) {
if (err)
return callback(err, null);
posts.getPostSummaryByPids(pids, function(err, posts) {
if (err) if (err)
return callback(err, null); return callback(err, null);
callback(null, posts);
posts.getPostSummaryByPids(pids, function (err, posts) {
if (err)
return callback(err, null);
callback(null, posts);
});
})
}
function searchTopics(callback) {
search(topicSearch, function (err, tids) {
if (err)
return callback(err, null);
topics.getTopicsByTids(tids, 0, function (topics) {
callback(null, topics);
}, 0);
}); });
}) }
}
function searchTopics(callback) { async.parallel([searchPosts, searchTopics], function (err, results) {
search(topicSearch, function(err, tids) {
if (err) if (err)
return callback(err, null); return next();
topics.getTopicsByTids(tids, 0, function(topics) { return res.json({
callback(null, topics); show_no_topics: results[1].length ? 'hide' : '',
}, 0); show_no_posts: results[0].length ? 'hide' : '',
}); show_results: '',
} search_query: req.params.term,
posts: results[0],
async.parallel([searchPosts, searchTopics], function(err, results) { topics: results[1]
if (err) });
return next();
return res.json({
show_no_topics: results[1].length ? 'hide' : '',
show_no_posts: results[0].length ? 'hide' : '',
show_results: '',
search_query: req.params.term,
posts: results[0],
topics: results[1]
}); });
}); });
});
app.get('/api/reset', function(req, res) { app.get('/reset', function (req, res) {
res.json({}); res.json({});
});
app.get('/api/reset/:code', function(req, res) {
res.json({
reset_code: req.params.code
}); });
});
app.get('/api/404', function(req, res) { app.get('/reset/:code', function (req, res) {
res.json({}); res.json({
}); reset_code: req.params.code
});
});
app.get('/api/403', function(req, res) { app.get('/404', function (req, res) {
res.json({}); res.json({});
});
app.get('/403', function (req, res) {
res.json({});
});
}); });
} }
}(exports)); }(exports));

@ -8,15 +8,15 @@ var user = require('./../user.js'),
nconf = require('nconf'), nconf = require('nconf'),
meta = require('./../meta'); meta = require('./../meta');
(function(User) { (function (User) {
User.create_routes = function(app) { User.create_routes = function (app) {
app.get('/uid/:uid', function(req, res) { app.get('/uid/:uid', function (req, res) {
if (!req.params.uid) if (!req.params.uid)
return res.redirect('/404'); return res.redirect('/404');
user.getUserData(req.params.uid, function(err, data) { user.getUserData(req.params.uid, function (err, data) {
if (data) { if (data) {
res.send(data); res.send(data);
} else { } else {
@ -27,156 +27,160 @@ var user = require('./../user.js'),
}); });
}); });
app.get('/users', function(req, res) { app.namespace('/users', function () {
app.build_header({ app.get('', function (req, res) {
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users", "users") + templates['footer']);
});
});
app.get('/users/latest', function(req, res) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users/latest", "users") + templates['footer']);
});
});
app.get('/users/sort-posts', function(req, res) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users/sort-posts", "users") + templates['footer']);
});
});
app.get('/users/sort-reputation', function(req, res) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users/sort-reputation", "users") + templates['footer']);
});
});
app.get('/users/online', function(req, res) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users/online", "users") + templates['footer']);
});
});
app.get('/users/search', function(req, res) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route("users/search", "users") + templates['footer']);
});
});
app.get('/user/:userslug', function(req, res, next) {
if (!req.params.userslug) {
next();
return;
}
user.get_uid_by_userslug(req.params.userslug, function(err, uid) {
if (!uid) {
return next();
}
app.build_header({ app.build_header({
req: req, req: req,
res: res res: res
}, function(err, header) { }, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug, 'account') + templates['footer']); res.send(header + app.create_route("users", "users") + templates['footer']);
}); });
});
app.get('/latest', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/latest", "users") + templates['footer']);
});
});
app.get('/sort-posts', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/sort-posts", "users") + templates['footer']);
});
});
app.get('/sort-reputation', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/sort-reputation", "users") + templates['footer']);
});
});
app.get('/online', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/online", "users") + templates['footer']);
});
});
app.get('/search', function (req, res) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route("users/search", "users") + templates['footer']);
});
}); });
}); });
app.get('/user/:userslug/edit', function(req, res) { app.namespace('/user', function () {
app.get('/:userslug', function (req, res, next) {
if (!req.user) if (!req.params.userslug) {
return res.redirect('/403'); next();
user.getUserField(req.user.uid, 'userslug', function(err, userslug) {
if (req.params.userslug && userslug === req.params.userslug) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']);
});
} else {
return res.redirect('/404');
}
});
});
app.get('/user/:userslug/settings', function(req, res) {
if (!req.user)
return res.redirect('/403');
user.getUserField(req.user.uid, 'userslug', function(err, userslug) {
if (req.params.userslug && userslug === req.params.userslug) {
app.build_header({
req: req,
res: res
}, function(err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']);
})
} else {
return res.redirect('/404');
}
});
});
app.post('/user/uploadpicture', function(req, res) {
if (!req.user)
return res.redirect('/403');
var uploadSize = meta.config.maximumProfileImageSize || 256;
if (req.files.userPhoto.size > uploadSize * 1024) {
res.send({
error: 'Images must be smaller than ' + uploadSize + ' kb!'
});
return;
}
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({
error: 'Allowed image types are png, jpg and gif!'
});
return;
}
user.getUserField(req.user.uid, 'uploadedpicture', function(err, oldpicture) {
if (!oldpicture) {
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
return; return;
} }
var absolutePath = path.join(process.cwd(), nconf.get('upload_path'), path.basename(oldpicture)); user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) {
fs.unlink(absolutePath, function(err) { return next();
if (err) {
winston.err(err);
} }
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug, 'account') + templates['footer']);
});
});
});
app.get('/:userslug/edit', function (req, res) {
if (!req.user)
return res.redirect('/403');
user.getUserField(req.user.uid, 'userslug', function (err, userslug) {
if (req.params.userslug && userslug === req.params.userslug) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/edit', 'accountedit') + templates['footer']);
});
} else {
return res.redirect('/404');
}
});
});
app.get('/:userslug/settings', function (req, res) {
if (!req.user)
return res.redirect('/403');
user.getUserField(req.user.uid, 'userslug', function (err, userslug) {
if (req.params.userslug && userslug === req.params.userslug) {
app.build_header({
req: req,
res: res
}, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/settings', 'accountsettings') + templates['footer']);
})
} else {
return res.redirect('/404');
}
});
});
app.post('/uploadpicture', function (req, res) {
if (!req.user)
return res.redirect('/403');
var uploadSize = meta.config.maximumProfileImageSize || 256;
if (req.files.userPhoto.size > uploadSize * 1024) {
res.send({
error: 'Images must be smaller than ' + uploadSize + ' kb!'
});
return;
}
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({
error: 'Allowed image types are png, jpg and gif!'
});
return;
}
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) {
if (!oldpicture) {
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
return;
}
var absolutePath = path.join(process.cwd(), nconf.get('upload_path'), path.basename(oldpicture));
fs.unlink(absolutePath, function (err) {
if (err) {
winston.err(err);
}
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
});
}); });
}); });
}); });
@ -197,7 +201,7 @@ var user = require('./../user.js'),
var is = fs.createReadStream(tempPath); var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath); var os = fs.createWriteStream(uploadPath);
is.on('end', function() { is.on('end', function () {
fs.unlinkSync(tempPath); fs.unlinkSync(tempPath);
var imageUrl = nconf.get('upload_url') + filename; var imageUrl = nconf.get('upload_url') + filename;
@ -210,7 +214,7 @@ var user = require('./../user.js'),
dstPath: uploadPath, dstPath: uploadPath,
width: 128, width: 128,
height: 128 height: 128
}, function(err, stdout, stderr) { }, function (err, stdout, stderr) {
if (err) { if (err) {
winston.err(err); winston.err(err);
} }
@ -221,7 +225,7 @@ var user = require('./../user.js'),
}); });
}); });
os.on('error', function(err) { os.on('error', function (err) {
fs.unlinkSync(tempPath); fs.unlinkSync(tempPath);
winston.err(err); winston.err(err);
}); });
@ -229,12 +233,12 @@ var user = require('./../user.js'),
is.pipe(os); is.pipe(os);
} }
app.get('/user/:userslug/following', function(req, res) { app.get('/user/:userslug/following', function (req, res) {
if (!req.user) if (!req.user)
return res.redirect('/403'); return res.redirect('/403');
user.get_uid_by_userslug(req.params.userslug, function(err, uid) { user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) { if (!uid) {
res.redirect('/404'); res.redirect('/404');
return; return;
@ -243,18 +247,18 @@ var user = require('./../user.js'),
app.build_header({ app.build_header({
req: req, req: req,
res: res res: res
}, function(err, header) { }, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/following', 'following') + templates['footer']); res.send(header + app.create_route('user/' + req.params.userslug + '/following', 'following') + templates['footer']);
}); });
}); });
}); });
app.get('/user/:userslug/followers', function(req, res) { app.get('/user/:userslug/followers', function (req, res) {
if (!req.user) if (!req.user)
return res.redirect('/403'); return res.redirect('/403');
user.get_uid_by_userslug(req.params.userslug, function(err, uid) { user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) { if (!uid) {
res.redirect('/404'); res.redirect('/404');
return; return;
@ -262,18 +266,18 @@ var user = require('./../user.js'),
app.build_header({ app.build_header({
req: req, req: req,
res: res res: res
}, function(err, header) { }, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/followers', 'followers') + templates['footer']); res.send(header + app.create_route('user/' + req.params.userslug + '/followers', 'followers') + templates['footer']);
}); });
}); });
}); });
app.get('/user/:userslug/favourites', function(req, res) { app.get('/user/:userslug/favourites', function (req, res) {
if (!req.user) if (!req.user)
return res.redirect('/403'); return res.redirect('/403');
user.get_uid_by_userslug(req.params.userslug, function(err, uid) { user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) { if (!uid) {
res.redirect('/404'); res.redirect('/404');
return; return;
@ -281,18 +285,18 @@ var user = require('./../user.js'),
app.build_header({ app.build_header({
req: req, req: req,
res: res res: res
}, function(err, header) { }, function (err, header) {
res.send(header + app.create_route('user/' + req.params.userslug + '/favourites', 'favourites') + templates['footer']); res.send(header + app.create_route('user/' + req.params.userslug + '/favourites', 'favourites') + templates['footer']);
}); });
}); });
}); });
app.get('/api/user/:userslug/following', function(req, res) { app.get('/api/user/:userslug/following', function (req, res) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) { if (userData) {
user.getFollowing(userData.uid, function(followingData) { user.getFollowing(userData.uid, function (followingData) {
userData.following = followingData; userData.following = followingData;
userData.followingCount = followingData.length; userData.followingCount = followingData.length;
res.json(userData); res.json(userData);
@ -306,12 +310,12 @@ var user = require('./../user.js'),
}); });
}); });
app.get('/api/user/:userslug/followers', function(req, res) { app.get('/api/user/:userslug/followers', function (req, res) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) { if (userData) {
user.getFollowers(userData.uid, function(followersData) { user.getFollowers(userData.uid, function (followersData) {
userData.followers = followersData; userData.followers = followersData;
userData.followersCount = followersData.length; userData.followersCount = followersData.length;
res.json(userData); res.json(userData);
@ -324,18 +328,18 @@ var user = require('./../user.js'),
}); });
}); });
app.get('/api/user/:userslug/edit', function(req, res) { app.get('/api/user/:userslug/edit', function (req, res) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
res.json(userData); res.json(userData);
}); });
}); });
app.get('/api/user/:userslug/settings', function(req, res, next) { app.get('/api/user/:userslug/settings', function (req, res, next) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
user.get_uid_by_userslug(req.params.userslug, function(err, uid) { user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) { if (!uid) {
res.json(404, { res.json(404, {
error: 'User not found!' error: 'User not found!'
@ -351,7 +355,7 @@ var user = require('./../user.js'),
} }
user.getUserFields(uid, ['username', 'userslug', 'showemail'], function(err, userData) { user.getUserFields(uid, ['username', 'userslug', 'showemail'], function (err, userData) {
if (err) if (err)
return next(err); return next(err);
@ -370,10 +374,10 @@ var user = require('./../user.js'),
}); });
}); });
app.get('/api/user/:userslug/favourites', function(req, res, next) { app.get('/api/user/:userslug/favourites', function (req, res, next) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
user.get_uid_by_userslug(req.params.userslug, function(err, uid) { user.get_uid_by_userslug(req.params.userslug, function (err, uid) {
if (!uid) { if (!uid) {
res.json(404, { res.json(404, {
error: 'User not found!' error: 'User not found!'
@ -388,12 +392,12 @@ var user = require('./../user.js'),
return; return;
} }
user.getUserFields(uid, ['username', 'userslug'], function(err, userData) { user.getUserFields(uid, ['username', 'userslug'], function (err, userData) {
if (err) if (err)
return next(err); return next(err);
if (userData) { if (userData) {
posts.getFavourites(uid, function(err, posts) { posts.getFavourites(uid, function (err, posts) {
if (err) if (err)
return next(err); return next(err);
userData.posts = posts; userData.posts = posts;
@ -409,15 +413,15 @@ var user = require('./../user.js'),
}); });
}); });
app.get('/api/user/:userslug', function(req, res) { app.get('/api/user/:userslug', function (req, res) {
var callerUID = req.user ? req.user.uid : '0'; var callerUID = req.user ? req.user.uid : '0';
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) { getUserDataByUserSlug(req.params.userslug, callerUID, function (userData) {
if (userData) { if (userData) {
user.isFollowing(callerUID, userData.theirid, function(isFollowing) { user.isFollowing(callerUID, userData.theirid, function (isFollowing) {
posts.getPostsByUid(userData.theirid, 0, 9, function(posts) { posts.getPostsByUid(userData.theirid, 0, 9, function (posts) {
userData.posts = posts.filter(function(p) { userData.posts = posts.filter(function (p) {
return p.deleted !== "1"; return p.deleted !== "1";
}); });
userData.isFollowing = isFollowing; userData.isFollowing = isFollowing;
@ -426,7 +430,7 @@ var user = require('./../user.js'),
if (callerUID !== userData.uid) if (callerUID !== userData.uid)
user.incrementUserFieldBy(userData.uid, 'profileviews', 1); user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
postTools.parse(userData.signature, function(err, signature) { postTools.parse(userData.signature, function (err, signature) {
userData.signature = signature; userData.signature = signature;
res.json(userData); res.json(userData);
}); });
@ -449,7 +453,7 @@ var user = require('./../user.js'),
function getUsersSortedByJoinDate(req, res) { function getUsersSortedByJoinDate(req, res) {
user.getUsers('users:joindate', 0, 49, function(err, data) { user.getUsers('users:joindate', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
@ -459,7 +463,7 @@ var user = require('./../user.js'),
} }
function getUsersSortedByPosts(req, res) { function getUsersSortedByPosts(req, res) {
user.getUsers('users:postcount', 0, 49, function(err, data) { user.getUsers('users:postcount', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
@ -469,7 +473,7 @@ var user = require('./../user.js'),
} }
function getUsersSortedByReputation(req, res) { function getUsersSortedByReputation(req, res) {
user.getUsers('users:reputation', 0, 49, function(err, data) { user.getUsers('users:reputation', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
@ -479,7 +483,7 @@ var user = require('./../user.js'),
} }
function getOnlineUsers(req, res) { function getOnlineUsers(req, res) {
user.getUsers('users:online', 0, 49, function(err, data) { user.getUsers('users:online', 0, 49, function (err, data) {
res.json({ res.json({
search_display: 'none', search_display: 'none',
loadmore_display: 'block', loadmore_display: 'block',
@ -497,14 +501,14 @@ var user = require('./../user.js'),
} }
function getUserDataByUserSlug(userslug, callerUID, callback) { function getUserDataByUserSlug(userslug, callerUID, callback) {
user.get_uid_by_userslug(userslug, function(err, uid) { user.get_uid_by_userslug(userslug, function (err, uid) {
if (uid === null) { if (uid === null) {
callback(null); callback(null);
return; return;
} }
user.getUserData(uid, function(err, data) { user.getUserData(uid, function (err, data) {
if (data) { if (data) {
data.joindate = new Date(parseInt(data.joindate, 10)).toISOString(); data.joindate = new Date(parseInt(data.joindate, 10)).toISOString();
@ -532,8 +536,8 @@ var user = require('./../user.js'),
data.yourid = callerUID; data.yourid = callerUID;
data.theirid = uid; data.theirid = uid;
user.getFollowingCount(uid, function(followingCount) { user.getFollowingCount(uid, function (followingCount) {
user.getFollowerCount(uid, function(followerCount) { user.getFollowerCount(uid, function (followerCount) {
data.followingCount = followingCount; data.followingCount = followingCount;
data.followerCount = followerCount; data.followerCount = followerCount;
callback(data); callback(data);

Loading…
Cancel
Save