Пример #1
0
def channel_details(server_data, token, channel_id):
    """
    channel_details function:
    A function that shows the details of a channel given a valid token, and that the user is authroized in the channel
    
    Input:
    - server_data (obj)
    - token (string)
    - channel_id (int)
    
    Output:
    - {name, owner_members, all_members}
    """
    
    # Check if the channel exists
    verify_channel_id(server_data, channel_id)

    # Verify token and get u_id
    u_id = verify_n_get_valid_token(server_data, token)

    # Verify if the user is a member of the channel
    verify_user_channel_status(server_data, channel_id, u_id, const.PERMISSION_LOCAL_MEMBER)

    # Update the channel's user list to ensure that it is up-to-date with the main user list
    update_channel_user_lists(server_data, channel_id)

    # Return the channel details
    channel_obj = server_data.get_channel_by_id(channel_id)
    rt_details = channel_obj.pack_channel_details()
    return rt_details
Пример #2
0
def remove_user(server_data, token, u_id):
    """
    A function that validates the token, and removes the user from the server

    Input:
    - (str): token
    - ()
    """
    # Verification steps
    verify_user_id(server_data, u_id)
    u_id_admin = verify_n_get_valid_token(server_data, token)

    if u_id_admin == u_id:
        raise AccessError(description="The user can not remove itself")

    # Get admin user and check global permission
    admin_user = server_data.get_user_by_id(u_id_admin)
    if admin_user.global_permission_id != const.PERMISSION_GLOBAL_OWNER:
        raise AccessError(description="The user is not user of slackr")

    cleanup_channels(server_data, u_id)
    cleanup_sessions(server_data, u_id)

    # Perform action
    server_data.remove_user(u_id)
    return {}
Пример #3
0
def channel_removeowner(server_data, token, channel_id, u_id):
    """
    Remove user with user u_id from an owner of this channel
    
    Input:
    - server_data(obj)
    - token (string)
    - channel_id (int)
    - u_id (int)
    
    Output:
    - {}
    """

    # Check if the channel exists
    verify_channel_id(server_data, channel_id)
        
    channel_obj = server_data.get_channel_by_id(channel_id)
        
    # Verify the token and get u_id from the active token
    u_id_owner = verify_n_get_valid_token(server_data, token)
    user_obj_owner = server_data.get_user_by_id(u_id_owner)

    # Verify if the u_id_owner is a owner of the channel or owner of slacker
    verify_user_channel_status(server_data, channel_id, u_id_owner, const.PERMISSION_LOCAL_OWNER)
            
    if channel_obj.is_user_owner(u_id):
        channel_obj.remove_owner(u_id) 
    else:
        # user is not an owner of the channel
        raise InputError(description="User is not an owner of the channel")

    return {}
Пример #4
0
def channel_leave(server_data, token, channel_id):
    """
    Given a channel_id, the user is removed as a member of this channel
    
    Input:
    - server_data(obj)
    - token (string)
    - channel_id (int)
    
    Output:
    - {}
    """

    # Check if the channel exists
    verify_channel_id(server_data, channel_id)

    # Verify token and get token if it is a valid session
    u_id = verify_n_get_valid_token(server_data, token)

    # Verify that the user is a member
    verify_user_channel_access(server_data, channel_id, u_id, const.USER_IN_CHANNEL)

    # Perform member removal
    channel_obj = server_data.get_channel_by_id(channel_id)
    channel_obj.remove_member(u_id) 
    return {}
Пример #5
0
def channel_addowner(server_data, token, channel_id, u_id):
    """
    Make user with user u_id an owner of this channel
    
    Input:
    - server_data(obj)
    - token (string)
    - channel_id (int)
    - u_id (int)
    
    Output:
    - {}
    """

    # Check if the channel exists
    verify_channel_id(server_data, channel_id)
   
    # Verify if token is active, and if so, get u_id from the active session
    u_id_owner = verify_n_get_valid_token(server_data, token)

    # Verify if the u_id_owner is a owner of the channel or owner of slacker
    verify_user_channel_status(server_data, channel_id, u_id_owner, const.PERMISSION_LOCAL_OWNER)
    
    channel_obj = server_data.get_channel_by_id(channel_id)        
    user_obj = server_data.get_user_by_id(u_id)
    # Is either slackr owner or channel owner
    # Now check if the user being added is already an owner
    if not channel_obj.is_user_owner(u_id): 
        assert channel_obj.add_owner(user_obj.get_user_member_info())
    else:
        raise InputError(description="User is already an owner")

    return {}
Пример #6
0
def channels_create(server_data, token, name, is_public):

    """
    channel_create function:
    A function that creates a channel in the server_data, if the token is valid. Returns a channel_id from the server_data function

    Inputs:
    - server_data (obj)
    - token (string)
    - name (string)
    - is_public (bool)

    Outputs
    - {channel_id} (dics)
    """

    # Check if the token is active, and get u_id if the token is active
    # Get u_id from the active session
    u_id = verify_n_get_valid_token(server_data, token)

    # Get user object from the u_id
    user_obj = server_data.get_user_by_id(u_id)
    # Then get the user info containing {u_id, name_first, name_last}
    user_info = user_obj.get_user_member_info()

    # Now check if the name is longer than 20 characters
    if len(name) > 20:
        raise InputError(description="Channel name too long")

    # Everything checks out, create channel in the database
    channel_id = server_data.create_channel(name, user_info, is_public)

    return {"channel_id": channel_id}
