def channel_name_change(token, channel_id, name): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = load_channel() check_valid_channel_name(name) email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="You must be logged in to change the name of a channel!" ) u_id = token_to_u_ID(loggedInUsersDB, token) u_id = int(u_id) channel_id = int(channel_id) if not is_inChannel( u_id=u_id, channel_id=channel_id, channelsDB=channelsDB): raise AccessError( description= 'You must be in the channel to change the name of a channel!') for channel in channelsDB: if channel['channel_id'] == channel_id: for owner in channel['owner_members']: if u_id == owner['u_id']: channel['name'] = name save_channel() return {} raise AccessError( description= 'You must be an owner of the channel to change the name of the channel!' )
def join_special(channelDatabaseListDict, channel_id, u_id, firstName, lastName, profile_img_url): loggedInUsersDB = get_global_loggedInUsers() for channel in channelDatabaseListDict: if channel['channel_id'] == channel_id: found = False for owner in channel['owner_members']: if owner['u_id'] == u_id: found = True if not found: user = { 'u_id': u_id, 'name_first': firstName, 'name_last': lastName, 'profile_img_url': profile_img_url } channel['owner_members'].append(user) for loggedinUser in loggedInUsersDB: if loggedinUser['u_id'] == u_id: channel['online_members'].append(user) save_channel() return {}
def channel_delete(token, channel_id): loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="You must be in a channel to delete a channel!") u_id = token_to_u_ID(loggedInUsersDB, token) u_id = int(u_id) channel_id = int(channel_id) if not is_inChannel( u_id=u_id, channel_id=channel_id, channelsDB=channelsDB): raise AccessError( description='You must be in the channel to delete a channel!') for channel in channelsDB: if channel['channel_id'] == channel_id: for owner in channel['owner_members']: if u_id == owner['u_id']: channelsDB.remove(channel) save_channel() return {} raise ValueError(description='You must be an owner to delete the channel!')
def join_nonSpecial(channelDatabaseListDict, channel_id, u_id, firstName, lastName, profile_img_url): loggedInUsersDB = get_global_loggedInUsers() for channel in channelDatabaseListDict: if channel['channel_id'] == channel_id: if not channel['is_public']: raise AccessError( description= "You cant join a private channel; but you can get invited to one! Ask a friend to invite you!" ) else: found = False for member in channel['other_members']: if member['u_id'] == u_id: found = True if not found: user = { 'u_id': u_id, 'name_first': firstName, 'name_last': lastName, 'profile_img_url': profile_img_url } channel['other_members'].append(user) for loggedinUser in loggedInUsersDB: if loggedinUser['u_id'] == u_id: channel['online_members'].append(user) save_channel() return {}
def channel_addowner(token, channel_id, u_id): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="you must be logged in to add an owner to a channel!") ownerU_ID = token_to_u_ID(loggedInUsersDB, token) add_member_to_owner(channelsDB, ownerU_ID, u_id, channel_id) save_channel() return {}
def channel_details(token, channel_id): registeredUsersDB = load_user() loggedInUsersDB = get_global_loggedInUsers() channelsDB = load_channel() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description= "You must be logged in to view the details of a channel!") u_id = token_to_u_ID(loggedInUsersDB, token) channelDictionary = get_channel_details(channelsDB, u_id, channel_id) save_channel() return channelDictionary
def update_profile_photo(databaseListDict,channelsDB, u_id, local_url): for user in databaseListDict: if user['u_id'] == u_id: user['profile_img_url'] = local_url save_registered_users() for channel in channelsDB: for owner in channel['owner_members']: if owner['u_id'] == u_id: #UPDATE owner['profile_img_url'] = local_url save_channel() for member in channel['other_members']: if member['u_id'] == u_id: #UPDATE member['profile_img_url'] = local_url save_channel()
def channel_leave(token, channel_id): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="You must be logged in to leave a channel!") u_id = token_to_u_ID(loggedInUsersDB, token) u_id = int(u_id) channel_id = int(channel_id) remove_user_from_channel(channelsDB, u_id, channel_id) save_channel() return {}
def update_name(userDB, channelsDB, u_id, name_first, name_last): u_id = int(u_id) # update user in users dictionary for user in userDB: if user['u_id'] == u_id: user['name_first'] = name_first user['name_last'] = name_last save_registered_users() # update user in channels dictionary for channel in channelsDB: for owner in channel['owner_members']: if owner['u_id'] == u_id: #UPDATE owner['name_last'] = name_last owner['name_first'] = name_first for member in channel['other_members']: if member['u_id'] == u_id: #UPDATE member['name_last'] = name_last member['name_first'] = name_first save_channel()
def channel_messages(token, channel_id, start): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() start = int(start) end = start + PAGINATION() channel_id = int(channel_id) email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description= "You must be logged in to view the messages of a channel!") u_id = token_to_u_ID(loggedInUsersDB, token) channel = getChannel(channelsDB, channel_id) messagesLength = len(channel['messagesListDict']) if messagesLength < start: raise ValueError(description="Starting message too big") returnDictionary = {'messages': []} if messagesLength < end: returnDictionary['end'] = -1 returnDictionary['start'] = start else: returnDictionary['end'] = end returnDictionary['start'] = start for message in channel['messagesListDict']: if start == end: break returnDictionary['messages'].append(message) start += 1 save_channel() return returnDictionary
def user_profile_delete(token, password): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() password = hashPassword(password) email = token_to_email(token) u_id = token_to_u_ID(registeredUsersDB, token) u_id = int(u_id) if not is_loggedIn(loggedInUsersDB, email): raise AccessError("You must be logged in to delete your account") delete_user(registeredUsersDB, u_id, password) delete_user(loggedInUsersDB, u_id, password) for channel in channelsDB: remove_user_from_channel(channelsDB, u_id, channel['channel_id']) save_channel() save_registered_users() return {}
def channel_invite(token, channel_id, u_id): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) u_id = int(u_id) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="You must be logged in invite a friend into a channel!" ) if not is_already_registered(registeredUsersDB, email): raise ValueError(description=f"User with ID {u_id} does not exist") ownerU_ID = token_to_u_ID(loggedInUsersDB, token) ownerU_ID = int(ownerU_ID) channel_id = int(channel_id) add_to_channel(channelsDB, ownerU_ID, u_id, channel_id) save_channel() return {}
def channels_create(token, name, is_public): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError( description="You must be logged in to create a channel!") is_public = true_or_false(is_public) check_valid_channel_name(name) u_id = token_to_u_ID(loggedInUsersDB, token) u_id = int(u_id) channel_id = generateChannelID(channelsDB, name, u_id) first_name = get_firstName(u_id) last_name = get_lastName(u_id) url = get_url(u_id) channelDict = create_channel_details(name=name, channel_id=channel_id, is_public=is_public, u_id=u_id, name_first=first_name, name_last=last_name, profile_img_url=url) channelsDB.append(channelDict) save_channel() return {'channel_id': channel_id}
def standup_active(token, channel_id): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError(description="You must be logged in to see if a standup is active!") channel_id = int(channel_id) channel = getChannel(channelsDB, channel_id) u_id = token_to_u_ID(loggedInUsersDB, token) if not channel['is_standup_running']: for standup in channel['standups']: standup['isActive'] = False return {'is_active':False, 'time_finish': None} for standup in channel['standups']: if standup['isActive']: return {'is_active':True, 'time_finish': standup['time_end']} channel['is_standup_running'] = False save_channel()
def auth_login(email, password): registeredUsersDB = load_user() loggedInUsersDB = get_global_loggedInUsers() channelsDB = load_channel() check_valid_email(email) check_valid_password(password) password = hashPassword(password) # Check if the email and password belongs to a registered user userDictionary = get_registered_user(registeredUsersDB, email, password) # Set the reset code to be none, in case this log in was right after resetting password userDictionary['reset_code'] = None u_id = get_u_ID(registeredUsersDB, email) if not is_loggedIn(loggedInUsersDB, email): loggedInUsersDB.append(userDictionary) make_online(channelsDB, u_id) save_channel() userDictionary = generateUserDictionary(email, password) token = generateToken(userDictionary) return {'u_id': u_id, 'token': token}
def make_offline(channelDictionary, u_id): for channel in channelDictionary: for user in channel['online_members']: if user['u_id'] == u_id: channel['online_members'].remove(user) save_channel()