diff --git a/graph/loaders/tags.js b/graph/loaders/tags.js index b76bc36bd..cb767fdf6 100644 --- a/graph/loaders/tags.js +++ b/graph/loaders/tags.js @@ -8,7 +8,7 @@ const TagModel = require('../../models/tag'); /** * Gets tags based on their item id's. */ -const genTagsByItemID = (_, item_ids) => { +const getByItemID = (_, item_ids) => { return TagsService .findByItemIdArray(item_ids) .then(util.arrayJoinBy(item_ids, 'item_id')); @@ -31,7 +31,7 @@ const getItemIdsByItemType = (_, item_type) => { */ module.exports = (context) => ({ Tags: { - getByID: new DataLoader((ids) => genTagsByItemID(context, ids)), + getByID: new DataLoader((ids) => getByItemID(context, ids)), getByTypes: ({item_type}) => getItemIdsByItemType(context, item_type) } }); diff --git a/graph/resolvers/comment.js b/graph/resolvers/comment.js index 19ea11efe..f94cfaf52 100644 --- a/graph/resolvers/comment.js +++ b/graph/resolvers/comment.js @@ -6,6 +6,9 @@ const Comment = { return Comments.get.load(parent_id); }, + tags({id}, _, {loaders: {Tags}}) { + return Tags.getByID.load([id]); + }, user({author_id}, _, {loaders: {Users}}) { return Users.getByID.load(author_id); }, @@ -23,9 +26,9 @@ const Comment = { }, replyCount({id}, {excludeIgnored}, {user, loaders: {Comments}}) { if (user && excludeIgnored) { - return Comments.countByParentIDPersonalized({id, excludeIgnored}); + return Comments.countByParentIDPersonalized({id, excludeIgnored}); } - return Comments.countByParentID.load(id); + return Comments.countByParentID.load(id); }, actions({id}, _, {user, loaders: {Actions}}) { diff --git a/models/tag.js b/models/tag.js index b6e29a6ab..85bb0845a 100644 --- a/models/tag.js +++ b/models/tag.js @@ -62,6 +62,16 @@ const TagSchema = new Schema({ } }); +// Add the indixies on the tag on an item. +TagSchema.index({ + 'item_type': 1, + 'item_id': 1, + 'name': 1 +}, { + unique: true, + background: false +}); + const Tag = mongoose.model('Tag', TagSchema); module.exports = Tag; diff --git a/services/tags.js b/services/tags.js index 172148727..c4010e562 100644 --- a/services/tags.js +++ b/services/tags.js @@ -28,6 +28,22 @@ module.exports = class TagsService { }); } + /** + * Finds actions in an array of ids. + * @param {String} ids array of user identifiers (uuid) + */ + static async findByItemIdArray(item_ids) { + let tags = await TagModel.find({ + 'item_id': {$in: item_ids} + }); + + if (tags === null) { + return []; + } + + return tags; + } + /** * Add a tag. * @param {string} name the actual tag