Esempio n. 1
0
def get_user_post(username, post_id):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    post_user = UserMethods.get_user_by_username(username)
    if current_user.id == post_user.id or \
            post_user.status.value == "public" or \
            FollowerMethods.is_following(current_user.id, post_user.id):
        # send all data like comments, likes and post details
        if not PostMethods.get_record_with_id(post_id):
            raise NotFound("Post not found")
        post = PostMethods.get_record_with_id(post_id)
        comments = post.post_comments
        like_count = post.total_number_of_likes
        likes = post.post_likes
        post_data = dict(caption=post.caption,
                         image=post.image._public_url,
                         post_id=post.id,
                         user_id=post.user_id)
        response = dict(post=post_data,
                        comments=comments,
                        like_count=like_count,
                        likes=likes)
        return jsonify(response)
    else:
        return jsonify(
            dict(message="Profile is private! Follow to see the post !")), 400
Esempio n. 2
0
def generate_user_from_auth_token(auth_token):
    payload = decode_auth_token(
        auth_token,
        Entity.USER.value
    )
    current_username = payload.get('entity_id')
    current_user = UserMethods.get_user_by_username(current_username)
    return current_user
Esempio n. 3
0
def unlike_action_on_post(username, post_id):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    post_user = UserMethods.get_user_by_username(username)
    if current_user.id == post_user.id or \
            post_user.status.value == "public" or \
            FollowerMethods.is_following(current_user.id, post_user.id):
        # send all data like comments, likes and post details
        if not PostMethods.get_record_with_id(post_id):
            raise NotFound("Post not found")
        LikeMethods.delete_record(post_id, post_user.id)
        db.commit()
        return jsonify(dict(message="Successfully unliked the post"))
    else:
        return jsonify(
            dict(message="Profile is private! Follow to unlike the post !")
        ), 400
Esempio n. 4
0
def unfollow(followee_username):
    follower_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    followee_user = UserMethods.get_user_by_username(followee_username)
    if followee_user is None:
        raise NotFound('No user with username {username} found'.format(
            username=followee_username))
    # same user id unfollow request
    if follower_user.id == followee_user.id:
        raise BadRequest('Request cannot be proceesed')

    # check if it follows
    if FollowerMethods.is_following(follower_user.id, followee_user.id):
        unfollow_user(follower_user.id, followee_user.id)

    db.commit()
    return jsonify({"message": "Successfully unfollowed"}), 200
Esempio n. 5
0
def user_profile(username):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    profile_user = UserMethods.get_user_by_username(username)

    response = dict(
        name=profile_user.name,
        username=profile_user.username,
        display_picture=dict(profile_user.display_picture)
    )
    #  HATEOS CONCEPT USED FOR POSTS
    #  no circular checking
    if current_user.username != username:
        # check if following
        if FollowerMethods.is_following(current_user.id, profile_user.id):
            response['posts'] = profile_user.all_posts
    else:
        response['posts'] = profile_user.all_posts
    return response
Esempio n. 6
0
def follow(followee_username):
    follower_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    followee_user = UserMethods.get_user_by_username(followee_username)
    if followee_user is None:
        raise NotFound('No user with username {username} found'.format(
            username=followee_username))

    # same user id follow request
    if follower_user.id == followee_user.id:
        raise BadRequest('Request cannot be proceesed')

    # check if not follows
    if not FollowerMethods.is_following(follower_user.id, followee_user.id):
        if followee_user.status.value == "public":
            follow_public_user(follower_user.id, followee_user.id)
            db.commit()
            return jsonify({"message": "Successfully followed"}), 200
        else:
            send_follow_request(follower_user.id, followee_user.id)
            db.commit()
            return jsonify({"message": "Follow request sent"}), 200