feat: comments delete + refresh on post + formatting
This commit is contained in:
@@ -40,7 +40,25 @@ module.exports = {
|
||||
* Fetch list of comments for a page
|
||||
*/
|
||||
async list (obj, args, context) {
|
||||
return []
|
||||
const page = await WIKI.models.pages.getPage(args)
|
||||
if (page) {
|
||||
if (WIKI.auth.checkAccess(context.req.user, ['read:comments'], {
|
||||
path: page.path,
|
||||
locale: page.localeCode
|
||||
})) {
|
||||
const comments = await WIKI.models.comments.query().where('pageId', page.id)
|
||||
return comments.map(c => ({
|
||||
...c,
|
||||
authorName: c.name,
|
||||
authorEmail: c.email,
|
||||
authorIP: c.ip
|
||||
}))
|
||||
} else {
|
||||
throw new WIKI.Error.PageViewForbidden()
|
||||
}
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
},
|
||||
CommentMutation: {
|
||||
@@ -49,12 +67,31 @@ module.exports = {
|
||||
*/
|
||||
async create (obj, args, context) {
|
||||
try {
|
||||
// WIKI.data.commentProvider.create({
|
||||
// ...args,
|
||||
// user: context.req.user
|
||||
// })
|
||||
const cmId = await WIKI.models.comments.postNewComment({
|
||||
...args,
|
||||
user: context.req.user,
|
||||
ip: context.req.ip
|
||||
})
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('New comment posted successfully')
|
||||
responseResult: graphHelper.generateSuccess('New comment posted successfully'),
|
||||
id: cmId
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Delete an Existing Comment
|
||||
*/
|
||||
async delete (obj, args, context) {
|
||||
try {
|
||||
await WIKI.models.comments.deleteComment({
|
||||
id: args.id,
|
||||
user: context.req.user,
|
||||
ip: context.req.ip
|
||||
})
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('Comment deleted successfully')
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
|
@@ -18,7 +18,8 @@ type CommentQuery {
|
||||
providers: [CommentProvider] @auth(requires: ["manage:system"])
|
||||
|
||||
list(
|
||||
pageId: Int!
|
||||
locale: String!
|
||||
path: String!
|
||||
): [CommentPost]! @auth(requires: ["read:comments", "manage:system"])
|
||||
|
||||
single(
|
||||
@@ -41,7 +42,7 @@ type CommentMutation {
|
||||
content: String!
|
||||
guestName: String
|
||||
guestEmail: String
|
||||
): DefaultResponse @auth(requires: ["write:comments", "manage:system"])
|
||||
): CommentCreateResponse @auth(requires: ["write:comments", "manage:system"])
|
||||
|
||||
update(
|
||||
id: Int!
|
||||
@@ -85,3 +86,8 @@ type CommentPost {
|
||||
createdAt: Date!
|
||||
updatedAt: Date!
|
||||
}
|
||||
|
||||
type CommentCreateResponse {
|
||||
responseResult: ResponseStatus
|
||||
id: Int
|
||||
}
|
||||
|
@@ -97,6 +97,26 @@ module.exports = {
|
||||
message: 'Too many attempts! Try again later.',
|
||||
code: 1008
|
||||
}),
|
||||
CommentGenericError: CustomError('CommentGenericError', {
|
||||
message: 'An unexpected error occured.',
|
||||
code: 8001
|
||||
}),
|
||||
CommentPostForbidden: CustomError('CommentPostForbidden', {
|
||||
message: 'You are not authorized to post a comment on this page.',
|
||||
code: 8002
|
||||
}),
|
||||
CommentContentMissing: CustomError('CommentContentMissing', {
|
||||
message: 'Comment content is missing or too short.',
|
||||
code: 8003
|
||||
}),
|
||||
CommentManageForbidden: CustomError('CommentManageForbidden', {
|
||||
message: 'You are not authorized to manage comments on this page.',
|
||||
code: 8004
|
||||
}),
|
||||
CommentNotFound: CustomError('CommentNotFound', {
|
||||
message: 'This comment does not exist.',
|
||||
code: 8005
|
||||
}),
|
||||
InputInvalid: CustomError('InputInvalid', {
|
||||
message: 'Input data is invalid.',
|
||||
code: 1012
|
||||
|
@@ -1,4 +1,8 @@
|
||||
const Model = require('objection').Model
|
||||
const validate = require('validate.js')
|
||||
const _ = require('lodash')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
/**
|
||||
* Comments model
|
||||
@@ -52,4 +56,102 @@ module.exports = class Comment extends Model {
|
||||
this.createdAt = new Date().toISOString()
|
||||
this.updatedAt = new Date().toISOString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Post New Comment
|
||||
*/
|
||||
static async postNewComment ({ pageId, replyTo, content, guestName, guestEmail, user, ip }) {
|
||||
// -> Input validation
|
||||
if (user.id === 2) {
|
||||
const validation = validate({
|
||||
email: _.toLower(guestEmail),
|
||||
name: guestName
|
||||
}, {
|
||||
email: {
|
||||
email: true,
|
||||
length: {
|
||||
maximum: 255
|
||||
}
|
||||
},
|
||||
name: {
|
||||
presence: {
|
||||
allowEmpty: false
|
||||
},
|
||||
length: {
|
||||
minimum: 2,
|
||||
maximum: 255
|
||||
}
|
||||
}
|
||||
}, { format: 'flat' })
|
||||
|
||||
if (validation && validation.length > 0) {
|
||||
throw new WIKI.Error.InputInvalid(validation[0])
|
||||
}
|
||||
}
|
||||
|
||||
content = _.trim(content)
|
||||
if (content.length < 2) {
|
||||
throw new WIKI.Error.CommentContentMissing()
|
||||
}
|
||||
|
||||
// -> Load Page
|
||||
const page = await WIKI.models.pages.getPageFromDb(pageId)
|
||||
if (page) {
|
||||
if (!WIKI.auth.checkAccess(user, ['write:comments'], {
|
||||
path: page.path,
|
||||
locale: page.localeCode
|
||||
})) {
|
||||
throw new WIKI.Error.CommentPostForbidden()
|
||||
}
|
||||
} else {
|
||||
throw new WIKI.Error.PageNotFound()
|
||||
}
|
||||
|
||||
// -> Process by comment provider
|
||||
return WIKI.data.commentProvider.create({
|
||||
page,
|
||||
replyTo,
|
||||
content,
|
||||
user: {
|
||||
...user,
|
||||
...(user.id === 2) ? {
|
||||
name: guestName,
|
||||
email: guestEmail
|
||||
} : {},
|
||||
ip
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an Existing Comment
|
||||
*/
|
||||
static async deleteComment ({ id, user, ip }) {
|
||||
// -> Load Page
|
||||
const pageId = await WIKI.data.commentProvider.getPageIdFromCommentId(id)
|
||||
if (!pageId) {
|
||||
throw new WIKI.Error.CommentNotFound()
|
||||
}
|
||||
const page = await WIKI.models.pages.getPageFromDb(pageId)
|
||||
if (page) {
|
||||
if (!WIKI.auth.checkAccess(user, ['manage:comments'], {
|
||||
path: page.path,
|
||||
locale: page.localeCode
|
||||
})) {
|
||||
throw new WIKI.Error.CommentManageForbidden()
|
||||
}
|
||||
} else {
|
||||
throw new WIKI.Error.PageNotFound()
|
||||
}
|
||||
|
||||
// -> Process by comment provider
|
||||
await WIKI.data.commentProvider.remove({
|
||||
id,
|
||||
page,
|
||||
user: {
|
||||
...user,
|
||||
ip
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -74,7 +74,7 @@ module.exports = {
|
||||
ip: user.ip
|
||||
}
|
||||
|
||||
// Check for Spam with Akismet
|
||||
// -> Check for Spam with Akismet
|
||||
if (akismetClient) {
|
||||
let userRole = 'user'
|
||||
if (user.groups.indexOf(1) >= 0) {
|
||||
@@ -106,16 +106,33 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
// Save Comment
|
||||
await WIKI.models.comments.query().insert(newComment)
|
||||
// -> Save Comment to DB
|
||||
const cm = await WIKI.models.comments.query().insert(newComment)
|
||||
|
||||
// -> Return Comment ID
|
||||
return cm.id
|
||||
},
|
||||
async update ({ id, content, user, ip }) {
|
||||
|
||||
},
|
||||
/**
|
||||
* Delete an existing comment by ID
|
||||
*/
|
||||
async remove ({ id, user, ip }) {
|
||||
|
||||
return WIKI.models.comments.query().findById(id).delete()
|
||||
},
|
||||
async count ({ pageId }) {
|
||||
|
||||
/**
|
||||
* Get the page ID from a comment ID
|
||||
*/
|
||||
async getPageIdFromCommentId (id) {
|
||||
const result = await WIKI.models.comments.query().select('pageId').findById(id)
|
||||
return (result) ? result.pageId : false
|
||||
},
|
||||
/**
|
||||
* Get the total comments count for a page ID
|
||||
*/
|
||||
async count (pageId) {
|
||||
const result = await WIKI.models.comments.query().count('* as total').where('pageId', pageId).first()
|
||||
return _.toSafeInteger(result.total)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user