Example #1
0
def channel_addowner(token, channel_id, u_id):

    # Check the validity of the inputs
    check_token(token)
    check_channel_id(channel_id)
    check_u_id(u_id)

    # Check that the user is not an owner of the specified channel
    if is_owner(u_id, channel_id) is True:
        raise ValueError("User is already an owner of the channel")

    # Check that the user is authorised to add the owner
    auth_user = get_user_from_token(token)["u_id"]
    if is_owner(auth_user, channel_id) is False:
        raise AccessError(
            "Authorised user is not an owner of slackr, or an owner of the channel"
        )

    # Get user info
    user = get_user_from_id(u_id)

    # Create a dict of type member
    member_details = {
        info: user[info]
        for info in ["u_id", "name_first", "name_last"]
    }

    # Add user to channel
    get_channel(channel_id)["owner_members"].append(member_details)

    save()
    return {}
Example #2
0
def standup_send(token, channel_id, message):

    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # Check the message is less than 1000 characters
    if len(message) > 1000:
        raise ValueError("Message can't be more than 1000 characters")

    # get the channel from channel_id
    channel = get_channel(channel_id)

    # Access error if user is not in the channel
    if get_member(token) not in get_channel(channel_id)["all_members"]:
        raise AccessError("User is not part of the channel")

    # Check if a standup is already running
    if channel["active_standup"] is False:
        raise ValueError("Standup is not running")

    # get the handle from token
    user = get_user_from_token(token)
    handle = user["handle_str"]

    # add handle to message
    string = handle + ": " + message.strip() + "\n"

    # add string to the standup message
    channel["standup_message"]["message"] += string

    save()
    return {}
Example #3
0
def channel_join(token, channel_id):
    # check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # get data
    user = get_user_from_token(token)
    channel = get_channel(channel_id)

    # create a dict of type member
    member_details = get_member(token)

    # check if user is authorised to join
    if channel["is_public"] is False and user["permission_id"] == 3:
        raise AccessError("Channel is private")

    # check if user is already in the channel
    if member_details in channel["all_members"]:
        return {}

    # Add user to channel
    channel["all_members"].append(member_details)

    # Add user to owner list if they have perms
    if user["permission_id"] != 3:
        channel["owner_members"].append(member_details)
    save()
    return {}
Example #4
0
def channel_details(token, channel_id):

    # check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # get the channel
    channel = get_channel(channel_id)

    # Check if authorised user is a member of the channel
    u_id = get_user_from_token(token)["u_id"]
    if is_member(u_id, channel_id) is False:
        raise AccessError("User is not a member of the channel")

    # create a copy to return with the correct urls
    owner_members_copy = copy.deepcopy(channel["owner_members"])
    all_members_copy = copy.deepcopy(channel["all_members"])
    for member in owner_members_copy:
        member["profile_img_url"] = get_host_from_path(
            member["profile_img_url"])
    for member in all_members_copy:
        member["profile_img_url"] = get_host_from_path(
            member["profile_img_url"])

    return {
        "name": channel["name"],
        "owner_members": owner_members_copy,
        "all_members": all_members_copy,
    }
Example #5
0
def search(token, query_str):

    # update messages
    for channel in data["channels"]:
        channel_id = channel["channel_id"]
        check_future(channel_id)
        check_standup(channel_id)

    # check that token is valid
    check_token(token)

    results = []

    channels = channels_list(token)["channels"]
    for channel in channels:
        for message in get_channel(channel["channel_id"])["messages"]:
            if query_str.lower() in message["message"].lower():
                results.append(message)
    u_id = get_user_from_token(token)["u_id"]

    # need to add is_this_user_reacted
    results = get_is_this_user_reacted(u_id, results)

    # now need to change datetimes to strings
    for message in results:
        message["time_created"] = get_serializable_datetime(
            message["time_created"])

    return {"messages": results}
Example #6
0
def channel_messages(token, channel_id, start):

    # check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # get the data
    user = get_user_from_token(token)
    channel = get_channel(channel_id)

    check_future(channel_id)
    check_standup(channel_id)

    # no negative values allowed
    if start < 0:
        raise ValueError("Index starts from 0 being the latest message")

    # user is not a member of the channel
    if not is_member(user["u_id"], channel["channel_id"]):
        raise AccessError("User is not a member of the channel")

    # if the start is greater than the total number of messages
    if start > len(channel["messages"]):
        raise ValueError("start is greater than the total number of messages")

    # set the end
    end = start + 50

    # change end if end is too large
    if end > len(channel["messages"]):
        end = len(channel["messages"])
        return_dict = {
            "messages": channel["messages"][start:end],
            "start": start,
            "end": -1
        }
    else:
        return_dict = {
            "messages": channel["messages"][start:end],
            "start": start,
            "end": end
        }

    return_dict["messages"] = get_is_this_user_reacted(user["u_id"],
                                                       return_dict["messages"])
    # now need to change datetimes to strings
    for message in return_dict["messages"]:
        message["time_created"] = get_serializable_datetime(
            message["time_created"])

    # print(return_dict)
    return return_dict
Example #7
0
def standup_active(token, channel_id):
    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    channel = get_channel(channel_id)

    if channel["active_standup"]:
        time_finish = get_serializable_datetime(
            channel["standup_message"]["time_created"])
        return {"is_active": True, "time_finish": time_finish}

    return {"is_active": False, "time_finish": None}
