def message_sendlater(token, channel_id, messsage, time_sent): data = getData() #Test for valid message if len(message) > 1000: myexcept.invalid_message() #Test for valid token flag = 0 for user in data['users']: if str(user['token']) == token and token != None: u_id = user['u_id'] flag = 1 if flag == 0: myexcept.token_error() #Test for valid time format = '%Y-%m-%d %H:%M:%S' present_time = datetime.now() send_time = datetime.strptime(time_sent, format) convert_present_time = datetime.strftime(present_time, format) present = datetime.strptime(convert_present_time, format) if send_time < present: myexcept.invalid_time() tdelta = send_time - present sendlater_time = present + timedelta(minutes=1) + tdelta delay_time = (sendlater_time - present).total_seconds() #Test if channel_id is valid and that user is in the channel_id channel_found = 0 member_found = 0 for channel in data['channels']: if str(channel['channel_id']) == channel_id: channel_found = 1 for member in channel['all_members']: if member['u_id'] == u_id: member_found = 1 time.sleep(delay_time) answer = { 'message_id': 1, 'u_id': u_id, 'message': message, 'time_created': datetime.now(), 'reacts': {}, 'is_pinned': False } channel['messages'].append(answer) sendlater = {'message_id': answer['message_id']} updateData(data) return sendlater if channel_found == 0: myexcept.channel_not_found() elif member_found == 0: myexcept.member_not_in_channel()
def channel_invite(token, channel_id, u_id): data = getData() # checking that token is a valid user authorised_user_exists = False for user in data['users']: if str(user['token']) == token and token != None: auth_u_id = user['u_id'] perm_id = user['permission_id'] authorised_user_exists = True if authorised_user_exists == False: myexcept.token_error() # checking that u_id is a valid user member_exists = False for user in data['users']: if user['u_id'] == u_id: name_first = user['first_name'] name_last = user['last_name'] member_exists = True if member_exists == False: myexcept.invalid_user() token_is_member = False valid_channel = False # checking authorised user is already a member of the channel for channel in data['channels']: if channel['channel_id'] == channel_id: valid_channel = True for member in channel['all_members']: if member['u_id'] == auth_u_id: token_is_member = True if valid_channel == False: myexcept.channel_not_found() if token_is_member == False: myexcept.authorised_member_not_in_channel() # adding user to channel for channel in data['channels']: if channel['channel_id'] == channel_id: add_user = { 'u_id': u_id, 'name_first': name_first, 'name_last': name_last } channel['all_members'].append(add_user) if perm_id == '1' or perm_id == '2': channel['owner_members'].append(add_user) updateData(data) return {}
def channel_removeowner(token, channel_id, u_id): data = getData() found_Token = False found_User = False # finding token and user data for user in data['users']: if str(user['token']) == token: tu_id = user['u_id'] found_Token = True if str(user['u_id']) == u_id: name_first = user['first_name'] name_last = user['last_name'] found_User = True if found_Token == False: myexcept.token_error() if found_User == False: myexcept.invalid_user() found_Token = False found_User = False # checking user and token belongs to an owner for channel in data['channels']: if str(channel['channel_id']) == channel_id: for owner in channel['owner_members']: if owner['u_id'] == tu_id: found_Token = True if str(owner['u_id']) == u_id: found_User = True if found_Token == False: myexcept.authorised_member_not_admin() if found_User == False: myexcept.user_not_owner() send_Success = False # remove user from owner_members list for channel in data['channels']: if str(channel['channel_id']) == channel_id: channel['owner_members'].remove({ 'u_id': u_id, 'name_first': name_first, 'name_last': name_last }) send_Success = True updateData(data) return {} if send_Success == False: myexcept.channel_not_found()
def channel_addowner(token, channel_id, u_id): data = getData() send_Success = False found_Token = False # finding user data for user in data['users']: if str(user['u_id']) == u_id: name_first = user['first_name'] name_last = user['last_name'] send_Success = True if str(user['token']) == token: tu_id = user['u_id'] found_Token = True if send_Success == False: myexcept.invalid_user() if found_Token == False: myexcept.token_error() send_Success = False # checking token belongs to an owner for channel in data['channels']: if str(channel['channel_id']) == channel_id: for owner in channel['owner_members']: if owner['u_id'] == tu_id: send_Success = True if send_Success == False: myexcept.token_error() send_Success = False # adding user to owner_members list for channel in data['channels']: if str(channel['channel_id']) == channel_id: channel['owner_members'].append({ 'u_id': u_id, 'name_first': name_first, 'name_last': name_last }) send_Success = True updateData(data) return {} if send_Success == False: myexcept.channel_not_found()
def channel_messages(token, channel_id, start): data = getData() send_Success = False for channel in data['channels']: if channel['channel_id'] == channel_id: #for message in channel['messages']: # checking start is smaller than total number of messages if start > len(channel['messages']): myexcept.start_message_invalid() messages = channel['messages'] end = 50 + int(start) send_Success = True end2 = end if len(channel['messages']) - int(start) < 50: end2 = -1 return { 'messages': messages[int(start):end], 'start': start, 'end': end2 } if send_Success == False: myexcept.channel_not_found()
def message_send(token, channel_id, message): if len(message) > 1000: myexcept.invalid_message() data_new = getData() flag = 0 #Test for valid token for i in data_new['users']: if i['token'] == token and token != None: u_id = i['u_id'] flag = 1 if flag == 0: myexcept.token_error() #Test if channel_id is valid and that user is in the channel_id channel_found = 0 member_found = 0 for j in data_new['channels']: if j['channel_id'] == channel_id: channel_found = 1 for k in j['all_members']: if k['u_id'] == u_id: member_found = 1 answer = {} # assuming message_id is randomly generated message_id = randint(0, 1000000) answer['message_id'] = message_id answer['u_id'] = u_id answer['message'] = message answer['time_created'] = "12:04" answer['reacts'] = [] answer['is_pinned'] = False j['messages'].append(answer) updateData(data_new) return {'message_id': message_id} if channel_found == 0: myexcept.channel_not_found() elif member_found == 0: myexcept.member_not_in_channel()
def channel_leave(token, channel_id): data_new = getData() flag = 0 #Test for valid token for i in data_new['users']: if i['token'] == token and token != None: u_id = i['u_id'] flag = 1 if flag == 0: myexcept.token_error() member_found = 0 channel_found = 0 for channels in data_new['channels']: if channels['channel_id'] == channel_id: channel_found = 1 for members in channels['all_members']: member_count = len(channels['all_members']) if members['u_id'] == u_id: member_found = 1 if members in channels['owner_members']: channels['owner_members'].remove(members) channels['all_members'].remove(members) if member_count == 1: data['channels'].remove(channels) answer = {} updateData(data_new) return answer if channel_found == 0: myexcept.channel_not_found() if member_found == 0: myexcept.member_not_in_channel() answer = {} return answer
def channel_join(token, channel_id): data = getData() #Test for valid token flag = 0 for user in data['users']: # If token is valid, get user's u_id, first name, last name if user['token'] == token and token != None: u_id = user['u_id'] name_first = user['first_name'] name_last = user['last_name'] perm_id = user['permission_id'] flag = 1 if flag == 0: myexcept.token_error() channel_found = 0 member_not_found = 0 # search for the channel with channel_id for channel in data['channels']: if channel['channel_id'] == channel_id: if channel['is_public'] == False: if perm_id == '1' or perm_id == '2': channel_found = 1 # search in channel members, if the user is not already in the channel for member in channel['all_members']: if member['u_id'] != u_id: member_not_found = 1 add_user = { 'u_id': u_id, 'name_first': name_first, 'name_last': name_last } channel['all_members'].append(add_user) channel['owner_members'].append(add_user) return {} else: myexcept.private_channel_denied() elif channel['is_public'] == True: channel_found = 1 # search in channel members, if the user is not already in the channel for member in channel['all_members']: if member['u_id'] != u_id: member_not_found = 1 if perm_id == '1' or perm_id == '2': p_id = '1' else: p_id = '2' add_user = { 'u_id': u_id, 'name_first': name_first, 'name_last': name_last } channel['all_members'].append(add_user) updateData(data) return {} if channel_found == 0: myexcept.channel_not_found() elif member_not_found == 0: myexcept.member_in_channel()