mirror of
https://github.com/wassname/talk.git
synced 2026-08-15 12:55:10 +08:00
Fix tests related to tags on comments.
This commit is contained in:
@@ -6,6 +6,7 @@ const Assets = require('./assets');
|
||||
const Comments = require('./comments');
|
||||
const Metrics = require('./metrics');
|
||||
const Settings = require('./settings');
|
||||
const Tags = require('./tags');
|
||||
const Users = require('./users');
|
||||
|
||||
const plugins = require('../../services/plugins');
|
||||
@@ -18,6 +19,7 @@ let loaders = [
|
||||
Comments,
|
||||
Metrics,
|
||||
Settings,
|
||||
Tags,
|
||||
Users,
|
||||
|
||||
// Load the plugin loaders from the manager.
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
const DataLoader = require('dataloader');
|
||||
|
||||
const util = require('./util');
|
||||
|
||||
const TagsService = require('../../services/tags');
|
||||
const TagModel = require('../../models/tag');
|
||||
|
||||
/**
|
||||
* Gets tags based on their item id's.
|
||||
*/
|
||||
const genTagsByItemID = (_, item_ids) => {
|
||||
return TagsService
|
||||
.findByItemIdArray(item_ids)
|
||||
.then(util.arrayJoinBy(item_ids, 'item_id'));
|
||||
};
|
||||
|
||||
/**
|
||||
* Search for tags based on their item_type and ensures that
|
||||
* the tags returned have unique item id's.
|
||||
* @param {String} item_type the item id to search by
|
||||
* @return {Promise} resolves to distinct items tags
|
||||
*/
|
||||
const getItemIdsByItemType = (_, item_type) => {
|
||||
return TagModel.distinct('item_id', {item_type});
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a set of loaders based on a GraphQL context.
|
||||
* @param {Object} context the context of the GraphQL request
|
||||
* @return {Object} object of loaders
|
||||
*/
|
||||
module.exports = (context) => ({
|
||||
Tags: {
|
||||
getByID: new DataLoader((ids) => genTagsByItemID(context, ids)),
|
||||
getByTypes: ({item_type}) => getItemIdsByItemType(context, item_type)
|
||||
}
|
||||
});
|
||||
@@ -5,6 +5,8 @@ const ActionsService = require('../../services/actions');
|
||||
const CommentsService = require('../../services/comments');
|
||||
const linkify = require('linkify-it')();
|
||||
|
||||
const TagModel = require('../../models/tag');
|
||||
|
||||
const Wordlist = require('../../services/wordlist');
|
||||
|
||||
/**
|
||||
@@ -18,20 +20,18 @@ const Wordlist = require('../../services/wordlist');
|
||||
*/
|
||||
const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id = null}, status = 'NONE') => {
|
||||
|
||||
let tags = [];
|
||||
if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) {
|
||||
tags = [{name: 'STAFF'}];
|
||||
}
|
||||
|
||||
return CommentsService.publicCreate({
|
||||
body,
|
||||
asset_id,
|
||||
parent_id,
|
||||
status,
|
||||
tags,
|
||||
author_id: user.id
|
||||
})
|
||||
.then((comment) => {
|
||||
.then(async (comment) => {
|
||||
|
||||
if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) {
|
||||
await CommentsService.addTag(comment.id, 'STAFF', user.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. We should
|
||||
|
||||
Reference in New Issue
Block a user