Exemple #1
0
def handle_user_profile():
    """
        HTTP Route: /users/profile
        HTTP Method: GET
        Params: (token, user_id)
        Returns JSON: { user_id, email, username, profile_img_url, is_connected_to, connection_is_pending }
    """
    token = request.args.get("token")
    user_id = int(request.args.get("user_id"))
    user = get_user_from_id(user_id)
    printColour(" ➤ User Profile: Fetching user profile data: {}".format(
        user.username),
                colour="blue")
    return jsonify(users.users_profile(token, user_id))
Exemple #2
0
def handle_users_bio_fetch():
    """
        HTTP Route: /users/bio
        HTTP Method: GET
        Params: (token, user_id)
        Returns JSON: {
            first_name, last_name, cover_img_url, summary, location, title, education
        }
    """
    token = request.args.get("token")
    user_id = int(request.args.get("user_id"))
    user = get_user_from_id(user_id)
    printColour(" ➤ User Bio: Fetching user {}'s bio".format(user.username),
                colour="blue")
    return jsonify(users.users_bio_fetch(token, user_id))
Exemple #3
0
def handle_user_profile_upload_photo():
    """
        HTTP Route: /users/profile/uploadphoto
        HTTP Method: POST
        Params: (token, user_id, x_start, y_start, x_end, y_end, file)
        Returns JSON: { succeeded }
    """
    token = request.form["token"]
    user_id = request.form["user_id"]
    x_start = int(request.form["x_start"])
    y_start = int(request.form["y_start"])
    x_end = int(request.form["x_end"])
    y_end = int(request.form["y_end"])

    user = get_user_from_id(user_id)
    printColour(
        " ➤ User Profile Upload Photo: Uploading profile picture image for {}".
        format(user.username),
        colour="blue")
    printColour(" ➤ Crop coordinates: start = ({}, {}), end = ({}, {})".format(
        x_start, y_start, x_end, y_end),
                colour="cyan")

    # check if the post request has the file part
    if 'file' not in request.files:
        printColour(" ➤ User didn't upload a photo", colour="red")
        return jsonify({"succeeded": False})
    else:
        file = request.files['file']
        if file.filename == '':
            # If user does not select file, browser also submits an empty part without filename
            printColour(" ➤ No selected file", colour="red")
            return jsonify({"succeeded": False})
        if file and allowed_file(file.filename):
            # Saving the image to local storage
            filename = get_latest_filename(
                "user_{}_profile.jpg".format(user_id))
            file.save(os.path.join(UPLOAD_FOLDER, filename))
            crop_image_file(filename, x_start, y_start, x_end, y_end)
            # Saving the image endpoint to the user's bio tuple under the profile_img_url field
            image_endpoint = "{0}/images/{1}".format(BASE_URL, filename)
            users.users_profile_upload_photo(token, user_id, image_endpoint)
            printColour(
                " ➤ Successfully uploaded profile image for {}. Available at: {}"
                .format(user.username, image_endpoint),
                colour="green",
                bordersOn=False)
            return jsonify({"succeeded": True})
def handle_connection_get_info():
    """
        Fetches details regarding the connection between two users
        Params: (token, user_id)
        Returns JSON: {
            is_connected, connection_is_pending, is_requester
        }
    """
    token = request.args.get("token")
    user_id = int(request.args.get("user_id"))
    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(user_id)
    printColour(" ➤ Connections: {} is fetching connection details with {}".format(
        calling_user.username,
        target_user.username
    ), colour="blue")
    return jsonify(connections.connection_fetch_info(token, user_id))
def handle_conection_fetch_messages():
    """
        HTTP Route: /connections/message
        HTTP Method: GET
        Params: (token, user_id)
        Returns JSON: { messages: [ { message_id, message, sender_id, time_created } ] }
    """
    token = request.args.get("token")
    user_id = int(request.args.get("user_id"))

    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(user_id)
    printColour(" ➤ Connections Messages: {} fetched messages with {}".format(
        calling_user.username, 
        target_user.username
    ), colour="blue")

    return jsonify(connections.connection_fetch_messages(token, user_id))