Example #8
0
def message_send(token, channel_id, message):
    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # Check if any future messages or standup messages need to be sent
    check_future(channel_id)
    check_standup(channel_id)

    # Invalid name
    if len(message) > 1000:
        raise ValueError("Message can't be more than 1000 characters")

    # grab the user data from token
    user = get_user_from_token(token)

    # Get channel
    channel = get_channel(channel_id)

    # Check if the user has joined the channel
    if is_member(user["u_id"], channel_id) is False:
        raise AccessError("User is not a member of the channel.")

    message = {
        "message_id":
        data["n_messages"] + 1,
        "u_id":
        user["u_id"],
        "message":
        message,
        "time_created":
        datetime.utcnow(),
        "reacts": [{
            "react_id": 0,
            "u_ids": []
        }, {
            "react_id": 1,
            "u_ids": []
        }, {
            "react_id": 2,
            "u_ids": []
        }],
        "is_pinned":
        False
    }

    channel["messages"].insert(0, message)
    data["n_messages"] += 1
    save()
    return {"message_id": message["message_id"]}
Example #9
0
def channel_leave(token, channel_id):
    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # create a dict of type member
    member_details = get_member(token)

    # Remove user from a channel
    channel = get_channel(channel_id)
    if member_details in channel["all_members"]:
        channel["all_members"].remove(member_details)
        if member_details in channel["owner_members"]:
            channel["owner_members"].remove(member_details)
    save()
    return {}
Example #10
0
def channel_invite(token, channel_id, u_id):

    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)
    check_u_id(u_id)

    # get info about inviter
    inviter = get_user_from_token(token)

    # Raise access error if they aren't a member of the channel they are creating an invite for
    if is_member(inviter["u_id"], channel_id) is False:
        raise AccessError("Cannot invite to a channel you are not in")

    # get user data from u_id
    invitee = get_user_from_id(u_id)

    # get channel
    channel = get_channel(channel_id)

    # create a dict of type member
    invitee_profile = user_profile(token, u_id)
    member_details = {
        "u_id": u_id,
        "name_first": invitee_profile["name_first"],
        "name_last": invitee_profile["name_last"]
    }

    if is_member(u_id, channel_id) is False:
        # Add user to channel
        channel["all_members"].append(member_details)

        # Add user to owner list if they have perms
        if invitee["permission_id"] != 3:
            channel["owner_members"].append(member_details)

    save()
    return {}
Example #11
0
def message_sendlater(token, channel_id, message, time_sent):
    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # Check that message is less than 1000 characters
    if len(message) > 1000:
        raise ValueError("Message can't be more than 1000 characters")

    # Check that the time sent is not the past
    time_now = datetime.utcnow()
    if time_sent < time_now:
        raise ValueError("Invalid time set")

    # Get channel
    channel = get_channel(channel_id)

    # Raise an access error if user not in channel
    if get_member(token) not in channel["all_members"]:
        raise AccessError("User is not a member of the channel.")

    # Send a message from authorised_user to a specific channel at a specified time in the future

    # Grab the user data
    user = get_user_from_token(token)

    # Set the message dictionary up
    message = {
        "message_id":
        data["n_messages"] + 1,
        "u_id":
        user["u_id"],
        "message":
        message,
        "time_created":
        time_sent,
        "reacts": [{
            "react_id": 0,
            "u_ids": []
        }, {
            "react_id": 1,
            "u_ids": []
        }, {
            "react_id": 2,
            "u_ids": []
        }],
        "is_pinned":
        False
    }
    # Reserving the unique message id. this message id is not valid until message is sent,
    # but the message id will be usable after it is sent
    data["n_messages"] += 1

    future_messages = channel["future_messages"]
    future_messages.append(message)

    # sort the list by timestamp(Source:
    # https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary)
    future_messages = sorted(future_messages, key=lambda k: k["time_created"])

    # save the sorted list
    channel["future_messages"] = future_messages

    save()
    return {"message_id": message["message_id"]}
Example #12
0
def standup_start(token, channel_id, length):

    # Check validity of inputs
    check_token(token)
    check_channel_id(channel_id)

    # Get the user id from token
    u_id = get_user_from_token(token)["u_id"]

    # Access error if user is not in the channel
    if is_member(u_id, channel_id) is False:
        raise AccessError("Member not in channel")

    # Check if a standup is already running
    for channel in data["channels"]:
        if channel["channel_id"] == channel_id and channel["active_standup"]:
            raise ValueError("Standup is already running")

    # Turn on standup
    for channel in data["channels"]:
        if channel["channel_id"] == channel_id and (channel["active_standup"]
                                                    is False):
            channel["active_standup"] = True

    # create temp standup message dictionary
    standup_message = {}

    # Find standup time and standup finish
    # Due to changes in iteration 3, the standup interval will be a given
    # variable input in seconds called 'length'

    time_finish = datetime.utcnow() + timedelta(seconds=length)

    # create temp standup message dictionary
    standup_message = {
        "message_id":
        data["n_messages"] + 1,
        "u_id":
        u_id,
        "message":
        "",
        "time_created":
        time_finish,
        "reacts": [{
            "react_id": 0,
            "u_ids": []
        }, {
            "react_id": 1,
            "u_ids": []
        }, {
            "react_id": 2,
            "u_ids": []
        }],
        "is_pinned":
        False
    }

    # reserve the message_id
    data["n_messages"] += 1

    # add the standup message to the channel
    get_channel(channel_id)["standup_message"] = standup_message

    save()
    return {"time_finish": get_serializable_datetime(time_finish)}