def end_standup(payload): # pylint: disable=R1711 'run this function where it collates all messages into one' messages = get_messages_store() standup_message = '' channel = get_channel(payload['channel_id']) standup = channel['standup'] user = get_user_token(payload['token']) # formating all the messages into one message package for msg in standup['messages']: standup_message += msg['Name_first'] standup_message += ': ' standup_message += msg['Message'] standup_message += '\n' standup_message = standup_message[0:(len(standup_message)-1)] new_message = create_message() new_message['channel_id'] = channel['channel_id'] new_message['u_id'] = user['u_id'] new_message['message'] = standup_message messages.append(new_message) channel['messages'].append(new_message) # reset standup standup['is_active'] = False standup['time_finish'] = 'N/A' print('finished standup') return
def send(payload): ''' function to take a payload, create a new mesaage, fill in the parameters and append it to the channels messages store as well as the gloabal message_store ''' user = get_user_from('token', payload['token']) channel = get_channel(payload['channel_id']) if not test_in_channel(user['u_id'], channel): raise AccessError(description='User is not in channel') # create a message data type, and fill in details # then append to the channels list of messages txt = payload['message'] if len(txt) > 1000: raise InputError(description='Message is more than 1000 characters') # create the message dictionary new_message = create_message() new_message['u_id'] = user['u_id'] new_message['message'] = txt new_message['channel_id'] = payload['channel_id'] channel['messages'].append(new_message) if txt == '/hangman': if not channel['hangman_active']: hangman.start(channel) channel['hangman_active'] = True else: hangman.message(channel, 'There is already an active game running') hangman.message(channel, hangman.display_hangman()) if txt.split()[0] == '/guess': if not channel['hangman_active']: hangman.message( channel, 'There is not a current game of hangman running.\n' + r'If you would like to start one, type \hangman into the chat') else: if len(txt.split()) == 2: new_guess = txt.split()[1] if new_guess in r'!@#$%^&*()_+-=[]\;<>?/~`:': hangman.message(channel, 'Invalid guess, guess again') else: hangman.guess(channel, new_guess) else: hangman.message(channel, 'Invalid guess, guess again') # append it to the messages_store messages = get_messages_store() messages.append(new_message) save_messages_store() # append it to the channels file save_channel_store() return new_message
def find_message(message_id): ''' Function to find a message and return its details ''' messages_store = get_messages_store() for i in messages_store: if i['message_id'] == int(message_id): return i raise InputError(description='Message not found')
def test_admin_user_remove_successful3(): 'Successful case with messages' workspace_reset() channel_store = get_channel_data_store() messages_store = get_messages_store() ret = register_and_create() user1 = ret['user'] token1 = user1['token'] u_id1 = user1['u_id'] user2 = reg_user2() token2 = user2['token'] u_id2 = user2['u_id'] channel_info = ret['channel'] channel_id = channel_info['channel_id'] channel.join(token2, channel_id) payload = {'token': token1, 'u_id': u_id2} msg1 = send_msg1(user2, channel_info) # Remove user1 from slackr user_remove(payload) user2_dets = user_details(u_id2) channel_info = ret['channel'] channel_id = channel_info['channel_id'] for curr_channel in channel_store: channel_id = curr_channel['channel_id'] if user2_dets in curr_channel['members']: assert channel.details(token1, channel_id)['all_members'] == [{ "u_id": u_id1, "name_first": 'Kennan', "name_last": 'Wong' }] if user2_dets in curr_channel['owners']: assert channel.details(token1, channel_id)['owner_members'] == [{ "u_id": u_id1, "name_first": 'Kennan', "name_last": 'Wong' }] assert msg1 not in messages_store assert msg1 not in channel_info['messages']
def sendlater(payload): ''' Function to create a message and have it be sent at a ''' user = get_user_from('token', payload['token']) channel = get_channel(payload['channel_id']) messages = get_messages_store() if not test_in_channel(user['u_id'], channel): raise InputError(description='User is not in channel') # create a message data type, and fill in details # then append to the channels list of messages txt = payload['message'] if len(txt) > 1000: raise InputError(description='Message is more than 1000 characters') # create the message dictionary time = payload['time_sent'] print(type(time)) if time < int(datetime.now().timestamp()): raise InputError(description='Unable to send as ' + 'time sent is a time in the past') new_message = create_message() new_message['time_created'] = time new_message['u_id'] = user['u_id'] new_message['message'] = txt new_message['channel_id'] = payload['channel_id'] # append it to the messages_store first messages.append(new_message) save_messages_store() interval = (time - datetime.now().timestamp()) # append to the channel message store at a later time timer = threading.Timer(interval, append_later, args=[new_message['message_id']]) timer.start() # debugging purposes for msg in channel['messages']: print(msg['message']) return new_message['message_id']
def test_remove1(): ''' Test a valid use of message.remove ''' workspace_reset() messages_store = get_messages_store() ret = register_and_create() user = ret['user'] channel1 = ret['channel'] msg1 = send_msg1(user, channel1) message.remove({'token': user['token'], 'message_id': msg1['message_id']}) assert msg1 not in messages_store assert msg1 not in channel1['messages']
def remove(payload): # pylint: disable=R1711 ''' Function to remove a message from a channel ''' user = get_user_from('token', payload['token']) messages = get_messages_store() message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if message['u_id'] != user['u_id']: if not check_owner(user, channel): raise AccessError(description='You do not have permission') messages.remove(message) channel['messages'].remove(message) return
def user_remove(payload): 'This is the function for admin_user_remove' auth_store = get_auth_data_store() channel_store = get_channel_data_store() messages_store = get_messages_store() # remover_id = user_id_from_token(payload['token']) # remover_dets = user_details(remover_id) removee = get_user_uid(payload['u_id']) removee_dets = user_details(payload['u_id']) # InputError for invalid user ID of the person we are removing if not is_valid_user_id(int(payload['u_id'])): raise InputError(description='Invalid user_id') removee_token = removee['token'] # Removing any messages the user had sent for messages in messages_store: if message_belong_user(removee_token, messages['message_id']): messages_store.remove(messages) # Removing user from any channels they were in for channel in channel_store: if removee_dets in channel['members']: channel['members'].remove(removee_dets) if removee_dets in channel['owners']: channel['owners'].remove(removee_dets) for message in channel['messages']: if message['u_id'] == removee['u_id']: channel['messages'].remove(message) # AccessError when the authorised user is not an owner of the slackr if not check_owner_slackr(payload['token']): raise AccessError(description='User is not an owner of the slackr') else: auth_store.remove(removee) return {}
def edit(payload): ''' Function to remove a message from a channel ''' message_store = get_messages_store() user = get_user_from('token', payload['token']) message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if message['u_id'] != user['u_id']: if not check_owner(user, channel): raise AccessError(description='You do not have permission') if len(payload['message']) == 0: # pylint: disable=C1801, R1705 channel['messages'].remove(message) message_store.remove(message) return else: message['message'] = payload['message'] return
def test_edit3(): ''' Someone attempts to edit a message by replacing it witha a blank string ''' workspace_reset() message_store = get_messages_store() ret = register_and_create() user1 = ret['user'] channel1 = ret['channel'] msg1 = send_msg1(user1, channel1) message.edit({ 'token': user1['token'], 'message_id': msg1['message_id'], 'message': '' }) assert msg1 not in message_store assert msg1 not in channel1['messages']
def test_remove2(): ''' The admin of a channel is attempting to remove a message ''' workspace_reset() messages_store = get_messages_store() #register user1 and create channel1 ret = register_and_create() user1 = ret['user'] channel1 = ret['channel'] user2 = reg_user2() channel.invite(user1['token'], channel1['channel_id'], user2['u_id']) msg1 = send_msg1(user2, channel1) message.remove({'token': user1['token'], 'message_id': msg1['message_id']}) assert msg1 not in messages_store assert msg1 not in channel1['messages']
''' A file thatcontains helper function to simplify code and do repetitive tasks ''' import re import hashlib from datetime import datetime, timezone from error import InputError from data_stores import get_auth_data_store, get_channel_data_store, get_messages_store import string import random MSG_COUNT = 1 MSG_COUNT = len(get_messages_store()) + 1 def generate_token(u_id): ''' Function to generate a token ''' return hashlib.sha256(str(u_id).encode()).hexdigest() def create_message(): ''' Function to generate a blank message dictionary ''' global MSG_COUNT