def dm_remove_v1(user_token, dm_id): ''' the function is given a token and a dm_id. we need to user the parameters to remove the exactly dm with the dm_id. also, we need to check the validity of the paremeters. Arguments: user_token (string) - some encoded information about one auth user dm_id (int) - the id of the dm needed removing ... Exceptions: AccessError - Occurs when the token is not a valid token InputError - Occurs when dm_id is not valid AccessError - Occurs when the auth_user is not the owner of the dm Return Value: Returns {} ''' database = {} with open(file_address, "r") as f: database = json.load(f) #get the required value kicker_id = findUser(user_token) #check if the input is valid if dm_id > database['dms'][len(database['dms']) - 1]['dm_id'] or database['dms'][dm_id - 1]['dm_id'] == -1: raise InputError(description="The dm_id is not valid.") if kicker_id != database['dms'][dm_id - 1]['dm_owner']: raise AccessError(description="The one who wants to remove the dm_id does not have right.") for msg_id in database['dms'][dm_id - 1]['message_ids']: for message in database['messages']: if message['message_id'] == msg_id: msg = message dm = get_dm_details(msg, database) dm_key = database['dms'].index(dm) database['dms'][dm_key]['message_ids'].remove(msg['message_id']) database['messages'].remove(msg) #remove the dm with some specific dm_id database['dms'][dm_id - 1]['dm_id'] = -1 append_user_stats(findUser(user_token), database) for u_id in database['dms'][dm_id - 1]['dm_members']: append_user_stats(u_id, database) append_dream_stats(findUser(user_token), database) #put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) return {}
def dm_leave_v1(user_token, dm_id): ''' The function is given the token who is gonna leave the dm without destroy the dm. Arguments: user_token (string) - some encoded information about one auth user dm_id (int) - the id of the dm needed removing ... Exceptions: AccessError - Occurs when the token is not a valid token InputError - Occurs when dm_id is not valid AccessError - Occurs when the auth_user is not the member of the dm Return Value: Returns {} ''' database = {} with open(file_address, "r") as f: database = json.load(f) #get the required value auth_user_id = findUser(user_token) #check if the input is valid if dm_id > database['dms'][-1]['dm_id'] or database['dms'][dm_id - 1]['dm_id'] == -1: raise InputError(description="The dm_id is not valid.") if auth_user_id not in database['dms'][dm_id - 1]['dm_members']: raise AccessError(description="The one who wants to leave the dm is not in the dm.") #remove the dm with some specific dm_id pos = database['dms'][dm_id - 1]['dm_members'].index(auth_user_id, 0, len(database['dms'][dm_id - 1]['dm_members'])) database['dms'][dm_id - 1]['dm_members'].pop(pos) if len(database['dms'][dm_id - 1]['dm_members']) == 0: database['dms'][dm_id - 1]['dm_id'] = -1 elif database['dms'][dm_id - 1]['dm_owner'] == auth_user_id: database['dms'][dm_id - 1]['dm_owner'] = database['dms'][dm_id - 1]['dm_members'][0] append_user_stats(findUser(user_token), database) append_dream_stats(findUser(user_token), database) #put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) return {}
def dm_list_v1(user_token): ''' the function is given a token and we need to return the dms which the user is in. the returned dictionary should be in right format. Arguments: user_token (string) - some encoded information about one auth user Exceptions: AccessError - Occurs when the token is not a valid token Return Value: Returns {'dms': dms(a list of dm detail which the user is in)} ''' database = {} with open(file_address, "r") as f: database = json.load(f) #get the required value auth_user_id = findUser(user_token) dms = [] for some_dm in database['dms']: if auth_user_id in some_dm['dm_members'] and some_dm['dm_id'] != -1: dm_id = some_dm['dm_id'] dm_name = some_dm['dm_name'] dms.append({'dm_id': dm_id, 'name': dm_name,}) return {'dms': dms,}
def dm_details_v1(user_token, dm_id): ''' the function is given a token and dm_id, we need to check the validity first and return the details of the dm in a dictionary in correct format. Arguments: user_token (string) - some encoded information about one auth user dm_id (int) - the id of the dm needed removing ... Exceptions: AccessError - Occurs when the token is not a valid token InputError - Occurs when dm_id is not valid AccessError - Occurs when the auth_user is not the member of the dm Return Value: Returns {'name': dm_name,(the name of dm) 'members': members,(the members of dm)} ''' database = {} with open(file_address, "r") as f: database = json.load(f) #get the required value auth_user_id = findUser(user_token) #check if the input is valid if dm_id > database['dms'][len(database['dms']) - 1]['dm_id'] or database['dms'][dm_id - 1]['dm_id'] == -1: raise InputError(description="The dm_id is not valid.") if auth_user_id not in database['dms'][dm_id - 1]['dm_members']: raise AccessError(description="The one who wants to show the dm details is not in the dm.") dm_name = database['dms'][dm_id - 1]['dm_name'] members = [] for some_member_id in database['dms'][dm_id - 1]['dm_members']: some_user = use_id_to_find_user(some_member_id) name_first = some_user['name_first'] name_last = some_user['name_last'] u_id = some_user['u_id'] email = some_user['email'] handle_str = some_user['handle_str'] new_member = { 'u_id': u_id, 'name_first': name_first, 'name_last': name_last, 'email': email, 'handle_str': handle_str, 'profile_img_url' : some_user['profile_img_url'], } members.append(new_member) return {'name': dm_name, 'members': members,}
def standup_start_v1(user_token, channel_id, length): """The function start a standup and will last for some seconds. All the message sent in that time period will be buffered and send together afer that. Args: user_token (string): a token string used to authorise and get the user id channel_id (int): the channel id where the standup starts length (int): the number of seconds Raises: InputError: channel id invalid InputError: standup already starts AccessError: user not in the Returns: dictionary: {'time_finish': time_finish} """ database = getData() auth_user_id = findUser(user_token) if is_channel_valid(database, channel_id) == False: raise InputError(description="Channel id is not valid") index = get_channel_index(database, channel_id) channel = database['channels'][index] if channel['standup']['is_active'] == True: raise InputError(description="Standup is already active") if is_user_in_channel(auth_user_id, channel) == False: raise AccessError(description="You are no in the channel") time_finish = (datetime.datetime.now()+datetime.timedelta(seconds=length)).strftime("%Y-%m-%d %H:%M:%S") time_finish = time.strptime(time_finish, "%Y-%m-%d %H:%M:%S") time_finish = time.mktime(time_finish) standup_length = length database['channels'][index]['standup']['is_active'] = True database['channels'][index]['standup']['standup_length'] = standup_length database['channels'][index]['standup']['time_finish'] = time_finish database['channels'][index]['standup']['messages'] = "" with open(file_address, "w") as f: json.dump(database, f) new_thread = threading.Timer(length, standup_package, args=[index, user_token, channel_id, time_finish]) new_thread.start() return {'time_finish': time_finish}
def standup_send_v1(user_token, channel_id, message): """get the messages that will be buffered Args: user_token (string): a token string used to authorise and get the user id channel_id (int): the channel id where the standup starts message (string): the message Raises: InputError: channel id invalid InputError: message too long InputError: standup not started yet AccessError: user not in the channel Returns: dic: {} """ database = getData() auth_user_id = findUser(user_token) if is_channel_valid(database, channel_id) == False: raise InputError(description="Channel id is not valid") if len(message) > 1000: raise InputError(description="Too much charecters in message") index = get_channel_index(database, channel_id) channel = database['channels'][index] if channel['standup']['is_active'] == False: raise InputError(description="The channel does not have an active standup") if is_user_in_channel(auth_user_id, channel) == False: raise AccessError(description="You are no in the channel") user = use_id_to_find_user(auth_user_id) original_msg = database['channels'][index]['standup']['messages'] if original_msg == "": original_msg = user['handle_str'] + ": " + message else: original_msg = original_msg + "\n" + user['handle_str'] + ": " + message database['channels'][index]['standup']['messages'] = original_msg with open(file_address, "w") as f: json.dump(database, f) return {}
def dm_messages_v1(user_token, dm_id, start): ''' the function is given a token, a dm_id and, a start number. We need to return the dm message in the specific dm and should start from some position. Arguments: user_token (string) - some encoded information about one auth user dm_id (int) - the id of the dm needed removing start (int) - the position where the message should start with ... Exceptions: AccessError - Occurs when the token is not a valid token InputError - Occurs when dm_id is not valid InputsError - Occurs when the start is not valid AccessError - Occurs when the auth_user is not the member of the dm Return Value: Returns {'messages': messages, 'start': start, 'end': end,} ''' database = getData() auth_user_id = findUser(user_token) #check if the dm id valid if dm_id > database['dms'][len(database['dms']) - 1]['dm_id'] or database['dms'][dm_id - 1]['dm_id'] == -1: raise InputError(description="The dm_id is not valid.") if start > len(database['dms'][dm_id - 1]['message_ids']): raise InputError(description="The start of the messages of dm exceed the least recent one.") if auth_user_id not in database['dms'][dm_id - 1]['dm_members']: raise AccessError(description="The one who wants to send the direct message is not in the dm.") messages = [] dm_message_list = database['dms'][dm_id - 1]['message_ids'] dm_message_list = list(reversed(dm_message_list)) end = min(start + 49, len(dm_message_list) - 1) for i in range(start, end + 1): message_id = dm_message_list[i] msg = {} for l in database['messages']: if message_id == l['message_id']: msg = l time_created = msg['time_created'] u_id = msg['u_id'] message = msg['message'] reacts = get_react_from_id(message_id, u_id, database) messages.append({'message_id': message_id, 'u_id': u_id, 'message': message, 'time_created': time_created, 'reacts' : reacts, 'is_pinned' : msg['is_pinned']}) if end == len(dm_message_list) - 1: end = -1 else: end = start + 50 return {'messages': messages, 'start': start, 'end': end,}
def dm_invite_v1(user_token, dm_id, u_id): ''' the function is given a invitor token, a dm id, and the invited's id. the invited id should be added into the member list in the dm. Arguments: user_token (string) - some encoded information about one auth user dm_id (int) - the id of the dm needed removing u_id (int) - the id of the user been invited ... Exceptions: AccessError - Occurs when the token is not a valid token InputError - Occurs when dm_id is not valid InputsError - Occurs when the u_id is not valid AccessError - Occurs when the auth_user is not the member of the dm Return Value: Returns {} ''' #get the value invited_id = u_id database = {} with open(file_address, "r") as f: database = json.load(f) #get the required value invitor_id = findUser(user_token) #check if the dm id valid if dm_id > database['dms'][len(database['dms']) - 1]['dm_id'] or database['dms'][dm_id - 1]['dm_id'] == -1: raise InputError(description="The dm_id is not valid.") invited_user = use_id_to_find_user(invited_id) if invited_user == {} or invited_id in database['dms'][dm_id - 1]['dm_members']: raise InputError(description="The user who will be invited does not have the correct id.") if invitor_id not in database['dms'][dm_id - 1]['dm_members']: raise AccessError(description="The one who wants to invite someone to the dm is not in the dm.") database['dms'][dm_id - 1]['dm_members'].append(invited_id) #put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) # Open file again to get updated database with open(file_address, "r") as f: database = json.load(f) # Create a notification for the added user user_notification = new_added_notification(user_token, -1, dm_id, u_id) database['user_notifications'].append(user_notification) append_user_stats(invited_id, database) append_dream_stats(findUser(user_token), database) # Put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) return {}
def dm_create_v1(user_token, u_id_list): ''' the function is given a user_token, which is encoded and is a string. Also, the function is given a list of user id. We need to use the proper parameters to create a direct message\ and return the dm_id as a integer Arguments: user_token (string) - some encoded information about one auth user u_id_list (list of int) - a list of user ids who are going to be in the dm ... Exceptions: AccessError - Occurs when the token is not a valid token Return Value: Returns {'dm_id': dm_id, 'dm_name': dm_name,} ''' database = {} with open(file_address, "r") as f: database = json.load(f) auth_user_id = findUser(user_token) #the user_id of the creater of the dm user_name_list = [] u_id_list.append(auth_user_id) #check all if all the u_id is valid and get their handle to generate the name of dm for some_u_id in u_id_list: new_user = use_id_to_find_user(some_u_id) if new_user == {}: raise InputError(description="Invalid u_id in the u_ids list.") user_name_list.append(new_user['handle_str']) user_name_list.sort(reverse=False) #generate the name of the new dm dm_name = "" dm_name += user_name_list[0] for i in range(1, len(user_name_list)): dm_name += ", " dm_name += user_name_list[i] #generate the new dm_id if len(database['dms']) == 0: dm_id = 1 else: dm_id = database['dms'][len(database['dms']) - 1]['dm_id'] + 1 #store the new dm into the database new_dm = {'dm_id' : dm_id, 'dm_name': dm_name, 'dm_owner': auth_user_id, 'dm_members': u_id_list, 'message_ids':[],} database['dms'].append(new_dm) #put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) # Open file again to get updated database with open(file_address, "r") as f: database = json.load(f) # Create a notification for the added users for user in u_id_list: if auth_user_id != user: user_notification = new_added_notification(user_token, -1, dm_id, user) database['user_notifications'].append(user_notification) append_user_stats(findUser(user_token), database) for u_id in u_id_list: append_user_stats(u_id, database) append_dream_stats(findUser(user_token), database) # Put the data back to the data file with open(file_address, "w") as f: json.dump(database, f) return {'dm_id': dm_id, 'dm_name': dm_name,}
def standup_package(index, user_token, channel_id, time_finish): """a helper function to store the messages sent in the standup period """ database = getData() new_message = database['channels'][index]['standup']['messages'] auth_user_id = findUser(user_token) if database['messages'] == []: new_message_id = 1 else: new_message_id = database['messages'][-1]['message_id'] + 1 database['messages'].append({ 'message_id' : new_message_id, 'u_id' : auth_user_id, 'channel_id': channel_id, 'dm_id' : -1, 'message' : new_message, 'time_created' : time_finish, 'reacts' : [], 'is_pinned' : False, }) database['channels'][index]['messages'].append(new_message_id) database['channels'][index]['standup']['is_active'] = False database['channels'][index]['standup']['standup_length'] = 0 database['channels'][index]['standup']['time_finish'] = 0 database['channels'][index]['standup']['messages'] = "" append_user_stats(auth_user_id, database) append_dream_stats(auth_user_id, database) words = new_message.split() handle_list = [] for word in words: if word[0] == '@': word = word.replace(',','') handle_list.append(word[1:]) u_id = -1 handle_status = False member_status = False for handle in handle_list: u_id = -1 handle_status = False member_status = False for user in database['users']: if handle == user['handle_str']: handle_status = True u_id = user['u_id'] channel_ids = {'channel_id' : channel_id} channel = get_channel_details(channel_ids, database) for members in channel['all_members']: if u_id == members['u_id']: member_status = True with open(file_address, "w") as f: json.dump(database, f) if handle_status == True and member_status == True: user_notification = new_tagged_notification(user_token, channel_id, -1, u_id, new_message) database['user_notifications'].append(user_notification) with open(file_address, "w") as f: json.dump(database, f)