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'),
|
|
|
|
pipeline = require('../utils/pipeline'),
|
|
|
|
apiUtils = require('./utils'),
|
|
|
|
models = require('../models'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
i18n = require('../i18n'),
|
|
|
|
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 = {
|
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) {
|
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) {
|
2017-09-12 18:31:14 +03:00
|
|
|
return models.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 = [
|
2017-09-12 18:31:14 +03:00
|
|
|
apiUtils.validate(docName, {opts: opts, attrs: attrs}),
|
|
|
|
apiUtils.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
|
|
|
|
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;
|