def channel_addowner(token, channel_id, u_id): global channels try: # Get user information userID = decodeToken(token) users = getUser(int(u_id)) target = {'u_id':int(u_id), 'name_first':users['name_first'], 'name_last':users['name_last'], "profile_img_url": users['profile_img_url']} grantusers = getUser(userID) granter = {'u_id':userID, 'name_first':grantusers['name_first'], 'name_last':grantusers['name_last'], "profile_img_url": grantusers['profile_img_url']} permissions = grantusers['permission_id'] # Channel operations channel = getChannel(channel_id) if granter in channel['owner_members'] or permissions < 3: if target in channel['owner_members']: raise ValueError("User already an owner") elif target in channel['all_members']: channel['owner_members'].append(target) return {} else: channel['owner_members'].append(target) channel['all_members'].append(target) return {} raise AccessError("User is not an owner") except ValueError as e: raise e except AccessError as e: raise e
def channel_join(token, channel_id): global channels try: # Get user information u_id = decodeToken(token) users = getUser(u_id) permission = users['permission_id'] userDetails = { "u_id": u_id, "name_first": users['name_first'], "name_last": users['name_last'], "profile_img_url": users['profile_img_url'] } # Channel operations channel = getChannel(channel_id) if not channel[ 'is_public'] and permission == 3: #Checks if channel is public raise AccessError("You do not have access to this channel") else: channel['all_members'].append(userDetails) if userDetails not in channel['owner_members'] and permission < 3: channel['owner_members'].append(userDetails) return {} except ValueError as e: raise e except AccessError as e: raise e
def standup_send(token, channel_id, message): try: u_id = decodeToken(token) # Check length is OK if len(message) > 1000: raise ValueError("Length is too long") # Get user permissions for user in data: if user['u_id'] == u_id: permissions = user['permission_id'] name_first = user['name_first'] # Prepare a new message newMessage = name_first + ': ' + message + ' ' # Add the new message if allowed channel = getChannel(channel_id) for member in channel['all_members']: if member['u_id'] == u_id or permissions < 3: if channel['standup_status']: channel['standup_message'] += newMessage return {} raise ValueError("Standup not running!") raise AccessError("User is not an owner!") except ValueError as e: raise e except AccessError as e: raise e
def standup_finish(channel_id, u_id): # Send the message at the end global index timeStamp = datetime.now(timezone.utc).timestamp() channel = getChannel(channel_id) channel['messages'].append({ "message_id": index, "u_id": u_id, "message": channel['standup_message'], "time_created": timeStamp, "reacts": [] }) channel['standup_status'] = False channel['standup_message'] = "" channel['time_finish'] = None index += 1
def channel_leave(token, channel_id): try: u_id = decodeToken(token) # Channel operations channel = getChannel(channel_id) for user in channel['all_members']: if user['u_id'] == u_id: channel['all_members'].remove(user) if user in channel['owner_members']: channel['owner_members'].remove(user) return {} raise AccessError('User is not a member') except ValueError as e: raise e except AccessError as e: raise e
def standup_start(token, channel_id, length): try: u_id = decodeToken(token) permissions = getUserPerm(u_id) channel = getChannel(channel_id) for owner in channel['owner_members']: if owner['u_id'] == u_id or permissions < 3: if not channel['standup_status']: time_finish = datetime.now(timezone.utc).timestamp() + length channel['standup_status'] = True channel['time_finish'] = time_finish return {"time_finish":time_finish} raise ValueError("Standup already running!") raise AccessError("User is not an owner!") except ValueError as e: raise e except AccessError as e: raise e
def channel_messages(token, channel_id, start): global channels try: u_id = decodeToken(token) # Channel operations channel = getChannel(channel_id) for users in channel['all_members']: if users['u_id'] == u_id or data[u_id]['permission_id'] < 3: if int(start) > len(channel['messages']): raise ValueError( "Start is greater than the number of messages") newMsg = channel[ 'messages'] #inject is_user_reacts into messages to return for messages in newMsg: for reacts in messages['reacts']: reacts['is_this_user_reacted'] = False #default for react_users in reacts['u_ids']: if react_users == u_id: reacts[ 'is_this_user_reacted'] = True #if user has reacted if len(newMsg) <= int(start) + 50: return { 'messages': newMsg[len(channel['messages']) - int(start)::-1], 'start': start, 'end': -1 } else: return { 'messages': newMsg[len(channel['messages'] ):len(channel['messages']) - 51:-1], 'start': start, 'end': int(start) + 50 } raise AccessError("Must be a member of channel to see messages") except ValueError as e: raise e except AccessError as e: raise e
def channel_details(token, channel_id): try: # Get user details u_id = decodeToken(token) user = getUser(u_id) permissions = user['permission_id'] # Get channel details channel = getChannel(channel_id) if channel['is_public'] == 'true' or permissions < 3: return { "name": channel['name'], "owner_members": channel['owner_members'], "all_members": channel['all_members'] } raise AccessError("User is not member of channel") except ValueError as e: raise e except AccessError as e: raise e
def message_send(token, channel_id, message): global data global valid_tokens global channels try: u_id = decodeToken(token) channel = getChannel(channel_id) isUserChan(u_id, channel) #Will raise error if fails if len(message) > 1000: raise ValueError( "Message can not be greather than 1000 character limit") elif len(message) == 0: #this appears to be handled by front end... raise ValueError("Message can not be empty") else: return addMessage(channel, message, u_id) except ValueError as e: raise e except AccessError as e: raise e
def channel_invite(token, channel_id, u_id): try: # Get user information userID = decodeToken(token) grant_user = getUser(userID) granter = {'u_id':userID, 'name_first':grant_user['name_first'], 'name_last':grant_user['name_last'], "profile_img_url": grant_user['profile_img_url']} target_user = getUser(int(u_id)) target = {'u_id':int(u_id), 'name_first':target_user['name_first'], 'name_last':target_user['name_last'], 'profile_img_url': target_user['profile_img_url']} # Can't invite yourself... if granter == target: raise ValueError("Can't invite yourself!") # Channel operations channel = getChannel(channel_id) if granter in channel['all_members']: channel['all_members'].append(target) return {} raise AccessError("User not a member of channel") except ValueError as e: raise e except AccessError as e: raise e