def get_user_from_token(token): # search the user using their token data = getdata() for user in data['users']: if user['token'] == token: return user return False
def get_user_from_email(email): # search the user using their email data = getdata() for user in data['users']: if user['email'] == email: return user return False
def get_message_from_mid(message_id): data = getdata() for channel in data['channels']: for message in channel['messages']: if message['message_id'] == message_id: return {'message_dic': message, "channel_dic": channel} return False
def get_user_from_uid(uid): # search the user using their u_id data = getdata() for user in data['users']: if user['u_id'] == uid: return user return False
def channels_create(token, name, is_public): check_tokenlogin(token) data = getdata() auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] # check if the channel name valid if len(name) > 20: raise ValueError('channel name not valid') channel_id = -1 for channel in data['channels']: channel_id = channel['channel_id'] # channel id will be automaticlly updated with every creation channel_id = channel_id + 1 channel_dic = { 'channel_id': channel_id, 'name': name, 'is_public': is_public, 'standup_messages': '', 'standup_is_active': False, 'standup_time_finish': None, 'owner_members': [auid], 'all_members': [auid], # the one who created this channel will be the owner of the channel 'messages': [] } # channel will be added into the channels list after creation data['channels'].append(channel_dic) return {'channel_id': channel_id}
def auth_register(email, password, name_first, name_last): #set up the initial value data = getdata() permission_ids_dic = get_permission_ids() owner = permission_ids_dic['Owner'] member = permission_ids_dic['Member'] uid = 0 # argument checking # check password check_validpassword(password) # check email check_validemail(email) # check firstname, lastname check_validname(name_first, name_last) # get a new u_id for new user, and check whether the email has been used for user in data['users']: uid = user['u_id'] if user['email'] == email: raise ValueError("email has been used.") uid = uid + 1 # if uid = 1, which means this is the first one to register the slackr # this user is the owner of slackr # else the user is just a member if uid == owner: permissionid = owner else: permissionid = member handle = creatHandle(name_first, name_last) userdic = { 'email': email, 'password': str(encode({'password': password})), 'u_id': uid, 'name_first': name_first, 'name_last': name_last, 'is_log_in': True, 'handle': handle, 'permission_id':permissionid, 'profile_img_url': None } token = generateToken(userdic) userdic.update({'token':token}) data['users'].append(userdic) return {'u_id': uid, 'token': token}
def auth_passwordreset_reset(resetCode, newPassword): #verify if this user is the correct user then check the format of the new password #then update data data = getdata() check_validpassword(newPassword) newPassword = str(encode({'password': newPassword})) for user in data['users']: if user['resetCode'] == resetCode: user['password'] = newPassword return {} raise ValueError("not a vaild reset code")
def users_all(token): check_tokenlogin(token) users = getdata()['users'] users_profile = [] for user in users: # do not get the uid 0 user, cause the first user with uid -1 just a sample if user['u_id'] == 0: continue this_profile = user_profile(token, user['u_id']) users_profile.append(this_profile) return {'users' : users_profile}
def search(token, query_str): check_tokenlogin(token) channels = getdata()['channels'] auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] collection = [] for channel in channels: # firstly search fot all the channels the user is in if auid in channel['all_members']: for message in channel['messages']: #then search for all the messages this user sent in a channel if query_str in message['message']: # lastly append the message to the collection collection.append(message) return {'messages': collection}
def channels_list(token): check_tokenlogin(token) data = getdata() auser_dic = get_user_from_token(token) auid = auser_dic['u_id'] channels = [] # get the list of channels, which the token user in for channel in data['channels']: if auid in channel['all_members']: channels.append({ 'channel_id': channel['channel_id'], 'name': channel['name'] }) return {'channels': channels}
def channels_listall(token): check_tokenlogin(token) data = getdata() channels = [] # get list of all channels # do not include the first channel in channel list # because in data structure, the first on is just a model first = True for channel in data['channels']: if not first: channels.append({ 'channel_id': channel['channel_id'], 'name': channel['name'] }) first = False return {'channels': channels}
def user_profile_setemail(token, email): check_tokenlogin(token) users = getdata()['users'] auser_dic = get_user_from_token(token) check_validemail(email) # check if the email is valid for user in users: if user['email'] == email: raise ValueError("email has been used.") if auser_dic['email'] == email: raise ValueError('No new email entered') auser_dic['email'] = email return {}
def get_channel_from_cid(channel_id): data = getdata() for channel in data['channels']: if channel['channel_id'] == channel_id: return channel return False
def handle_is_unique(handle): data = getdata() for user in data['users']: if user['handle'] == handle: return False return True