Author pages

refs #3076

- This is a first draft implementation, just to make it work so that we can get casper working
This commit is contained in:
Hannah Wolfe 2014-07-20 17:32:14 +01:00
parent d8818a12d9
commit 2d01e15a18
3 changed files with 96 additions and 4 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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);