Exemplo n.º 1
0
def channel_addowner(token, channel_id, u_id):
    '''
    This will set the user with u_id as the owner of target channel.
    Token refers to one of the owner of target channel.
    It will return an empty dictionaty.

    Args:
        param1: authorised user's token.
        param2: target channel.
        param3: new owner's u_id

    Returns:
        This will return an empty dictionary.

    Raises:
        InputError:
            1. Channel ID is not a valid channel
            2. When user with user id u_id is already an owner of the channel
        AccessError:
            1. when the authorised user is not an owner of
                the flockr, or an owner of this channel
            2. given token does not refer to a valid token
    '''
    auth_user = get_user_from_token(token)
    channel = get_channel_from_id(channel_id)
    invited_user = get_user_from_id(u_id)
    # access error when the token does not refer to a valid token
    if auth_user is None:
        raise AccessError(description='Invalid token')

    # input error when Channel ID is not a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')

    # input error when u_id does not refer to a valid user
    if invited_user is None:
        raise InputError(description='Invalid u_id')

    # input error when When user with user id u_id
    # is already an owner of the channel
    if invited_user['u_id'] in channel['owner_members']:
        raise InputError(description='Already an owner')

    # access error when the authorised user is not
    # an owner of the flockr, or an owner of this channel
    if is_user_an_owner(token, channel_id) is False:
        raise AccessError(description='Not permitted to add')

    channel['owner_members'].append(u_id)
    return {}
Exemplo n.º 2
0
def channel_removeowner(token, channel_id, u_id):
    '''
    This will remove the user with u_id from the owners of target channel.
    If u_id refers to the owner of flockr, it will ignore the request.
    Token refers to one of the owner of target channel.
    It will return an empty dictionaty.

    Args:
        param1: authorised user's token.
        param2: target channel.
        param3: the user's u_id who is removed from owners

    Returns:
        This will return an empty dictionary.

    Raises:
        InputError:
            1. Channel ID is not a valid channel
            2. When user with user id u_id is not an owner of the channel
        AccessError:
            1. when the authorised user is not an owner of
                the flockr, or an owner of this channel
            2. given token does not refer to a valid token
    '''
    auth_user = get_user_from_token(token)
    channel = get_channel_from_id(channel_id)
    removed_user = get_user_from_id(u_id)
    # access error when given token does not refer to a valid user
    if auth_user is None:
        raise AccessError(description='Invalid token')
    # input error when Channel ID is not a valid channel
    if channel is None:
        raise InputError(description='Invalid channel_id')
    # input error when u_id does not refer to a valid user
    if removed_user is None:
        raise InputError(description='Invalid u_id')
    # input error when user with user id u_id is not an owner of the channel
    if removed_user['u_id'] not in channel['owner_members']:
        raise InputError(description='Not a owner of channel')

    # accesss error when the authorised user is not
    # an owner of the flockr, or an owner of this channel
    if is_user_an_owner(token, channel_id) is False:
        raise AccessError(description='Not permitted to remove')
    channel['owner_members'].remove(u_id)
    return {}
Exemplo n.º 3
0
def message_pin(token, message_id):
    '''
    This function marks a message as 'pinned' to be given special viewership
    on the frontend

    Args:
        param1(str): authorised user's token
        param2(int): id of target msg

    Returns:
        it will return an empty dict

    Raises:
        InputError:
            1. message_id is not a valid message
            2. Message with ID message_id is already pinned
        AccessError:
            1. The authorised user is not a member of the channel that the message is within
            2. The authorised user is not an owner
    '''
    auth_user = get_user_from_token(token)
    msg_info = get_message_info(message_id)
    # access error when given token is invalid
    if auth_user is None:
        raise AccessError(description='Invalid token')
    # input error when given message_id is invalid
    if msg_info is None:
        raise InputError(description='Invalid message_id')

    ### AccessError if user is not a member of the channel that the message is within
    if auth_user['u_id'] not in msg_info['channel']['all_members']:
        raise AccessError(
            description='Not a member of the channel that the message is within'
        )

    ### AccessError if user is not an owner of the channel
    if is_user_an_owner(token, msg_info['channel_id']) is False:
        raise AccessError(description='User isnt an owner of the channel')

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

    ### Pin message
    msg_info['message']['is_pinned'] = True
    return {}
Exemplo n.º 4
0
def message_edit(token, message_id, message):
    '''
    This function will edit the message with given message_id.
    if given message is empty, it will detele that message

    Args:
        param1: authorised user's token
        param2: target message_id
        param3: new message

    Returns:
        it will return an empty dictionary

    Raises:
        AccessError:
            1. given token does not refer to a valid user
            2. Message with message_id was sent by the authorised user making this request
            3. The authorised user is an owner of this channel or the flockr
    '''
    # check entered message, if it's empty, call remove func
    if message == '':
        message_remove(token, message_id)
        return {}

    auth_user = get_user_from_token(token)
    msg_info = get_message_info(message_id)
    # access error when given token does not refer to a valid user
    if auth_user is None:
        raise AccessError(description='Invalid token')

    # access error when auth user does not have permission
    permittd = False
    if msg_info['u_id'] == auth_user['u_id']:
        permittd = True
    if is_user_an_owner(token, msg_info['channel_id']) is True:
        permittd = True
    if permittd is False:
        raise AccessError(description='User must be an owner')

    # do edit work
    for msg in msg_info['msg_list']:
        if msg['message_id'] == message_id:
            msg['message'] = message
    return {}
Exemplo n.º 5
0
def message_remove(token, message_id):
    '''
    This function will remove the message with given message_id.

    Args:
        param1: authorised user's token
        param2: target message_id

    Returns:
        it will return an empty dictionary

    Raises:
        InputError:
            Message (based on ID) no longer exists
        AccessError:
            1. given token does not refer to a valid user
            2. Message with message_id was sent by the authorised user making this request
            3. The authorised user is an owner of this channel or the flockr
    '''
    auth_user = get_user_from_token(token)
    msg_info = get_message_info(message_id)

    # access error when given token does not refer to a valid user
    if auth_user is None:
        raise AccessError(description='Invalid token.')

    # input error when given message_id does not refer to a valid message
    if msg_info is None:
        raise InputError(description='Invalid message ID.')

    # access error when auth user does not have permission
    permittd = False
    if msg_info['u_id'] == auth_user['u_id']:
        permittd = True
    if is_user_an_owner(token, msg_info['channel_id']) is True:
        permittd = True
    if permittd is False:
        raise AccessError(description='User must be an owner.')

    # do remove work
    msg_info['msg_list'].remove(msg_info['message'])
    return {}