Example #1
0
def addowner(token, channel_id, u_id):
    'This is the function for channel_addowner'
    auth_store = get_auth_data_store
    channels_store = get_channel_data_store

    channel = get_channel(channel_id)
    user_id_adder = user_id_from_token(token)
    adder_dets = user_details(user_id_adder)
    addee_dets = user_details(u_id)

    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # InputError when user with user id u_id is already an owner of the channel
    if addee_dets in channel['owners']:
        raise InputError(description='Cannot add if user is already an owner of the channel')

    # InputError if user isn't even a member of the channel
    if not test_in_channel(u_id, channel):
        raise InputError(description='User is not a member of channel')

    # AccessError when the authorised user is not an owner of the slackr, or an owner of this
    # channel
    # Can't add someone as an owner if you're not an owner yourself
    if not check_owner(adder_dets, channel):
        raise AccessError(description='User is not an owner of the slackr, or an owner of this channel')
    # Otherwise user is added as an owner
    else:
        channel['owners'].append(addee_dets)
        channel['members'].remove(addee_dets)
        #channel['members'].remove(addee_dets)
        return {}
Example #2
0
def test_game_start():
    reset_workspace()

    # Register a user
    user1 = reg_user1()

    channel1 = create_ch1(user1)

    channel1_dets = get_channel(channel1['channel_id'])

    # start a game of hangman
    data = json.dumps({
        'token': user1['token'],
        'channel_id': channel1['channel_id'],
        'message': '/hangman'
    }).encode('utf-8')
    req = urllib.request.urlopen(
        urllib.request.Request(f"{BASE_URL}/message/send",
                               data=data,
                               headers={'Content-Type': 'application/json'}))
    payload = json.load(req)

    channel1_dets = get_channel(channel1['channel_id'])

    print(channel1_dets)
    '''

    assert channel1_dets['hangman_active'] == True
    assert {"u_id": 2, "name_first": "Hangman", "name_last": "Bot"} in channel1_dets['members']
    
    phrase = get_phrase()
    
    guess = phrase[0]

    #make a guess
    data = json.dumps({
        'token': user1['token'],
        'channel_id': channel1['channel_id'],
        'message': '/guess ' + str(guess)
    }).encode('utf-8')
    req = urllib.request.urlopen(urllib.request.Request(
        f"{BASE_URL}/message/send",
        data=data,
        headers={'Content-Type':'application/json'}
    ))
    payload2 = json.load(req)

    empty_guess = get_empty_guess()

    assert guess in empty_guess
    '''
    assert 1 == 1
Example #3
0
def removeowner(token, channel_id, u_id):
    'This is the function for channel_removeowner'
    auth_store = get_auth_data_store
    channels_store = get_channel_data_store

    user_id_remover = user_id_from_token(token)
    remover_dets = user_details(user_id_remover)

    removee_dets = user_details(u_id)

    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # InputError when user with user id u_id is not an owner of the channel
    if removee_dets not in channel['owners']:
        raise InputError(description='Cannot remove if user is not an owner of the channel')

    # AccessError when the authorised user is not an owner of the slackr, or an owner of this
    # channel
    # Can't remove someone if you're not owner yourself
    print(remover_dets)
    print(channel['owners'])
    if remover_dets not in channel['owners']:
        raise AccessError(description='User is not an owner of the slackr, or an owner of this channel')
    # Otherwise user is removed if they're an owner
    else:
        channel['owners'].remove(removee_dets)
        channel['members'].append(removee_dets)
        return {}
Example #4
0
def invite(token, channel_id, u_id):
    'This is the function for channel_invite'
    auth_store = get_auth_data_store

    # User ID of person inviting
    user_id_inviter = user_id_from_token(token)

    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # Check if userID is valid, InputError if not
    if not is_valid_user_id(u_id):
        raise InputError(description='Invalid user_id')

    # InputError if channel_id does not refer to a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')

    # Check if u_id is already in channel, InputError if not
    if test_in_channel(int(u_id), channel):
        raise InputError(description='User is already a part of the channel')

    # Check if authorised user is a member of the channel, AccessError if not
    if not test_in_channel(user_id_inviter, channel):
        raise AccessError(description='Authorised user is not a member of the channel')

    # User is added
    new_user = user_details(u_id)
    channel['members'].append(new_user)

    return {}
