0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added basic comment CRUD model + API

refs https://github.com/TryGhost/Team/issues/1664
This commit is contained in:
Hannah Wolfe 2022-07-05 12:07:20 +02:00 committed by Simon Backx
parent a5874f90fe
commit 00110e541e
5 changed files with 180 additions and 7 deletions

View file

@ -0,0 +1,146 @@
const Promise = require('bluebird');
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const models = require('../../models');
const ALLOWED_INCLUDES = ['post', 'member'];
const UNSAFE_ATTRS = ['status'];
const messages = {
commentNotFound: 'Comment could not be found'
};
module.exports = {
docName: 'comments',
browse: {
options: [
'include',
'page',
'limit',
'fields',
'filter',
'order',
'debug'
],
validation: {
options: {
include: ALLOWED_INCLUDES
}
},
permissions: true,
query(frame) {
return models.Comment.findPage(frame.options);
}
},
read: {
options: [
'include'
],
data: [
'id',
'email'
],
validation: {
options: {
include: ALLOWED_INCLUDES
}
},
permissions: true,
query(frame) {
return models.Comment.findOne(frame.data, frame.options)
.then((model) => {
if (!model) {
return Promise.reject(new errors.NotFoundError({
message: tpl(messages.commentNotFound)
}));
}
return model;
});
}
},
edit: {
headers: {},
options: [
'id',
'include'
],
validation: {
options: {
include: {
values: ALLOWED_INCLUDES
},
id: {
required: true
}
}
},
permissions: true,
query(frame) {
return models.Comment.edit(frame.data.comments[0], frame.options)
.then((model) => {
if (!model) {
return Promise.reject(new errors.NotFoundError({
message: tpl(messages.commentNotFound)
}));
}
return model;
});
}
},
add: {
statusCode: 201,
options: [
'include'
],
validation: {
options: {
include: ALLOWED_INCLUDES
},
data: {
member_id: {
required: true
},
post_id: {
required: true
}
}
},
permissions: {
unsafeAttrs: UNSAFE_ATTRS
},
query(frame) {
return models.Comment.add(frame.data.comments[0], frame.options);
}
},
destroy: {
statusCode: 204,
options: [
'include',
'id'
],
validation: {
options: {
include: ALLOWED_INCLUDES
}
},
permissions: true,
query(frame) {
frame.options.require = true;
return models.Comment.destroy(frame.options)
.then(() => null)
.catch(models.Comment.NotFoundError, () => {
return Promise.reject(new errors.NotFoundError({
message: tpl(messages.commentNotFound)
}));
});
}
}
};

View file

@ -219,5 +219,13 @@ module.exports = {
get offersPublic() {
return shared.pipeline(require('./offers-public'), localUtils, 'content');
},
/**
* Comment API
*/
get commentsComments() {
return shared.pipeline(require('./comments-comments'), localUtils, 'comments');
}
};

View file

@ -31,13 +31,13 @@ const Comment = ghostBookshelf.Model.extend({
model.emitChange('added', options);
}
});
const Comments = ghostBookshelf.Collection.extend({
model: Comment
}, {
async permissible(model, action, context, unsafeAttrs, loadedPermissions, hasUserPermission, hasApiKeyPermission, hasMemberPermission) {
console.log('checking permissions', hasMemberPermission);
return true;
}
});
module.exports = {
Comment: ghostBookshelf.model('Comment', Comment),
Comments: ghostBookshelf.collection('Comments', Comments)
Comment: ghostBookshelf.model('Comment', Comment)
};

View file

@ -1,9 +1,11 @@
const debug = require('@tryghost/debug')('comments');
const errorHandler = require('@tryghost/mw-error-handler');
const cors = require('cors');
const bodyParser = require('body-parser');
const express = require('../../../shared/express');
const urlUtils = require('../../../shared/url-utils');
const sentry = require('../../../shared/sentry');
const routes = require('./routes');
module.exports = function setupCommentsApp() {
debug('Comments App setup start');
@ -13,8 +15,10 @@ module.exports = function setupCommentsApp() {
const siteUrl = new URL(urlUtils.getSiteUrl());
commentsApp.use(cors(siteUrl.origin));
commentsApp.use(bodyParser.json({limit: '50mb'}));
// Routing
commentsApp.get('/api/comments');
commentsApp.use('/api/', routes());
// API error handling
commentsApp.use('/api', errorHandler.resourceNotFound);

View file

@ -0,0 +1,15 @@
const express = require('../../../shared/express');
const api = require('../../api').endpoints;
const http = require('../../api').shared.http;
module.exports = function apiRoutes() {
const router = express.Router('content api');
router.get('/comments', http(api.commentsComments.browse));
router.get('/comments/:id', http(api.commentsComments.read));
router.post('/comments', http(api.commentsComments.add));
router.put('/comments/:id', http(api.commentsComments.edit));
router.delete('/comments/:id', http(api.commentsComments.destroy));
return router;
};