2014-06-03 17:05:25 +04:00
|
|
|
// # Slug API
|
|
|
|
// RESTful API for the Slug resource
|
2015-08-11 17:03:57 +03:00
|
|
|
var dataProvider = require('../models'),
|
2014-05-06 02:38:05 +04:00
|
|
|
errors = require('../errors'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2015-07-02 17:38:31 +03:00
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
utils = require('./utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2015-07-02 17:38:31 +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
|
|
|
|
*
|
|
|
|
* **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 = {
|
|
|
|
post: dataProvider.Post,
|
|
|
|
tag: dataProvider.Tag,
|
|
|
|
user: dataProvider.User,
|
|
|
|
app: dataProvider.App
|
|
|
|
};
|
|
|
|
|
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) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.BadRequestError({message: 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) {
|
2015-07-01 21:17:56 +03:00
|
|
|
return dataProvider.Base.Model.generateSlug(allowedTypes[options.type], options.data.name, {status: 'all'});
|
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 = [
|
|
|
|
utils.validate(docName, {opts: opts, attrs: attrs}),
|
2015-08-11 17:03:57 +03:00
|
|
|
utils.handlePermissions(docName, 'generate'),
|
|
|
|
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
|
|
|
|
return pipeline(tasks, options).then(function (slug) {
|
|
|
|
if (!slug) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.GhostError({message: i18n.t('errors.api.slugs.couldNotGenerateSlug')}));
|
2014-06-04 09:47:16 +04:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:38:31 +03:00
|
|
|
return {slugs: [{slug: slug}]};
|
2014-05-06 02:38:05 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = slugs;
|