Пример #7
0
def channel_messages(server_data, token, channel_id, start):
    """
    channel_messages function
    a function that returns a list of messages from a channel, given the start point and a valid token
    
    Input:
    - server_data(obj)
    - token (string)
    - channel_id (int)
    - start (int)
    
    Output:
    - {messages, start, end}
    """
    
    # Check if the channel exists
    verify_channel_id(server_data, channel_id)
    
    # Check if the token is active
    u_id = verify_n_get_valid_token(server_data, token)

    channel_obj = server_data.get_channel_by_id(channel_id)
    if channel_obj.get_num_messages() == 0 and start == 0:
        return {
            "start": 0,
            "end": 0,
            "messages": [],
        }

    if start >= channel_obj.get_num_messages():
        raise InputError(description="Invalid start point")

    # Verify if the user is a member of the channel
    verify_user_channel_status(server_data, channel_id, u_id, const.PERMISSION_LOCAL_MEMBER)
        
    # All errors are detected, return the outcome as usual
    # Calculate the end point from the start
    end = start + 50
    if end > channel_obj.get_num_messages():
        end = -1
    
    # Get all message objects
    message_obj_list = channel_obj.get_message_range(start, end)
    
    rt_infos = {}
    rt_infos["start"] = start
    rt_infos["end"] = end
    rt_infos["messages"] = []
    
    for message_obj in message_obj_list:
        info = message_obj.pack_message_info(u_id)
        rt_infos["messages"].append(info)
        
    
    return rt_infos    
Пример #8
0
def channels_listall(server_data, token):
    """
    channels_listall function:
    A function that lists all channels and their details with a valid token

    Input:
    - token (string)

    Output:
    - {channels} (dic of list of {channel_id, name})
    """


    # Check if the token is active
    u_id = verify_n_get_valid_token(server_data, token)
    rt_list = server_data.pack_all_channel_infos()
    return rt_list
Пример #9
0
def channel_invite(server_data, token, channel_id, u_id):
    """
    channel_invite function
    a function that invites a user to join a channel, given that the user is valid and the token is valid
    
    input:
    - server_data (obj)
    - token (string)
    - channel_id (int)
    - u_id (int)
    
    output:
    - {}
    """
    
    # Verifying inputs
    verify_user_id(server_data, u_id)
    verify_channel_id(server_data, channel_id)
    u_id_from = verify_n_get_valid_token(server_data, token)
    verify_user_channel_access(server_data, channel_id, u_id, const.USER_NOT_IN_CHANNEL)

    # Get the target user object
    user_target = server_data.get_user_by_id(u_id)

    # Check if the user is already a part of the channel
    channel_obj = server_data.get_channel_by_id(channel_id)
    
        
    # Check if the user is in the channel
    verify_user_channel_status(server_data, channel_id, u_id_from, const.PERMISSION_LOCAL_MEMBER)

    # No more errors from now on, proceed to execute invitation
    user_info = user_target.get_user_member_info()
            
    # Check if the target user is owner of slackr
    if user_target.is_owner_of_slackr():
        # If user is owner of slackr, add owner and add member
        channel_obj.add_owner(user_info)
    else:
        # If user is regular member, add member
        channel_obj.add_member(user_info)
                
    return {}
Пример #10
0
def channels_list(server_data, token):
    """
    channels_list function:
    A function that lists all channels and their details with a valid token that a user is a part of

    Input:
    - token (string)

    Output:
    - {channels} (dic of list of {channel_id, name})
    """


    # Check if the token is active
    # If active, get the u_id from this token and perform action required
    u_id = verify_n_get_valid_token(server_data, token)
    rt_list = server_data.pack_channels_info_by_user(u_id)

    return rt_list
Пример #11
0
def channel_join(server_data, token, channel_id):
    """
    Given a channel_id of a channel that the authorised user can join, adds them  to that channel 
    
    Input:
    - server_data(obj)
    - token (string)
    - channel_id (int)

    Output:
    - {}
    """

    # Check if the channel exists
    verify_channel_id(server_data, channel_id)
  
    # Verify if the token is active, and get u_id and channel_id from the active session
    u_id = verify_n_get_valid_token(server_data, token)
    # Verify if the user is not in channel
    verify_user_channel_access(server_data, channel_id, u_id, const.USER_NOT_IN_CHANNEL)

    channel_obj = server_data.get_channel_by_id(channel_id)
    # Check if user is already a member of the channel
    user_obj = server_data.get_user_by_id(u_id)
    user_info = user_obj.get_user_member_info()

    if user_obj.is_owner_of_slackr():
        # User is an owner of slack
        channel_obj._member_list.append(user_info) 
        channel_obj._owner_list.append(user_info) 
    else: 
        # User is not an owner of slack
        if channel_obj._is_public:
            # Public channel
            channel_obj._member_list.append(user_info)       
        else:
            # Private channel
            raise AccessError(description="User not authorised")


    return {}