def channel_invite(token, channel_id, u_id): ''' The following function invites a given user into a given channel ''' # may need to alter this so that only AUTHORISED members can invite others # Perform required checks check_valid_channel(channel_id) curr_user_id = token_to_user(token) check_valid_user(curr_user_id) check_valid_user(u_id) # Check the current user is actually a member of target channel if not check_channel_member(curr_user_id, channel_id): raise AccessError('User is not part of the target channel') # Check the target user is not already a member of target channel if check_channel_member(u_id, channel_id): raise ValueError( 'Target user is already a member of the target channel') # If it reaches here, all parameters are valid # target user becomes a member of the channel for channel in server_data.data['channels']: if channel['channel_id'] == channel_id: channel['members'].append({'u_id': u_id, 'channel_permission': 0}) break # channel is in the user's channel list for user in server_data.data['users']: if user['u_id'] == u_id: user['channels'].append(channel_id) break return {}
def channel_messages(token, channel_id, start): ''' The following function shows up to 50 messages in a given channel ''' # check token curr_user_id = token_to_user(token) # Check if the user exists check_valid_user(curr_user_id) check_valid_channel(channel_id) cha_data = server_data.data['channels'][channel_id] if not check_channel_member(curr_user_id, channel_id) and not cha_data['is_public']: raise AccessError('User is not in the target channel. Error code: 1') #tests for validity of token, channel_id and start # checks if start is valid else raise error valid_start(start) if start >= ( server_data.data["channels"][channel_id]["channel_n_messages"]): raise ValueError("Start is greater than total number of messages") # checks if channel_id is valid else raise error channel_id_exists(channel_id) return_amount = 49 messages = [] actual_start = server_data.data["channels"][channel_id][ "channel_n_messages"] - start - 1 counter = actual_start if actual_start > return_amount: last = actual_start - return_amount for counter in range(actual_start, last, -1): messages.append( server_data.data["channels"][channel_id]["messages"][counter]) end = start + return_amount else: end = -1 while counter > -1: messages.append( server_data.data["channels"][channel_id]["messages"][counter]) counter = counter - 1 # return correct react types for msg in messages: for react in msg['reacts']: if curr_user_id in react['u_ids']: react['is_this_user_reacted'] = True else: react['is_this_user_reacted'] = False return {'messages': messages, 'start': start, 'end': end}
def message_unreact(token, message_id, react_id): ''' This function unreacts to a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(message_id) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # check if the react_id is valid, only 1 in iteration 2 react_id_list = [1] if react_id not in react_id_list: raise ValueError("React ID is not valid") # check if the message already has the react_id # check if the user has reacted to that message present_flag = 0 for react_dict in message['reacts']: if react_dict['react_id'] == react_id: present_flag = 1 if curr_user_id in react_dict['u_ids']: # the current user has reacted react_dict['u_ids'].remove(curr_user_id) if len(react_dict['u_ids']) == 0: # remove react entirely if no more members del react_dict if present_flag == 0: raise ValueError("Message does not have that react") return {}
def channel_details(token, channel_id): ''' The following function gives details about a given channel ''' # check token curr_user_id = token_to_user(token) # Check if the user exists check_valid_user(curr_user_id) check_valid_channel(channel_id) channel_data = server_data.data['channels'][channel_id] if not check_channel_member(curr_user_id, channel_id) and not channel_data['is_public']: raise AccessError('User is not in the target private channel') # Return channel details # format: { name, owner_members, all_members } # add owner and all members owner_members = [] all_members = [] for member in channel_data['members']: user = server_data.data['users'][member['u_id']] all_members.append({ 'u_id': user['u_id'], 'name_first': user['name_first'], 'name_last': user['name_last'], 'profile_img_url': user['profile_img_url'] }) if member['channel_permission'] == 1: owner_members.append({ 'u_id': user['u_id'], 'name_first': user['name_first'], 'name_last': user['name_last'], 'profile_img_url': user['profile_img_url'] }) return ({ 'name': channel_data['name'], 'owner_members': owner_members, 'all_members': all_members })
def message_react(token, message_id, react_id): ''' This function reacts to a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(message_id) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # check if the react_id is valid, only 1 in iteration 2 react_id_list = [1] if react_id not in react_id_list: raise ValueError("React ID is not valid") # if the message already has the react id for react in message["reacts"]: if react['react_id'] == react_id: # check if the current user has already reacted if curr_user_id in react['u_ids']: raise ValueError("Message already reacted by current user") # react to the message! react['u_ids'].append(curr_user_id) return {} # otherwise, the message has not been reacted too message['reacts'].append({'react_id': react_id, 'u_ids': [curr_user_id]}) return {}
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_pin(token, message_id): ''' The following function pins a message ''' curr_user_id = token_to_user(token) message = get_msg_dict(message_id) channel = msg_to_channel(message_id) # check if the message_id is valid if message_id >= server_data.data["n_messages"]: raise ValueError("message_id is invalid") is_msg_removed(int(message_id)) # check if the user is valid check_valid_user(curr_user_id) # check if the channel is valid check_valid_channel(channel["channel_id"]) # check if the user is in the channel if not check_channel_member(curr_user_id, channel["channel_id"]): raise AccessError("User is not a member of the channel") # User must be a admin of either the channel or slackr check_permission = server_data.data["users"][curr_user_id]["permission"] check_isowner = is_owner(curr_user_id, channel["channel_id"]) if (not check_isowner) and check_permission == 3: raise ValueError("User is not authorised for this action") # check if the message is already pinned if message["is_pinned"]: raise ValueError("Message is already pinned") message["is_pinned"] = True return {}
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