added more db optims

This commit is contained in:
Wyatt Johnson
2017-08-23 15:42:30 -06:00
parent 994923cae0
commit acf792089d
5 changed files with 31 additions and 32 deletions
+18 -23
View File
@@ -1,32 +1,27 @@
const {decorateWithTags} = require('./util');
const {
SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS,
} = require('../../perms/constants');
const Asset = {
async comment({id}, {id: commentId}, {loaders: {Comments}, user}) {
const statuses = user && user.can(SEARCH_NON_NULL_OR_ACCEPTED_COMMENTS)
? ['NONE', 'ACCEPTED', 'PREMOD', 'REJECTED']
: ['NONE', 'ACCEPTED'];
async comment({id}, {id: commentId}, {loaders: {Comments}}) {
const comments = await Comments.getByQuery({
asset_id: id,
ids: commentId,
statuses,
});
// Load the comment from the database.
const comment = Comments.get.load(id);
if (!comment) {
return null;
}
return comments.nodes[0];
// If the comment asset mismatches, then don't return it!
if (comment.asset_id !== id) {
return null;
}
return comment;
},
comments({id}, {query: {sort, sortBy, limit, excludeIgnored, tags}, deep}, {loaders: {Comments}}) {
return Comments.getByQuery({
asset_id: id,
sort,
sortBy,
limit,
parent_id: deep ? undefined : null,
tags,
excludeIgnored,
});
comments({id}, {query, deep}, {loaders: {Comments}}) {
if (!deep) {
query.parent_id = null;
}
return Comments.getByQuery(query);
},
commentCount({id, commentCount}, {tags}, {loaders: {Comments}}) {
if (commentCount != null) {
+4 -2
View File
@@ -2,9 +2,11 @@ const {SEARCH_OTHER_USERS} = require('../../perms/constants');
const CommentStatusHistory = {
assigned_by({assigned_by}, _, {user, loaders: {Users}}) {
if (user && user.can(SEARCH_OTHER_USERS) && assigned_by != null) {
return Users.getByID.load(assigned_by);
if (!user || !user.can(SEARCH_OTHER_USERS) || assigned_by != null) {
return null;
}
return Users.getByID.load(assigned_by);
}
};
+4 -2
View File
@@ -8,9 +8,11 @@ const FlagAction = {
return group_id;
},
user({user_id}, _, {loaders: {Users}}) {
if (user_id) {
return Users.getByID.load(user_id);
if (!user_id) {
return null;
}
return Users.getByID.load(user_id);
},
};
+2 -2
View File
@@ -39,8 +39,8 @@ const RootQuery = {
return null;
}
const {asset_url} = query;
if (asset_url && asset_url.length > 0) {
const {asset_url, asset_id} = query;
if ((!asset_id || asset_id.length === 0) && asset_url && asset_url.length > 0) {
let asset = await Assets.findByUrl(asset_url);
if (asset) {
query.asset_id = asset.id;
+3 -3
View File
@@ -95,11 +95,11 @@ module.exports = class CommentsService {
},
});
if (originalComment === null) {
if (originalComment == null) {
// Try to get the comment.
const comment = await CommentsService.findById(id);
if (comment === null) {
if (comment == null) {
debug('rejecting comment edit because comment was not found');
throw errors.ErrNotFound;
}
@@ -253,7 +253,7 @@ module.exports = class CommentsService {
$set: {status}
});
if (originalComment === null) {
if (originalComment == null) {
throw errors.ErrNotFound;
}