Example #5
0
def details(token, channel_id):
    'This is the function for channel_details'
    user = get_user_token(token)
    u_id1 = user['u_id']

    owner_members = []
    all_members = []

    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # If the channel is public, safe to display details
    # If not then raise error
    for owner in channel['owners']:
        owner_members.append(user_details(owner['u_id']))
        all_members.append(user_details(owner['u_id']))
    for member in channel['members']:
        all_members.append(user_details(member['u_id']))
    ret_package = {
        "name": channel['name'],
        "owner_members": owner_members,
        "all_members": all_members
    }

    # Get details of owners and members in the channel
    #if channel['is_public']:
    #    return ret_package
        #name = channel['name']

    # AccessError when authorised user is not a member of the channel
    if test_in_channel(u_id1, channel):
        return ret_package
    else:
        raise AccessError(description='Authorised user is not a member of the channel')
Example #6
0
def leave(token, channel_id):
    'This is the function for channel_leave'
    auth_store = get_auth_data_store
    channels_store = get_channel_data_store

    user = get_user_token(token)
    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # Check if authorised user is a member of the channel, AccessError if not
    if not test_in_channel(user['u_id'], channel):
        raise AccessError(description='Cannot leave channel when user is not a member in the first place')

    # User leaving channel
    user_det = user_details(user['u_id'])
    print(user_det)
    print(channel['owners'])
    if user_det in channel['owners']:
        channel['owners'].remove(user_det)

    print(channel['members'])
    if user_det in channel['members']:
        channel['members'].remove(user_det)

    return {}
Example #7
0
def start(payload):
    'function to start a standup'

    user = get_user_token(payload['token'])
    channel = get_channel(payload['channel_id'])

    standup = channel['standup']


    if standup['is_active'] is False:
        if test_in_channel(user['u_id'], channel) is True:
            if payload['length'] > 0:
                length = payload['length']
                standup['is_active'] = True

                time_finish = (datetime.now() + timedelta(seconds=length)).strftime("%H:%M:%S")

                standup['time_finish'] = time_finish

                timer = threading.Timer(length, end_standup, args=[payload])
                timer.start()

            else:
                raise InputError(description='Negative length is invalid')

        else:
            raise AccessError(description='User not in channel')

    else:
        raise InputError(description='An active standup is running')


    return time_finish
Example #8
0
def end_standup(payload):   # pylint: disable=R1711
    'run this function where it collates all messages into one'
    messages = get_messages_store()

    standup_message = ''

    channel = get_channel(payload['channel_id'])
    standup = channel['standup']
    user = get_user_token(payload['token'])

    # formating all the messages into one message package
    for msg in standup['messages']:
        standup_message += msg['Name_first']
        standup_message += ': '
        standup_message += msg['Message']
        standup_message += '\n'

    standup_message = standup_message[0:(len(standup_message)-1)]

    new_message = create_message()
    new_message['channel_id'] = channel['channel_id']
    new_message['u_id'] = user['u_id']
    new_message['message'] = standup_message

    messages.append(new_message)
    channel['messages'].append(new_message)

    # reset standup
    standup['is_active'] = False
    standup['time_finish'] = 'N/A'
    print('finished standup')
    return
Example #9
0
def join(token, channel_id):
    'This is the function for channel_join'
    auth_store = get_auth_data_store
    channels_store = get_channel_data_store

    user = get_user_token(token)

    user_dets = {
        'u_id' : user['u_id'],
        'name_first': user['name_first'],
        'name_last': user['name_last']
    }
    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # InputError if channel_id does not refer to a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')

    #or if not channel['is_public']
    if channel['is_public'] is True and user['permission_id'] == 1:
        channel['owners'].append(user_dets)
    elif channel['is_public'] is True and user['permission_id'] == 2:
        channel['members'].append(user_dets)
    elif channel['is_public'] is False and user['permission_id'] == 1:
        channel['owners'].append(user_dets)
    else:
        # AccessError when attempting to join a private channel
        raise AccessError(description='Cannot join a private channel')

    return {}
Example #10
0
def send(payload): # pylint: disable=R1711
    'sends a message to get buffered in the standup queue'
    user = get_user_token(payload['token'])
    channel = get_channel(payload['channel_id'])

    standup = channel['standup']

    if not test_in_channel(user['u_id'], channel):
        raise AccessError(description='User is not part of channel')

    if standup['is_active'] is not True:
        raise InputError(description='Active standup is not running')

    message = payload['message']
    if len(message) > 1000:
        raise InputError(description='Message too long')

    new_message = {
        'Name_first': user['name_first'],
        'Message': message
    }

    standup['messages'].append(new_message)

    return
