Example #1
0
def update_topic(id):
    """ Updates a followed topic """
    data = get_json_or_400()
    user_id = session['user']

    if not is_user_topic(id, user_id):
        abort(403)

    # If notifications are enabled, check to see if user is configured to receive emails
    if data.get('notifications'):
        user = get_resource_service('users').find_one(req=None, _id=user_id)
        if not user.get('receive_email'):
            return "", gettext(
                'Please enable \'Receive notifications via email\' option in your profile to receive topic notifications'
            )  # noqa

    updates = {
        'label': data.get('label'),
        'notifications': data.get('notifications', False),
        'query': data.get('query')
    }

    get_resource_service('topics').patch(id=ObjectId(id), updates=updates)
    push_user_notification('topics')
    return jsonify({'success': True}), 200
Example #2
0
def delete(id):
    """ Deletes a followed topic by given id """
    if not is_user_topic(id, session['user']):
        abort(403)

    get_resource_service('topics').delete({'_id': ObjectId(id)})
    push_user_notification('topics')
    return jsonify({'success': True}), 200
Example #3
0
def bookmark():
    data = get_json_or_400()
    assert data.get('items')
    update_action_list(data.get('items'), 'bookmarks', item_type='agenda')
    push_user_notification(
        'saved_items',
        count=get_resource_service('agenda').get_saved_items_count())
    return flask.jsonify(), 200
Example #4
0
def post_topic(_id):
    """Creates a user topic"""
    if flask.session['user'] != str(_id):
        flask.abort(403)

    topic = get_json_or_400()
    topic['user'] = ObjectId(_id)

    ids = get_resource_service('topics').post([topic])
    push_user_notification('topic_created')
    return jsonify({'success': True, '_id': ids[0]}), 201
Example #5
0
def bookmark():
    """Bookmark an item.

    Stores user id into item.bookmarks array.
    Uses mongodb to update the array and then pushes updated array to elastic.
    """
    data = get_json_or_400()
    assert data.get('items')
    update_action_list(data.get('items'), 'bookmarks', item_type='items')
    user_id = get_user_id()
    push_user_notification('saved_items', count=get_bookmarks_count(user_id, 'media_releases'))
    return flask.jsonify(), 200
Example #6
0
def update_topic(id):
    """ Updates a followed topic """
    data = get_json_or_400()

    if not is_user_topic(id, session['user']):
        abort(403)

    updates = {
        'label': data.get('label'),
        'notifications': data.get('notifications', False),
        'query': data.get('query')
    }

    get_resource_service('topics').patch(id=ObjectId(id), updates=updates)
    push_user_notification('topics')
    return jsonify({'success': True}), 200
Example #7
0
def follow():
    data = get_json_or_400()
    assert data.get('items')
    for item_id in data.get('items'):
        user_id = get_user_id()
        item = get_entity_or_404(item_id, 'agenda')
        coverage_updates = {'coverages': item.get('coverages') or []}
        for c in coverage_updates['coverages']:
            if c.get('watches') and user_id in c['watches']:
                c['watches'].remove(user_id)

        if request.method == 'POST':
            updates = {
                'watches': list(set((item.get('watches') or []) + [user_id]))
            }
            if item.get('coverages'):
                updates.update(coverage_updates)

            get_resource_service('agenda').patch(item_id, updates)
        else:
            if request.args.get('bookmarks'):
                user_item_watches = [
                    u for u in (item.get('watches') or [])
                    if str(u) == str(user_id)
                ]
                if not user_item_watches:
                    # delete user watches of all coverages
                    get_resource_service('agenda').patch(
                        item_id, coverage_updates)
                    return flask.jsonify(), 200

            update_action_list(data.get('items'),
                               'watches',
                               item_type='agenda')

    push_user_notification(
        'saved_items',
        count=get_resource_service('agenda').get_saved_items_count())
    return flask.jsonify(), 200