示例#1
0
def channel_messages(token, channel_id, start):
    """
    Obtain message chain for user in a channel.
    """
    # Open database to get info for error testing
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    # Error checking
    if not channel:
        raise InputError('Channel does not exist')
    if channel_member['permissions'] != OWNER_PERMISSIONS:
        if channel_member['u_id'] not in channel['members_id']:
            raise AccessError('Not a member in the channel')
    if start >= len(channel['messages']) and start != 0:
        raise InputError('Non-existent message')
    result = {}
    result['start'] = start
    # Use slicing and try statement to slice list of messages
    if start + 50 > len(channel['messages']):
        result['end'] = -1
    else:
        result['end'] = start + 50
    result['messages'] = channel['messages'][start:start + 50]
    return result
示例#2
0
def message_send(token, channel_id, message):
    """
    Send message in channel.
    """
    # Raise error if message is too long
    if len(message) > 1000:
        raise InputError('Message over 1000 words')
    database = DataBase()
    user_id = database.get_account_info(token)['u_id']
    channel = database.get_channel_info(channel_id)
    # Raise error is user_id is not part of channel.
    if user_id not in channel['members_id']:
        raise AccessError('Not a channel member')
    # Create message_id
    channel_id_str = str(NO_CHANNELS - channel_id)
    message_id = channel_id_str + str(channel['no_sent_messages']).zfill(5)
    message_id += str(user_id)
    message_id = int(message_id)
    # Create message_dict
    dt = datetime.now()
    timestamp = int(dt.replace(tzinfo=timezone.utc).timestamp())
    message_dict = {}
    message_dict['message_id'] = message_id
    message_dict['u_id'] = user_id
    message_dict['message'] = message
    message_dict['time_created'] = timestamp
    message_dict['reacts'] = [{'react_id': 1, 'u_ids': [], 'is_this_user_reacted': False}]
    message_dict['is_pinned'] = False
    channel['messages'] = [message_dict] + channel['messages']
    channel['no_sent_messages'] += 1
    database.close()
    return {
        'message_id': message_id,
    }
示例#3
0
def test_standup_active_ends_and_sends_message(standup_init):
    standup_start(standup_init[0]['token'], standup_init[2], 10)
    standup_send(standup_init[0]['token'], standup_init[2], "heyo")
    standup_send(standup_init[0]['token'], standup_init[2], "heyo0000")
    time.sleep(11)
    # Not active
    result = standup_active(standup_init[0]['token'], standup_init[2])
    assert not result['is_active']
    database = DataBase()
    messages = database.get_channel_info(standup_init[2])['messages']
    assert messages[0]
示例#4
0
def test_channel_leave_remote(url, channel_init):
    '''
    test_channel_leave_remote
    '''
    requests.post(f'{url}channel/leave',
                  json={
                      'token': channel_init[1]['token'],
                      'channel_id': channel_init[2]
                  })
    database = DataBase()
    assert channel_init[1]['u_id'] not in database.get_channel_info(
        channel_init[2])['members_id']
示例#5
0
def test_channel_removeowner_remote(url, channel_init):
    '''
    test_channel_removeowner_remote
    '''
    requests.post(f'{url}channel/removeowner',
                  json={
                      'token': channel_init[0]['token'],
                      'channel_id': channel_init[2],
                      'u_id': channel_init[0]['u_id']
                  })
    database = DataBase()
    assert channel_init[0]['u_id'] not in database.get_channel_info(
        channel_init[2])['owners_id']
示例#6
0
def test_channel_join_remote(url, channel_init):
    '''
    test_channel_join_remote
    '''
    test_channel_public_id = channels_create(channel_init[0]['token'],
                                             f"test_public{UNIQUE_ID}", True)
    requests.post(f'{url}channel/join',
                  json={
                      'token': channel_init[1]['token'],
                      'channel_id': test_channel_public_id['channel_id']
                  })
    database = DataBase()
    assert channel_init[1]['u_id'] in database.get_channel_info(
        test_channel_public_id['channel_id'])['members_id']
示例#7
0
def test_channel_details_remote(channel_init, url):
    '''
    test_channel_details_remote
    '''
    query_string = urllib.parse.urlencode({
        'token': channel_init[0]['token'],
        'channel_id': channel_init[2]
    })
    #print(f'{url}channel/details?{query_string}')
    print(f'{url}channel/details?{query_string}')
    response = requests.get(f'{url}channel/details?{query_string}')
    print(response.text)
    payload = response.json()
    database = DataBase()
    assert database.get_channel_info(
        channel_init[2])['name'] == payload['name']
示例#8
0
def channel_leave(token, channel_id):
    """
    Remove user from channel with valid token.
    """
    # Get info from database for error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    # Error checking
    if not channel:
        raise InputError('Channel does not exist')
    if channel_member['permissions'] != OWNER_PERMISSIONS:
        if channel_member['u_id'] not in channel['members_id']:
            raise AccessError('Not a member in the channel')
    # Change database
    channel['members_id'].remove(channel_member['u_id'])
    database.close()
    return {}
示例#9
0
def channel_removeowner(token, channel_id, u_id):
    """
    Remove the ownwer of channel with user with valid channel ownership.
    """
    # Get info for error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    # Check for errors
    if not channel or u_id not in channel['owners_id']:
        raise InputError('Channel does not exist or user is not an owner')
    if (channel_member['u_id'] not in channel['owners_id'] and 
        channel_member['permissions'] != OWNER_PERMISSIONS):
        raise AccessError('Not an owner of this channel')
    # Update database
    channel['owners_id'].remove(u_id)
    database.close()
    return {}
