def channel_addowner (token, channel_id, u_id): curr_u_id = auth_login if is_not_owner(token, channel_id): raise AccessError ("current user is not owner within target channel") if is_channel (channel_id): if in_channel (token, channel_id): if is_not_owner(u_id, channel_id): #do something pass else: raise ValueError ("target user already owner") else: raise AccessError ("current user is not within target channel") else: raise ValueError ("channel is invalid") pass
def channel_invite(token, channel_id, u_id): data = getData() inviter_id = getUidFromToken(token) # check if u_id refers to a valid user isUidValid(u_id) # check if channel exists channel = getChannelFromChannelId(channel_id) # check if u_id is already in the channel if u_id in channel['all_members']: raise ValueError_http("User is already part of the channel") # # check if user is attempting to invite themself # if inviter_id == u_id: # raise ValueError_http("You are not allowed to invite yourself") # check if authorized user is part of channel if inviter_id not in channel['all_members']: raise AccessError("Authorised user not a member of channel") # check permission of invited user invitee_permission = checkUserPermission(u_id) if invitee_permission == 1 or invitee_permission == 2: channel['owner_members'].append(u_id) channel['all_members'].append(u_id) details_channel = getChannelDetailsSimple(channel_id) getUserFromUid(u_id)['joined_channels'].append(details_channel) return ()
def message_send(token, channel_id, message, time_sent): global GLOBAL_DATA # Send message from authorised user to channel if len(message) > 1000: raise ValueError("Message is more than 1000 characters") elif time_sent < 0: raise ValueError elif channel_id < 0 or len(GLOBAL_DATA["channels"]) <= channel_id: raise ValueError # raise AccessError when user has has not joined the channel target_channel = GLOBAL_DATA["channels"][channel_id] user_name = get_user_from_token(token) if user_name not in target_channel.members: raise AccessError("authorised user has not joined the channel they are trying to post to") #timer start now = time.time() future = now + time_sent while time.time() < future: pass new_message = Message(token, message) target_channel.messages.append(new_message) GLOBAL_DATA["messages"].append(new_message) return {"message_id": new_message.message_id}
def message_sendlater(token, channel_id, message, time_sent): global message_id # check for access error (user must be in the channel) if user_is_in_channel(getUidFromToken(token), channel_id) is False: raise AccessError('User not in the channel') if len(message) > 1000: raise ValueError_http("Message cannot be longer than 1000 characters") #time sent has to be in the future if float(time_sent) < datetime.datetime.now(datetime.timezone.utc).astimezone().timestamp(): raise ValueError_http("Time set has to be in the future") # add message details channel = getChannelDetails(channel_id) message_id += 1 channel['messages'].insert(0, { 'message_id': message_id, 'u_id': getUidFromToken(token), 'message': message, 'time_created': str(time_sent), 'reacts': [{'react_id': 1, 'u_ids': [], 'is_this_user_reacted' : False}], 'is_pinned': False }) return {'message_id': message_id}
def Channel_join(token, channel_id): if is_channel(token, channel_id): if in_channel(token, channel_id): return ValueError("user already in target channel") elif is_private(token, channel_id) == False: #do something pass elif is_private(token, channel_id): return AccessError("target channel is private") else: return ValueError("Channel is invalid")
def message_remove(token, message_id): # message doesnt exist if messageExists(message_id) is False: raise ValueError_http('Message does not exist') channel_id = findMessage(message_id)['channel_id'] # user must be authorized if isAdmin(token, channel_id) is True or message_belong_to_user(token, message_id): channel = findMessage(message_id) channel['messages'].remove(getMessageInfo(message_id)) else: raise AccessError('User not authorized to remove message')
def downloadImage(imageUrl: str, filePath: str): img = sendRequest(imageUrl) if (img == False): raise AccessError("Please enter a valid URL (image has to be .jpg)") if not img.content[:4] == b'\xff\xd8\xff\xe0': raise ValueError_http( "Please enter a valid URL (image has to be .jpg)") urllib.request.urlretrieve(imageUrl, filePath)
def message_edit(token, message_id, message): # message doesnt exist if messageExists(message_id) is False: raise ValueError_http('Message does not exist') if len(message) > 1000: raise ValueError_http('Message cannot be longer than 1000 characters') channel_id = findMessage(message_id)['channel_id'] # user must be authorized if isAdmin(token, channel_id) is True or message_belong_to_user(token, message_id): getMessageInfo(message_id)['message'] = message else: raise AccessError('User not authorized to edit message') pass
def message_unpin(token, message_id): # user has to be admin message = getMessageInfo(message_id) channel_id = findMessage(message_id)['channel_id'] channel = getChannelDetails(channel_id) u_id = getUidFromToken(token) if u_id not in channel['all_members']: raise AccessError('User is not a member ofo the channel') if isAdmin(token, channel_id) is True and message['is_pinned'] is True: message['is_pinned'] = False elif isAdmin(token, channel_id) is False: raise ValueError_http('User has to be admin to pin') elif message['is_pinned'] is False: raise ValueError_http('Message is already unpinned')
def channel_addowner(token, channel_id, u_id): data = getData() adder_id = getUidFromToken(token) # check if channel exists channel = getChannelFromChannelId(channel_id) # check if authorized user is an owner of slackr / owner of channel if adder_id not in channel['owner_members']: raise AccessError("You are not authorized to add an owner") # check if user is already an owner of channel / member of channel if u_id in channel['owner_members']: raise ValueError_http("User is already an owner of the channel") # check if user is a member of the channel if u_id not in channel['all_members']: raise ValueError_http("User is not a member of the channel") channel['owner_members'].append(u_id) return ()
def channel_join(token, channel_id): data = getData() u_id = getUidFromToken(token) # check if channel exists channel = getChannelFromChannelId(channel_id) # check if channel_id is private if channel['is_public'] == False: raise AccessError("You cannot join a private channel") # check permission of invited user permission = checkUserPermission(u_id) if permission == 1 or permission == 2: channel['owner_members'].append(u_id) channel['all_members'].append(u_id) details_channel = getChannelDetailsSimple(channel_id) getUserFromToken(token)['joined_channels'].append(details_channel) return ()
def Message_sendlater(token, channel_id, message, time_sent): if invalid_token(token): raise ValueError("invalid token") elif is_channel(token, channel_id): if in_channel(token, channel_id): if len(message) > 1000: return ValueError("Message length > 1000 chars") elif time_sent < time.clock(): return ValueError("Invalid message send time") elif len(message) < 1000 and time_sent > time.clock(): #do something pass else: return AccessError("Not in channel") else: return ValueError("Channel id invalid")
def channel_leave(token, channel_id): data = getData() u_id = getUidFromToken(token) # check if channel exists channel = getChannelFromChannelId(channel_id) # check that u are a member of channel if u_id not in channel['all_members']: raise AccessError("You are not a member of this channel") channel['all_members'].remove(u_id) details_channel = getChannelDetailsSimple(channel_id) getUserFromToken(token)['joined_channels'].remove(details_channel) # check if empty channel if len(channel['all_members']) == 0: # delete channel data['channel'].remove(channel) return ()
def channel_removeowner(token, channel_id, u_id): data = getData() remover_id = getUidFromToken(token) # check if channel exists channel = getChannelFromChannelId(channel_id) # check if authorized user is an owner of slackr / owner of channel if remover_id not in channel['owner_members']: raise AccessError("You are not authorized to remove an owner") # check if authorized user is the only owner and they are attempting to remove themself if len(channel['owner_members'] ) == 1 and remover_id in channel['owner_members']: raise ValueError_http( "You cannot remove yourself as an owner because you are the only owner" ) # check if user is not an owner of channel if u_id in channel['owner_members']: channel['owner_members'].remove(u_id) return () else: raise ValueError_http("User is not an owner of the channel")
def message_send(token, channel_id, message): global GLOBAL_DATA # Send message from authorised user to channel if len(message) > 1000: raise ValueError("Message is more than 1000 characters") # raise AccessError when user has has not joined the channel target_channel = GLOBAL_DATA["channels"][channel_id] user_name = get_user_from_token(token) if user_name not in target_channel.members: raise AccessError( "authorised user has not joined the channel they are trying to post to" ) # creates a new message instance and adds it to global data new_message = Message(token, message) target_channel.messages.append(new_message) GLOBAL_DATA["messages"].append(new_message) return {"message_id": new_message.message_id}
def channel_messages(token, channel_id, start): data = getData() u_id = getUidFromToken(token) # check if channel exists channel = getChannelFromChannelId(channel_id) # check if authorized user is a member of channel if u_id not in channel['all_members']: raise AccessError("Authorised User not a member of channel") # check if there are no messages if len(channel['messages']) == 0: return {'messages': [], 'start': start, 'end': start} # check if start < total number of messages if start < len(channel['messages']): if len(channel['messages'][start:]) > 50: end = start + 50 mes = getMessages(u_id, channel['messages'], start, end) else: end = -1 mes = getMessages(u_id, channel['messages'], start, end) return {'messages': mes, 'start': start, 'end': -1} else: raise ValueError_http("Start is greater")
def message_send(token, channel_id, message): global message_id # check for access error (user must be in the channel) if user_is_in_channel(getUidFromToken(token), channel_id) == False: raise AccessError('User not in the channel') if len(message) > 1000: raise ValueError_http("Message cannot be longer than 1000 characters") #if message contains /satndup add to standup dict # add message details channel = getChannelDetails(channel_id) message_id += 1 channel['messages'].insert(0, { 'message_id': message_id, 'u_id': getUidFromToken(token), 'message': message, 'time_created': str(datetime.datetime.now(datetime.timezone.utc).astimezone().timestamp()), #convert to sydney timezone 'reacts': [{'react_id': 1, 'u_ids': [], 'is_this_user_reacted' : False}], 'is_pinned': False }) return {'message_id': message_id}
def channel_details(token, channel_id): data = getData() u_id = getUidFromToken(token) owner_members = [] all_members = [] # check if channel exists channel = getChannelFromChannelId(channel_id) # check if authorized user is a member of channel if u_id in channel['all_members']: name = channel['name'] for owners in channel['owner_members']: owner_members_dic = getDetailsFromUid(owners) owner_members.append(owner_members_dic) for members in channel['all_members']: all_members_dic = getDetailsFromUid(members) all_members.append(all_members_dic) return { 'name': name, 'owner_members': owner_members, 'all_members': all_members } else: raise AccessError("Authorized user not a member of channel")
def admin_userpermission_change(token, u_id, permission_id): # check if permission_id is a valid value for permission_id if permission_id != 1 and permission_id != 2 and permission_id != 3: raise ValueError_http( "Permission_id doesn't refer to a value permission.") foundUser1 = getUserFromToken(token) # check if token is valid foundUser2 = getUserById(u_id) # check if u_id is valid # check if token refer to a member, not an admin or owner if foundUser1['permission_id'] == 3: raise AccessError("Authorised user is not an admin or owner") # check if token refers to admin elif foundUser1['permission_id'] == 2: # u_id refers to member if foundUser2['permission_id'] == 3: # permission_id is 3 (member) if permission_id == 3: raise ValueError_http("User is already a member.") # permission_id is 2 (admin) elif permission_id == 2: foundUser2['permission_id'] = 2 # permission_id is 2 (owner) elif permission_id == 1: raise ValueError_http("Authorised user is not an owner.") # u_id refers to an admin elif foundUser2['permission_id'] == 2: # permission_id is 3 (member) if permission_id == 3: foundUser2['permission_id'] = 3 # permission_id is 2 (admin) elif permission_id == 2: raise ValueError_http("User is already an admin.") elif permission_id == 1: raise ValueError_http("Authorised user is not an owner.") # u_id refers to an owner elif foundUser2['permission_id'] == 1: # permission_id is 3 (member) if permission_id == 3: raise ValueError_http("Authorised user is not an owner.") # permission_id is 2 (admin) elif permission_id == 2: raise ValueError_http("Authorised user is not an owner.") elif permission_id == 1: raise ValueError_http("Authorised user is not an owner.") # check if token refers to an owner elif foundUser1['permission_id'] == 1: # u_id refers to a member if foundUser2['permission_id'] == 3: # permission_id is 3 (member) if permission_id == 3: raise ValueError_http("User is already a member.") # permission_id is 2 (admin) elif permission_id == 2: foundUser2['permission_id'] = 2 # permission_id is 2 (owner) elif permission_id == 1: foundUser2['permission_id'] = 1 # u_id refers to an admin elif foundUser2['permission_id'] == 2: # permission_id is 3 (member) if permission_id == 3: foundUser2['permission_id'] = 3 # permission_id is 2 (admin) elif permission_id == 2: raise ValueError_http("User is already an admin.") elif permission_id == 1: foundUser2['permission_id'] = 1 # u_id refers to an owner elif foundUser2['permission_id'] == 1: if foundUser1 == foundUser2: foundOwner = check_last_owner(u_id) if foundOwner == 1: raise ValueError_http("You're the last owner!") elif foundOwner > 1: foundUser2['permission_id'] = 1 # permission_id is 3 (member) if permission_id == 3: foundUser2['permission_id'] = 3 # permission_id is 2 (admin) elif permission_id == 2: foundUser2['permission_id'] = 2 elif permission_id == 1: raise ValueError_http("User is already an owner.") return ({})