def handle_connection_fetch():
    """
        Fetches all of the users that the calling user is connected with
        Params: (token)
        Returns JSON: { 
            users: [ {
                user_id, 
                email, 
                username, 
                profile_img_url, 
                summary,
                first_name,
                last_name,
                location,
                education,
                ... other bio fields     
            }, ... ]
        }
    """
    token = request.args.get("token")

    calling_user = get_user_from_token(token)
    printColour(" ➤ Connections: {} is fetching a list of their existing connections".format({
        calling_user.username
    }), colour="blue")
    
    return jsonify(connections.connection_fetch_users(token))
Exemple #2
0
def handle_connection_send_message(token, user_id, message, room):
    try:
        connection_send_message(token, user_id, message)
    except InputError as err:
        emit_error(err.get_message())
    printColour("Client sent a direct message to {}".format(user_id))
    emit("receive_connection_message",
         "The server says: your chat partner has sent a new message",
         broadcast=True,
         room=room)
Exemple #3
0
def handle_connection_remove_message(token, message_id, room):
    try:
        connection_remove_message(token, message_id)
    except InputError as err:
        emit_error(err.get_message())
    printColour("Client removed a direct message")
    emit("connection_message_removed",
         "The server says: your chat partner has removed a message",
         broadcast=True,
         room=room)
Exemple #4
0
def handle_auth_logout():
    """
        HTTP Route: /auth/logout
        HTTP Method: POST
        Params: (token)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    printColour(" ➤ Logged out: {}".format(request_data["token"]),
                colour="blue")
    return jsonify(auth_logout(request_data["token"]))
Exemple #5
0
def handle_users_all():
    """
        HTTP Route: /users/all
        HTTP Method: GET
        Params: (token)
        Returns JSON: { users }
            Where users: array of objects { user_id, email, username, profile_img_url }
    """
    token = request.args.get("token")
    printColour(" ➤ User List All: Getting all list of all users",
                colour="blue")
    return jsonify(users.users_all(token))
Exemple #6
0
def handle_auth_login():
    """
        HTTP Route: /auth/login
        HTTP Method: POST
        Params: (email, password)
        Returns JSON: { token, user_id, username, profile_img_url }
    """
    request_data = request.get_json()
    email = request_data["email"]
    password = request_data['password']
    printColour(" ➤ Logged in: {}".format(email), colour="blue")
    return jsonify(auth_login(email, password))
Exemple #7
0
def handle_auth_signup():
    """
        HTTP Route: /auth/register
        HTTP Method: POST
        Params: (email, username, password)
        Returns JSON: { token, user_id, username, profile_img_url }
    """
    request_data = request.get_json()
    email = request_data["email"]
    password = request_data["password"]
    username = request_data["username"]
    printColour(" ➤ Signed up: {}, {}".format(username, email), colour="blue")
    return jsonify(auth_signup(email, password, username))
Exemple #8
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 #9
0
def handle_user_get_channels():
    """
        HTTP Route: /users/profile/channels
        HTTP Method: GET
        Params: (token, user_id)
        Returns JSON: {
            channels: [{ channel_id, name, description, visibility, member_of, owner_of }, ...]
        }
    """
    request_data = request.get_json()
    token = request.args.get("token")
    user_id = int(request.args.get("user_id"))
    printColour(" ➤ User Profile Channels: getting channels", colour="blue")
    return jsonify(users.user_channels_list(token, user_id))
Exemple #10
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 #11
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})
Exemple #12
0
def handle_channels_listall():
    """
        HTTP Route: /channels/listall
        HTTP Method: GET
        Params: (token)
        Returns JSON: {
            channels: [{ channel_id, name, description, visibility, member_of, owner_of }, ...]
        }
    """
    token = request.args.get("token")
    calling_user = get_user_from_token(token)
    printColour(
        " ➤ Channel List All: {} fetched a list of channels they belong to".
        format(calling_user.username),
        colour="blue")
    return jsonify(channels.channels_listall(token))
Exemple #13
0
def handle_user_channel_leave(event_data):
    """
        Parameter event_data is a dictionary containing:
            - token
            - room
    """
    token = event_data["token"]
    user = get_user_from_token(token)
    room = event_data["room"]
    leave_room(room)

    printColour(
        " ➤ Socket event: connection_user_leave. User {} left channel {}".
        format(user.username, room),
        colour="cyan",
        bordersOn=False)
Exemple #14
0
def handle_user_profile_update():
    """
        HTTP Route: /users/profile
        HTTP Method: POST
        Params: (token, email, username)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    email = request_data["email"]
    username = request_data["username"]
    printColour(
        " ➤ User Profile Update: setting username to {}, email to {}".format(
            username, email),
        colour="blue")
    return jsonify(users.users_profile_update(token, email, username))
def handle_connection_outgoing():
    """
        Fetches all of the users that the calling user has sent requests to
        Params: (token)
        Returns JSON: { 
            users: [ { user_id, email, username, profile_img_url }, ... ]
        }
    """
    token = request.args.get("token")

    calling_user = get_user_from_token(token)
    printColour(" ➤ Connections: {} is fetching a list of their outgoing connections".format(
        calling_user.username
    ), colour="blue")

    return jsonify(connections.connection_fetch_outgoing_users(token))
