def channel_details(token, channel_id): data = getData() #Test for valid token flag = 0 for user in data['users']: if user['token'] == token and token != None: u_id = user['u_id'] flag = 1 if flag == 0: myexcept.token_error() flag = 0 flag2 = 0 for channel in data['channels']: if channel['channel_id'] == channel_id: flag = 1 for member in channel['all_members']: if member['u_id'] == u_id: flag2 = 1 answer = { 'name': channel['channel_name'], 'owner_members': channel['owner_members'], 'all_members': channel['all_members'] } return answer if flag == 0 or flag2 == 0: myexcept.member_not_in_channel()
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 message_pin(token, message_id): data = getData() # checking that token is an admin owner = False authorised_user_exists = False for user in data['users']: if user['token'] == token and token != None: auth_u_id = user['u_id'] perm_id = user['permission_id'] authorised_user_exists = True # find channel_id for channels in data['channels']: channel_id = channels['channel_id'] for messages in channels['messages']: if messages['message_id'] == message_id: break # checking that token is an owner of the channel for channels in data['channels']: if channels['channel_id'] == channel_id: for owners in channels['owner_members']: if owners['u_id'] == auth_u_id: owner = True if perm_id == 3 and owner == False: myexcept.not_an_admin() if authorised_user_exists == False: myexcept.auth_token_not_found() #Test if message_id exists message_found = False member_has_admin = False member_found = False for channels in data['channels']: #Finding the message for messages in channels['messages']: if messages['message_id'] == message_id: message_found = True # checking if the authorised user is in the channel for members in channels['all_members']: if members['u_id'] == auth_u_id: member_found = True if member_found == False: myexcept.member_not_in_channel() # checking if the message is already pinned if messages['is_pinned'] == True: myexcept.message_already_pinned() messages['is_pinned'] = True answer = {} updateData(data) return answer # checking if the message_id exists if message_found == False: myexcept.message_not_found()
def message_react(token, message_id, react_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() #Test if message_id exists message_found = 0 member_found = 0 for j in data_new['channels']: #Finding the message for k in j['messages']: if k['message_id'] == message_id: message_uid = k['u_id'] message_found = 1 #Testing to see if user is in the channel and has owner permission for l in j['all_members']: if l['u_id'] == u_id: member_found = 1 if member_found == 0: myexcept.member_not_in_channel() react_check = 0 for react in k['reacts']: if len(react) != 0 and react['react_id'] == react_id: react_check = 1 if u_id in react['u_ids']: myexcept.message_already_reacted() react['u_ids'].append(u_id) if message_uid == u_id: react['is_this_user_reacted'] = True else: react['is_this_user_reacted'] = False if react_check == 0: answer = {} answer['react_id'] = react_id answer['u_ids'] = [u_id] if message_uid == u_id: answer['is_this_user_reacted'] = True else: answer['is_this_user_reacted'] = False k['reacts'].append(answer) answer = {} updateData(data_new) return answer if message_found == 0: myexcept.message_not_found()
def standup_start(token, channel_id): data = getData() #Test for valid token token_valid = False for user in data['users']: if str(user['token']) == token and token != None: u_id = user['u_id'] token_valid = True if token_valid == False: myexcept.token_error() now = datetime.now() present_time_str = datetime.strftime(now, '%H:%M') present_time = datetime.strptime(present_time_str, '%H:%M') present_time_dict = {'time_finish': present_time_str} standup_end = present_time + timedelta(minutes=15) standup_end_dict = {'time_finish': standup_end} channel_found = False member_found = False for channel in data['channels']: if str(channel['channel_id']) == channel_id: channel_found = True if channel['standup_active'] == True: myexcept.active_stand_up() for member in channel['all_members']: if member['u_id'] == u_id: member_found = True channel['standup_active'] == True # can't input more messages but can test for 1 message while present_time_dict != standup_end_dict: now_update = datetime.now() present_time_str = datetime.strftime( now_update, '%H:%M') present_time_dict = {'time_finish': present_time_str} timerAction() return sendSuccess(standup_end_dict) if channel_found == False: myexcept.channel_name_invalid() if member_found == False: myexcept.member_not_in_channel()
def message_unreact(token, message_id, react_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() #Test if message_id exists message_found = 0 member_found = 0 for j in data_new['channels']: #Finding the message for k in j['messages']: if k['message_id'] == message_id: message_uid = k['u_id'] message_found = 1 #Testing to see if user is in the channel and has owner permission for l in j['all_members']: if l['u_id'] == u_id: member_found = 1 if member_found == 0: myexcept.member_not_in_channel() react_check = 0 for react in k['reacts']: react_dic = react if str(react['react_id']) == react_id: print(f"\n\n\n\n\n {react_dic} \n\n\n\n\n\n") k['reacts'].remove(react_dic) react_check = 1 if message_uid == u_id: react['is_this_user_reacted'] = False if react_check == 0: myexcept.message_already_unreacted() answer = {} updateData(data_new) return answer if message_found == 0: myexcept.invalid_message_id()
def message_unpin(token, message_id): data = getData() flag = 0 # checking that token is an admin 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.auth_token_not_found() if perm_id == 3: myexcept.not_an_admin() #Test if message_id exists message_found = False member_has_admin = False member_found = False for channels in data['channels']: #Finding the message for messages in channels['messages']: if str(messages['message_id']) == message_id: message_found = True # checking if the authorised user is in the channel for members in channels['all_members']: if members['u_id'] == auth_u_id: member_found = True if member_found == False: myexcept.member_not_in_channel() # checking if the message is already unpinned if messages['is_pinned'] == False: myexcept.message_already_unpinned() messages['is_pinned'] = False answer = {} updateData(data) return answer # checking if the message_id exists if message_found == False: myexcept.message_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 standup_send(token, channel_id, message): data = getData() token_valid = False for user in data['users']: if str(user['token']) == token and token != None: u_id = user['u_id'] token_valid = True if token_valid == False: myexcept.token_error() if len(message) > 1000: myexcept.invalid_message() channel_found = False member_found = False standup_queue = [] for channel in data['channels']: if str(channel['channel_id']) == channel_id: channel_found = True if channel['standup_active'] == False: myexcept.inactive_stand_up() for member in channel['all_members']: if member['u_id'] == u_id: member_found = True standup_start.standup_start(token, channel_id) standup_queue.append(message) return sendSuccess({}) if channel_found == False: myexcept.channel_name_invalid() if member_found == False: myexcept.member_not_in_channel()