mirror of
https://github.com/wassname/talk.git
synced 2026-09-09 11:38:08 +08:00
replaced eslint:recommended with prettier
This commit is contained in:
+98
-79
@@ -7,39 +7,43 @@ const SettingsService = require('./settings');
|
||||
const cloneDeep = require('lodash/cloneDeep');
|
||||
const errors = require('../errors');
|
||||
const events = require('./events');
|
||||
const {
|
||||
COMMENTS_NEW,
|
||||
COMMENTS_EDIT,
|
||||
} = require('./events/constants');
|
||||
const { COMMENTS_NEW, COMMENTS_EDIT } = require('./events/constants');
|
||||
|
||||
module.exports = class CommentsService {
|
||||
|
||||
/**
|
||||
* Creates a new Comment that came from a public source.
|
||||
* @param {Mixed} comment either a single comment or an array of comments.
|
||||
* @return {Promise}
|
||||
*/
|
||||
static async publicCreate(comment) {
|
||||
|
||||
// Check to see if this is an array of comments, if so map it out.
|
||||
if (Array.isArray(comment)) {
|
||||
return Promise.all(comment.map(CommentsService.publicCreate));
|
||||
}
|
||||
|
||||
const {
|
||||
status = 'NONE',
|
||||
} = comment;
|
||||
const { status = 'NONE' } = comment;
|
||||
|
||||
const commentModel = new CommentModel(Object.assign({
|
||||
status_history: status ? [{
|
||||
type: status,
|
||||
created_at: new Date()
|
||||
}] : [],
|
||||
body_history: [{
|
||||
body: comment.body,
|
||||
created_at: new Date()
|
||||
}]
|
||||
}, comment));
|
||||
const commentModel = new CommentModel(
|
||||
Object.assign(
|
||||
{
|
||||
status_history: status
|
||||
? [
|
||||
{
|
||||
type: status,
|
||||
created_at: new Date(),
|
||||
},
|
||||
]
|
||||
: [],
|
||||
body_history: [
|
||||
{
|
||||
body: comment.body,
|
||||
created_at: new Date(),
|
||||
},
|
||||
],
|
||||
},
|
||||
comment
|
||||
)
|
||||
);
|
||||
|
||||
const savedCommentModel = await commentModel.save();
|
||||
|
||||
@@ -55,13 +59,10 @@ module.exports = class CommentsService {
|
||||
* @param {Object} comment the comment to get the last status of
|
||||
*/
|
||||
static lastUnmoderatedStatus(comment) {
|
||||
const UNMODERATED_STATUSES = [
|
||||
'NONE',
|
||||
'PREMOD',
|
||||
];
|
||||
const UNMODERATED_STATUSES = ['NONE', 'PREMOD'];
|
||||
|
||||
for (let i = comment.status_history.length - 1; i >= 0; i--) {
|
||||
const {type} = comment.status_history[i];
|
||||
const { type } = comment.status_history[i];
|
||||
|
||||
if (UNMODERATED_STATUSES.includes(type)) {
|
||||
return type;
|
||||
@@ -77,7 +78,7 @@ module.exports = class CommentsService {
|
||||
* @param {String} body the new Comment body
|
||||
* @param {String} status the new Comment status
|
||||
*/
|
||||
static async edit({id, author_id, body, status}) {
|
||||
static async edit({ id, author_id, body, status }) {
|
||||
const EDITABLE_STATUSES = ['NONE', 'PREMOD', 'ACCEPTED'];
|
||||
|
||||
const query = {
|
||||
@@ -90,7 +91,9 @@ module.exports = class CommentsService {
|
||||
|
||||
// Establish the edit window (if it exists) and add the condition to the
|
||||
// original query.
|
||||
const {editCommentWindowLength: editWindowMs} = await SettingsService.retrieve();
|
||||
const {
|
||||
editCommentWindowLength: editWindowMs,
|
||||
} = await SettingsService.retrieve();
|
||||
const lastEditableCommentCreatedAt = new Date(Date.now() - editWindowMs);
|
||||
query.created_at = {
|
||||
$gt: lastEditableCommentCreatedAt,
|
||||
@@ -109,12 +112,11 @@ module.exports = class CommentsService {
|
||||
status_history: {
|
||||
type: status,
|
||||
created_at: new Date(),
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (originalComment == null) {
|
||||
|
||||
// Try to get the comment.
|
||||
const comment = await CommentsService.findById(id);
|
||||
if (comment == null) {
|
||||
@@ -124,13 +126,17 @@ module.exports = class CommentsService {
|
||||
|
||||
// Check to see if the user was't allowed to edit it.
|
||||
if (comment.author_id !== author_id) {
|
||||
debug('rejecting comment edit because author id does not match editing user');
|
||||
debug(
|
||||
'rejecting comment edit because author id does not match editing user'
|
||||
);
|
||||
throw errors.ErrNotAuthorized;
|
||||
}
|
||||
|
||||
// Check to see if the comment had a status that was editable.
|
||||
if (!EDITABLE_STATUSES.includes(comment.status)) {
|
||||
debug('rejecting comment edit because original comment has a non-editable status');
|
||||
debug(
|
||||
'rejecting comment edit because original comment has a non-editable status'
|
||||
);
|
||||
throw errors.ErrNotAuthorized;
|
||||
}
|
||||
|
||||
@@ -160,29 +166,32 @@ module.exports = class CommentsService {
|
||||
// previously, we should mark the comment as 'NONE' or 'PREMOD', which ever
|
||||
// was most recent if the new comment is destined to be `NONE` or `PREMOD`.
|
||||
if (originalComment.status === 'ACCEPTED' && status === 'NONE') {
|
||||
|
||||
const lastUnmoderatedStatus = CommentsService.lastUnmoderatedStatus(originalComment);
|
||||
const lastUnmoderatedStatus = CommentsService.lastUnmoderatedStatus(
|
||||
originalComment
|
||||
);
|
||||
|
||||
// If the last moderated status was found and the current comment doesn't
|
||||
// match this already.
|
||||
if (lastUnmoderatedStatus && status !== lastUnmoderatedStatus) {
|
||||
|
||||
// Update the comment model (if at this point, the status is still
|
||||
// accepted) with the previously unmoderated status
|
||||
await CommentModel.update({
|
||||
id,
|
||||
status,
|
||||
}, {
|
||||
$set: {
|
||||
status: lastUnmoderatedStatus,
|
||||
await CommentModel.update(
|
||||
{
|
||||
id,
|
||||
status,
|
||||
},
|
||||
$push: {
|
||||
status_history: {
|
||||
type: lastUnmoderatedStatus,
|
||||
created_at: new Date(),
|
||||
}
|
||||
},
|
||||
});
|
||||
{
|
||||
$set: {
|
||||
status: lastUnmoderatedStatus,
|
||||
},
|
||||
$push: {
|
||||
status_history: {
|
||||
type: lastUnmoderatedStatus,
|
||||
created_at: new Date(),
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// Update the returned comment.
|
||||
editedComment.status = lastUnmoderatedStatus;
|
||||
@@ -204,7 +213,7 @@ module.exports = class CommentsService {
|
||||
* @return {Promise}
|
||||
*/
|
||||
static findById(id) {
|
||||
return CommentModel.findOne({id});
|
||||
return CommentModel.findOne({ id });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +223,7 @@ module.exports = class CommentsService {
|
||||
*/
|
||||
static findByAssetId(asset_id) {
|
||||
return CommentModel.find({
|
||||
asset_id
|
||||
asset_id,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -230,8 +239,8 @@ module.exports = class CommentsService {
|
||||
return CommentModel.find({
|
||||
asset_id,
|
||||
status: {
|
||||
$in: statuses
|
||||
}
|
||||
$in: statuses,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -241,13 +250,16 @@ module.exports = class CommentsService {
|
||||
* @return {Promise}
|
||||
*/
|
||||
static findByActionType(action_type) {
|
||||
return ActionsService
|
||||
.findCommentsIdByActionType(action_type, 'COMMENTS')
|
||||
.then((actions) => CommentModel.find({
|
||||
return ActionsService.findCommentsIdByActionType(
|
||||
action_type,
|
||||
'COMMENTS'
|
||||
).then(actions =>
|
||||
CommentModel.find({
|
||||
id: {
|
||||
$in: actions.map((a) => a.item_id)
|
||||
}
|
||||
}));
|
||||
$in: actions.map(a => a.item_id),
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,9 +268,10 @@ module.exports = class CommentsService {
|
||||
* @return {Promise}
|
||||
*/
|
||||
static findIdsByActionType(action_type) {
|
||||
return ActionsService
|
||||
.findCommentsIdByActionType(action_type, 'COMMENTS')
|
||||
.then((actions) => actions.map((a) => a.item_id));
|
||||
return ActionsService.findCommentsIdByActionType(
|
||||
action_type,
|
||||
'COMMENTS'
|
||||
).then(actions => actions.map(a => a.item_id));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,7 +280,7 @@ module.exports = class CommentsService {
|
||||
* @return {Promise} resovles to comment array
|
||||
*/
|
||||
static findByStatus(status = 'NONE') {
|
||||
return CommentModel.find({status});
|
||||
return CommentModel.find({ status });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -280,16 +293,19 @@ module.exports = class CommentsService {
|
||||
*/
|
||||
static async pushStatus(id, status, assigned_by = null) {
|
||||
const created_at = new Date();
|
||||
const originalComment = await CommentModel.findOneAndUpdate({id}, {
|
||||
$push: {
|
||||
status_history: {
|
||||
type: status,
|
||||
created_at,
|
||||
assigned_by,
|
||||
}
|
||||
},
|
||||
$set: {status}
|
||||
});
|
||||
const originalComment = await CommentModel.findOneAndUpdate(
|
||||
{ id },
|
||||
{
|
||||
$push: {
|
||||
status_history: {
|
||||
type: status,
|
||||
created_at,
|
||||
assigned_by,
|
||||
},
|
||||
},
|
||||
$set: { status },
|
||||
}
|
||||
);
|
||||
|
||||
if (originalComment == null) {
|
||||
throw errors.ErrNotFound;
|
||||
@@ -323,7 +339,7 @@ module.exports = class CommentsService {
|
||||
item_type: 'COMMENTS',
|
||||
user_id,
|
||||
action_type,
|
||||
metadata
|
||||
metadata,
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -334,21 +350,24 @@ module.exports = class CommentsService {
|
||||
|
||||
const incrReplyCount = async (comment, value) => {
|
||||
try {
|
||||
await CommentModel.update({
|
||||
id: comment.parent_id,
|
||||
}, {
|
||||
$inc: {
|
||||
reply_count: value,
|
||||
await CommentModel.update(
|
||||
{
|
||||
id: comment.parent_id,
|
||||
},
|
||||
});
|
||||
{
|
||||
$inc: {
|
||||
reply_count: value,
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Can\'t mutate the reply count:', err);
|
||||
console.error("Can't mutate the reply count:", err);
|
||||
}
|
||||
};
|
||||
|
||||
// When a comment is created, if it is a reply, increment the reply count on the
|
||||
// parent's document.
|
||||
events.on(COMMENTS_NEW, async (comment) => {
|
||||
events.on(COMMENTS_NEW, async comment => {
|
||||
if (
|
||||
!comment || // Check that the comment is defined.
|
||||
(!comment.parent_id || comment.parent_id.length === 0) || // Check that the comment has a parent (is a reply).
|
||||
|
||||
Reference in New Issue
Block a user