mirror of
https://github.com/wassname/talk.git
synced 2026-07-26 13:37:38 +08:00
Moved tag creation into mutator
This commit is contained in:
@@ -36,6 +36,12 @@ const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id =
|
||||
}
|
||||
}
|
||||
|
||||
if (user.hasRoles('ADMIN')) {
|
||||
return CommentsService
|
||||
.addTag(comment.id, 'STAFF', user.id)
|
||||
.then(() => comment);
|
||||
}
|
||||
|
||||
return comment;
|
||||
});
|
||||
};
|
||||
|
||||
+42
-18
@@ -3,7 +3,9 @@ const CommentModel = require('../models/comment');
|
||||
const ActionModel = require('../models/action');
|
||||
const ActionsService = require('./actions');
|
||||
|
||||
const UsersService = require('./users');
|
||||
const ALLOWED_TAGS = [
|
||||
{name: 'STAFF'}
|
||||
];
|
||||
|
||||
module.exports = class CommentsService {
|
||||
|
||||
@@ -27,24 +29,46 @@ module.exports = class CommentsService {
|
||||
author_id
|
||||
} = comment;
|
||||
|
||||
return UsersService.isStaff(author_id).then((isStaff) => {
|
||||
comment = new CommentModel({
|
||||
body,
|
||||
asset_id,
|
||||
parent_id,
|
||||
status_history: status ? [{
|
||||
type: status,
|
||||
comment = new CommentModel({
|
||||
body,
|
||||
asset_id,
|
||||
parent_id,
|
||||
status_history: status ? [{
|
||||
type: status,
|
||||
created_at: new Date()
|
||||
}] : [],
|
||||
tags: [],
|
||||
status,
|
||||
author_id
|
||||
});
|
||||
|
||||
return comment.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a tag if it doesn't already exist on the comment.
|
||||
*/
|
||||
static addTag(id, name, assigned_by) {
|
||||
|
||||
if (ALLOWED_TAGS.find((t) => t.name === name) == null) {
|
||||
return Promise.reject(new Error('tag not allowed'));
|
||||
}
|
||||
|
||||
return CommentModel.update({
|
||||
id,
|
||||
tags: {
|
||||
$ne: {
|
||||
name
|
||||
}
|
||||
}
|
||||
}, {
|
||||
$push: {
|
||||
tags: {
|
||||
name,
|
||||
assigned_by,
|
||||
created_at: new Date()
|
||||
}] : [],
|
||||
status,
|
||||
tags: isStaff ? [{
|
||||
name: 'STAFF',
|
||||
assigned_by: null,
|
||||
created_at: new Date()
|
||||
}] : [],
|
||||
author_id
|
||||
});
|
||||
return comment.save();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -687,18 +687,4 @@ module.exports = class UsersService {
|
||||
Promise.reject(new Error('You do not have permission to update your username.'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the user is staff.
|
||||
* @param {String} id the id of the user to be enabled.
|
||||
* @return {Promise}
|
||||
*/
|
||||
static isStaff(id) {
|
||||
return UsersService.findById(id).then((user) => {
|
||||
if (user) {
|
||||
return user.hasRoles('ADMIN');
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user