Example #11
0
def send(payload):
    '''
    function to take a payload, create a new mesaage, fill
    in the parameters and append it to the channels messages
    store as well as the gloabal message_store
    '''
    user = get_user_from('token', payload['token'])
    channel = get_channel(payload['channel_id'])
    if not test_in_channel(user['u_id'], channel):
        raise AccessError(description='User is not in channel')

    # create a message data type, and fill in details
    # then append to the channels list of messages
    txt = payload['message']
    if len(txt) > 1000:
        raise InputError(description='Message is more than 1000 characters')

    # create the message dictionary
    new_message = create_message()
    new_message['u_id'] = user['u_id']
    new_message['message'] = txt
    new_message['channel_id'] = payload['channel_id']

    channel['messages'].append(new_message)

    if txt == '/hangman':
        if not channel['hangman_active']:
            hangman.start(channel)
            channel['hangman_active'] = True
        else:
            hangman.message(channel, 'There is already an active game running')
            hangman.message(channel, hangman.display_hangman())

    if txt.split()[0] == '/guess':
        if not channel['hangman_active']:
            hangman.message(
                channel, 'There is not a current game of hangman running.\n' +
                r'If you would like to start one, type \hangman into the chat')
        else:
            if len(txt.split()) == 2:
                new_guess = txt.split()[1]
                if new_guess in r'!@#$%^&*()_+-=[]\;<>?/~`:':
                    hangman.message(channel, 'Invalid guess, guess again')
                else:
                    hangman.guess(channel, new_guess)
            else:
                hangman.message(channel, 'Invalid guess, guess again')

    # append it to the messages_store
    messages = get_messages_store()
    messages.append(new_message)
    save_messages_store()

    # append it to the channels file
    save_channel_store()

    return new_message
Example #12
0
def sendlater(payload):
    '''
    Function to create a message and have it be sent at a
    '''

    user = get_user_from('token', payload['token'])
    channel = get_channel(payload['channel_id'])
    messages = get_messages_store()
    if not test_in_channel(user['u_id'], channel):
        raise InputError(description='User is not in channel')

    # create a message data type, and fill in details
    # then append to the channels list of messages
    txt = payload['message']
    if len(txt) > 1000:
        raise InputError(description='Message is more than 1000 characters')

    # create the message dictionary
    time = payload['time_sent']

    print(type(time))

    if time < int(datetime.now().timestamp()):
        raise InputError(description='Unable to send as ' +
                         'time sent is a time in the past')

    new_message = create_message()
    new_message['time_created'] = time
    new_message['u_id'] = user['u_id']
    new_message['message'] = txt
    new_message['channel_id'] = payload['channel_id']

    # append it to the messages_store first
    messages.append(new_message)
    save_messages_store()

    interval = (time - datetime.now().timestamp())

    # append to the channel message store at a later time
    timer = threading.Timer(interval,
                            append_later,
                            args=[new_message['message_id']])

    timer.start()

    # debugging purposes
    for msg in channel['messages']:
        print(msg['message'])

    return new_message['message_id']
Example #13
0
def pin(payload):  # pylint: disable=R1711
    'testing functionability for message pin'
    user = get_user_from('token', payload['token'])
    message = find_message(payload['message_id'])
    channel = get_channel(message['channel_id'])

    if message['is_pinned'] is True:
        raise InputError(description='Message is already pinned')

    if not test_in_channel(user['u_id'], channel):
        raise AccessError(
            description='You do not have permission to pin this message')

    if not check_owner(user, channel):
        raise InputError(description='You do not have permission')

    message['is_pinned'] = True

    return
Example #14
0
def remove(payload):  # pylint: disable=R1711
    '''
    Function to remove a message from a channel
    '''
    user = get_user_from('token', payload['token'])
    messages = get_messages_store()

    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if message['u_id'] != user['u_id']:
        if not check_owner(user, channel):
            raise AccessError(description='You do not have permission')

    messages.remove(message)
    channel['messages'].remove(message)

    return
Example #15
0
def active(payload):
    'check if a standup is active and return the neccessary details if it is'

    user = get_user_token(payload['token']) # pylint: disable=W0612
    channel = get_channel(payload['channel_id'])

    standup = channel['standup']


    if standup['is_active']:
        standup_info = {
            'is_active': True,
            'time_finish': standup['time_finish']
        }
    else:
        standup_info = {
            'is_active': False,
            'time_finish': 'N/A'
        }
    return standup_info
