initial pagination rewrite, cleanups to comment edit

This commit is contained in:
Wyatt Johnson
2017-05-11 17:27:02 -06:00
parent 5c5d014e23
commit d497ad3369
14 changed files with 721 additions and 743 deletions
+57 -92
View File
@@ -3,19 +3,7 @@ const CommentModel = require('../models/comment');
const ActionModel = require('../models/action');
const ActionsService = require('./actions');
const {ErrEditWindowHasEnded} = require('../errors');
// const ALLOWED_TAGS = [
// {name: 'STAFF'},
// {name: 'BEST'},
// ];
const STATUSES = [
'ACCEPTED',
'REJECTED',
'PREMOD',
'NONE',
];
const errors = require('../errors');
const EDIT_WINDOW_MS = 30 * 1000; // 30 seconds
@@ -54,84 +42,69 @@ module.exports = class CommentsService {
/**
* Edit a Comment
* @param {String} id comment.id you want to edit (or its ID)
* @param {String} asset_id asset_id of the comment
* @param {String} editor user.id of the user trying to edit the comment (will err if not comment author)
* @param {String} author_id user.id of the user trying to edit the comment (will err if not comment author)
* @param {String} body the new Comment body
* @param {String} status the new Comment status
*/
static async edit(id, asset_id, editor, {body, status, ignoreEditWindow}) {
if (status && ! STATUSES.includes(status)) {
throw new Error(`status ${status} is not supported`);
}
static async edit(id, author_id, {body, status, ignoreEditWindow = false}) {
const query = {
id,
author_id
};
// Establish the edit window (if it exists) and add the condition to the
// original query.
const lastEditableCommentCreatedAt = new Date((new Date()).getTime() - EDIT_WINDOW_MS);
const filter = Object.assign(
{
id,
asset_id,
author_id: editor,
},
ignoreEditWindow ? {} : {
created_at: {
$gt: lastEditableCommentCreatedAt,
},
}
);
const {nModified} = await CommentModel.update(
filter,
{
$set: {
body,
status,
},
$push: {
body_history: {
body,
created_at: new Date(),
},
status_history: {
type: status,
created_at: new Date(),
}
},
}
);
switch (nModified) {
case 0: {
// disambiguate possible error cases
const comment = await this.findById(id);
// return whether the comment should no longer be editable
// because its edit window expired
const editWindowExpired = (comment) => {
const now = new Date;
const editableUntil = this.getEditableUntilDate(comment);
return now > editableUntil;
if (!ignoreEditWindow) {
query.created_at = {
$gt: lastEditableCommentCreatedAt,
};
if ( ! comment || (comment.asset_id !== asset_id)) {
throw Object.assign(new Error('Comment not found'), {
name: 'CommentNotFound'
});
} else if (comment.author_id !== editor) {
throw Object.assign(new Error('You aren\'t allowed to edit that comment'), {
name: 'NotAuthorizedToEdit'
});
} else if (( ! ignoreEditWindow) && editWindowExpired(comment)) {
throw new ErrEditWindowHasEnded();
}
throw new Error('Failed to edit comment. This could be because it can\'t be found, the edit window expired, or because you\'re not allowed to edit it.');
}
}
}
/**
* Until when can the provided comment be edited?
* @param {Comment} comment - comment to check last edit date of
* @returns {Date} last date at which comment can be edited
*/
static getEditableUntilDate(comment) {
const {created_at} = comment;
return new Date(Number(created_at) + EDIT_WINDOW_MS);
const {
value: comment
} = await CommentModel.findOneAndUpdate(query, {
$set: {
body,
status,
},
$push: {
body_history: {
body,
created_at: new Date(),
},
status_history: {
type: status,
created_at: new Date(),
}
},
}, {
new: true,
rawResult: true
});
if (comment === null) {
// Try to get the comment.
const comment = await CommentsService.findById(id);
if (comment === null) {
throw errors.ErrNotFound;
}
// Check to see if the user was't allowed to edit it.
if (comment.author_id !== author_id) {
throw errors.ErrNotAuthorized;
}
// Check to see if the edit window expired.
if (!ignoreEditWindow && comment.created_at <= lastEditableCommentCreatedAt) {
throw errors.ErrEditWindowHasEnded;
}
throw new Error('comment edit failed for an unexpected reason');
}
return comment;
}
/**
@@ -304,14 +277,6 @@ module.exports = class CommentsService {
* @return {Promise}
*/
static pushStatus(id, status, assigned_by = null) {
// Check to see if the comment status is in the allowable set of statuses.
if (STATUSES.indexOf(status) === -1) {
// Comment status is not supported! Error out here.
return Promise.reject(new Error(`status ${status} is not supported`));
}
return CommentModel.findOneAndUpdate({id}, {
$push: {
status_history: {