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 admin_userpermission_change(token, u_id, permission_id): try: # Check if user is authenticated and get the requesting user ID from token granterID = decodeToken(token) # Check if the target uID format is valid if not u_id.isdigit(): raise ValueError("Invalid user ID!") # Check if the target permission ID is valid if int(permission_id) > 3 or int(permission_id) < 1: raise ValueError("Invalid permission id") # Find the requesting user permissions = getUser(granterID)['permission_id'] # Check if requesting user has permissions for the action if permissions < 3: setPermission(u_id, permission_id) return {} raise AccessError("Granter is not an admin or owner") except ValueError as e: raise e except AccessError as e: raise e
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 channel_join(token, channel_id): check_tokenlogin(token) channel_dic = get_channel_from_cid(channel_id) auser_dic = get_user_from_token(token) # check error check_validchannel(channel_dic) auid = auser_dic['u_id'] check_auser_in_channel(auid, channel_dic['all_members']) # when channel_id refers to a channel that is private # when the authorised user is not an admin if not channel_dic['is_public'] and not helper_check_user_admin(auser_dic): raise AccessError("channel is private") # add the auser to channel channel_dic['all_members'].append(auid) return {}
def admin_userpermission_change(token, u_id, permission_id): check_tokenlogin(token) permission_ids = get_permission_ids() auser_dic = get_user_from_token(token) user_dic = get_user_from_uid(u_id) check_validuid(user_dic) # 3 types of permissions: 1. owner 2. admin 3. member # only 1 and 2 can change permission owner_id = permission_ids['Owner'] admin_id = permission_ids['Admin'] member_id = permission_ids['Member'] if permission_id not in (owner_id, admin_id, member_id): raise ValueError("permission_id does not refer to a value permission") if not helper_check_user_admin(auser_dic): raise AccessError("the authorised user is not an admin or owner") user_dic['permission_id'] = permission_id return {}
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
def message_edit(token, message_id, new_message): global channels try: u_id = decodeToken(token) message = getMessage(message_id) channel = getMessageChannel(message_id) if message["u_id"] == u_id: #check if user is the sender doEdit(message, new_message, token) return {} for members in channel["owner_members"]: #user is owner of channel if members["u_id"] == u_id: doEdit(message, new_message, token) return {} users = getUser(u_id) #user is admin if users["permission_id"] == 1 or users["permission_id"] == 2: doEdit(message, new_message, token) return {} raise AccessError("User does not have permission to edit message!") except ValueError as e: raise e except AccessError as e: raise e
def message_remove(token, message_id): global channels try: u_id = decodeToken(token) message = getMessage(message_id) channel = getMessageChannel(message_id) if message["u_id"] == u_id: #check if user is the sender channel["messages"].remove(message) return {} for members in channel["owner_members"]: #user is owner of channel if members["u_id"] == u_id: channel["messages"].remove(message) return {} if getUserPerm(u_id) == 2 or getUserPerm(u_id) == 1: channel["messages"].remove(message) return {} raise AccessError("User does not have permission to remove message!") except ValueError as e: raise e except AccessError as e: raise e