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
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
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
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 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 {}
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 create(payload): 'implementations of channels create function' channel_store = get_channel_data_store() channel_owner_info = {} new_channel_info = {} user = get_user_token(payload['token']) new_channel_info = {} channel_owner_info = { 'u_id': user['u_id'], 'name_first': user['name_first'], 'name_last': user['name_last'], } if len(payload['name']) < 21: name = payload['name'] if payload['is_public']: new_channel_info = { 'channel_id': int(len(channel_store) + 1), 'name': name, 'is_public': True, 'members': [], 'owners': [], 'messages': [], 'standup': { 'is_active': False, 'messages': [], 'time_finish': [], }, 'hangman_active': False, 'hangman_data': [] } else: new_channel_info = { 'channel_id': int(len(channel_store) + 1), 'name': name, 'is_public': False, 'members': [], 'owners': [], 'messages': [], 'standup': { # pylint: disable=C0330 'is_active': False, # pylint: disable=C0330 'messages': [], # pylint: disable=C0330 'time_finish': [], # pylint: disable=C0330 }, # pylint: disable=C0330 'hangman_active': False } else: raise InputError(description='Name is too long') new_channel_info['owners'].append(channel_owner_info) channel_store.append(new_channel_info) save_channel_store() return new_channel_info
def profile_setemail(payload): ''' Update the authorised user's email address ''' #test email is valid and not been used before new_email = test_email(payload['email']) assert check_used_email(new_email) == 1 user = get_user_token(payload['token']) user['email'] = new_email return {}
def user_profile_uploadphoto(payload): ''' Given a URL of an image on the internet, crops the image within bounds ''' #Do a check to see if token is valid and to check if image is jpg global IMAGE_INDEX IMAGE_INDEX += 1 user = get_user_token(payload['token']) port = sys.argv[1] url = f"http://localhost:{port}/static/" file_path = './static/' + str(IMAGE_INDEX) + '.jpg' try: urllib.request.urlretrieve(payload['img_url'], file_path) except Exception as e: if isinstance(e) != 200: raise InputError(description="HTTP status not 200") elif imghdr.what(file_path) != 'jpg': raise InputError(description="Image not a jpg image") else: pass #crop the url image imageobject = Image.open(file_path) width, height = imageobject.size x_start = payload['x_start'] y_start = payload['y_start'] x_end = payload['x_end'] y_end = payload['y_end'] if x_start is None and x_end is None and y_start is None and y_end is None: x_start = 0 x_end = width y_start = 0 y_end = height #raises an input error if the values provided are outside #the bounds of the image if x_end > width or y_end > height or x_start >= x_end or y_start >= y_end: raise InputError("Crop has to be within the dimensions of the photo") cropped = imageobject.crop((x_start, y_start, x_end, y_end)) cropped.save(file_path) user['profile_img_url'] = url + ".jpg" return {}
def profile_sethandle(payload): ''' Update the authorised user's handle (i.e. display name) ''' #test handle is valid and not been used before if len(payload['handle_str']) < 3 or len(payload['handle_str']) > 20: raise InputError( description='handle_str should be between 3-20 characters') assert check_used_handle(payload['handle_str']) == 1 user = get_user_token(payload['token']) user['handle_str'] = payload['handle_str'] return {}
def profile_setname(payload): ''' Update the authorised user's first and last name ''' user = get_user_token(payload['token']) if not 1 < len(payload['name_first']) < 50: raise InputError( description='Invalid name_first, above the range of 50 characters') if not 1 < len(payload['name_last']) < 50: raise InputError( description='Invalid name_last, above the range of 50 characters') user['name_first'] = payload['name_first'] user['name_last'] = payload['name_last'] return {}
def permission_change(payload): #what does it mean for permision id to not refer to permission id? ''' changes the permision of a authorised uid We must also update their permissions in all channels they have joined ''' if validate_uid(payload['u_id']) is False: raise InputError(description='Invalid u_id') owner = get_user_token(payload['token']) change_user = get_user_from('u_id', payload['u_id']) if owner['permission_id'] != 1: raise AccessError(description='The authorised user is not an owner') change_user['permission_id'] = payload['permission_id'] '''
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
def List(token): # pylint: disable=C0103 'implementations of channels all functions' channel_store = get_channel_data_store() channels = [] channel_info = {} user = get_user_token(token) u_id = user['u_id'] for channel in channel_store: if test_in_channel(u_id, channel): channel_info = { 'channel_id': channel['channel_id'], 'name': channel['name'], } channels.append(channel_info) print(channels) return channels
def search(payload): ''' Given a query string, return a collection of messages in all of the channels that the user has joined that match the query. Results are sorted from most recent message to least recent message ''' channel_store = get_channel_data_store() return_message = [] user = get_user_token(payload['token']) query_str = payload['query_str'] # if the query is nothing if query_str == '': return [] for channel in channel_store: if test_in_channel(user['u_id'], channel): print("searching in channel: " + channel['name']) for msg in channel['messages']: if re.search(payload['query_str'].lower(), msg['message'].lower()): result = { 'message_id': msg['message_id'], 'u_id': msg['u_id'], 'message': msg['message'], 'time_created': msg['time_created'], 'reacts': msg['reacts'], 'is_pinned': msg['is_pinned'] } return_message.append(result) print(return_message) return return_message