def test_list_posts_facade(db_conn, posts_table):
    """
    Expect to get a list of posts, and the instances to match the kinds.
    """

    posts_data = [{
        'id': 'fghj4567',
        'user_id': 'abcd1234',
        'topic_id': 'wxyz7890',
        'body': 'abcd',
        'kind': 'post',
    }, {
        'id': 'yuio6789',
        'user_id': 'abcd1234',
        'topic_id': 'wxyz7890',
        'kind': 'vote',
        'replies_to_id': 'fghj4567',
    }, {
        'id': 'bnml3456',
        'user_id': 'abcd1234',
        'topic_id': 'erty4567',
        'body': 'abcd',
        'kind': 'post',
    }]
    posts_table.insert(posts_data).run(db_conn)
    posts = list_posts({'topic_id': 'wxyz7890'}, db_conn)
    assert len(posts) == 2
    assert posts[0]['id'] in ('fghj4567', 'yuio6789')
    assert posts[1]['id'] in ('fghj4567', 'yuio6789')
Beispiel #2
0
def get_my_recent_proposals(current_user, db_conn):
    """
    Gets a list of the user's most recent proposals.
    """

    return list_posts({
        'user_id': current_user['id'],
        'kind': 'proposal',
        'limit': 100,
    }, db_conn)
Beispiel #3
0
def get_user_route(request, user_id):
    """
    Get the user by their ID.
    """

    db_conn = request['db_conn']
    user = get_user({'id': user_id}, db_conn)
    current_user = get_current_user(request)
    # Posts if in request params
    # Subjects if in request params and allowed
    # Follows if in request params and allowed
    if not user:
        return abort(404)

    data = {}
    data['user'] = deliver_user(user,
                                access='private'
                                if current_user
                                and user['id'] == current_user['id']
                                else None)

    # TODO-2 SPLITUP create new endpoints for these instead
    if 'posts' in request['params']:
        data['posts'] = [deliver_post(post) for post in
                         list_posts({'user_id': user['id']}, db_conn)]
    if ('subjects' in request['params']
            and user['settings']['view_subjects'] == 'public'):
        data['subjects'] = [
            subject.deliver()
            for subject in list_user_subjects_entity(
                user['id'],
                {},
                db_conn)]
    if ('follows' in request['params']
            and user['settings']['view_follows'] == 'public'):
        data['follows'] = [deliver_follow(follow) for follow in
                           list_follows({'user_id': user['id']}, db_conn)]
    if 'avatar' in request['params']:
        size = int(request['params']['avatar'])
        data['avatar'] = get_avatar(user['email'], size if size else None)

    return 200, data
Beispiel #4
0
def update_entity_statuses(db_conn, proposal):
    """
    Update the entity's status based on the vote power received.
    Move to accepted or blocked if qualified.
    TODO-2 Update this to work as described in:
        https://github.com/heiskr/sagefy/wiki/Planning%3A-Contributor-Ratings
        This requires knowing two things:
        - Number of learners the entity impacts
        - The vote and proposal history of the contributor
    """

    # Get the entity version
    for p_entity_version in proposal['entity_versions']:
        entity_version = get_version(db_conn, p_entity_version['kind'],
                                     p_entity_version['id'])

        votes = list_posts({
            'kind': 'vote',
            'replies_to_id': proposal['id'],
        }, db_conn)
        changed, status = get_entity_status(entity_version['status'], votes)

        if changed:
            entity_version['status'] = status
            entity_version.save(db_conn)
            send_notices(
                db_conn,
                entity_id=p_entity_version['id'],
                entity_kind=p_entity_version['kind'],
                notice_kind=('block_proposal'
                             if status == 'blocked' else 'accept_proposal'),
                notice_data={
                    'user_name': '???',  # TODO-2
                    'proposal_name': proposal['name'],
                    'entity_kind': p_entity_version['kind'],
                    'entity_name': entity_version['name'],
                })
Beispiel #5
0
def get_posts_route(request, topic_id):
    """
    Get a reverse chronological listing of posts for given topic.
    Includes topic meta data and posts (or proposals or votes).
    Paginates.
    """

    db_conn = request['db_conn']

    # Is the topic valid?
    topic = get_topic({'id': topic_id}, db_conn)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'pgnNbqSP1VUWkOYq8MVGPrSS',
        }

    # Pull the entity
    entity_kind = topic['entity']['kind']
    entity = get_latest_accepted(db_conn, entity_kind, topic['entity']['id'])

    # Pull all kinds of posts
    posts = list_posts(
        {
            'limit': request['params'].get('limit') or 10,
            'skip': request['params'].get('skip') or 0,
            'topic_id': topic_id,
        }, db_conn)

    # For proposals, pull up the proposal entity version
    # ...then pull up the previous version
    # ...make a diff between the previous and the proposal entity version
    # diffs = {}
    entity_versions = {}
    for post_ in posts:
        if post_['kind'] == 'proposal':
            entity_versions[post_['id']] = [
                get_version(db_conn, p_entity_version['kind'],
                            p_entity_version['id'])
                for p_entity_version in post_['entity_versions']
            ]
            # TODO-2 re-enable diffs
            # previous_version = get_version(
            #     db_conn,
            #     p_entity_version['kind'],
            #     entity_version['previous_id']
            # )
            # if previous_version:
            #     diffs[post_['id']] = object_diff(previous_version.deliver(),
            #                                      entity_version.deliver())

    # TODO-2 SPLITUP create new endpoint for this instead
    users = {}
    for post_ in posts:
        user_id = post_['user_id']
        if user_id not in users:
            user = get_user({'id': user_id}, db_conn)
            if user:
                users[user_id] = {
                    'name': user['name'],
                    'avatar': get_avatar(user['email'], 48),
                }

    # TODO-2 SPLITUP create new endpoints for these instead
    output = {
        'topic': deliver_topic(topic),
        'posts': [deliver_post(p) for p in posts],
        'entity_versions': {
            p: [ev.deliver('view') for ev in evs]
            for p, evs in entity_versions.items()
        },
        # 'diffs': diffs,  TODO-2 this causes a circular dependency
        'users': users,
    }
    if entity:
        output[entity_kind] = entity.deliver()
    return 200, output