def message_sendlater(token, channel_id, message, time_sent): ''' sends a message at a given time_sent, where time_sent is a unix timestamp greater than the current time. ''' u_id = check_token(token) if not is_valid_channel(channel_id): raise InputError(description="No channel exists with that ID") if not is_user_a_member(channel_id, u_id): raise AccessError(description='You are not a member of this channel') if len(message) > 1000 or len(message) < 1: raise InputError( description= 'Your message should be less than 1000 characters and at least 1 character' ) curr_time = get_current_timestamp() if curr_time >= time_sent: raise InputError(description="You can not send a message back in time") delay = time_sent - curr_time message_id = get_num_messages() set_num_messages(message_id + 1) message_template = create_message(u_id, message_id, time_sent, message) timer = Timer(delay, sendlater_end, args=[channel_id, message_template]) timer.start() return {'message_id': message_id}
def message_send(token, channel_id, message): ''' adds message to a channels list of messages expects parameter types: token: str channel_id: int message: str returns dictionary ''' user_id = check_token(token) if len(message) > 1000 or len(message) < 1: raise InputError( description= 'Your message should be less than 1000 characters and at least 1 character' ) if not is_valid_channel(channel_id): raise InputError if not is_user_a_member(channel_id, user_id): raise AccessError message_id = get_num_messages() glob_channels = get_channels() channel = glob_channels[channel_id] time_created = get_current_timestamp() if message.startswith('/hangman'): start_hangman(channel_id, user_id, time_created, message_id) elif message.startswith('/guess'): guess(message, channel_id, user_id, time_created, message_id) else: channel['messages'].insert( 0, create_message(user_id, message_id, time_created, message)) set_num_messages(message_id + 1) return {'message_id': message_id}
def test_sendlater_invalid_token(inv_token, new_channel_and_user): ''' test function. checks that an access error is raised when sendlater is given an invalid token ''' channel_id = new_channel_and_user['channel_id'] time_sent = get_current_timestamp() + 2 with pytest.raises(AccessError): message_sendlater(inv_token, channel_id, 'message', time_sent)
def backup_data(): '''Pickles slackr data with a timestamp.''' slackr_data = { 'timestamp': get_current_timestamp(), 'global_users': get_users(), 'global_channels': get_channels(), 'global_num_messages': get_num_messages() } with open('slackr_data.p', 'wb') as FILE: #pylint: disable=invalid-name dump(slackr_data, FILE)
def test_sendlater_invalid_inputs(new_channel_and_user, user_chas): ''' test function. checks that errors are raised when sendlater is given invalid inputs ''' token = new_channel_and_user['token'] channel_id = new_channel_and_user['channel_id'] time_sent = get_current_timestamp() + 2 time_sent_invalid = get_current_timestamp() - 2 with pytest.raises(InputError): message_sendlater(token, -1, 'message', time_sent) with pytest.raises(InputError): message_sendlater(token, channel_id, '1' * 1001, time_sent) with pytest.raises(InputError): message_sendlater(token, channel_id, '', time_sent) with pytest.raises(AccessError): message_sendlater(user_chas['token'], channel_id, 'sdd', time_sent) with pytest.raises(AccessError): message_sendlater(user_chas['token'], channel_id, 'sdd', time_sent_invalid)
def test_sendlater_valid_inputs(new_channel_and_user): ''' checks that message is added to channels message after a delay ''' token = new_channel_and_user['token'] channel_id = new_channel_and_user['channel_id'] time_sent = get_current_timestamp() + 2 test_message = message_sendlater(token, channel_id, 'hi', time_sent) assert isinstance(test_message, dict) assert len(channel_messages(token, channel_id, 0)['messages']) == 0 sleep(2.5) assert len(channel_messages(token, channel_id, 0)['messages']) == 1
def standup_start(token, channel_id, length): ''' starts a standup in a given channel for length amount of time creates blank message with time_finish timestamp, placeholder message_id, and stores it in glob_standups ''' u_id = check_token(token) check_standup_inputs(channel_id, u_id) if is_standup_active(channel_id): raise InputError( description='A standup is already active in this channel') glob_standups = get_standups() time_finish = get_current_timestamp(length) message_template = create_message(u_id, -1, time_finish, []) glob_standups[channel_id] = message_template standup = Timer(length, standup_end, args=[channel_id]) standup.start() return {'time_finish': time_finish}