Example #16
0
def edit(payload):
    '''
    Function to remove a message from a channel
    '''
    message_store = get_messages_store()
    user = get_user_from('token', payload['token'])
    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if message['u_id'] != user['u_id']:
        if not check_owner(user, channel):
            raise AccessError(description='You do not have permission')

    if len(payload['message']) == 0:  # pylint: disable=C1801, R1705
        channel['messages'].remove(message)
        message_store.remove(message)
        return
    else:
        message['message'] = payload['message']
        return
Example #17
0
def react(payload):
    '''
    Function to add a react to a given message
    '''
    global REACT_IDS  # pylint: disable=W0603
    user = get_user_from('token', payload['token'])

    message = find_message(int(payload['message_id']))

    channel = get_channel(message['channel_id'])

    if int(payload['react_id']) not in REACT_IDS:
        raise InputError(description='Unable to react with react_id ' +
                         str(payload['react_id']))

    if not test_in_channel(user['u_id'], channel):
        raise InputError(description='Unable to react as you are ' +
                         'not a part of that channel')

    for i in message['reacts']:
        if i['react_id'] == payload['react_id']:
            # this react is already present in the message
            # just add another u_id
            if user['u_id'] in i['u_ids']:
                # if the user has reacted, unreact them
                unreact(payload)
            i['u_ids'].append(user['u_id'])
            return

    # no previous react wih react_id
    new_react = {
        'react_id': payload['react_id'],
        'u_ids': [],
        'is_user_reacted': False
    }
    new_react['u_ids'].append(user['u_id'])
    message['reacts'].append(new_react)

    return
Example #18
0
def unreact(payload):
    '''
    Function to remove a react from a message
    '''
    global REACT_IDS  # pylint: disable=W0603
    user = get_user_from('token', payload['token'])

    message = find_message(payload['message_id'])

    channel = get_channel(message['channel_id'])

    if int(payload['react_id']) not in REACT_IDS:
        raise InputError(description='Unable to react with react_id ' +
                         str(payload['react_id']))

    if not test_in_channel(user['u_id'], channel):
        raise InputError(description='Unable to react as you are ' +
                         'not a part of that channel')

    for i in message['reacts']:
        if i['react_id'] == payload['react_id']:
            # this react is already present in the message
            # just remove u_id
            if user['u_id'] not in i['u_ids']:
                raise InputError(description='Attempting to uncreact ' +
                                 'a message you have not reacted to')
            i['u_ids'].remove(user['u_id'])
            if len(i['u_ids']) == 0:  # pylint: disable=C1801
                # no one else has reacted, so remove react
                message['reacts'].remove(i)
                print(message['reacts'])
            return

    # unable to find react of react_id in messages
    raise InputError(description='Message with ID message_id ' +
                     'does not contain an active react with with ID react_id')
Example #19
0
def messages(token, channel_id, start):
    'This is the function for channel_messages'
    auth_store = get_auth_data_store

    u_id = user_id_from_token(token)
    # Check if channel exists using helper function
    channel = get_channel(channel_id)

    # Check if authorised user is a member of the channel, AccessError if not
    if not test_in_channel(u_id, channel):
        raise AccessError(description='Authorised user is not a member of the channel')

    # InputError if start is not of type int
    if not isinstance(start, int):
        raise InputError(description='Start is not of type integer')

    # InputError when start is greater than or equal to the total number of messages in the channel
    if start > len(channel['messages']):
        raise InputError(description='Start is greater than or equal to the total number of messages in the channel')

    # Incorrect indexing
    if start < 0:
        raise InputError(description='Incorrect indexing')

    # When there are 0 messages
    if (len(channel['messages'])) == 0:
        return {
            "messages": [],
            "start": start,
            "end": -1
        }

    messages_list = []
    messages_dict = {}
    if start + 50 > len(channel['messages']):
        interval = start + 50
    else:
        interval = len(channel['messages'])
    for msgs in channel['messages'][start: interval]:
        messages_dict = {
            'message_id': msgs['message_id'],
            'u_id': msgs['u_id'],
            'message': msgs['message'],
            'time_created': msgs['time_created'],
            'reacts': msgs['reacts'],
            'is_pinned': msgs['is_pinned']
        }
        for reacts in msgs['reacts']:
            for users_reacted in reacts['u_ids']:
                if u_id in reacts['u_ids']:
                    reacts['is_user_reacted'] = True
        messages_list.append(messages_dict)

    messages_list.reverse()

    if start < len(channel['messages']):
        if len(channel['messages'][start:]) > 50:
            end = start + 50
            return {
                "messages": messages_list,
                "start": start,
                "end": end
            }
        if len(channel['messages'][start:]) < 50:
            return {
                "messages": messages_list,
                "start": start,
                "end": -1
            }