2014-06-03 17:05:25 +04:00
|
|
|
// # Slug API
|
|
|
|
// RESTful API for the Slug resource
|
2017-09-12 18:31:14 +03:00
|
|
|
var Promise = require('bluebird'),
|
2017-12-14 00:20:02 +03:00
|
|
|
pipeline = require('../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-09-12 18:31:14 +03:00
|
|
|
models = require('../models'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-09-12 18:31:14 +03:00
|
|
|
docName = 'slugs',
|
2014-05-06 02:38:05 +04:00
|
|
|
slugs,
|
2014-08-14 01:58:12 +04:00
|
|
|
allowedTypes;
|
2014-05-06 02:38:05 +04:00
|
|
|
|
|
|
|
/**
|
2014-06-03 17:05:25 +04:00
|
|
|
* ## Slugs API Methods
|
|
|
|
*
|
2017-12-12 00:47:46 +03:00
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
2014-05-06 02:38:05 +04:00
|
|
|
*/
|
|
|
|
slugs = {
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Generate Slug
|
2014-06-04 09:47:16 +04:00
|
|
|
* Create a unique slug for the given type and its name
|
2014-06-03 17:05:25 +04:00
|
|
|
*
|
2014-06-04 09:47:16 +04:00
|
|
|
* @param {{type (required), name (required), context, transacting}} options
|
2014-06-03 17:05:25 +04:00
|
|
|
* @returns {Promise(String)} Unique string
|
|
|
|
*/
|
2014-05-06 02:38:05 +04:00
|
|
|
generate: function (options) {
|
2015-07-01 21:17:56 +03:00
|
|
|
var opts = ['type'],
|
|
|
|
attrs = ['name'],
|
|
|
|
tasks;
|
2014-05-06 02:38:05 +04:00
|
|
|
|
2014-08-14 01:58:12 +04:00
|
|
|
// `allowedTypes` is used to define allowed slug types and map them against its model class counterpart
|
|
|
|
allowedTypes = {
|
2017-09-12 18:31:14 +03:00
|
|
|
post: models.Post,
|
|
|
|
tag: models.Tag,
|
|
|
|
user: models.User,
|
|
|
|
app: models.App
|
2014-08-14 01:58:12 +04:00
|
|
|
};
|
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
/**
|
2015-08-11 17:03:57 +03:00
|
|
|
* ### Check allowed types
|
|
|
|
* check if options.type contains an allowed type
|
2015-07-02 17:38:31 +03:00
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
2015-08-11 17:03:57 +03:00
|
|
|
function checkAllowedTypes(options) {
|
|
|
|
if (allowedTypes[options.type] === undefined) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.BadRequestError({message: common.i18n.t('errors.api.slugs.unknownSlugType', {type: options.type})}));
|
2015-08-11 17:03:57 +03:00
|
|
|
}
|
|
|
|
return options;
|
2015-07-02 17:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Model Query
|
|
|
|
* Make the call to the Model layer
|
|
|
|
* @param {Object} options
|
|
|
|
* @returns {Object} options
|
|
|
|
*/
|
|
|
|
function modelQuery(options) {
|
2017-09-27 11:31:41 +03:00
|
|
|
return models.Base.Model.generateSlug(allowedTypes[options.type], options.data.name, {status: 'all'})
|
|
|
|
.then(function onModelResponse(slug) {
|
|
|
|
if (!slug) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.GhostError({
|
|
|
|
message: common.i18n.t('errors.api.slugs.couldNotGenerateSlug')
|
2017-09-27 11:31:41 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
slugs: [{slug: slug}]
|
|
|
|
};
|
|
|
|
});
|
2015-07-02 17:38:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push all of our tasks into a `tasks` array in the correct order
|
2015-07-01 21:17:56 +03:00
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.validate(docName, {opts: opts, attrs: attrs}),
|
|
|
|
localUtils.handlePermissions(docName, 'generate'),
|
2015-08-11 17:03:57 +03:00
|
|
|
checkAllowedTypes,
|
2015-07-01 21:17:56 +03:00
|
|
|
modelQuery
|
|
|
|
];
|
2015-07-02 17:38:31 +03:00
|
|
|
|
|
|
|
// Pipeline calls each task passing the result of one to be the arguments for the next
|
2017-09-27 11:31:41 +03:00
|
|
|
return pipeline(tasks, options);
|
2014-05-06 02:38:05 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = slugs;
|