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')
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 {}
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 {}
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 {}
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 {}
def test_admin_user_remove_successful3(): 'Successful case with messages' workspace_reset() channel_store = get_channel_data_store() messages_store = get_messages_store() ret = register_and_create() user1 = ret['user'] token1 = user1['token'] u_id1 = user1['u_id'] user2 = reg_user2() token2 = user2['token'] u_id2 = user2['u_id'] channel_info = ret['channel'] channel_id = channel_info['channel_id'] channel.join(token2, channel_id) payload = {'token': token1, 'u_id': u_id2} msg1 = send_msg1(user2, channel_info) # Remove user1 from slackr user_remove(payload) user2_dets = user_details(u_id2) channel_info = ret['channel'] channel_id = channel_info['channel_id'] for curr_channel in channel_store: channel_id = curr_channel['channel_id'] if user2_dets in curr_channel['members']: assert channel.details(token1, channel_id)['all_members'] == [{ "u_id": u_id1, "name_first": 'Kennan', "name_last": 'Wong' }] if user2_dets in curr_channel['owners']: assert channel.details(token1, channel_id)['owner_members'] == [{ "u_id": u_id1, "name_first": 'Kennan', "name_last": 'Wong' }] assert msg1 not in messages_store assert msg1 not in channel_info['messages']
def user_remove(payload): 'This is the function for admin_user_remove' auth_store = get_auth_data_store() channel_store = get_channel_data_store() messages_store = get_messages_store() # remover_id = user_id_from_token(payload['token']) # remover_dets = user_details(remover_id) removee = get_user_uid(payload['u_id']) removee_dets = user_details(payload['u_id']) # InputError for invalid user ID of the person we are removing if not is_valid_user_id(int(payload['u_id'])): raise InputError(description='Invalid user_id') removee_token = removee['token'] # Removing any messages the user had sent for messages in messages_store: if message_belong_user(removee_token, messages['message_id']): messages_store.remove(messages) # Removing user from any channels they were in for channel in channel_store: if removee_dets in channel['members']: channel['members'].remove(removee_dets) if removee_dets in channel['owners']: channel['owners'].remove(removee_dets) for message in channel['messages']: if message['u_id'] == removee['u_id']: channel['messages'].remove(message) # AccessError when the authorised user is not an owner of the slackr if not check_owner_slackr(payload['token']): raise AccessError(description='User is not an owner of the slackr') else: auth_store.remove(removee) return {}