mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 23:39:20 +08:00
initial pagination rewrite, cleanups to comment edit
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
"unexpectedError": "Unexpected error while saving changes. Sorry!"
|
||||
},
|
||||
"error": {
|
||||
"editWindowExpired": "You can no longer edit this comment. The time window to do so has expired.",
|
||||
"EDIT_WINDOW_ENDED": "You can no longer edit this comment. The time window to do so has expired.",
|
||||
"emailNotVerified": "Email address {0} not verified.",
|
||||
"email": "Not a valid E-Mail",
|
||||
"networkError": "Failed to connect to server. Check your internet connection and try again.",
|
||||
|
||||
@@ -112,10 +112,12 @@ const ErrContainsProfanity = new APIError('This username contains elements which
|
||||
});
|
||||
|
||||
const ErrNotFound = new APIError('not found', {
|
||||
translation_key: 'NOT_FOUND',
|
||||
status: 404
|
||||
});
|
||||
|
||||
const ErrInvalidAssetURL = new APIError('asset_url is invalid', {
|
||||
translation_key: 'INVALID_ASSET_URL',
|
||||
status: 400
|
||||
});
|
||||
|
||||
@@ -148,16 +150,17 @@ const ErrPermissionUpdateUsername = new APIError('You do not have permission to
|
||||
status: 500
|
||||
});
|
||||
|
||||
// ErrLoginAttemptMaximumExceeded is returned when the login maximum is exceeded.
|
||||
const ErrLoginAttemptMaximumExceeded = new APIError('You have made too many incorrect password attempts.', {
|
||||
translation_key: 'LOGIN_MAXIMUM_EXCEEDED',
|
||||
status: 429
|
||||
});
|
||||
|
||||
class ErrEditWindowHasEnded extends APIError {
|
||||
constructor(message) {
|
||||
super(message || 'Edit window is over.', {status: 403, translation_key: 'error.editWindowExpired'});
|
||||
}
|
||||
}
|
||||
// ErrEditWindowHasEnded is returned when the edit window has expired.
|
||||
const ErrEditWindowHasEnded = new APIError('Edit window is over', {
|
||||
translation_key: 'EDIT_WINDOW_ENDED',
|
||||
status: 403
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
ExtendableError,
|
||||
|
||||
+14
-17
@@ -25,34 +25,31 @@ const genAssetsByID = (context, ids) => AssetModel.find({
|
||||
* @param {String} asset_url the url passed in from the query
|
||||
* @returns {Promise} resolves to the asset
|
||||
*/
|
||||
const findOrCreateAssetByURL = (context, asset_url) => {
|
||||
const findOrCreateAssetByURL = async (context, asset_url) => {
|
||||
|
||||
// Verify that the asset_url is parsable.
|
||||
let parsed_asset_url = url.parse(asset_url);
|
||||
if (!parsed_asset_url.protocol) {
|
||||
return Promise.reject(errors.ErrInvalidAssetURL);
|
||||
throw errors.ErrInvalidAssetURL;
|
||||
}
|
||||
|
||||
return AssetsService.findOrCreateByUrl(asset_url)
|
||||
.then((asset) => {
|
||||
let asset = await AssetsService.findOrCreateByUrl(asset_url);
|
||||
|
||||
// If the asset wasn't scraped before, scrape it! Otherwise just return
|
||||
// the asset.
|
||||
if (!asset.scraped) {
|
||||
return scraper.create(asset).then(() => asset);
|
||||
}
|
||||
// If the asset wasn't scraped before, scrape it! Otherwise just return
|
||||
// the asset.
|
||||
if (!asset.scraped) {
|
||||
await scraper.create(asset);
|
||||
}
|
||||
|
||||
return asset;
|
||||
});
|
||||
return asset;
|
||||
};
|
||||
|
||||
const getAssetsForMetrics = ({loaders: {Actions, Comments}}) => {
|
||||
return Actions.getByTypes({action_type: 'FLAG', item_type: 'COMMENT'})
|
||||
.then((actions) => { // ALL ACTIONS :O
|
||||
const ids = actions.map(({item_id}) => item_id);
|
||||
const getAssetsForMetrics = async ({loaders: {Actions, Comments}}) => {
|
||||
let actions = await Actions.getByTypes({action_type: 'FLAG', item_type: 'COMMENT'});
|
||||
|
||||
return Comments.getByQuery({ids});
|
||||
});
|
||||
const ids = actions.map(({item_id}) => item_id);
|
||||
|
||||
return Comments.getByQuery({ids});
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -120,8 +120,8 @@ const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgn
|
||||
const ignoredUsers = freshUser.ignoresUsers;
|
||||
query.author_id = {$nin: ignoredUsers};
|
||||
}
|
||||
const count = await CommentModel.where(query).count();
|
||||
return count;
|
||||
|
||||
return CommentModel.where(query).count();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -263,13 +263,9 @@ const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, a
|
||||
comments = comments.where({parent_id});
|
||||
}
|
||||
|
||||
if (excludeIgnored && user) {
|
||||
|
||||
// load afresh, as `user` may be from cache and not have recent ignores
|
||||
const freshUser = await UsersService.findById(user.id);
|
||||
const ignoredUsers = freshUser.ignoresUsers;
|
||||
if (excludeIgnored && user && user.ignoresUsers) {
|
||||
comments = comments.where({
|
||||
author_id: {$nin: ignoredUsers}
|
||||
author_id: {$nin: user.ignoresUsers}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -294,6 +290,27 @@ const getCommentsByQuery = async ({user}, {ids, statuses, asset_id, parent_id, a
|
||||
.limit(limit);
|
||||
};
|
||||
|
||||
const getCommentsConnection = async ({user}, query) => {
|
||||
let {limit} = query;
|
||||
|
||||
// Increate the limit by one.
|
||||
query.limit++;
|
||||
|
||||
let comments = await getCommentsByQuery({user}, query);
|
||||
|
||||
if (!comments) {
|
||||
comments = [];
|
||||
}
|
||||
|
||||
return {
|
||||
edges: comments.slice(0, limit),
|
||||
pageInfo: {
|
||||
hasNextPage: Boolean(comments.length > limit),
|
||||
cursor: comments.length > 0 ? comments[comments.length - 1].created_at : null
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets the recent replies.
|
||||
* @param {Object} context graph context
|
||||
@@ -431,6 +448,7 @@ module.exports = (context) => ({
|
||||
Comments: {
|
||||
get: new DataLoader((ids) => genComments(context, ids)),
|
||||
getByQuery: (query) => getCommentsByQuery(context, query),
|
||||
getConnection: (query) => getCommentsConnection(context, query),
|
||||
getCountByQuery: (query) => getCommentCountByQuery(context, query),
|
||||
countByAssetID: new SharedCounterDataLoader('Comments.totalCommentCount', 3600, (ids) => getCountsByAssetID(context, ids)),
|
||||
countByAssetIDPersonalized: (query) => getCountsByAssetIDPersonalized(context, query),
|
||||
|
||||
+10
-10
@@ -12,23 +12,23 @@ const errors = require('../../errors');
|
||||
* @param {String} action_type type of the action
|
||||
* @return {Promise} resolves to the action created
|
||||
*/
|
||||
const createAction = ({user = {}}, {item_id, item_type, action_type, group_id, metadata = {}}) => {
|
||||
return ActionsService.insertUserAction({
|
||||
const createAction = async ({user = {}}, {item_id, item_type, action_type, group_id, metadata = {}}) => {
|
||||
let action = await ActionsService.insertUserAction({
|
||||
item_id,
|
||||
item_type,
|
||||
user_id: user.id,
|
||||
group_id,
|
||||
action_type,
|
||||
metadata
|
||||
}).then((action) => {
|
||||
if (item_type === 'USERS' && action_type === 'FLAG') {
|
||||
return UsersService
|
||||
.setStatus(item_id, 'PENDING')
|
||||
.then(() => action);
|
||||
}
|
||||
|
||||
return action;
|
||||
});
|
||||
|
||||
if (item_type === 'USERS' && action_type === 'FLAG') {
|
||||
|
||||
// Set the user as pending if it was a user flag.
|
||||
await UsersService.setStatus(item_id, 'PENDING');
|
||||
}
|
||||
|
||||
return action;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+101
-132
@@ -16,7 +16,7 @@ const Wordlist = require('../../services/wordlist');
|
||||
* @param {String} [status='NONE'] the status of the new comment
|
||||
* @return {Promise} resolves to the created comment
|
||||
*/
|
||||
const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, parent_id = null, tags = []}, status = 'NONE') => {
|
||||
const createComment = async ({user, loaders: {Comments}, pubsub}, {body, asset_id, parent_id = null, tags = []}, status = 'NONE') => {
|
||||
|
||||
// Building array of tags
|
||||
tags = tags.map(tag => ({name: tag}));
|
||||
@@ -26,37 +26,35 @@ const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, par
|
||||
tags.push({name: 'STAFF'});
|
||||
}
|
||||
|
||||
return CommentsService.publicCreate({
|
||||
let comment = await CommentsService.publicCreate({
|
||||
body,
|
||||
asset_id,
|
||||
parent_id,
|
||||
status,
|
||||
tags,
|
||||
author_id: user.id
|
||||
})
|
||||
.then((comment) => {
|
||||
|
||||
// If the loaders are present, clear the caches for these values because we
|
||||
// just added a new comment, hence the counts should be updated. We should
|
||||
// perform these increments in the event that we do have a new comment that
|
||||
// is approved or without a comment.
|
||||
if (status === 'NONE' || status === 'APPROVED') {
|
||||
if (parent_id != null) {
|
||||
Comments.countByParentID.incr(parent_id);
|
||||
} else {
|
||||
Comments.parentCountByAssetID.incr(asset_id);
|
||||
}
|
||||
Comments.countByAssetID.incr(asset_id);
|
||||
|
||||
if (pubsub) {
|
||||
|
||||
// Publish the newly added comment via the subscription.
|
||||
pubsub.publish('commentAdded', comment);
|
||||
}
|
||||
}
|
||||
|
||||
return comment;
|
||||
});
|
||||
|
||||
// If the loaders are present, clear the caches for these values because we
|
||||
// just added a new comment, hence the counts should be updated. We should
|
||||
// perform these increments in the event that we do have a new comment that
|
||||
// is approved or without a comment.
|
||||
if (status === 'NONE' || status === 'APPROVED') {
|
||||
if (parent_id != null) {
|
||||
Comments.countByParentID.incr(parent_id);
|
||||
} else {
|
||||
Comments.parentCountByAssetID.incr(asset_id);
|
||||
}
|
||||
Comments.countByAssetID.incr(asset_id);
|
||||
|
||||
if (pubsub) {
|
||||
|
||||
// Publish the newly added comment via the subscription.
|
||||
pubsub.publish('commentAdded', comment);
|
||||
}
|
||||
}
|
||||
|
||||
return comment;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -65,7 +63,7 @@ const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, par
|
||||
* @param {String} [asset_id] id of asset comment is posted on
|
||||
* @return {Object} resolves to the wordlist results
|
||||
*/
|
||||
const filterNewComment = ({body, asset_id}) => {
|
||||
const filterNewComment = (context, {body, asset_id}) => {
|
||||
|
||||
// Create a new instance of the Wordlist.
|
||||
const wl = new Wordlist();
|
||||
@@ -85,50 +83,40 @@ const filterNewComment = ({body, asset_id}) => {
|
||||
* @param {Object} [wordlist={}] the results of the wordlist scan
|
||||
* @return {Promise} resolves to the comment's status
|
||||
*/
|
||||
const resolveNewCommentStatus = ({asset_id, body}, wordlist = {}, settings = {}) => {
|
||||
const resolveNewCommentStatus = async (context, {asset_id, body}, wordlist = {}, settings = {}) => {
|
||||
|
||||
// Decide the status based on whether or not the current asset/settings
|
||||
// has pre-mod enabled or not. If the comment was rejected based on the
|
||||
// wordlist, then reject it, otherwise if the moderation setting is
|
||||
// premod, set it to `premod`.
|
||||
let status;
|
||||
|
||||
if (wordlist.banned) {
|
||||
status = Promise.resolve('REJECTED');
|
||||
} else if (settings.premodLinksEnable && linkify.test(body)) {
|
||||
status = Promise.resolve('PREMOD');
|
||||
} else if (asset_id) {
|
||||
status = AssetsService
|
||||
.rectifySettings(AssetsService.findById(asset_id).then((asset) => {
|
||||
if (!asset) {
|
||||
return Promise.reject(errors.ErrNotFound);
|
||||
}
|
||||
|
||||
// Check to see if the asset has closed commenting...
|
||||
if (asset.isClosed) {
|
||||
|
||||
// They have, ensure that we send back an error.
|
||||
return Promise.reject(new errors.ErrAssetCommentingClosed(asset.closedMessage));
|
||||
}
|
||||
|
||||
return asset;
|
||||
}))
|
||||
|
||||
// Return `premod` if pre-moderation is enabled and an empty "new" status
|
||||
// in the event that it is not in pre-moderation mode.
|
||||
.then(({moderation, charCountEnable, charCount}) => {
|
||||
|
||||
// Reject if the comment is too long
|
||||
if (charCountEnable && body.length > charCount) {
|
||||
return 'REJECTED';
|
||||
}
|
||||
return moderation === 'PRE' ? 'PREMOD' : 'NONE';
|
||||
});
|
||||
} else {
|
||||
status = 'NONE';
|
||||
return 'REJECTED';
|
||||
}
|
||||
|
||||
if (settings.premodLinksEnable && linkify.test(body)) {
|
||||
return 'PREMOD';
|
||||
}
|
||||
|
||||
return status;
|
||||
let asset = await AssetsService.findById(asset_id);
|
||||
if (!asset) {
|
||||
throw errors.ErrNotFound;
|
||||
}
|
||||
|
||||
// Check to see if the asset has closed commenting...
|
||||
if (asset.isClosed) {
|
||||
throw new errors.ErrAssetCommentingClosed(asset.closedMessage);
|
||||
}
|
||||
|
||||
// Return `premod` if pre-moderation is enabled and an empty "new" status
|
||||
// in the event that it is not in pre-moderation mode.
|
||||
let {moderation, charCountEnable, charCount} = await AssetsService.rectifySettings(asset);
|
||||
|
||||
// Reject if the comment is too long
|
||||
if (charCountEnable && body.length > charCount) {
|
||||
return 'REJECTED';
|
||||
}
|
||||
|
||||
return moderation === 'PRE' ? 'PREMOD' : 'NONE';
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -139,45 +127,42 @@ const resolveNewCommentStatus = ({asset_id, body}, wordlist = {}, settings = {})
|
||||
* @param {Object} commentInput the new comment to be created
|
||||
* @return {Promise} resolves to a new comment
|
||||
*/
|
||||
const createPublicComment = (context, commentInput) => {
|
||||
const createPublicComment = async (context, commentInput) => {
|
||||
|
||||
// First we filter the comment contents to ensure that we note any validation
|
||||
// issues.
|
||||
return filterNewComment(commentInput)
|
||||
let [wordlist, settings] = await filterNewComment(context, commentInput);
|
||||
|
||||
// We then take the wordlist and the comment into consideration when
|
||||
// considering what status to assign the new comment, and resolve the new
|
||||
// status to set the comment to.
|
||||
.then(([wordlist, settings]) => resolveNewCommentStatus(commentInput, wordlist, settings)
|
||||
// We then take the wordlist and the comment into consideration when
|
||||
// considering what status to assign the new comment, and resolve the new
|
||||
// status to set the comment to.
|
||||
let status = await resolveNewCommentStatus(context, commentInput, wordlist, settings);
|
||||
|
||||
// Then we actually create the comment with the new status.
|
||||
.then((status) => createComment(context, commentInput, status))
|
||||
.then((comment) => {
|
||||
// Then we actually create the comment with the new status.
|
||||
let comment = await createComment(context, commentInput, status);
|
||||
|
||||
// If the comment has a suspect word or a link, we need to add a
|
||||
// flag to it to indicate that it needs to be looked at.
|
||||
// Otherwise just return the new comment.
|
||||
// If the comment has a suspect word or a link, we need to add a
|
||||
// flag to it to indicate that it needs to be looked at.
|
||||
// Otherwise just return the new comment.
|
||||
|
||||
// TODO: Check why the wordlist is undefined
|
||||
if (wordlist != null && wordlist.suspect != null) {
|
||||
// TODO: Check why the wordlist is undefined
|
||||
if (wordlist != null && wordlist.suspect != null) {
|
||||
|
||||
// TODO: this is kind of fragile, we should refactor this to resolve
|
||||
// all these const's that we're using like 'COMMENTS', 'FLAG' to be
|
||||
// defined in a checkable schema.
|
||||
return ActionsService.insertUserAction({
|
||||
item_id: comment.id,
|
||||
item_type: 'COMMENTS',
|
||||
action_type: 'FLAG',
|
||||
user_id: null,
|
||||
group_id: 'Matched suspect word filter',
|
||||
metadata: {}
|
||||
})
|
||||
.then(() => comment);
|
||||
}
|
||||
// TODO: this is kind of fragile, we should refactor this to resolve
|
||||
// all these const's that we're using like 'COMMENTS', 'FLAG' to be
|
||||
// defined in a checkable schema.
|
||||
await ActionsService.insertUserAction({
|
||||
item_id: comment.id,
|
||||
item_type: 'COMMENTS',
|
||||
action_type: 'FLAG',
|
||||
user_id: null,
|
||||
group_id: 'Matched suspect word filter',
|
||||
metadata: {}
|
||||
});
|
||||
}
|
||||
|
||||
// Finally, we return the comment.
|
||||
return comment;
|
||||
}));
|
||||
// Finally, we return the comment.
|
||||
return comment;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -187,26 +172,23 @@ const createPublicComment = (context, commentInput) => {
|
||||
* @param {String} status the new status of the comment
|
||||
*/
|
||||
|
||||
const setCommentStatus = ({user, loaders: {Comments}}, {id, status}) => {
|
||||
return CommentsService
|
||||
.pushStatus(id, status, user ? user.id : null)
|
||||
.then((comment) => {
|
||||
const setCommentStatus = async ({user, loaders: {Comments}}, {id, status}) => {
|
||||
let comment = await CommentsService.pushStatus(id, status, user ? user.id : null);
|
||||
|
||||
// If the loaders are present, clear the caches for these values because we
|
||||
// just added a new comment, hence the counts should be updated. It would
|
||||
// be nice if we could decrement the counters here, but that would result
|
||||
// in us having to know the initial state of the comment, which would
|
||||
// require another database query.
|
||||
if (comment.parent_id != null) {
|
||||
Comments.countByParentID.clear(comment.parent_id);
|
||||
} else {
|
||||
Comments.parentCountByAssetID.clear(comment.asset_id);
|
||||
}
|
||||
// If the loaders are present, clear the caches for these values because we
|
||||
// just added a new comment, hence the counts should be updated. It would
|
||||
// be nice if we could decrement the counters here, but that would result
|
||||
// in us having to know the initial state of the comment, which would
|
||||
// require another database query.
|
||||
if (comment.parent_id != null) {
|
||||
Comments.countByParentID.clear(comment.parent_id);
|
||||
} else {
|
||||
Comments.parentCountByAssetID.clear(comment.asset_id);
|
||||
}
|
||||
|
||||
Comments.countByAssetID.clear(comment.asset_id);
|
||||
Comments.countByAssetID.clear(comment.asset_id);
|
||||
|
||||
return comment;
|
||||
});
|
||||
return comment;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -233,29 +215,16 @@ const removeCommentTag = ({user, loaders: {Comments}}, {id, tag}) => {
|
||||
* @param {Object} edit describes how to edit the comment
|
||||
* @param {String} edit.body the new Comment body
|
||||
*/
|
||||
const editComment = async ({user, loaders: {Comments}}, {id, asset_id, edit}) => {
|
||||
const {body} = edit;
|
||||
const determineStatusForComment = async ({body, asset_id}) => {
|
||||
const [wordlist, settings] = await filterNewComment({asset_id, body});
|
||||
const status = await resolveNewCommentStatus({asset_id, body}, wordlist, settings);
|
||||
return status;
|
||||
};
|
||||
const status = await determineStatusForComment({body, asset_id});
|
||||
try {
|
||||
await CommentsService.edit(id, asset_id, user.id, Object.assign({status}, edit));
|
||||
} catch (error) {
|
||||
switch (error.name) {
|
||||
case 'CommentNotFound':
|
||||
throw new errors.APIError('Comment not found', {
|
||||
status: 404,
|
||||
translation_key: 'NOT_FOUND',
|
||||
});
|
||||
case 'NotAuthorizedToEdit':
|
||||
throw errors.ErrNotAuthorized;
|
||||
default:
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const editComment = async (context, {id, asset_id, edit: {body}}) => {
|
||||
|
||||
// Get the wordlist and the settings object.
|
||||
const [wordlist, settings] = await filterNewComment(context, {asset_id, body});
|
||||
|
||||
// Determine the new status of the comment.
|
||||
const status = await resolveNewCommentStatus(context, {asset_id, body}, wordlist, settings);
|
||||
|
||||
await CommentsService.edit(id, context.user.id, {body, status});
|
||||
|
||||
return {status};
|
||||
};
|
||||
|
||||
|
||||
+6
-10
@@ -2,23 +2,19 @@ const errors = require('../../errors');
|
||||
const UsersService = require('../../services/users');
|
||||
|
||||
const setUserStatus = ({user}, {id, status}) => {
|
||||
return UsersService.setStatus(id, status)
|
||||
.then(res => res);
|
||||
return UsersService.setStatus(id, status);
|
||||
};
|
||||
|
||||
const suspendUser = ({user}, {id, message}) => {
|
||||
return UsersService.suspendUser(id, message)
|
||||
.then(res => {
|
||||
return res;
|
||||
});
|
||||
return UsersService.suspendUser(id, message);
|
||||
};
|
||||
|
||||
const ignoreUser = async ({user}, userToIgnore) => {
|
||||
return await UsersService.ignoreUsers(user.id, [userToIgnore.id]);
|
||||
const ignoreUser = ({user}, userToIgnore) => {
|
||||
return UsersService.ignoreUsers(user.id, [userToIgnore.id]);
|
||||
};
|
||||
|
||||
const stopIgnoringUser = async ({user}, userToStopIgnoring) => {
|
||||
return await UsersService.stopIgnoringUsers(user.id, [userToStopIgnoring.id]);
|
||||
const stopIgnoringUser = ({user}, userToStopIgnoring) => {
|
||||
return UsersService.stopIgnoringUsers(user.id, [userToStopIgnoring.id]);
|
||||
};
|
||||
|
||||
module.exports = (context) => {
|
||||
|
||||
@@ -10,7 +10,7 @@ const Asset = {
|
||||
return Comments.genRecentComments.load(id);
|
||||
},
|
||||
comments({id}, {sort, limit, excludeIgnored}, {loaders: {Comments}}) {
|
||||
return Comments.getByQuery({
|
||||
return Comments.getConnection({
|
||||
asset_id: id,
|
||||
sort,
|
||||
limit,
|
||||
|
||||
+28
-15
@@ -1,3 +1,12 @@
|
||||
################################################################################
|
||||
## Pagination
|
||||
################################################################################
|
||||
|
||||
type PageInfo {
|
||||
hasNextPage: Boolean!
|
||||
cursor: String
|
||||
}
|
||||
|
||||
################################################################################
|
||||
## Custom Scalar Types
|
||||
################################################################################
|
||||
@@ -37,16 +46,13 @@ type User {
|
||||
actions: [Action]
|
||||
|
||||
# the current roles of the user.
|
||||
roles: [USER_ROLES]
|
||||
roles: [USER_ROLES!]
|
||||
|
||||
# determines whether the user can edit their username
|
||||
canEditName: Boolean
|
||||
|
||||
# returns all comments based on a query.
|
||||
comments(query: CommentsQuery): [Comment]
|
||||
|
||||
# returns all users based on a query.
|
||||
users(query: UsersQuery): [User]
|
||||
comments(query: CommentsQuery): [Comment!]
|
||||
|
||||
# returns user status
|
||||
status: USER_STATUS
|
||||
@@ -81,6 +87,12 @@ input UsersQuery {
|
||||
## Comments
|
||||
################################################################################
|
||||
|
||||
# CommentConnection provides Comments with PageInfo.
|
||||
type CommentConnection {
|
||||
edges: [Comment!]
|
||||
pageInfo: PageInfo!
|
||||
}
|
||||
|
||||
# The statuses that a comment may have.
|
||||
enum COMMENT_STATUS {
|
||||
|
||||
@@ -190,7 +202,7 @@ type Comment {
|
||||
recentReplies: [Comment]
|
||||
|
||||
# the replies that were made to the comment.
|
||||
replies(sort: SORT_ORDER = CHRONOLOGICAL, limit: Int = 3, excludeIgnored: Boolean): [Comment]
|
||||
replies(sort: SORT_ORDER = CHRONOLOGICAL, limit: Int = 3, excludeIgnored: Boolean): CommentConnection
|
||||
|
||||
# The count of replies on a comment.
|
||||
replyCount(excludeIgnored: Boolean): Int
|
||||
@@ -429,7 +441,7 @@ type Asset {
|
||||
recentComments: [Comment]
|
||||
|
||||
# The top level comments that are attached to the asset.
|
||||
comments(sort: SORT_ORDER = REVERSE_CHRONOLOGICAL, limit: Int = 10, excludeIgnored: Boolean): [Comment]
|
||||
comments(sort: SORT_ORDER = REVERSE_CHRONOLOGICAL, limit: Int = 10, excludeIgnored: Boolean): CommentConnection
|
||||
|
||||
# The count of top level comments on the asset.
|
||||
commentCount(excludeIgnored: Boolean): Int
|
||||
@@ -536,7 +548,7 @@ type RootQuery {
|
||||
asset(id: ID, url: String): Asset
|
||||
|
||||
# Comments returned based on a query.
|
||||
comments(query: CommentsQuery!): [Comment]
|
||||
comments(query: CommentsQuery!): CommentConnection!
|
||||
|
||||
# Return the count of comments satisfied by the query. Note that this edge is
|
||||
# expensive as it is not batched. Requires the `ADMIN` role.
|
||||
@@ -600,6 +612,7 @@ enum TAG_TYPE {
|
||||
STAFF
|
||||
}
|
||||
|
||||
# CreateCommentInput is the input content used to create a new comment.
|
||||
input CreateCommentInput {
|
||||
|
||||
# The asset id
|
||||
@@ -729,19 +742,19 @@ type StopIgnoringUserResponse implements Response {
|
||||
errors: [UserError]
|
||||
}
|
||||
|
||||
# Input to editComment mutation
|
||||
# Input to editComment mutation.
|
||||
input EditCommentInput {
|
||||
|
||||
# Update body of the comment
|
||||
body: String!
|
||||
}
|
||||
|
||||
type CommentInfoAfterEdit {
|
||||
# New status of the edited comment
|
||||
status: COMMENT_STATUS!
|
||||
}
|
||||
|
||||
# EditCommentResponse contains the updated comment and any errors that occured.
|
||||
type EditCommentResponse implements Response {
|
||||
comment: CommentInfoAfterEdit!
|
||||
|
||||
# The edited comment.
|
||||
comment: Comment
|
||||
|
||||
# An array of errors relating to the mutation that occured.
|
||||
errors: [UserError]
|
||||
}
|
||||
|
||||
+2
-4
@@ -85,7 +85,7 @@
|
||||
"marked": "^0.3.6",
|
||||
"metascraper": "^1.0.6",
|
||||
"minimist": "^1.2.0",
|
||||
"mongoose": "^4.9.1",
|
||||
"mongoose": "^4.9.8",
|
||||
"morgan": "^1.8.1",
|
||||
"natural": "^0.5.0",
|
||||
"node-emoji": "^1.5.1",
|
||||
@@ -99,12 +99,10 @@
|
||||
"react-recaptcha": "^2.2.6",
|
||||
"recompose": "^0.23.1",
|
||||
"redis": "^2.7.1",
|
||||
"uuid": "^3.0.1",
|
||||
"simplemde": "^1.11.2",
|
||||
"subscriptions-transport-ws": "^0.5.5-alpha.0",
|
||||
"resolve": "^1.3.2",
|
||||
"semver": "^5.3.0",
|
||||
"simplemde": "^1.11.2",
|
||||
"subscriptions-transport-ws": "^0.5.5-alpha.0",
|
||||
"timekeeper": "^1.0.0",
|
||||
"uuid": "^3.0.1"
|
||||
},
|
||||
|
||||
+57
-92
@@ -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: {
|
||||
|
||||
@@ -64,6 +64,9 @@ describe('graph.mutations.editComment', () => {
|
||||
console.error(response.errors);
|
||||
}
|
||||
expect(response.errors).to.be.empty;
|
||||
if (response.data.editComment.errors && response.data.editComment.errors.length > 0) {
|
||||
console.error(response.data.editComment.errors);
|
||||
}
|
||||
expect(response.data.editComment.errors).to.be.null;
|
||||
|
||||
// assert body has changed
|
||||
@@ -97,9 +100,12 @@ describe('graph.mutations.editComment', () => {
|
||||
body: newBody
|
||||
}
|
||||
});
|
||||
if (response.errors && response.errors.length > 0) {
|
||||
console.error(response.errors);
|
||||
}
|
||||
expect(response.errors).to.be.empty;
|
||||
expect(response.data.editComment.errors).to.not.be.empty;
|
||||
expect(response.data.editComment.errors[0].translation_key).to.equal('error.editWindowExpired');
|
||||
expect(response.data.editComment.errors[0].translation_key).to.equal('EDIT_WINDOW_ENDED');
|
||||
const commentAfterEdit = await CommentsService.findById(comment.id);
|
||||
|
||||
// it *hasn't* changed from the original
|
||||
@@ -144,6 +150,9 @@ describe('graph.mutations.editComment', () => {
|
||||
body: newBody
|
||||
}
|
||||
});
|
||||
if (response.errors && response.errors.length > 0) {
|
||||
console.error(response.errors);
|
||||
}
|
||||
expect(response.errors).to.be.empty;
|
||||
expect(response.data.editComment.errors[0].translation_key).to.equal('NOT_FOUND');
|
||||
});
|
||||
|
||||
@@ -9,85 +9,94 @@ const Asset = require('../../../../models/asset');
|
||||
const CommentsService = require('../../../../services/comments');
|
||||
|
||||
describe('graph.queries.asset', () => {
|
||||
let asset, users;
|
||||
beforeEach(async () => {
|
||||
await SettingsService.init();
|
||||
asset = await Asset.create({id: '1', url: 'https://example.com'});
|
||||
users = await UsersService.createLocalUsers([
|
||||
{
|
||||
email: 'usernameA@example.com',
|
||||
password: 'password',
|
||||
username: 'usernameA'
|
||||
},
|
||||
{
|
||||
email: 'usernameB@example.com',
|
||||
password: 'password',
|
||||
username: 'usernameB'
|
||||
},
|
||||
{
|
||||
email: 'usernameC@example.com',
|
||||
password: 'password',
|
||||
username: 'usernameC'
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it('can get comments edge', async () => {
|
||||
const assetId = 'fakeAssetId';
|
||||
const assetUrl = 'https://bengo.is';
|
||||
await Asset.create({id: assetId, url: assetUrl});
|
||||
|
||||
const user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
|
||||
const context = new Context({user});
|
||||
const context = new Context({user: users[0]});
|
||||
|
||||
await CommentsService.publicCreate([1, 2].map(() => ({
|
||||
author_id: user.id,
|
||||
asset_id: assetId,
|
||||
body: `hello there! ${ String(Math.random()).slice(2)}`,
|
||||
author_id: users[0].id,
|
||||
asset_id: asset.id,
|
||||
body: `hello there! ${String(Math.random()).slice(2)}`,
|
||||
})));
|
||||
|
||||
const assetCommentsQuery = `
|
||||
query assetCommentsQuery($assetId: ID!, $assetUrl: String!) {
|
||||
asset(id: $assetId, url: $assetUrl) {
|
||||
query assetCommentsQuery($id: ID!) {
|
||||
asset(id: $id) {
|
||||
comments(limit: 10) {
|
||||
id,
|
||||
body,
|
||||
edges {
|
||||
id
|
||||
body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
const assetCommentsResponse = await graphql(schema, assetCommentsQuery, {}, context, {assetId, assetUrl});
|
||||
const comments = assetCommentsResponse.data.asset.comments;
|
||||
const res = await graphql(schema, assetCommentsQuery, {}, context, {id: asset.id});
|
||||
expect(res.erros).is.empty;
|
||||
const comments = res.data.asset.comments.edges;
|
||||
expect(comments.length).to.equal(2);
|
||||
});
|
||||
|
||||
it('can query comments edge to exclude comments ignored by user', async () => {
|
||||
const assetId = 'fakeAssetId1';
|
||||
const assetUrl = 'https://bengo.is/1';
|
||||
await Asset.create({id: assetId, url: assetUrl});
|
||||
const context = new Context({user: users[0]});
|
||||
|
||||
const userA = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
|
||||
const userB = await UsersService.createLocalUser('usernameB@example.com', 'password', 'usernameB');
|
||||
const userC = await UsersService.createLocalUser('usernameC@example.com', 'password', 'usernameC');
|
||||
const context = new Context({user: userA});
|
||||
|
||||
// create 2 comments each for userB, userC
|
||||
await Promise.all([userB, userC].map(user => CommentsService.publicCreate([1, 2].map(() => ({
|
||||
await Promise.all(users.slice(1, 3).map((user) => CommentsService.publicCreate({
|
||||
author_id: user.id,
|
||||
asset_id: assetId,
|
||||
body: `hello there! ${ String(Math.random()).slice(2)}`,
|
||||
})))));
|
||||
asset_id: asset.id,
|
||||
body: `hello there! ${String(Math.random()).slice(2)}`,
|
||||
})));
|
||||
|
||||
// Add the second user to the list of ignored users.
|
||||
context.user.ignoresUsers.push(users[1].id);
|
||||
|
||||
// ignore userB
|
||||
const ignoreUserMutation = `
|
||||
mutation ignoreUser ($id: ID!) {
|
||||
ignoreUser(id:$id) {
|
||||
errors {
|
||||
translation_key
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
const ignoreUserResponse = await graphql(schema, ignoreUserMutation, {}, context, {id: userB.id});
|
||||
if (ignoreUserResponse.errors && ignoreUserResponse.errors.length) {
|
||||
console.error(ignoreUserResponse.errors);
|
||||
}
|
||||
expect(ignoreUserResponse.errors).to.be.empty;
|
||||
|
||||
const assetCommentsWithoutIgnoredQuery = `
|
||||
query assetCommentsQuery($assetId: ID!, $assetUrl: String!, $excludeIgnored: Boolean!) {
|
||||
asset(id: $assetId, url: $assetUrl) {
|
||||
const query = `
|
||||
query assetCommentsQuery($id: ID!, $url: String!, $excludeIgnored: Boolean!) {
|
||||
asset(id: $id, url: $url) {
|
||||
comments(limit: 10, excludeIgnored: $excludeIgnored) {
|
||||
id,
|
||||
body,
|
||||
edges {
|
||||
id
|
||||
body
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
const assetCommentsResponse = await graphql(schema, assetCommentsWithoutIgnoredQuery, {}, context, {assetId, assetUrl, excludeIgnored: true});
|
||||
const comments = assetCommentsResponse.data.asset.comments;
|
||||
expect(comments.length).to.equal(2);
|
||||
|
||||
{
|
||||
const res = await graphql(schema, query, {}, context, {
|
||||
id: asset.id,
|
||||
url: asset.url,
|
||||
excludeIgnored: true
|
||||
});
|
||||
if (res.errors && res.errors.length) {
|
||||
console.error(res.errors);
|
||||
}
|
||||
expect(res.errors).is.empty;
|
||||
const comments = res.data.asset.comments.edges;
|
||||
expect(comments.length).to.equal(1);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user