Exemple #16
0
def handle_channel_details():
    """
        HTTP Route: /channels/details
        HTTP Method: GET
        Params: (token, channel_id)
        Returns JSON: { name, description, visibility, channel_img_url, owner_members, all_members }
    """
    token = request.args.get("token")
    channel_id = int(request.args.get("channel_id"))

    target_channel = select_channel(channel_id)
    calling_user = get_user_from_token(token)

    printColour(" ➤ Channel Details: {}'s details fetched by {}".format(
        target_channel.name, calling_user.username),
                colour="blue")
    return jsonify(channels.channels_details(token, channel_id))
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))
Exemple #18
0
def handle_remove_message(token, message_id, room):
    calling_user = get_user_from_token(token)
    printColour(
        " ➤ Socket event: remove_message. Channel message {} was edited by {}".
        format(message_id, calling_user.username),
        colour="cyan",
        bordersOn=False)
    printColour(" ➤ Emitting event: message_removed",
                colour="cyan",
                bordersOn=False)
    try:
        message_remove(token, message_id)
    except InputError as err:
        emit_error(err.get_message())
    emit("message_removed",
         "The server says: someone has deleted a message",
         broadcast=True,
         room=room)
def handle_conection_remove_message():
    """
        HTTP Route: /connections/message
        HTTP Method: DELETE
        Params: (token, message_id)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    message_id = int(request_data["message_id"])
    
    calling_user = get_user_from_token(token)
    printColour(" ➤ Connections Messages: {} removed message id: {}".format(
        calling_user.username,
        message_id
    ), colour="blue")

    return jsonify(connections.connection_remove_message(token, message_id))
Exemple #20
0
def handle_channel_messages():
    """
        HTTP Route: /channels/messages
        HTTP Method: GET
        Params: (token, channel_id, start)
        Returns JSON: { messages, exhausted }
    """
    token = request.args.get("token")
    channel_id = int(request.args.get("channel_id"))
    start = int(request.args.get("start"))

    target_channel = select_channel(channel_id)
    calling_user = get_user_from_token(token)

    printColour(" ➤ Channel Messages: {}'s messages fetched by {}".format(
        target_channel.name, calling_user.username),
                colour="blue")
    return jsonify(channels.channels_messages(token, channel_id, start))
Exemple #21
0
def handle_channel_leave():
    """
        HTTP Route: /channels/leave
        HTTP Method: POST
        Params: (token, channel_id)
        Returns JSON: {  }
    """
    request_data = request.get_json()
    token = request_data["token"]
    channel_id = int(request_data["channel_id"])

    target_channel = select_channel(channel_id)
    calling_user = get_user_from_token(token)

    printColour(" ➤ Channel Leave: {} left channel {}".format(
        calling_user.username, target_channel.name),
                colour="blue")
    return jsonify(channels.channels_leave(token, channel_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))
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))
Exemple #24
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)
Exemple #25
0
def on_join(event_data):
    """
        Parameter event_data is a dictionary containing:
            - token
            - user_id
            - channel_id
            - room
    """
    token = event_data["token"]
    user = get_user_from_token(token)
    room = event_data["room"]
    join_room(room)

    printColour(
        " ➤ Socket event: user_enter. User {} entered channel {}".format(
            user.username, room),
        colour="cyan",
        bordersOn=False)
    emit("user_entered", broadcast=True, room=room)
Exemple #26
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 #27
0
def handle_send_message(token, channel_id, message, room):
    calling_user = get_user_from_token(token)
    target_channel = select_channel(channel_id)
    printColour(
        " ➤ Socket event: send_message. Message sent by {} to channel {}".
        format(calling_user.username, target_channel.name),
        colour="cyan")
    printColour(" ➤ Emitting event: receive_message",
                colour="cyan",
                bordersOn=False)
    try:
        message_send(token, int(channel_id), message)
    except InputError as err:
        emit_error(err.get_message())
    # Broadcast the newly sent message to all listening client sockets so the
    # chat field updates in 'realtime'
    emit("receive_message",
         "The server says: someone has sent a new message",
         broadcast=True,
         room=room)
Exemple #28
0
def error_handler(err):
    """
        Default error handler
    """
    response = err.get_response()
    try:
        printColour(" ➤ Error: {} {}".format(err, err.get_message()),
                    colour="red")
        response.data = dumps({
            "code": err.code,
            "name": "System Error",
            "message": err.get_message(),
        })
        response.content_type = 'application/json'
        return response
    except:
        printColour(" ➤ Error: {}".format(err), colour="red")
        response.data = dumps({"code": err.code, "name": "System Error"})
        response.content_type = 'application/json'
        return response
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 #30
0
def handle_search():
    """
        HTTP Route: /channels/search
        HTTP Method: GET
        Params: (token, channel_id, query_str)
        Returns JSON: {
            messages: [ { message_id, u_id, message, time_created, reacts }, ...]
        }
    """
    token = request.args.get("token")
    channel_id = request.args.get("channel_id")
    query_str = request.args.get("query_str")

    calling_user = get_user_from_token(token)
    target_channel = select_channel(channel_id)

    printColour(
        " ➤ Channel Search: {} looking for '{}' in {}'s messages".format(
            calling_user.username, query_str, target_channel.name),
        colour="blue")
    return jsonify(messages.messages_search_match(token, channel_id,
                                                  query_str))