def channel_leave(token, channel_id): '''Removes member from a channel. Parameters: token (str): authorization hash channel_id (int): channel identification ''' if channel_check(channel_id) == False: raise InputError if check_if_user_in_channel_member(token, channel_id) == False: raise AccessError channel = channel_check(channel_id) user = token_check(token) for inner in channel['all_members']: if int(inner['u_id']) == int(user['u_id']): channel['all_members'].remove(inner) for leave in user['channel_id_part']: if int(leave) == int(channel_id): user['channel_id_part'].remove(leave) return {}
def c_add_owner(): """ This is a flask wrapper for the channel_addowner function Parameters: No parameters Returns: (dictionary): Empty dictionary """ #Request information data = request.get_json() token = data['token'] u_id = data['u_id'] channel_id = data['channel_id'] if not channel_check(channel_id): raise InputError(description="Channel is invalid") if check_if_user_in_channel_owner_uid(u_id, channel_id): raise InputError(description="User already owner of channel") if check_if_user_in_channel_owner(token, channel_id): raise AccessError(description="User already owner of channel") channel_addowner(token, channel_id, u_id) return dumps({})
def message_send(token, channel_id, message): """ Sends a message to the designated channel Parameters: token (string) channel_id(int) message(string) Returns: (dictionary): A dictionary containing the message_id of the message that was sent. """ user = token_check(token) if not user: raise InputError channel = channel_check(channel_id) if channel is None: raise InputError #if check_user_in_channel(user['u_id'], channel_id) == False: # raise AccessError if (len(message) > 1000): raise InputError if not member_channel_check(token, channel_id): raise AccessError # message_store = get_messages_store() for member in channel['all_members']: if user['u_id'] == member['u_id']: message_id = make_message(message, channel_id, user['u_id'], 0) if message == "/hangman": channel['Hangman']['is_hangman_active'] = True make_message( "Welcome to Hangman!\n Please input your guess to start the game in the format /guess x where x is your guess\n", channel_id, user['u_id'], 0) if message[0:6] == "/guess" and channel['Hangman'][ 'is_hangman_active'] == True: hangman = play_hangman(message[7]) print('full hang', hangman) print('this is whats being used to print') print('current word:', hangman['current_word']) print('Hangman drwing:', hangman['hang_man_drawing']) if len(hangman['final']) != 0: channel['Hangman']['is_hangman_active'] = False make_message( hangman['hang_man_drawing'] + "\nWord to guess: " + hangman['print_word'] + hangman['final'], channel_id, user['u_id'], 0) return { 'message_id': message_id, }
def channel_join(token, channel_id): '''Adds a member to a channel. Parameters: token (str): authorization hash channel_id (int): channel identification ''' if channel_check(channel_id) == False: raise InputError if (check_if_channel_is_public(channel_id) == False or check_if_user_in_channel_member(token, channel_id) == True): raise AccessError channel_store = get_channel_store() channel = channel_check(channel_id) user = token_check(token) for channel in channel_store["Channels"]: #print("gets in for loop") if channel["channel_id"] == int(channel_id): #print("gets in if statement") channel["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'] }) user['channel_id_part'].append(channel_id) return {}
def channel_details(token, channel_id): '''Provides user with the basic information of a channel, given channel id, if user is part of channel. Parameters: token (str): authorization hash channel_id (int): channel identification Returns: (dict): returns details about channel ''' if channel_check(channel_id) == False: raise InputError if check_if_user_in_channel_member(token, channel_id) == False: raise AccessError ch_dict = {} channel_info = channel_check(channel_id) ch_dict['name'] = channel_info['name'] ch_dict['owner_members'] = channel_info['owner_members'] ch_dict['all_members'] = channel_info['all_members'] return ch_dict
def message_send_later(token, channel_id, message, time_sent): """ Sends a message to the designated channel at a specified time Parameters: token (string) channel_id(int) message(string) time_sent (datetime) Returns: (dictionary): A dictionary containing the message_id of the message that was sent. """ user = token_check(token) if user == False: raise InputError channel = channel_check(channel_id) if channel == False: raise InputError if member_channel_check(token, channel_id) == False: raise AccessError if (len(message) > 1000): raise InputError if int(time_sent) < int( datetime.datetime.now().replace(tzinfo=timezone.utc).timestamp()): raise InputError message_store = get_messages_store() for member in channel['all_members']: if user['u_id'] == member['u_id']: wait_time = time_sent - datetime.datetime.now().replace( tzinfo=timezone.utc).timestamp() time.sleep(wait_time) #wait_time = time.mktime(datetime.datetime.now().timetuple()) - time.mktime(time_sent.timetuple()) message_id = make_message(message, channel_id, user['u_id'], 0) return { 'message_id': message_id, }
def channel_addowner(token, channel_id, u_id): '''Adds someone as owner to a channel. Parameters: token (str): authorization hash channel_id (int): channel identification u_id (int): user identification ''' if channel_check(channel_id) == False: raise InputError if check_if_user_in_channel_owner_uid(u_id, channel_id) == True: raise InputError permission_error = token_check(token) if check_if_user_in_channel_owner(token, channel_id) == False: if permission_error['permission_id'] != 1: raise AccessError else: pass channel_store = get_channel_store() user = u_id_check(u_id) for channel in channel_store["Channels"]: if channel["channel_id"] == channel_id: if member_channel_check(user['token'], channel_id) == False: channel["all_members"].append({ "u_id": user["u_id"], "name_first": user['name_first'], "name_last": user["name_last"] }) channel["owner_members"].append({ "u_id": user["u_id"], "name_first": user['name_first'], "name_last": user["name_last"] }) user['channel_id_owned'].append(channel_id) user['channel_id_part'].append(channel_id) return {}
def c_details(): """ This is a flask wrapper for the channel_details function Parameters: No parameters Returns: (dictionary): A dictionary which contains details about the channel(the name of the channel, owners of the channels and all the members of the channel) """ token = request.args.get('token') channel_id = int(request.args.get('channel_id')) if not channel_check(channel_id): raise InputError(description="channel id not found") if not check_if_user_in_channel_member(token, channel_id): raise AccessError(description="User in channel members not found") return_dict = channel_details(token, channel_id) return dumps(return_dict)
def c_leave(): """ This is a flask wrapper for the channel_leave function Parameters: No parameters Returns: (dictionary): Empty dictionary """ data = request.get_json() channel_id = data['channel_id'] token = data['token'] if channel_check(channel_id) is None: raise InputError(description="Channel invalid id") if not check_if_user_in_channel_member(token, channel_id): raise AccessError(description="User not a member of channel") channel_leave(token, channel_id) return dumps({})
def standup_start_flask(): """ This is a flask wrapper for the standup_start function Parameters: No parameters Returns: (dictionary): A dictionary containing the key named time_finish. time_finish refers to the time that the standup finishes. """ data = request.get_json() token = data['token'] channel_id = int(data['channel_id']) standup_length = int(data['length']) required_channel = channel_check(channel_id) #if required_channel is None: # raise InputError(description="Wrong channel ID") #if required_channel['standup']['time_standup_finished'] != None: # if datetime.utcnow().replace(tzinfo=timezone.utc).timestamp() < required_channel['standup']['time_standup_finished']: # raise InputError(description="There is already a standup running in this channel. Only one standup can run at a time.") time_finish = standup_start(token, channel_id, standup_length) return dumps(time_finish)
def c_invite(): """ This is a flask wrapper for the channel_invite function Parameters: No parameters Returns: (dictionary): Empty dictionary """ data = request.get_json() token = data['token'] channel_id = int(data['channel_id']) u_id = int(data['u_id']) if not channel_check(channel_id): raise InputError(description="Invalid channel") if check_if_user_in_channel_member_uid(token, channel_id): raise AccessError(description="User is not a member in channel") out = channel_invite(token, channel_id, u_id) return dumps(out)
def channel_invite(token, channel_id, u_id): ''' Invites a user to join a channel that they are not already in Parameters: token (str): authorization hash channel_id (int): channel identification u_id (int): unique user identification ''' if not channel_check(channel_id): raise InputError if not u_id_check(u_id): return InputError if check_if_user_in_channel_member_uid(u_id, channel_id): raise AccessError channel_store = get_channel_store() user = u_id_check(u_id) for channel in channel_store["Channels"]: if channel["channel_id"] == channel_id: channel["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'] }) user['channel_id_part'].append(channel_id) return {}
def channel_removeowner(token, channel_id, u_id): '''Removes someone from owner to a channel. Parameters: token (str): authorization hash channel_id (int): channel identification u_id (int): user identification ''' if channel_check(channel_id) == False: raise InputError if check_if_user_in_channel_owner_uid(u_id, channel_id) == False: raise InputError permission_error = token_check(token) if check_if_user_in_channel_owner(token, channel_id) == False: if permission_error['permission_id'] != 1: raise AccessError else: pass user = u_id_check(u_id) channel_store = get_channel_store() for channel in channel_store["Channels"]: if channel["channel_id"] == channel_id: for member in channel["owner_members"]: if member["u_id"] == u_id: channel["owner_members"].remove(member) for leave in user['channel_id_owned']: if leave == channel_id: user['channel_id_owned'].remove(leave) return {}
def c_join(): """ This is a flask wrapper for the channel_join function Parameters: No parameters Returns: (dictionary): Empty dictionary """ data = request.get_json() channel_id = data['channel_id'] token = data['token'] if channel_check(channel_id) is None: raise InputError(description="Wrong channel ID") if not check_if_channel_is_public(channel_id): raise AccessError(description="Channel is not public") if check_if_user_in_channel_member(token, channel_id): raise AccessError(description="User already member of channel") channel_join(token, channel_id) return dumps({})
def c_removeowner(): """ This is a flask wrapper for the channel_removeowner function Parameters: No parameters Returns: (dictionary): Empty dictionary """ data = request.get_json() token = data['token'] channel_id = int(data['channel_id']) u_id = int(data['u_id']) if not channel_check(channel_id): raise InputError(description="Channel is invalid") if not check_if_user_in_channel_owner_uid(u_id, channel_id): raise InputError(description="User not owner of channel") if not check_if_user_in_channel_owner(token, channel_id): raise AccessError(description="User not owner of channel") out = channel_removeowner(token, channel_id, u_id) return dumps(out)
def c_messages(): """ This is a flask wrapper for the channel_messages function Parameters: No parameters Returns: (dictionary): A dictionary containing messages which are between the start value and the end value. This dictionary contains the keys of messages, start and end. """ token = request.args.get('token') ch_id = int(request.args.get('channel_id')) start = int(request.args.get('start')) if not channel_check(ch_id): raise InputError(description="channel id not found") if not check_if_user_in_channel_member(token, ch_id): raise AccessError("User not a member of channel") return_dict = channel_messages(token, ch_id, start) return dumps(return_dict)
def channel_messages(token, channel_id, start): '''Returns a range of 50 messages in a channel, if user is part of that channel. Parameters: token (str): authorization hash channel_id (int): channel identification start (int): which message wants to start range at Returns: (list): returns list of messages from channel ''' if channel_check(channel_id) == False: raise InputError if check_if_user_in_channel_member(token, channel_id) == False: raise AccessError sum_of_messages = 0 message_store = get_messages_store() for x in message_store['Messages']: if x['channel_id'] == channel_id: sum_of_messages += 1 if start > sum_of_messages: raise InputError proto_dict = {'messages': []} final_dict = {'messages': []} proto_dict = get_messages_store()['Messages'] counter = 0 if len(proto_dict) != 0: #print('in the if loop aye ') for message in reversed(proto_dict): if int(message['channel_id']) == int(channel_id): if counter >= start: dict_to_app = { 'message_id': message['message_id'], 'u_id': message['user_id'], 'message': message['message'], 'time_created': message['time_created'].replace( tzinfo=timezone.utc).timestamp(), 'reacts': message['reacts'], 'is_pinned': message['is_pinned'] } final_dict['messages'].append(dict_to_app) counter = counter + 1 if counter >= 50: counter = -1 break final_dict['start'] = start final_dict['end'] = counter return final_dict
hayden_full_dict = token_check(hayden_dict['token']) gary_full_dict = token_check(gary_dict['token']) print(hayden_dict) #standup_start(hayden_dict['token'], 1, 10) #invalid channel id def test_standup_start_invalid_InputError1(): with pytest.raises(InputError): standup_start(hayden_dict['token'], 1, 10) #invalid channel id time_length = 3 standup_start(hayden_dict['token'], chan_id['channel_id'], time_length) channel = channel_check(chan_id['channel_id']) final = int(time_length + datetime.datetime.now().replace(tzinfo=timezone.utc).timestamp()) def test_standup_start(): assert int(channel['standup']['time_standup_finished']) == final def test_standup_start_invalid_InputError2(): with pytest.raises(InputError): standup_start(hayden_dict['token'], chan_id['channel_id'], time_length) def test_standup_active_invalid_InputError1():