Ejemplo n.º 1
0
def test_get_channel_info(channel1):
    """
    Returns channel info when given corresponding channel_id
    """
    channel_id = channel1["channel_id"]
    assert data.get_channel_info(channel_id) == data.channels[channel_id]
    assert data.get_channel_info(561) == None
    
    other.clear()
Ejemplo n.º 2
0
def search(token, query_str):
    """
    Returns a dictionary containing a list of all users in Flockr

    Parameters:
        token(string): A user authorisation hash

    Returns:
        messages: A dictionary containing a list of all messages that the user has sent 
                  with the corresponding query string
    """
    # Check that token is valid
    validation.check_valid_token(token)

    # Initialises messages to return to user
    messages = []

    # Finds all messages that the user has sent containing the query string

    user_channels = channels.channels_list(token)["channels"]
    print(user_channels)
    for channel in user_channels:
        channel_info = data.get_channel_info(channel["channel_id"])
        for message in channel_info["messages"].values():
            print(message['message'])
            if query_str in message["message"]:
                messages.append(message)

    return {'messages': messages}
Ejemplo n.º 3
0
def check_active_hangman(channel_id):
    """
    Checks if a hangman session is active
    """
    channel = data.get_channel_info(channel_id)
    if not channel['hangman']['is_active']:
        raise InputError(description="There is no currently active hangman session")
Ejemplo n.º 4
0
def check_can_start_hangman(channel_id):
    """
    Checks if its ok to start hangman session

    Parameters:
        channel_id(int): The id of channel
    Returns:
        Raises error if not enough members
        If it is valid it returns nothing
    """
    channel = data.get_channel_info(channel_id)
    if len(channel['members']) < 2:
        raise InputError(description="Not enough people to start hangman")
Ejemplo n.º 5
0
def check_channel_is_public(channel_id):
    """
    Check if channel is public or private

    Parameters:
        channel_id(int): The id of channel
    Returns:
    """
    channel = data.get_channel_info(channel_id)
    if channel["is_public"]:
        return
    else:
        raise AccessError(description = "Cannot join private channel")
Ejemplo n.º 6
0
def check_valid_channel_id(channel_id):
    """
    Determines whether given channel_id matches an existing channel

    Parameters:
        channel_id(int): Identifier for a channel

    Returns:
        Raises an error if channel doesn't exist
        Returns nothing if channel exists
    """
    if data.get_channel_info(channel_id) is None:
        raise InputError(description = "Channel does not exist")
Ejemplo n.º 7
0
def channel_messages(token, channel_id, start):
    """
    Checks that given information (token, channel_id, u_id) is valid then 
    given a Channel with ID channel_id that the authorised user is part of,
    return up to 50 messages between index "start" and "start + 50". Message
    with index 0 is the most recent message in the channel. This function
    returns a new index "end" which is the value of "start + 50", or, if this
    function has returned the least recent messages in the channel, returns -1
    in "end" to indicate there are no more messages to load after this return.

    Parameters:
        token(string): an authorisation hash
        channel_id(int): identifier for channel
        start(int): index of starting message

    Returns:
        (dict): {messages, start, end}
    """
    # Check if valid token
    u_id = validation.check_valid_token(token)

    # Check if given valid channel_id
    validation.check_valid_channel_id(channel_id)

    # Check if user is authorised(member of channel)
    validation.check_user_in_channel(u_id, channel_id)

    # Proceed to getting messages
    messages_list = {"messages": [], "start": start, "end": start + 50}

    channel = data.get_channel_info(channel_id)
    # Checking if start value is greater than number of total messages
    # and if so, raise Input Error
    if len(channel["messages"]) < start:
        raise InputError(description="Start value is too high")
    for message in reversed(
            list(channel["messages"].keys())[start:start + 50]):

        for react in channel["messages"][message]["reacts"]:
            if u_id in react['u_ids']:
                react['is_this_user_reacted'] = True
            else:
                react['is_this_user_reacted'] = False

        messages_list["messages"].append(channel["messages"][message])
    # Returns -1 when "end" is less than start + 50 and no more messages
    # to load
    if len(channel["messages"]) < start + 50:
        messages_list["end"] = -1
    return messages_list
Ejemplo n.º 8
0
def check_stop_permission(u_id, channel_id):
    """
    Checks if user has permission to use '/hangman stop'

    Parameters:
        u_id(int): Identifier used for users
        channel_id(int): Identifier used for channels

    Returns:
        Nothing if user has permission
        Raises InputError if user does not have permission
    """
    channel = data.get_channel_info(channel_id)
    hang_info = data.get_hangman_info(channel_id)
    if u_id not in channel['owners'] and hang_info['u_id'] != u_id: # pragma: no cover
        raise InputError(description="User does not have permission to use command")
Ejemplo n.º 9
0
def channel_details(token, channel_id):
    """
    Checks that given information (token, channel_id, u_id) is valid then 
    given a Channel with ID channel_id that the authorised user is part of,
    provide basic details about the channel

    Parameters:
        token(string): an authorisation hash
        channel_id(int): identifier for channel

    Returns:
        (dict): { name, owner_members, all_members }
    """
    # Check if token is valid
    u_id = validation.check_valid_token(token)

    # Check if given valid channel_id
    validation.check_valid_channel_id(channel_id)

    # Check if user is authorised(member of channel)
    validation.check_user_in_channel(u_id, channel_id)

    # Everything valid, Proceed with getting details
    channel_info = {"name": "", "owner_members": [], "all_members": []}
    # Find channel and copy infomation into channel_details
    channel = data.get_channel_info(channel_id)
    channel_info["name"] = channel["name"]
    for u_id in channel["members"]:
        user = data.get_user_info(u_id)
        member = {}
        member["u_id"] = user["u_id"]
        member["name_first"] = user["name_first"]
        member["name_last"] = user["name_last"]
        member["name_last"] = user["name_last"]
        member["profile_img_url"] = user["profile_img_url"]
        channel_info["all_members"].append(member)

    for u_id in channel["owners"]:
        user = data.get_user_info(u_id)
        owner = {}
        owner["u_id"] = user["u_id"]
        owner["name_first"] = user["name_first"]
        owner["name_last"] = user["name_last"]
        owner["profile_img_url"] = user["profile_img_url"]
        channel_info["owner_members"].append(owner)
    return channel_info