def channel_leave(token, channel_id): ''' Using a valid token, the user leaves a channel specified by 'channel_id' Raises errors if: - Channel doesn't exist ''' is_valid_token(token) # When channel Id is invalid if not channel_exists(channel_id): raise ValueError('Channel does not exist') # Remove user from 'members' list in channel # get members list (from channel_id) members = members_list(channel_id) # remove user from members list user_id = token_to_user(token) for member in members: if member['u_id'] == user_id: members.remove(member) # Remove channel from users 'channels' list user_info = get_userinfo(user_id) user_info['channels'].remove(channel_id)
def channels_listall(token): ''' The following function list all the channels ''' is_valid_token(token) full_list = [] for channel in server_data.data['channels']: if channel['is_public']: full_list.append({ 'channel_id': channel['channel_id'], 'name': channel['name'] }) return full_list
def channels_list(token): ''' The following function creates a channel ''' is_valid_token(token) curr_user_id = token_to_user(token) channel_list = server_data.data['users'][curr_user_id]['channels'] full_list = [] for c_id in channel_list: curr_channel = server_data.data['channels'][c_id] full_list.append({ 'channel_id': curr_channel['channel_id'], 'name': curr_channel['name'] }) return full_list
def channel_removeowner(token, channel_id, u_id): ''' Using a valid token, remove a users permission as an owner in a specified channel Raises errors if: - Channel doesn't exist - the user is already an not an owner of that channel - token's user doesn't have permission to remove owners ''' is_valid_token(token) check_valid_user(u_id) token_id = token_to_user(token) check_valid_user(token_id) check_valid_channel(channel_id) if not check_channel_member(u_id, channel_id): raise ValueError('Target user is not in the channel') # User is already not an owner of that channel if not is_owner(u_id, channel_id): raise ValueError('User is already not an owner') curr_user_id = token_to_user(token) # When user is not an admin or owner, only owners can remove other owners (AccessError) if not is_owner(curr_user_id, channel_id) and not is_slackr_admin(curr_user_id): raise AccessError('You do not have permission to remove this owner') if server_data.data['users'][u_id]['permission'] == 1: raise AccessError('Cannot change the permission of Slackr creator') # Remove user permission as owner # get members list (from channel_id) members = members_list(channel_id) # change user permission to '0' for member in members: if member['u_id'] == u_id: member['channel_permission'] = 0
def message_sendlater(token, channel_id, message, time_sent): ''' Using a valid token, the user sends a message to a channel that they are part of at a specific time in the future ''' if not channel_exists(channel_id): raise ValueError(f"channel does not exist") if message is None: raise ValueError(f"No message") if int(time_sent) < t.time(): raise ValueError(f"Time is in the past") is_valid_token(token) is_valid_message(message) curr_user_id = token_to_user(token) check_valid_user(curr_user_id) if not check_channel_member(curr_user_id, channel_id): raise AccessError( f"User is not part of specified channel. Please join the channel.") message_id = server_data.data['n_messages'] server_data.data['n_messages'] += 1 messages_later = server_data.messages_later new_message = dict() new_message['message_id'] = message_id new_message['u_id'] = curr_user_id new_message['message'] = message new_message['time_created'] = int(time_sent) new_message['channel_id'] = channel_id new_message['reacts'] = [] new_message['is_pinned'] = False messages_later.append(dict(new_message)) return message_id
def channel_addowner(token, channel_id, u_id): ''' Using a valid token, add a user specified by 'u_id' as an owner of a specific channel Raises errors if: - Channel doesn't exist - the user is already an owner of that channel - token's user doesn't have permission to assign owners ''' is_valid_token(token) check_valid_user(u_id) token_id = token_to_user(token) check_valid_user(token_id) check_valid_channel(channel_id) if not check_channel_member(u_id, channel_id): raise ValueError('Target user is not in the channel') # When user is not an admin or owner, only owners can add other owners (AccessError) if not is_owner(token_id, channel_id) and not is_slackr_admin(token_id): raise AccessError('You do not have permission to assign an owner') # User is already an owner of that channel if is_owner(u_id, channel_id): raise ValueError('User is already an owner') # Change user permission to owner # get members list (from channel_id) members = members_list(channel_id) # change user permission to '1' for member in members: if member['u_id'] == u_id: member['channel_permission'] = 1
def channel_join(token, channel_id): ''' Using a valid token, the user joins a channel specified by 'channel_id' Raises errors if: - Channel doesn't exist - User tries to join a private channel ''' is_valid_token(token) # When channel Id is invalid if not channel_exists(int(channel_id)): raise ValueError('Channel does not exist') # When user tries to join a private channel ( AccessError) if not server_data.data['channels'][int(channel_id)]['is_public']: raise AccessError('Channel is invite only') user_id = token_to_user(token) # Add user to channel 'members' list (channel_permission 2) # create a new dictionary with u_id and default channel permission data = { 'u_id': user_id, 'channel_permission': 0, } # append this to the channel list members = members_list(channel_id) members.append(data) # Add channel to users 'channels' list # get user info user_info = get_userinfo(user_id) # add channel_id to 'channels' list user_info['channels'].append(channel_id)