Ghost/core/server/services/routing/helpers/entry-lookup.js
Fabien O'Carroll cc1f62438d
Added support for serverside rendering of members content (#10522)
no-issue

- Added member auth middleware to siteApp
- Passed member as context in routing service
- set Cache-Control: private for member requests
- fucked up some tests
- Added member as global template variable
- Updated tokens to have expiry of subscription_period_end
2019-02-25 17:03:27 +01:00

56 lines
1.7 KiB
JavaScript

const _ = require('lodash'),
Promise = require('bluebird'),
url = require('url'),
debug = require('ghost-ignition').debug('services:routing:helpers:entry-lookup'),
routeMatch = require('path-match')();
function entryLookup(postUrl, routerOptions, locals) {
debug(postUrl);
const api = require('../../../api')[locals.apiVersion];
const targetPath = url.parse(postUrl).path;
const permalinks = routerOptions.permalinks;
let isEditURL = false;
// CASE: e.g. /:slug/ -> { slug: 'value' }
const matchFunc = routeMatch(permalinks);
const params = matchFunc(targetPath);
debug(targetPath);
debug(params);
debug(permalinks);
// CASE 1: no matches, resolve
// CASE 2: params can be empty e.g. permalink is /featured/:options(edit)?/ and path is /featured/
if (params === false || !Object.keys(params).length) {
return Promise.resolve();
}
// CASE: redirect if url contains `/edit/` at the end
if (params.options && params.options.toLowerCase() === 'edit') {
isEditURL = true;
}
/**
* Query database to find entry.
* @deprecated: `author`, will be removed in Ghost 3.0
*/
return api[routerOptions.query.controller]
.read(_.extend(_.pick(params, 'slug', 'id'), {include: 'author,authors,tags', context: {member: locals.member}}))
.then(function then(result) {
const entry = result[routerOptions.query.resource][0];
if (!entry) {
return Promise.resolve();
}
return {
entry: entry,
isEditURL: isEditURL,
isUnknownOption: isEditURL ? false : !!params.options
};
});
}
module.exports = entryLookup;