Unverified Commit b71a6ffa authored by Robert Knight's avatar Robert Knight Committed by Sean Hammond

Move flag count retrieval to a helper function

Move the flag count to a helper which is easier to test and re-use.
parent 396b9d16
...@@ -180,9 +180,21 @@ function location(annotation) { ...@@ -180,9 +180,21 @@ function location(annotation) {
return Number.POSITIVE_INFINITY; return Number.POSITIVE_INFINITY;
} }
/**
* Return the number of times the annotation has been flagged
* by other users. If moderation data is unavailable, returns 0.
*/
function flagCount(ann) {
if (!ann.moderation) {
return 0;
}
return ann.moderation.flag_count;
}
module.exports = { module.exports = {
documentMetadata: documentMetadata, documentMetadata: documentMetadata,
domainAndTitle: domainAndTitle, domainAndTitle: domainAndTitle,
flagCount: flagCount,
isAnnotation: isAnnotation, isAnnotation: isAnnotation,
isNew: isNew, isNew: isNew,
isOrphan: isOrphan, isOrphan: isOrphan,
......
...@@ -7,11 +7,7 @@ function ModerationBannerController(annotationUI, flash, store) { ...@@ -7,11 +7,7 @@ function ModerationBannerController(annotationUI, flash, store) {
var self = this; var self = this;
this.flagCount = function () { this.flagCount = function () {
var moderation = self.annotation.moderation; return annotationMetadata.flagCount(self.annotation);
if (!moderation) {
return 0;
}
return moderation.flag_count;
}; };
this.isHidden = function () { this.isHidden = function () {
......
...@@ -316,4 +316,17 @@ describe('annotation-metadata', function () { ...@@ -316,4 +316,17 @@ describe('annotation-metadata', function () {
assert.isFalse(isWaitingToAnchor(pending)); assert.isFalse(isWaitingToAnchor(pending));
}); });
}); });
describe('.flagCount', function () {
var flagCount = annotationMetadata.flagCount;
it('returns 0 if the user is not a moderator', function () {
assert.equal(flagCount(fixtures.defaultAnnotation()), 0);
});
it('returns the flag count if present', function () {
var ann = fixtures.moderatedAnnotation({ flagCount: 10});
assert.equal(flagCount(ann), 10);
});
});
}); });
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