diff --git a/core/server/controllers/frontend.js b/core/server/controllers/frontend.js index cc9cf0a92e..906e8fe79d 100644 --- a/core/server/controllers/frontend.js +++ b/core/server/controllers/frontend.js @@ -165,6 +165,66 @@ frontendControllers = { }); }).otherwise(handleError(next)); }, + 'author': function (req, res, next) { + + // Parse the page number + var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1, + options = { + page: pageParam, + author: req.params.slug + }; + + + + // Get url for tag page + function authorUrl(author, page) { + var url = config().paths.subdir + '/author/' + author + '/'; + + if (page && page > 1) { + url += 'page/' + page + '/'; + } + + return url; + } + + // No negative pages, or page 1 + if (isNaN(pageParam) || pageParam < 1 || (req.params.page !== undefined && pageParam === 1)) { + return res.redirect(authorUrl(options.author)); + } + + return getPostPage(options).then(function (page) { + // If page is greater than number of pages we have, redirect to last page + if (pageParam > page.meta.pagination.pages) { + return res.redirect(authorUrl(options.author, page.meta.pagination.pages)); + } + + setReqCtx(req, page.posts); + if (page.meta.filters.author) { + setReqCtx(req, page.meta.filters.author); + } + + // Render the page of posts + filters.doFilter('prePostsRender', page.posts).then(function (posts) { + api.settings.read({key: 'activeTheme', context: {internal: true}}).then(function (response) { + var activeTheme = response.settings[0], + paths = config().paths.availableThemes[activeTheme.value], + view = paths.hasOwnProperty('author.hbs') ? 'author' : 'index', + + // Format data for template + result = _.extend(formatPageResponse(posts, page), { + author: page.meta.filters.author ? page.meta.filters.author : '' + }); + + // If the resulting author is '' then 404. + if (!result.author) { + return next(); + } + res.render(view, result); + }); + }); + }).otherwise(handleError(next)); + }, + 'single': function (req, res, next) { var path = req.path, params, diff --git a/core/server/models/post.js b/core/server/models/post.js index 6020995d7b..6726962c0b 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -6,8 +6,9 @@ var _ = require('lodash'), Showdown = require('showdown'), ghostgfm = require('../../shared/lib/showdown/extensions/ghostgfm'), converter = new Showdown.converter({extensions: [ghostgfm]}), - Tag = require('./tag').Tag, + Tag = require('./tag').Tag, Tags = require('./tag').Tags, + User = require('./user').User, ghostBookshelf = require('./base'), xmlrpc = require('../xmlrpc'), @@ -278,7 +279,8 @@ Post = ghostBookshelf.Model.extend({ options = options || {}; var postCollection = Posts.forge(), - tagInstance = options.tag !== undefined ? Tag.forge({slug: options.tag}) : false; + tagInstance = options.tag !== undefined ? Tag.forge({slug: options.tag}) : false, + authorInstance = options.author !== undefined ? User.forge({slug: options.author}) : false; if (options.limit) { options.limit = parseInt(options.limit) || 15; @@ -329,7 +331,14 @@ Post = ghostBookshelf.Model.extend({ return false; } - return when(fetchTagQuery()) + function fetchAuthorQuery() { + if (authorInstance) { + return authorInstance.fetch(); + } + return false; + } + + return when.join(fetchTagQuery(), fetchAuthorQuery()) // Set the limit & offset for the query, fetching // with the opts (to specify any eager relations, etc.) @@ -344,6 +353,11 @@ Post = ghostBookshelf.Model.extend({ .query('join', 'posts_tags', 'posts_tags.post_id', '=', 'posts.id') .query('where', 'posts_tags.tag_id', '=', tagInstance.id); } + + if (authorInstance) { + postCollection + .query('where', 'author_id', '=', authorInstance.id); + } return postCollection .query('limit', options.limit) .query('offset', options.limit * (options.page - 1)) @@ -371,6 +385,9 @@ Post = ghostBookshelf.Model.extend({ qb.join('posts_tags', 'posts_tags.post_id', '=', 'posts.id'); qb.where('posts_tags.tag_id', '=', tagInstance.id); } + if (authorInstance) { + qb.where('author_id', '=', authorInstance.id); + } return qb.count(tableName + '.' + idAttribute + ' as aggregate'); }) @@ -419,6 +436,13 @@ Post = ghostBookshelf.Model.extend({ } } + if (authorInstance) { + meta.filters = {}; + if (!authorInstance.isNew()) { + meta.filters.author = authorInstance.toJSON(); + } + } + return data; }) .catch(errors.logAndThrowError); diff --git a/core/server/routes/frontend.js b/core/server/routes/frontend.js index f0aed39df6..fe5612f464 100644 --- a/core/server/routes/frontend.js +++ b/core/server/routes/frontend.js @@ -20,11 +20,19 @@ frontendRoutes = function () { res.redirect(301, subdir + '/rss/'); }); - + // Tags router.get('/tag/:slug/rss/', frontend.rss); router.get('/tag/:slug/rss/:page/', frontend.rss); router.get('/tag/:slug/page/:page/', frontend.tag); router.get('/tag/:slug/', frontend.tag); + + // Authors + router.get('/author/:slug/rss/', frontend.rss); + router.get('/author/:slug/rss/:page/', frontend.rss); + router.get('/author/:slug/page/:page/', frontend.author); + router.get('/author/:slug/', frontend.author); + + // Default router.get('/page/:page/', frontend.homepage); router.get('/', frontend.homepage); router.get('*', frontend.single);