Commit 55458533 authored by Robert Knight's avatar Robert Knight

Do not display moderation banner unless moderation metadata present

Annotations which are hidden may still be returned to non-moderators
with the `hidden` flag set, if they have replies. In this case the
moderation banner should not be shown.
parent 99e385b1
...@@ -182,11 +182,13 @@ function location(annotation) { ...@@ -182,11 +182,13 @@ function location(annotation) {
/** /**
* Return the number of times the annotation has been flagged * Return the number of times the annotation has been flagged
* by other users. If moderation data is unavailable, returns 0. * by other users. If moderation metadata is not present, returns `null`.
*
* @return {number|null}
*/ */
function flagCount(ann) { function flagCount(ann) {
if (!ann.moderation) { if (!ann.moderation) {
return 0; return null;
} }
return ann.moderation.flagCount; return ann.moderation.flagCount;
} }
......
...@@ -14,6 +14,11 @@ function ModerationBannerController(annotationUI, flash, store) { ...@@ -14,6 +14,11 @@ function ModerationBannerController(annotationUI, flash, store) {
return self.annotation.hidden; return self.annotation.hidden;
}; };
this.isHiddenOrFlagged = function () {
var flagCount = self.flagCount();
return flagCount !== null && (flagCount > 0 || self.isHidden());
};
this.isReply = function () { this.isReply = function () {
return annotationMetadata.isReply(self.annotation); return annotationMetadata.isReply(self.annotation);
}; };
......
...@@ -4,6 +4,7 @@ var angular = require('angular'); ...@@ -4,6 +4,7 @@ var angular = require('angular');
var util = require('../../directive/test/util'); var util = require('../../directive/test/util');
var fixtures = require('../../test/annotation-fixtures'); var fixtures = require('../../test/annotation-fixtures');
var unroll = require('../../../shared/test/util').unroll;
var moderatedAnnotation = fixtures.moderatedAnnotation; var moderatedAnnotation = fixtures.moderatedAnnotation;
...@@ -52,10 +53,37 @@ describe('moderationBanner', function () { ...@@ -52,10 +53,37 @@ describe('moderationBanner', function () {
return bannerEl; return bannerEl;
} }
it('does not display if annotation is not flagged or hidden', function () { unroll('displays if user is a moderator and annotation is hidden or flagged', function (testCase) {
var banner = createBanner({ annotation: fixtures.defaultAnnotation() }); var banner = createBanner({ annotation: testCase.ann });
if (testCase.expectVisible) {
assert.notEqual(banner.textContent.trim(), '');
} else {
assert.equal(banner.textContent.trim(), ''); assert.equal(banner.textContent.trim(), '');
}); }
},[{
// Not hidden or flagged and user is not a moderator
ann: fixtures.defaultAnnotation(),
expectVisible: false,
},{
// Hidden, but user is not a moderator
ann: Object.assign(fixtures.defaultAnnotation(), {
hidden: true,
}),
expectVisible: false,
},{
// Not hidden or flagged and user is a moderator
ann: fixtures.moderatedAnnotation({ flagCount: 0, hidden: false }),
expectVisible: false,
},{
// Flagged but not hidden
ann: fixtures.moderatedAnnotation({ flagCount: 1, hidden: false }),
expectVisible: true,
},{
// Hidden but not flagged. The client only allows moderators to hide flagged
// annotations but an unflagged annotation can still be hidden via the API.
ann: fixtures.moderatedAnnotation({ flagCount: 0, hidden: true }),
expectVisible: true,
}]);
it('displays the number of flags the annotation has received', function () { it('displays the number of flags the annotation has received', function () {
var ann = fixtures.moderatedAnnotation({ flagCount: 10 }); var ann = fixtures.moderatedAnnotation({ flagCount: 10 });
...@@ -74,7 +102,10 @@ describe('moderationBanner', function () { ...@@ -74,7 +102,10 @@ describe('moderationBanner', function () {
}); });
it('reports if the annotation was hidden', function () { it('reports if the annotation was hidden', function () {
var ann = moderatedAnnotation({ hidden: true }); var ann = moderatedAnnotation({
flagCount: 1,
hidden: true,
});
var banner = createBanner({ annotation: ann }); var banner = createBanner({ annotation: ann });
assert.include(banner.textContent, 'Hidden from users'); assert.include(banner.textContent, 'Hidden from users');
}); });
...@@ -100,7 +131,10 @@ describe('moderationBanner', function () { ...@@ -100,7 +131,10 @@ describe('moderationBanner', function () {
}); });
it('unhides the annotation if "Unhide" is clicked', function () { it('unhides the annotation if "Unhide" is clicked', function () {
var ann = moderatedAnnotation({ hidden: true }); var ann = moderatedAnnotation({
flagCount: 1,
hidden: true,
});
var banner = createBanner({ annotation: ann }); var banner = createBanner({ annotation: ann });
banner.querySelector('button').click(); banner.querySelector('button').click();
...@@ -109,7 +143,10 @@ describe('moderationBanner', function () { ...@@ -109,7 +143,10 @@ describe('moderationBanner', function () {
}); });
it('reports an error if unhiding the annotation fails', function (done) { it('reports an error if unhiding the annotation fails', function (done) {
var ann = moderatedAnnotation({ hidden: true }); var ann = moderatedAnnotation({
flagCount: 1,
hidden: true,
});
var banner = createBanner({ annotation: ann }); var banner = createBanner({ annotation: ann });
fakeStore.annotation.unhide.returns(Promise.reject(new Error('Network Error'))); fakeStore.annotation.unhide.returns(Promise.reject(new Error('Network Error')));
......
<div class="moderation-banner" <div class="moderation-banner"
ng-if="vm.flagCount() > 0 || vm.isHidden()" ng-if="vm.isHiddenOrFlagged()"
ng-class="{'is-flagged': vm.flagCount() > 0, ng-class="{'is-flagged': vm.flagCount() > 0,
'is-hidden': vm.isHidden(), 'is-hidden': vm.isHidden(),
'is-reply': vm.isReply()}"> 'is-reply': vm.isReply()}">
......
...@@ -320,8 +320,8 @@ describe('annotation-metadata', function () { ...@@ -320,8 +320,8 @@ describe('annotation-metadata', function () {
describe('.flagCount', function () { describe('.flagCount', function () {
var flagCount = annotationMetadata.flagCount; var flagCount = annotationMetadata.flagCount;
it('returns 0 if the user is not a moderator', function () { it('returns `null` if the user is not a moderator', function () {
assert.equal(flagCount(fixtures.defaultAnnotation()), 0); assert.equal(flagCount(fixtures.defaultAnnotation()), null);
}); });
it('returns the flag count if present', function () { it('returns the flag count if present', function () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment