Exemplo n.º 1
0
def message_unpin(token, message_id):
    """
    Given a message within a channel, mark it as "pinned" to be given special display treatment
    by the frontend.
    """
    # Check that token is valid
    check_token(token)

    # Check that the message is valid
    channel_id = get_channel_id_from_message(message_id)
    if channel_id is None:
        raise ValueError("Invalid message")

    # Check the user permissions
    user = get_user_from_token(token)
    if user["permission_id"] == 3:
        raise ValueError("The authorised user is not an admin")

    # Check if the messages is not pinned
    message = get_info_about_message(message_id)
    if message["is_pinned"] is False:
        raise ValueError("Message with ID message_id is already unpinned")

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

    # unpin the message
    message["is_pinned"] = False
    save()
    return {}
Exemplo n.º 2
0
def message_react(token, message_id, react_id):
    # check validity of inputs
    check_token(token)
    check_react_id(react_id)

    # get the channel id & check if message is valid
    channel_id = get_channel_id_from_message(message_id)
    if channel_id is None:
        raise ValueError("Invalid message")

    # get the user id & check if the user is in the channel
    u_id = get_user_from_token(token)["u_id"]
    if not is_member(u_id, channel_id):
        raise ValueError("User is not part of the channel")

    # get the msg
    msg = get_info_about_message(message_id)
    for reacts in msg["reacts"]:
        # find the react id
        if reacts["react_id"] == react_id:
            # check if the user did the same reacts already
            for user_id in reacts["u_ids"]:
                if user_id == u_id:
                    raise ValueError(
                        "User already reacted to the message with the same react"
                    )
            reacts["u_ids"].append(u_id)
            save()
            break
    return {}
Exemplo n.º 3
0
def message_unreact(token, message_id, react_id):
    # check validity of inputs
    check_token(token)
    check_react_id(react_id)

    # get the channel id & check if message is valid
    channel_id = get_channel_id_from_message(message_id)
    if channel_id is None:
        raise ValueError("Invalid message")

    # get the user id & check if the user is in the channel
    u_id = get_user_from_token(token)["u_id"]
    if not is_member(u_id, channel_id):
        raise ValueError("User is not part of the channel")

    # get the msg
    msg = get_info_about_message(message_id)
    for reacts in msg["reacts"]:
        # find the react id
        if reacts["react_id"] == react_id:
            # have to do it this way because we redefined ValueError
            if u_id in reacts["u_ids"]:
                reacts["u_ids"].remove(u_id)
                save()
            else:
                raise ValueError("User has not reacted to the message")
    return {}
Exemplo n.º 4
0
def channels_listall(token):
    # check validity of token
    check_token(token)

    # get the data
    user = get_user_from_token(token)

    # if the user is only a member he/she can only see all channels that is public
    # or he/she is part of
    channels = []
    if user["permission_id"] == 3:
        for channel in data["channels"]:
            if channel["is_public"] or is_member(user["u_id"],
                                                 channel["channel_id"]):
                # append to the channels
                channels.append({
                    "channel_id": channel["channel_id"],
                    "name": channel["name"],
                })
    else:  # if user is owner/admin
        for channel in data["channels"]:
            channels.append({
                "channel_id": channel["channel_id"],
                "name": channel["name"],
            })
    # return channels
    return {"channels": channels}
Exemplo n.º 5
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,
    }
Exemplo n.º 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
Exemplo n.º 7
0
def test_channel_join():
    helper.reset_data()
    # SETUP START
    user1 = helper.token_admin()
    user2 = helper.token_account_1()

    public_channel_id = helper.channelid_public(user1["token"])
    private_channel_id = helper.channelid_private(user1["token"])
    # SETUP END

    # Check that error is raised if an invalid token is passed in
    with pytest.raises(validation_helper.AccessError):
        stub.channel_join("fake token", public_channel_id)

    # Check that error is raised if an invalid channel_id is passed in
    with pytest.raises(validation_helper.ValueError):
        stub.channel_join(user1["token"], -1)

    with pytest.raises(validation_helper.ValueError):
        # check that validation_helper.ValueError is raised if channel does not exist
        # Assumption: channel_id cannot be negative
        stub.channel_join(user1["token"], -1)

    # check that nothing happens if user is already a member of the channel (assumption)
    stub.channel_join(user1["token"], public_channel_id)  # should raise no errors

    # check that channel_join is successful when channel is public
    stub.channel_join(user2["token"], public_channel_id)
    assert validation_helper.is_member(user2["u_id"], public_channel_id)

    with pytest.raises(validation_helper.AccessError):
        # check that validation_helper.AccessError is raised if channel
        # is private and user is not an admin
        stub.channel_join(user2["token"], private_channel_id)

    # check that channel_join is successful when channel is private and user is an admin
    stub.admin_userpermission_change(user1["token"], user2["u_id"], 2)
    stub.channel_join(user2["token"], private_channel_id)
    assert validation_helper.is_member(user2["u_id"], private_channel_id)
Exemplo n.º 8
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 {}
Exemplo n.º 9
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"]}
Exemplo n.º 10
0
def channels_list(token):
    # check that token is valid
    check_token(token)

    channels = []
    u_id = get_user_from_token(token)["u_id"]

    for channel in data["channels"]:
        if is_member(u_id, channel["channel_id"]) is True:
            channels.append({
                "channel_id": channel["channel_id"],
                "name": channel["name"],
            })
    return {
        "channels": channels,
    }
Exemplo n.º 11
0
def test_channel_leave():
    helper.reset_data()
    # SETUP START
    user = helper.token_admin()
    new_channel_id = helper.channelid_public(user["token"])
    # SETUP END

    # Check that error is raised if an invalid token is passed in
    with pytest.raises(validation_helper.AccessError):
        stub.channel_leave("fake token", new_channel_id)

    with pytest.raises(validation_helper.ValueError):
        # check that validation_helper.ValueError is raised if channel does not exist
        # Assumption: channel_id cannot be negative
        stub.channel_leave(user["token"], -1)

    # check that call is successful if channel exists and user is a member of it
    stub.channel_leave(user["token"], new_channel_id)
    assert validation_helper.is_member(user["u_id"], new_channel_id) is not True

    # check that nothing happens if user is not in the channel (assumption)
    stub.channel_leave(user["token"], new_channel_id)  # should raise no errors
Exemplo n.º 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)}