예제 #1
0
def handle_annotation_event(message, socket):
    """
    Get message about annotation event `message` to be sent to `socket`.

    Inspects the embedded annotation event and decides whether or not the
    passed socket should receive notification of the event.

    Returns None if the socket should not receive any message about this
    annotation event, otherwise a dict containing information about the event.
    """
    action = message['action']

    if action == 'read':
        return None

    if message['src_client_id'] == socket.client_id:
        return None

    # We don't send anything until we have received a filter from the client
    if socket.filter is None:
        return None

    notification = {
        'type': 'annotation-notification',
        'options': {
            'action': action
        },
    }
    id_ = message['annotation_id']

    # Return early when action is delete
    serialized = None
    if action == 'delete':
        serialized = message['annotation_dict']
    else:
        annotation = storage.fetch_annotation(socket.request.db, id_)
        if annotation is None:
            return None

        serialized = presenters.AnnotationJSONPresenter(
            socket.request, annotation).asdict()

    userid = serialized.get('user')
    if has_nipsa(userid) and socket.request.authenticated_userid != userid:
        return None

    permissions = serialized.get('permissions')
    if not _authorized_to_read(socket.request, permissions):
        return None

    if not socket.filter.match(serialized, action):
        return None

    notification['payload'] = [serialized]
    if action == 'delete':
        notification['payload'] = [{'id': id_}]
    return notification
예제 #2
0
파일: messages.py 프로젝트: bZichett/h
def handle_annotation_event(message, socket):
    """
    Get message about annotation event `message` to be sent to `socket`.

    Inspects the embedded annotation event and decides whether or not the
    passed socket should receive notification of the event.

    Returns None if the socket should not receive any message about this
    annotation event, otherwise a dict containing information about the event.
    """
    action = message['action']

    if action == 'read':
        return None

    if message['src_client_id'] == socket.client_id:
        return None

    # We don't send anything until we have received a filter from the client
    if socket.filter is None:
        return None

    notification = {
        'type': 'annotation-notification',
        'options': {'action': action},
    }
    id_ = message['annotation_id']

    # Return early when action is delete
    serialized = None
    if action == 'delete':
        serialized = message['annotation_dict']
    else:
        annotation = storage.fetch_annotation(socket.request.db, id_)
        if annotation is None:
            return None

        serialized = presenters.AnnotationJSONPresenter(
            socket.request, annotation).asdict()

    userid = serialized.get('user')
    if has_nipsa(userid) and socket.request.authenticated_userid != userid:
        return None

    permissions = serialized.get('permissions')
    if not _authorized_to_read(socket.request, permissions):
        return None

    if not socket.filter.match(serialized, action):
        return None

    notification['payload'] = [serialized]
    if action == 'delete':
        notification['payload'] = [{'id': id_}]
    return notification
예제 #3
0
def test_has_nipsa_returns_False_if_user_is_not_nipsad(NipsaUser):
    NipsaUser.get_by_userid.return_value = None

    assert logic.has_nipsa(userid="test_user") is False
예제 #4
0
def test_has_nipsa_returns_True_if_user_has_nipsa(NipsaUser):
    nipsa_user = mock.Mock()
    nipsa_user.userid = "test_id"
    NipsaUser.get_by_userid.return_value = nipsa_user

    assert logic.has_nipsa(userid="test_id") is True
예제 #5
0
def test_has_nipsa_gets_user_by_id(NipsaUser):
    logic.has_nipsa(userid="test_id")

    NipsaUser.get_by_userid.assert_called_once_with("test_id")
예제 #6
0
def transform_annotation(event):
    """Add a {"nipsa": True} field on annotations whose users are flagged."""
    annotation = event.annotation_dict
    if 'user' in annotation and logic.has_nipsa(annotation['user']):
        annotation['nipsa'] = True
예제 #7
0
파일: logic_test.py 프로젝트: ackermann/h
def test_has_nipsa_returns_False_if_user_is_not_nipsad(NipsaUser):
    NipsaUser.get_by_userid.return_value = None

    assert logic.has_nipsa(userid="test_user") is False
예제 #8
0
파일: logic_test.py 프로젝트: ackermann/h
def test_has_nipsa_returns_True_if_user_has_nipsa(NipsaUser):
    nipsa_user = mock.Mock()
    nipsa_user.userid = "test_id"
    NipsaUser.get_by_userid.return_value = nipsa_user

    assert logic.has_nipsa(userid="test_id") is True
예제 #9
0
파일: logic_test.py 프로젝트: ackermann/h
def test_has_nipsa_gets_user_by_id(NipsaUser):
    logic.has_nipsa(userid="test_id")

    NipsaUser.get_by_userid.assert_called_once_with("test_id")