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 standup_send(token, channel_id, message): 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="Unauthorised Access") channel_id = int(channel_id) channel = getChannel(channelsDB, channel_id) u_id = token_to_u_ID(loggedInUsersDB, token) if not channel['is_standup_running']: raise ValueError(description="No standup is running") if len(message) > 1000: raise ValueError(description="Message too long") if not is_inChannel(channelsDB,channel_id, u_id): raise AccessError(description='You must be in a channel to send a standup') handle = get_user_handle(registeredUsersDB, u_id) message = handle + ' : ' + message +'\n' for standup in channel['standups']: if standup['isActive']: standup['messages'] += message return {}
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 add_to_channel(channelDatabaseListDict, ownerU_ID, nonMemberU_ID, channel_id): loggedInUsersDB = get_global_loggedInUsers() nonMemberU_ID = int(nonMemberU_ID) channel_id = int(channel_id) ownerU_ID = int(ownerU_ID) firstName = get_firstName(nonMemberU_ID) lastName = get_lastName(nonMemberU_ID) profile_img_url = get_url(nonMemberU_ID) nonMemberP_ID = get_user_permission(nonMemberU_ID) if not is_inChannel(channelDatabaseListDict, channel_id, ownerU_ID): raise AccessError( description='You must be in the channel to invite other members') if nonMemberP_ID < MEMBER(): temp = join_special(channelDatabaseListDict, channel_id, nonMemberU_ID, firstName, lastName, profile_img_url) return else: temp = join_nonSpecial(channelDatabaseListDict, channel_id, nonMemberU_ID, firstName, lastName, profile_img_url) return raise ValueError(description="Unauthorised Access")
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_join(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 join of a channel!") # CHECK THE VALIDITY u_id = token_to_u_ID(loggedInUsersDB, token) u_id = int(u_id) channel_id = int(channel_id) first_name = get_firstName(u_id) last_name = get_lastName(u_id) profile_img_url = get_url(u_id) permission = get_user_permission(u_id) if permission < MEMBER(): return join_special(channelsDB, channel_id, u_id, first_name, last_name, profile_img_url) else: return join_nonSpecial(channelsDB, channel_id, u_id, first_name, last_name, profile_img_url) raise ValueError(description="Invalid Channel ID")
def message_unreact(token, message_id, react_id): loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() message_id = int(message_id) react_id = int(react_id) check_reaction(react_id) u_id = token_to_u_ID(loggedInUsersDB, token) channel = get_channel(channelsDB, message_id) if not is_inChannel(channelsDB=channelsDB, u_id=u_id, channel_id=channel['channel_id']): raise AccessError( description='You must be in the channel to unreact to a message!') for message in channel['messagesListDict']: if message['message_id'] == message_id: for reaction in message['reacts']: if reaction['react_id'] == react_id: if u_id in reaction['u_ids']: reaction['u_ids'].remove(u_id) reaction['is_this_user_reacted'] = False for userID in reaction['u_ids']: if get_user_permission(userID) < 2: reaction['is_this_user_reacted'] = True return else: raise ValueError( description='You cant unreact a unreacted message!' ) raise ValueError("Error unreacting")
def channels_listall(token): 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 view a list of channels!") if not channelsDB: return {'channels': []} u_id = token_to_u_ID(registeredUsersDB, token) u_id = int(u_id) pID = get_user_permission(u_id) channelListDict = [] for channel in channelsDB: if pID < MEMBER(): channelDict = channel_name_id_dictionary(channel) channelListDict.append(channelDict) elif channel['is_public']: channelDict = channel_name_id_dictionary(channel) channelListDict.append(channelDict) return {'channels': channelListDict}
def message_send_later(token, channel_id, message, time_to_send): messageQueue = get_global_messageQueue() time_to_send = float(time_to_send) time_to_send = int(time_to_send) if len(message) > 1000: raise ValueError(description="Message too long") loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() messageQueue = get_global_messageQueue() channel_id = int(channel_id) u_id = token_to_u_ID(loggedInUsersDB, token) if not is_inChannel( channelsDB=channelsDB, u_id=u_id, channel_id=channel_id): raise AccessError( description='You must join the channel to send a message!') timeNow = (datetime.datetime.utcnow() - datetime.timedelta(seconds=1)).replace( tzinfo=timezone.utc).timestamp() if time_to_send < timeNow: raise ValueError(description='You selected a time in the past!') length = len(messageQueue) + 1 msgElement = create_message_later(u_id, message, time_to_send, channel_id) messageQueue.append(msgElement) # This is just temporary; it will get updated once the message actually sends return {'message_id': length}
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end, base): channelsDB = get_global_existingChannels() loggedInUsersDB = get_global_loggedInUsers() registeredUsersDB = get_global_registeredUsers() email = token_to_email(token) u_id = token_to_u_ID(registeredUsersDB, token) u_id = int(u_id) if not base: try: update_profile_photo(registeredUsersDB, channelsDB, u_id, img_url) return update_online_profile_photo(loggedInUsersDB, u_id, img_url) except: raise AccessError(description="Error uploading the photo") base = str(base) x_start = int(x_start) y_start = int(y_start) x_end = int(x_end) y_end = int(y_end) if not is_loggedIn(loggedInUsersDB, email): raise AccessError("You must be logged in to change your photo") try: request.urlopen(img_url) except: raise ValueError( description="Cannot open image URL. Please try other images") request.urlretrieve(img_url, f'frontend/prebundle/images/{u_id}.jpg') crop_image(f'frontend/prebundle/images/{u_id}.jpg', x_start, y_start, x_end, y_end).save(f'frontend/prebundle/images/cropped_{u_id}.jpg') port = base[-5:-1] port = int(port) port += 3000 string = 'http://localhost:' + str(port) + '/' local_url = string + f'images/cropped_{u_id}.jpg' #adding port and http to match the localhost'port' print(local_url, '\n\n\n\n\n\n\n\n\n\n\n') # UNTIL HERE ONLYhttp://localhost # DONT TOUCH THE CODE BELOW try: update_profile_photo(registeredUsersDB, channelsDB, u_id, local_url) return update_online_profile_photo(loggedInUsersDB, u_id, local_url) except: raise AccessError(description="Error uploading the photo")
def decodeToken(token): secret = secretKey() token = str.encode(token) try: userDictionary = jwt.decode(token, secret, algorithm=('HS256')) except: raise AccessError(description='Invalid token') return userDictionary
def users_all(token): registeredUsersDB = get_global_registeredUsers() loggedInUsersDB = get_global_loggedInUsers() email = token_to_email(token) if not is_loggedIn(loggedInUsersDB, email): raise AccessError("You must be logged in to view all users") userList = [] for user in registeredUsersDB: userList.append(user) return {'users': userList}
def getChannel(u_id, channel_id): channelsDB = get_global_existingChannels() for channel in channelsDB: if channel['channel_id'] == channel_id: for owner in channel['owner_members']: if u_id == owner['u_id']: return channel for member in channel['other_members']: if u_id == member['u_id']: return channel raise AccessError(description='The channel does not exist!')
def message_unpin(token, message_id): loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() message_id = int(message_id) u_id = token_to_u_ID(loggedInUsersDB, token) channel = get_channel(channelsDB, message_id) if not is_inChannel(channelsDB=channelsDB, u_id=u_id, channel_id=channel['channel_id']): raise AccessError( description='You must be in the channel to unpin a message!') for owner in channel['owner_members']: if u_id == owner['u_id']: return unpin_message(channel, message_id) for member in channel['other_members']: if u_id == member['u_id']: raise AccessError( description="You must be an owner of the channel to unpin!")
def message_edit(token, message_id, editMessage): loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() message_id = int(message_id) if len(editMessage) > 1000: raise ValueError(description="Message too long") u_id = token_to_u_ID(loggedInUsersDB, token) channel = get_channel(channelsDB, message_id) if not is_inChannel(channelsDB=channelsDB, u_id=u_id, channel_id=channel['channel_id']): raise AccessError( description='You must be in the channel to edit a message!') for message in channel['messagesListDict']: if message['message_id'] == message_id: if message['u_id'] == u_id: if editMessage.isspace(): message_remove(token, message_id) else: message['message'] = editMessage return {} else: for owner in channel['owner_members']: if u_id == owner['u_id']: if editMessage.isspace(): message_remove(token, message_id) else: message['message'] = editMessage return {} raise AccessError( description= "You did not write this message. You need to be an owner of the channel to edit a message you did not write!" ) raise ValueError(description="Error editting")
def remove_owner(channelDatabaseListDict, userRemoving, userToBeRemoved, channel_id): userRemoving = int(userRemoving) userToBeRemoved = int(userToBeRemoved) channel_id = int(channel_id) userRemoving_PID = get_user_permission(userRemoving) userToBeRemoved_PID = get_user_permission(userToBeRemoved) if userRemoving == userToBeRemoved: raise ValueError(description='You cant remove yourself!') if userRemoving_PID > userToBeRemoved_PID: # ACCESS ERROR raise AccessError( description="You cannot remove a user with higher permission ") for channel in channelDatabaseListDict: if channel['channel_id'] == channel_id: for member in channel['other_members']: if userRemoving == member['u_id']: raise AccessError(description="You are not an owner") if userToBeRemoved == member['u_id']: raise AccessError(description='User is not an owner') found = False for owner in channel['owner_members']: if owner['u_id'] == userRemoving: found = True if not found: raise ValueError( description="You must be an owner to remove an owner") for owner in channel['owner_members']: if owner['u_id'] == userToBeRemoved: channel['other_members'].append(owner) channel['owner_members'].remove(owner) return
def standup_start(token, channel_id, length): 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 should be logged in to start a standup") channel_id = int(channel_id) channel = getChannel(channelsDB, channel_id) u_id = token_to_u_ID(loggedInUsersDB, token) if channel['is_standup_running']: raise ValueError("Another standup is currently running") # Only owner of the channel can start a standup found = False for owner in channel['owner_members']: if owner['u_id'] == u_id: found = True if not found: raise AccessError(description='You must be an owner of a channel to start a standup') now = datetime.datetime.utcnow() time_start = int(now.replace(tzinfo=timezone.utc).timestamp()) time_end = int(time_start + int(length)) tempDict = {} tempDict['u_id'] = u_id tempDict['isActive'] = True tempDict['time_start'] = time_start tempDict['time_end'] = time_end tempDict['messages'] = '' channel['is_standup_running'] = True channel['standups'].append(tempDict) return {'time_finish': time_end}
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 message_remove(token, message_id): loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() message_id = int(message_id) u_id = token_to_u_ID(loggedInUsersDB, token) channel = get_channel(channelsDB, message_id) if not is_inChannel(channelsDB=channelsDB, u_id=u_id, channel_id=channel['channel_id']): raise AccessError( description='You must be in the channel to remove a message!') for message in channel['messagesListDict']: if message['message_id'] == message_id: if message['u_id'] == u_id: channel['messagesListDict'].remove(message) return {} for owner in channel['owner_members']: if u_id == owner['u_id']: channel['messagesListDict'].remove(message) return {} raise AccessError(description="You are not allowed to remove this message")
def user_profile(token, 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 view a profile!") viewerID = token_to_u_ID(registeredUsersDB, token) userDictionary = get_user_profile(registeredUsersDB, u_id) save_registered_users() return userDictionary
def channels_list(token): 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 view a list of channels!") u_id = token_to_u_ID(registeredUsersDB, token) if not channelsDB: return {'channels': []} channelListDict = listUserChannels(channelsDB, u_id) return {'channels': channelListDict}
def send_message_help(u_id, channel_id, message, time_created): global message_id channelsDB = get_global_existingChannels() channel = getChannel(u_id=u_id, channel_id=channel_id) print('\n\n\n\n\n\n', message_id, '\n\n\n\n\n\n') try: msgElement = create_message(message_id, u_id, message, time_created) channel['messagesListDict'].insert(0, msgElement) print(channel['messagesListDict'], '\n\n\n\n\n\n\n\n\n\n\n') message_id += 1 save_message_id() return {'message_id': message_id} except: raise AccessError( description='Cannot send message; you are not in the channel!')
def message_send(token, channel_id, message, time_sent): if len(message) > 1000: raise ValueError(description="Message too long") loggedInUsersDB = get_global_loggedInUsers() channelsDB = get_global_existingChannels() channel_id = int(channel_id) u_id = token_to_u_ID(loggedInUsersDB, token) if not is_inChannel( channelsDB=channelsDB, u_id=u_id, channel_id=channel_id): raise AccessError( description='You must join the channel to send a message!') return send_message_help(u_id, channel_id, message, time_sent)
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 user_profile_sethandle(token, handle): 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 change your handle") check_valid_handle(registeredUsersDB, handle) u_id = token_to_u_ID(registeredUsersDB, token) update_handle(registeredUsersDB, u_id, handle) save_registered_users() return {}
def user_setname(token, name_first, name_last): 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 change your name!") check_valid_name(name_first) check_valid_name(name_last) u_id = token_to_u_ID(registeredUsersDB, token) update_name(registeredUsersDB, channelsDB, u_id, name_first, name_last) return {}
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_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 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 {}