2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const urlUtils = require('../../lib/url-utils');
|
2019-11-06 14:32:43 +03:00
|
|
|
const {mega} = require('../../services/mega');
|
2019-11-06 12:30:11 +03:00
|
|
|
const allowedIncludes = ['tags', 'authors', 'authors.roles', 'email'];
|
2019-10-08 16:44:27 +03:00
|
|
|
const unsafeAttrs = ['status', 'authors', 'visibility'];
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'posts',
|
|
|
|
browse: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'filter',
|
|
|
|
'fields',
|
|
|
|
'formats',
|
|
|
|
'limit',
|
|
|
|
'order',
|
|
|
|
'page',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls'
|
|
|
|
],
|
|
|
|
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',
|
|
|
|
'debug',
|
|
|
|
'absolute_urls',
|
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
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: [
|
|
|
|
'include',
|
2019-11-14 20:29:03 +03:00
|
|
|
'source',
|
|
|
|
'send_email_when_published'
|
2019-08-09 17:11:24 +03:00
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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',
|
|
|
|
'id',
|
|
|
|
'source',
|
2019-11-14 20:29:03 +03:00
|
|
|
'send_email_when_published',
|
2019-08-09 17:11:24 +03:00
|
|
|
// NOTE: only for internal context
|
|
|
|
'forUpdate',
|
|
|
|
'transacting'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
include: {
|
|
|
|
values: allowedIncludes
|
|
|
|
},
|
|
|
|
id: {
|
|
|
|
required: true
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
values: ['html']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
unsafeAttrs: unsafeAttrs
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Post.edit(frame.data.posts[0], frame.options)
|
2019-11-06 14:32:43 +03:00
|
|
|
.then(async (model) => {
|
|
|
|
if (!model.get('send_email_when_published')) {
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!model.get('email') && (model.get('status') === 'published') && model.wasChanged()) {
|
|
|
|
const email = await mega.addEmail(model.toJSON());
|
2019-11-07 13:03:05 +03:00
|
|
|
model.set('email', email);
|
2019-11-06 14:32:43 +03:00
|
|
|
}
|
2019-11-07 13:03:05 +03:00
|
|
|
|
|
|
|
return model;
|
2019-11-06 14:32:43 +03:00
|
|
|
})
|
2019-08-09 17:11:24 +03:00
|
|
|
.then((model) => {
|
|
|
|
if (
|
|
|
|
model.get('status') === 'published' && model.wasChanged() ||
|
|
|
|
model.get('status') === 'draft' && model.previous('status') === 'published'
|
|
|
|
) {
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
} else if (
|
|
|
|
model.get('status') === 'draft' && model.previous('status') !== 'published' ||
|
|
|
|
model.get('status') === 'scheduled' && model.wasChanged()
|
|
|
|
) {
|
|
|
|
this.headers.cacheInvalidate = {
|
|
|
|
value: urlUtils.urlFor({
|
|
|
|
relativeUrl: urlUtils.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')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|