def get_posts(user_id, page=1, per_page=None, perm=0):
    """Returns a users posts as a pagination object."""
    if per_page is None:
        per_page = app.config.get('FEED_ITEMS_PER_PAGE')

    # Get the user object we need the email for Gravatar.
    user = m.db.users.find_one({'_id': user_id},
                               {'avatar': True, 'donated': True})

    lookup_dict = {
        'user_id': user_id,
        'reply_to': {'$exists': False}
    }

    lookup_dict['permission'] = {'$lte': perm}

    total = m.db.posts.find(lookup_dict).count()
    cursor = m.db.posts.find(lookup_dict).sort(
        'created', -1).skip((page - 1) * per_page).limit(per_page)

    posts = []
    for post in cursor:
        post['user_avatar'] = user.get('avatar')
        post['user_donated'] = user.get('donated', False)
        posts.append(post)

    return Pagination(posts, total, page, per_page)