def handle_conection_request():
    """
        HTTP Route: /connections/request
        HTTP Method: POST
        Params: (token, user_id)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    user_id = int(request_data["user_id"])

    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(user_id)
    printColour(" ➤ Connections Request: {} sent a connection request to {}".format(
        calling_user.username, 
        target_user.username
    ), colour="blue")
    return jsonify(connections.connection_request(token, user_id))
Exemple #7
0
def on_join(event_data):
    """
        Parameter event_data is a dictionary containing:
            - token
            - user_id
            - room
    """
    token = event_data["token"]
    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(int(event_data["user_id"]))
    room = str(get_connection(calling_user.id, target_user.id).id)
    join_room(room)

    printColour(
        " ➤ Socket event: connection_user_enter. User {} started connection chat with {} (room: {})"
        .format(calling_user.username, target_user.username, room),
        colour="cyan",
        bordersOn=False)
    emit("connection_user_entered", room, broadcast=True, room=room)
def handle_conection_send_message():
    """
        HTTP Route: /connections/message
        HTTP Method: POST
        Params: (token, user_id, message)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    user_id = int(request_data["user_id"])
    message = request_data["message"]

    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(user_id)
    printColour(" ➤ Connections Messages: {} sent a message to {}".format(
        calling_user.username, 
        target_user.username
    ), colour="blue")

    return jsonify(connections.connection_send_message(token, user_id, message))
Exemple #9
0
def handle_channel_add_owner():
    """
        HTTP Route: /channels/addowner
        HTTP Method: POST
        Params: (token, channel_id, user_id)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    channel_id = int(request_data["channel_id"])
    user_id = int(request_data["user_id"])

    target_channel = select_channel(channel_id)
    calling_user = get_user_from_token(token)
    target_user = get_user_from_id(user_id)

    printColour(" ➤ Channel Add Owner: {} added {} as an owner in {}".format(
        calling_user.username, target_user.username, target_channel.name),
                colour="blue")
    return jsonify(channels.channels_addowner(token, channel_id, user_id))
Exemple #10
0
def handle_channel_invite():
    """
        HTTP Route: /channels/invite
        HTTP Method: POST
        Params: (token, channel_id, user_id)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    channel_id = int(request_data["channel_id"])
    user_id = int(request_data["user_id"])

    target_channel = select_channel(channel_id)
    target_user = get_user_from_id(user_id)
    calling_user = get_user_from_token(token)

    printColour(
        " ➤ Channel Invite: invitation to {} ({}) sent from {} to {}".format(
            target_channel.name, target_channel.visibility,
            calling_user.username, target_user.username),
        colour="blue")
    return jsonify(channels.channels_invite(token, channel_id, user_id))
Exemple #11
0
def handle_users_bio_update():
    """
        HTTP Route: /users/bio
        HTTP Method: POST
        Params: (token, user_id, first_name, last_name, cover_img_url, summary, location, title, education)
        Returns JSON: { succeeded }
    """
    request_data = request.get_json()
    token = request_data["token"]
    user_id = int(request_data["user_id"])
    printColour("Updating user " + str(user_id) + "'s bio")
    bio = {
        "first_name": request_data["first_name"],
        "last_name": request_data["last_name"],
        "cover_img_url": request_data["cover_img_url"],
        "summary": request_data["summary"],
        "location": request_data["location"],
        "education": request_data["education"],
        "title": request_data["title"]
    }
    user = get_user_from_id(user_id)
    printColour(" ➤ User Bio: Updated user {}'s bio".format(user.username),
                colour="blue")
    return jsonify(users.users_bio_update(token, user_id, bio))
Exemple #12
0
def user_channels_list(token, user_id):
    """
        Provide a list of all channels (and their associated details)
        Parameters:
            token   (str)
            user_id (int)
        Returns: 
            { channels }
        Where:
            List of dictionaries: { channel_id, name, channel_img_url, description, visibility, member_of, owner_of }
    """
    verify_token(token)
    user = get_user_from_id(user_id)
    channels_list = []
    all_channels = Channel.query.all()
    for each_channel in all_channels:
        curr_channel_data = {
            "channel_id": each_channel.id,
            "name": each_channel.name,
            "channel_img_url": each_channel.channel_img_url,
            "channel_cover_img_url": each_channel.channel_cover_img_url,
            "description": each_channel.description,
            "visibility": each_channel.visibility,
            "member_of": False,
            "owner_of": False
        }
        memberships = each_channel.channel_membership
        for membership in memberships:
            if membership.user_id == user.id:
                curr_channel_data["member_of"] = True
                if membership.is_owner:
                    curr_channel_data["owner_of"] = True 
        channels_list.append(curr_channel_data)
    return {
        "channels": channels_list
    }