Adjusted BE beheviour

This commit is contained in:
Wyatt Johnson
2017-02-22 13:55:48 -07:00
parent 5ef5a01f59
commit 9e759c7667
3 changed files with 96 additions and 20 deletions
@@ -1,13 +1,15 @@
import React from 'react';
import {compose} from 'react-apollo';
import {mostLikedComments} from 'coral-admin/src/graphql/queries';
import I18n from 'coral-framework/modules/i18n/i18n';
import translations from 'coral-admin/src/translations';
// import I18n from 'coral-framework/modules/i18n/i18n';
// import translations from 'coral-admin/src/translations';
import ModerationQueue from 'coral-admin/src/containers/ModerationQueue/ModerationQueue';
import {Spinner} from 'coral-ui';
import styles from './Widget.css';
const lang = new I18n(translations);
// const lang = new I18n(translations);
const MostLikedCommentsWidget = props => {
+25 -17
View File
@@ -17,6 +17,10 @@ const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit})
.then((actionSummaries) => {
commentMetrics = actionSummaries.reduce((acc, {item_id, action_type, count}) => {
if (action_type !== sort) {
return acc;
}
if (!(item_id in acc)) {
acc[item_id] = [];
}
@@ -47,29 +51,24 @@ const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit})
}));
return {action_summaries, id: asset_id};
});
})
.filter((asset) => {
let contextActionSummary = asset.action_summaries.find((({action_type}) => action_type === sort));
if (contextActionSummary === null) {
return false;
}
return true;
})
// Sort these metrics by the predefined sort order. This will ensure that
// if the action summary does not exist on the object, that it is less
// prefered over the one that does have it.
assetMetrics.sort((a, b) => {
.sort((a, b) => {
let aActionSummary = a.action_summaries.find((({action_type}) => action_type === sort));
let bActionSummary = b.action_summaries.find((({action_type}) => action_type === sort));
// If either a or b don't have this action type, then one of them will
// automatically win.
if (aActionSummary == null || bActionSummary == null) {
if (bActionSummary != null) {
return 1;
}
if (aActionSummary != null) {
return -1;
}
return 0;
}
// Both of them had an actionCount, hence we can determine that we could
// compare the actual values directly.
return bActionSummary.actionCount - aActionSummary.actionCount;
@@ -138,7 +137,16 @@ const getCommentMetrics = ({loaders: {Metrics, Comments}}, {from, to, sort, limi
commentActionSummaries = _.groupBy(actionSummaries, 'item_id');
let commentIDs = _.uniq(actionSummaries.map(({item_id}) => item_id));
// Grab the comment id's for comment where they have at least one of the
// actions being sorted by.
let commentIDs = Object.keys(commentActionSummaries).filter((item_id) => {
let contextActionSummary = commentActionSummaries[item_id].find(({action_type}) => action_type === sort);
if (contextActionSummary == null) {
return false;
}
return true;
});
// Only keep the top `limit`.
commentIDs = commentIDs.slice(0, limit);
+66
View File
@@ -0,0 +1,66 @@
const {expect} = require('chai');
const {graphql} = require('graphql');
const schema = require('../../../graph/schema');
const Context = require('../../../graph/context');
const UserModel = require('../../../models/user');
const SettingsService = require('../../../services/settings');
const ActionModel = require('../../../models/action');
const CommentModel = require('../../../models/comment');
describe('graph.loaders.Metrics', () => {
beforeEach(() => SettingsService.init());
describe('#Comments', () => {
const query = `
query CommentMetrics($from: Date!, $to: Date!) {
liked: commentMetrics(from: $from, to: $to, sort: LIKE) {
id
}
flagged: commentMetrics(from: $from, to: $to, sort: FLAG) {
id
}
}
`;
describe('different comment states', () => {
beforeEach(() => CommentModel.create([
{id: '1', body: 'a new comment!'}
]));
[
{liked: 0, flagged: 0, actions: []},
{liked: 1, flagged: 0, actions: [{action_type: 'LIKE', item_id: '1', item_type: 'COMMENTS'}]},
{liked: 0, flagged: 1, actions: [{action_type: 'FLAG', item_id: '1', item_type: 'COMMENTS'}]},
{liked: 1, flagged: 1, actions: [
{action_type: 'FLAG', item_id: '1', item_type: 'COMMENTS'},
{action_type: 'LIKE', item_id: '1', item_type: 'COMMENTS'}
]}
].forEach(({liked, flagged, actions}) => {
describe(`with actions=${actions.length}`, () => {
beforeEach(() => ActionModel.create(actions));
it(`returns the correct amount of metrics liked=${liked} flagged=${flagged}`, () => {
const context = new Context({user: new UserModel({roles: ['ADMIN']})});
return graphql(schema, query, {}, context, {
from: (new Date()).setMinutes((new Date()).getMinutes() - 5),
to: (new Date()).setMinutes((new Date()).getMinutes() + 5)
})
.then(({data, errors}) => {
expect(errors).to.be.undefined;
expect(data.liked).to.have.length(liked);
expect(data.flagged).to.have.length(flagged);
});
});
});
});
});
});
});