示例#10
0
def channel_addowner(token, channel_id, u_id):
    """
    Add channel ownwers for user with valid channel ownership.
    """
    # Get info for error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    # Error checking
    if not channel or u_id in channel['owners_id']:
        raise InputError('Channel does not exist or user is not an owner')
    if channel_member['permissions'] != OWNER_PERMISSIONS:
        if channel_member['u_id'] not in channel['owners_id']:
            raise AccessError('Not an owner of the channel')
    # Update database
    channel['owners_id'].append(u_id)
    database.close()
    return {}
示例#11
0
def channel_join(token, channel_id):
    """
    Add valid user to channel.
    """
    # Get info from database for error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    joining_member = database.get_account_info(token)
    # Error checking
    if not channel:
        raise InputError('Channel does not exist')
    if joining_member['permissions'] != OWNER_PERMISSIONS:
        if not channel['is_public']:
            raise AccessError('Channel is private')
    # Update database
    channel['members_id'].append(joining_member['u_id'])
    database.close()
    return {}
示例#12
0
def channel_invite(token, channel_id, u_id):
    """
    Invite registered users to channels.
    """
    # Open database and gather channel info and member info
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    add_member = database.get_info_from_id(u_id)
    # Error testing
    if not channel or not add_member:
        raise InputError('Channel doesn\'t exist or member doesn\'t exist')
    if channel_member['permissions'] != OWNER_PERMISSIONS:
        if channel_member['u_id'] not in channel['members_id']:
            raise AccessError('Not a member in the channel')
    # Update database
    channel['members_id'].append(u_id)
    database.close()
    return {}
示例#13
0
def message_remove(token, message_id):
    """
    Remove message in channel
    """
    database = DataBase()
    user_id = database.get_account_info(token)['u_id']
    channel_id = str(message_id)  
    channel_id = NO_CHANNELS - int(channel_id[0:5])
    channel = database.get_channel_info(channel_id)
    channel_msgs = database.get_channel_messages(channel_id)
    message_user_id = int(str(message_id)[10:])
    # Check users allowed to delete messages
    if user_id not in channel['owners_id'] and user_id != message_user_id:
        raise AccessError('Not a member of channel')
    # Check if message exists to delete in list
    message_index = find_message_index(message_id, channel_msgs)
    channel_msgs.pop(message_index)
    database.close()
    return {}
示例#14
0
def channel_details(token, channel_id):
    """
    Obtain channel details from database with a valid token.
    """
    # Open database amd gather important channel info for error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    # Error checking
    if not channel:
        raise InputError('Channel does not exist')
    if channel_member['permissions'] != OWNER_PERMISSIONS:
        if channel_member['u_id'] not in channel['members_id']:
            raise AccessError('Not a member in the channel')
    result = {}
    result['name'] = channel['name']
    result['owner_members'] = make_user_list(channel['owners_id'])
    result['all_members'] = make_user_list(channel['members_id'])
    return result
示例#15
0
def message_unpin(token, message_id):
    """
    Unpin messages.
    """
    database = DataBase()
    channel_id = str(message_id)  
    channel_id = NO_CHANNELS - int(channel_id[0:5])
    channel_msgs = database.get_channel_messages(channel_id)
    message_index = find_message_index(message_id, channel_msgs)
    # Error checking
    msg = channel_msgs[message_index]
    channel = database.get_channel_info(channel_id)
    channel_member_id = database.get_account_info(token)['u_id']
    if channel_member_id not in channel['members_id']:
        raise AccessError('Not a member in the channel')
    if not msg['is_pinned']:
        raise InputError('Message is already unpinnned')
    # Unpin messages
    channel_msgs[message_index]['is_pinned'] = False
    database.close()
    return {}
示例#16
0
def message_edit(token, message_id, message):
    """
    Edit messages or delete them.
    """
    # Get channel which message_id attaches itself too
    database = DataBase()
    user_id = database.get_account_info(token)['u_id']
    channel_id = str(message_id)  
    channel_id = NO_CHANNELS - int(channel_id[0:5])
    channel = database.get_channel_info(channel_id)
    message_user_id = int(str(message_id)[10:])
    message_index = find_message_index(message_id, channel['messages'])
    # Error handling
    if user_id not in channel['owners_id'] and user_id != message_user_id:
        raise AccessError('Not member of channel')
    # Edit or delete message
    if message:
        channel['messages'][message_index]['message'] = message
    else:
        channel['messages'].pop(message_index)
    database.close()
    return {}
示例#17
0
def message_sendlater(token, channel_id, message, time_sent):
    '''
    Send message at set time
    Time_sent can be both integer and timestring
    '''
    #Error checking
    database = DataBase()
    channel = database.get_channel_info(channel_id)
    channel_member = database.get_account_info(token)
    if not channel or len(message) > 1000 or time_sent < 0:
        raise InputError
    if channel_member['u_id'] not in channel['members_id']:
        raise AccessError('Not a member in the channel')
    #Implementation
    with concurrent.futures.ThreadPoolExecutor(1) as exe:
        def sleep_for(secs):
            time.sleep(secs)
            return 1
        exe.submit(sleep_for, time_sent)
        future = exe.submit(message_send, token, channel_id, message)
        return_val = future.result()
        return return_val