2018-10-10 17:34:16 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const urlService = require('../../services/url');
|
2019-02-22 06:17:14 +03:00
|
|
|
const allowedIncludes = ['tags', 'authors', 'authors.roles'];
|
|
|
|
const unsafeAttrs = ['status', 'authors'];
|
2018-10-10 17:34:16 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'posts',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
2018-10-18 12:05:51 +03:00
|
|
|
'page',
|
2018-11-05 20:07:45 +03:00
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.findPage(frame.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
2018-11-05 20:07:45 +03:00
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'id',
|
|
|
|
'slug',
|
|
|
|
'uuid'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
formats: {
|
|
|
|
values: models.Post.allowedFormats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.findOne(frame.data, frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (!model) {
|
|
|
|
throw new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
add: {
|
|
|
|
statusCode: 201,
|
|
|
|
headers: {},
|
|
|
|
options: [
|
2019-01-18 14:29:56 +03:00
|
|
|
'include',
|
|
|
|
'source'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
2019-01-18 14:29:56 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2018-10-10 17:34:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.add(frame.data.posts[0], frame.options)
|
|
|
|
.then((model) => {
|
|
|
|
if (model.get('status') !== 'published') {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
edit: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'include',
|
2019-01-18 14:29:56 +03:00
|
|
|
'id',
|
|
|
|
'source'
|
2018-10-10 17:34:16 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
2019-01-18 14:29:56 +03:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
2018-10-10 17:34:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.edit(frame.data.posts[0], frame.options)
|
|
|
|
.then((model) => {
|
2019-02-08 00:08:36 +03:00
|
|
|
if (model.get('status') === 'published' && model.wasChanged() ||
|
2019-01-21 23:53:11 +03:00
|
|
|
model.get('status') === 'draft' && model.previous('status') === 'published') {
|
2018-10-10 17:34:16 +03:00
|
|
|
this.headers.cacheInvalidate = true;
|
2019-01-21 23:53:11 +03:00
|
|
|
} else if (model.get('status') === 'draft' && model.previous('status') !== 'published') {
|
2018-10-10 17:34:16 +03:00
|
|
|
this.headers.cacheInvalidate = {
|
|
|
|
value: urlService.utils.urlFor({
|
|
|
|
relativeUrl: urlService.utils.urlJoin('/p', model.get('uuid'), '/')
|
|
|
|
})
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.headers.cacheInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return model;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'id'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
frame.options.require = true;
|
|
|
|
|
|
|
|
return models.Post.destroy(frame.options)
|
|
|
|
.return(null)
|
|
|
|
.catch(models.Post.NotFoundError, () => {
|
|
|
|
throw new common.errors.NotFoundError({
|
|
|
|
message: common.i18n.t('errors.api.posts.postNotFound')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|