barisusakli 10 years ago
parent c450992882
commit b2fc4d5dc4

@ -3,6 +3,7 @@
var categoriesController = {}, var categoriesController = {},
async = require('async'), async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
validator = require('validator'),
privileges = require('../privileges'), privileges = require('../privileges'),
user = require('../user'), user = require('../user'),
categories = require('../categories'), categories = require('../categories'),
@ -22,7 +23,7 @@ categoriesController.recent = function(req, res, next) {
} }
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
data['rssFeedUrl'] = nconf.get('relative_path') + '/recent.rss'; data.rssFeedUrl = nconf.get('relative_path') + '/recent.rss';
data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]); data.breadcrumbs = helpers.buildBreadcrumbs([{text: '[[recent:title]]'}]);
res.render('recent', data); res.render('recent', data);
}); });
@ -92,6 +93,72 @@ categoriesController.unreadTotal = function(req, res, next) {
}); });
}; };
categoriesController.list = function(req, res, next) {
async.parallel({
header: function (next) {
res.locals.metaTags = [{
name: "title",
content: validator.escape(meta.config.title || 'NodeBB')
}, {
name: "description",
content: validator.escape(meta.config.description || '')
}, {
property: 'og:title',
content: 'Index | ' + validator.escape(meta.config.title || 'NodeBB')
}, {
property: 'og:type',
content: 'website'
}];
if(meta.config['brand:logo']) {
res.locals.metaTags.push({
property: 'og:image',
content: meta.config['brand:logo']
});
}
next(null);
},
categories: function (next) {
var uid = req.user ? req.user.uid : 0;
categories.getCategoriesByPrivilege(uid, 'find', function (err, categoryData) {
if (err) {
return next(err);
}
var childCategories = [];
for(var i=categoryData.length - 1; i>=0; --i) {
if (Array.isArray(categoryData[i].children) && categoryData[i].children.length) {
childCategories.push.apply(childCategories, categoryData[i].children);
}
if (categoryData[i].parent && categoryData[i].parent.cid) {
categoryData.splice(i, 1);
}
}
async.parallel([
function(next) {
categories.getRecentTopicReplies(categoryData, uid, next);
},
function(next) {
categories.getRecentTopicReplies(childCategories, uid, next);
}
], function(err) {
next(err, categoryData);
});
});
}
}, function (err, data) {
if (err) {
return next(err);
}
// TODO: template should be called categories.tpl
res.render('home', data);
});
};
categoriesController.get = function(req, res, next) { categoriesController.get = function(req, res, next) {
var cid = req.params.category_id, var cid = req.params.category_id,
page = req.query.page || 1, page = req.query.page || 1,
@ -259,7 +326,7 @@ categoriesController.get = function(req, res, next) {
data.currentPage = page; data.currentPage = page;
data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
data['rssFeedUrl'] = nconf.get('relative_path') + '/category/' + data.cid + '.rss'; data.rssFeedUrl = nconf.get('relative_path') + '/category/' + data.cid + '.rss';
data.pagination = pagination.create(data.currentPage, data.pageCount); data.pagination = pagination.create(data.currentPage, data.pageCount);
data.pagination.rel.forEach(function(rel) { data.pagination.rel.forEach(function(rel) {

@ -1,21 +1,10 @@
"use strict"; "use strict";
var topicsController = require('./topics'), var async = require('async'),
categoriesController = require('./categories'),
tagsController = require('./tags'),
searchController = require('./search'),
usersController = require('./users'),
groupsController = require('./groups'),
accountsController = require('./accounts'),
staticController = require('./static'),
apiController = require('./api'),
adminController = require('./admin'),
helpers = require('./helpers'),
async = require('async'),
nconf = require('nconf'), nconf = require('nconf'),
validator = require('validator'), validator = require('validator'),
winston = require('winston'), winston = require('winston'),
auth = require('../routes/authentication'), auth = require('../routes/authentication'),
meta = require('../meta'), meta = require('../meta'),
user = require('../user'), user = require('../user'),
@ -23,85 +12,32 @@ var topicsController = require('./topics'),
topics = require('../topics'), topics = require('../topics'),
plugins = require('../plugins'), plugins = require('../plugins'),
categories = require('../categories'), categories = require('../categories'),
privileges = require('../privileges'); privileges = require('../privileges'),
helpers = require('./helpers');
var Controllers = { var Controllers = {
topics: topicsController, topics: require('./topics'),
categories: categoriesController, categories: require('./categories'),
tags: tagsController, tags: require('./tags'),
search: searchController, search: require('./search'),
users: usersController, users: require('./users'),
groups: groupsController, groups: require('./groups'),
accounts: accountsController, accounts: require('./accounts'),
static: staticController, static: require('./static'),
api: apiController, api: require('./api'),
admin: adminController admin: require('./admin'),
}; };
Controllers.home = function(req, res, next) { Controllers.home = function(req, res, next) {
async.parallel({ var route = meta.config.homePageRoute || 'home';
header: function (next) { if (route === 'home') {
res.locals.metaTags = [{ return Controllers.categories.list(req, res, next);
name: "title", } else if (route === 'recent') {
content: validator.escape(meta.config.title || 'NodeBB') Controllers.categories.recent(req, res, next);
}, { } else if (route === 'popular') {
name: "description", Controllers.categories.popular(req, res, next);
content: validator.escape(meta.config.description || '') }
}, {
property: 'og:title',
content: 'Index | ' + validator.escape(meta.config.title || 'NodeBB')
}, {
property: 'og:type',
content: 'website'
}];
if(meta.config['brand:logo']) {
res.locals.metaTags.push({
property: 'og:image',
content: meta.config['brand:logo']
});
}
next(null);
},
categories: function (next) {
var uid = req.user ? req.user.uid : 0;
categories.getCategoriesByPrivilege(uid, 'find', function (err, categoryData) {
if (err) {
return next(err);
}
var childCategories = [];
for(var i=categoryData.length - 1; i>=0; --i) {
if (Array.isArray(categoryData[i].children) && categoryData[i].children.length) {
childCategories.push.apply(childCategories, categoryData[i].children);
}
if (categoryData[i].parent && categoryData[i].parent.cid) {
categoryData.splice(i, 1);
}
}
async.parallel([
function(next) {
categories.getRecentTopicReplies(categoryData, uid, next);
},
function(next) {
categories.getRecentTopicReplies(childCategories, uid, next);
}
], function(err) {
next(err, categoryData);
});
});
}
}, function (err, data) {
if (err) {
return next(err);
}
res.render('home', data);
});
}; };
Controllers.reset = function(req, res, next) { Controllers.reset = function(req, res, next) {

@ -0,0 +1,90 @@
"use strict";
var async = require('async'),
nconf = require('nconf'),
fs = require('fs'),
path = require('path'),
meta = require('../meta'),
plugins = require('../plugins'),
utils = require('../../public/src/utils'),
templatesController = {};
var availableTemplatesCache = null;
var configCache = null;
templatesController.getTemplatesListing = function(req, res, next) {
async.parallel({
availableTemplates: function(next) {
getAvailableTemplates(next);
},
templatesConfig: function(next) {
async.waterfall([
function(next) {
readConfigFile(next);
},
function(config, next) {
config.custom_mapping['^/?$'] = meta.config.homePageRoute || 'home';
plugins.fireHook('filter:templates.get_config', config, next);
}
], next);
},
}, function(err, results) {
if (err) {
return next(err);
}
res.json(results);
});
};
function readConfigFile(callback) {
if (configCache) {
return callback(null, configCache);
}
fs.readFile(path.join(nconf.get('views_dir'), 'config.json'), function(err, config) {
if (err) {
return callback(err);
}
try {
config = JSON.parse(config.toString());
} catch (err) {
return callback(err);
}
configCache = config;
callback(null, config);
});
}
function getAvailableTemplates(callback) {
if (availableTemplatesCache) {
return callback(null, availableTemplatesCache);
}
async.parallel({
views: function(next) {
utils.walk(nconf.get('views_dir'), next);
},
extended: function(next) {
plugins.fireHook('filter:templates.get_virtual', [], next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var availableTemplates = results.views.filter(function(value, index, self) {
return value && self.indexOf(value) === index;
}).map(function(el) {
return el && el.replace(nconf.get('views_dir') + '/', '');
});
availableTemplatesCache = availableTemplates = availableTemplates.concat(results.extended);
callback(null, availableTemplates)
});
}
module.exports = templatesController;

@ -1,17 +1,11 @@
"use strict"; "use strict";
var path = require('path'), var express = require('express'),
async = require('async'),
fs = require('fs'),
nconf = require('nconf'),
express = require('express'),
posts = require('../posts'), posts = require('../posts'),
categories = require('../categories'), categories = require('../categories'),
plugins = require('../plugins'), uploadsController = require('../controllers/uploads'),
utils = require('../../public/src/utils'), templatesController = require('../controllers/templates');
uploadsController = require('../controllers/uploads');
module.exports = function(app, middleware, controllers) { module.exports = function(app, middleware, controllers) {
@ -22,7 +16,7 @@ module.exports = function(app, middleware, controllers) {
router.get('/widgets/render', controllers.api.renderWidgets); router.get('/widgets/render', controllers.api.renderWidgets);
router.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID); router.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID);
router.get('/get_templates_listing', getTemplatesListing); router.get('/get_templates_listing', templatesController.getTemplatesListing);
router.get('/categories/:cid/moderators', getModerators); router.get('/categories/:cid/moderators', getModerators);
router.get('/recent/posts/:term?', getRecentPosts); router.get('/recent/posts/:term?', getRecentPosts);
@ -40,56 +34,6 @@ function getModerators(req, res, next) {
}); });
} }
var templatesListingCache = {};
function getTemplatesListing(req, res, next) {
if (templatesListingCache.availableTemplates && templatesListingCache.templatesConfig) {
return res.json(templatesListingCache);
}
async.parallel({
views: function(next) {
utils.walk(nconf.get('views_dir'), next);
},
extended: function(next) {
plugins.fireHook('filter:templates.get_virtual', [], next);
},
config: function(next) {
fs.readFile(path.join(nconf.get('views_dir'), 'config.json'), function(err, config) {
if (err) {
return next(err);
}
try {
config = JSON.parse(config.toString());
} catch (err) {
return next(err);
}
plugins.fireHook('filter:templates.get_config', config, next);
});
},
}, function(err, results) {
if (err) {
return next(err);
}
var data = results.views.filter(function(value, index, self) {
return value && self.indexOf(value) === index;
}).map(function(el) {
return el && el.replace(nconf.get('views_dir') + '/', '');
});
data = data.concat(results.extended);
templatesListingCache = {
availableTemplates: data,
templatesConfig: results.config
};
res.json(templatesListingCache);
});
}
function getRecentPosts(req, res, next) { function getRecentPosts(req, res, next) {
var uid = (req.user) ? req.user.uid : 0; var uid = (req.user) ? req.user.uid : 0;

@ -50,6 +50,7 @@ function tagRoutes(app, middleware, controllers) {
} }
function categoryRoutes(app, middleware, controllers) { function categoryRoutes(app, middleware, controllers) {
setupPageRoute(app, '/categories', middleware, [], controllers.categories.list);
setupPageRoute(app, '/popular/:term?', middleware, [], controllers.categories.popular); setupPageRoute(app, '/popular/:term?', middleware, [], controllers.categories.popular);
setupPageRoute(app, '/recent', middleware, [], controllers.categories.recent); setupPageRoute(app, '/recent', middleware, [], controllers.categories.recent);
setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread); setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread);

@ -28,6 +28,20 @@
</div> </div>
</div> </div>
<div class="panel panel-default">
<div class="panel-heading">Home Page</div>
<div class="panel-body">
<form>
<label>Home Page Route</label>
<select class="form-control" data-field="homePageRoute">
<option value="home">Categories</option>
<option value="recent">Recent</option>
<option value="popular">Popular</option>
</select>
</form>
</div>
</div>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">Site Logo</div> <div class="panel-heading">Site Logo</div>

Loading…
Cancel
Save