Skip to content

Commit

Permalink
Merge pull request #5343 from hypothesis/hidden-field
Browse files Browse the repository at this point in the history
Index hidden field into ES such that if an annotation and all of its replies are hidden it is set to True otherwise False.
  • Loading branch information
Hannah Stepanek committed Oct 4, 2018
2 parents 6b9d519 + eee4676 commit 894d355
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
12 changes: 4 additions & 8 deletions h/presenters/annotation_searchindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ def asdict(self):

# Mark an annotation as hidden if it and all of it's children have been
# moderated and hidden.
result['hidden'] = False
if self.annotation.thread_ids:
ann_mod_svc = self.request.find_service(name='annotation_moderation')
annotation_hidden = ann_mod_svc.hidden(self.annotation)
thread_ids_hidden = len(ann_mod_svc.all_hidden(self.annotation.thread_ids)) == len(self.annotation.thread_ids)

if annotation_hidden and thread_ids_hidden:
result['hidden'] = True
parents_and_replies = [self.annotation.id] + self.annotation.thread_ids

ann_mod_svc = self.request.find_service(name='annotation_moderation')
result['hidden'] = len(ann_mod_svc.all_hidden(parents_and_replies)) == len(parents_and_replies)

return result

Expand Down
13 changes: 6 additions & 7 deletions tests/h/presenters/annotation_searchindex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def test_it_marks_annotation_hidden_when_it_and_all_children_are_moderated(self,
userid='acct:luke@hypothes.is',
thread_ids=thread_ids)

moderation_service.hidden.return_value = True
moderation_service.all_hidden.return_value = thread_ids
moderation_service.all_hidden.return_value = [annotation.id] + thread_ids

annotation_dict = AnnotationSearchIndexPresenter(annotation, pyramid_request).asdict()

Expand Down Expand Up @@ -105,8 +104,7 @@ def test_it_does_not_mark_annotation_hidden_when_children_are_not_moderated(self
userid='acct:luke@hypothes.is',
thread_ids=thread_ids)

moderation_service.all_hidden.return_value = thread_ids[1:]
moderation_service.hidden.return_value = True
moderation_service.all_hidden.return_value = [annotation.id] + thread_ids[1:]

annotation_dict = AnnotationSearchIndexPresenter(annotation, pyramid_request).asdict()

Expand All @@ -123,9 +121,11 @@ def test_it_does_not_mark_annotation_hidden_when_not_moderated_and_no_replies(se
assert annotation_dict['hidden'] is False

def test_it_marks_annotation_hidden_when_moderated_and_no_replies(self, pyramid_request, moderation_service):
annotation = mock.MagicMock(userid='acct:luke@hypothes.is')
thread_ids = []
annotation = mock.MagicMock(userid='acct:luke@hypothes.is',
thread_ids=thread_ids)

moderation_service.hidden.return_value = True
moderation_service.all_hidden.return_value = [annotation.id] + thread_ids

annotation_dict = AnnotationSearchIndexPresenter(annotation, pyramid_request).asdict()

Expand All @@ -142,7 +142,6 @@ def DocumentSearchIndexPresenter(self, patch):
def moderation_service(pyramid_config):
svc = mock.create_autospec(AnnotationModerationService, spec_set=True, instance=True)
svc.all_hidden.return_value = []
svc.hidden.return_value = False
pyramid_config.register_service(svc, name='annotation_moderation')
return svc

Expand Down
9 changes: 9 additions & 0 deletions tests/h/search/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import h.search.index
from h.services.group import GroupService
from h.services.annotation_moderation import AnnotationModerationService


@pytest.fixture(autouse=True)
Expand All @@ -16,6 +17,14 @@ def group_service(pyramid_config):
return group_service


@pytest.fixture(autouse=True)
def moderation_service(pyramid_config):
svc = mock.create_autospec(AnnotationModerationService, spec_set=True, instance=True)
svc.all_hidden.return_value = []
pyramid_config.register_service(svc, name='annotation_moderation')
return svc


@pytest.fixture
def Annotation(factories, index):
"""Create and index an annotation.
Expand Down
12 changes: 1 addition & 11 deletions tests/h/search/index_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import pytest

import h.search.index
from h.services.annotation_moderation import AnnotationModerationService

from tests.common.matchers import Matcher


@pytest.mark.usefixtures("annotations", "moderation_service")
@pytest.mark.usefixtures("annotations")
class TestIndex(object):
def test_annotation_ids_are_used_as_elasticsearch_ids(self, es_client,
factories,
Expand Down Expand Up @@ -495,12 +494,3 @@ def _get(annotation_id):
index=es_client.index, doc_type=es_client.mapping_type,
id=annotation_id)["_source"]
return _get


@pytest.fixture
def moderation_service(pyramid_config):
svc = mock.create_autospec(AnnotationModerationService, spec_set=True, instance=True)
svc.all_hidden.return_value = []
svc.hidden.return_value = False
pyramid_config.register_service(svc, name='annotation_moderation')
return svc

0 comments on commit 894d355

Please sign in to comment.