Ejemplo n.º 1
0
def channel_details(token, channel_id):
    global data

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)

    # raise AccessError('authorised user is not in channel')
    acct = user_from_token(token)
    if not(channel_id in acct.in_channel):
        raise AccessError(description = 'authorised user is not in channel')

    channel_name = data['channels'][index].name

    owners_dict = []
    members_dict = []

    for i in data['channels'][index].owners:
        owner_member = user_from_uid(i)
        owners_dict.append({'u_id': i, 'name_first': owner_member.name_first, 'name_last': owner_member.name_last, 'profile_img_url': owner_member.prof_pic})
    
    for i in data['channels'][index].members:
        member = user_from_uid(i)
        members_dict.append({'u_id': i, 'name_first': member.name_first, 'name_last': member.name_last, 'profile_img_url': member.prof_pic})

    return {
        'name': channel_name,
        'owner_members': owners_dict,
        'all_members': members_dict
    }
Ejemplo n.º 2
0
def msg_remove(token, msg_id):
    global data
    remover = user_from_token(token)
    found_msg = find_msg(msg_id)
    # find the channel where the message belongs to
    msg_channel = find_channel(found_msg.in_channel)
    if found_msg.sender != remover.u_id:
        raise AccessError(
            description=
            'You do not have the permission to delete this message as you are not the sender!'
        )
    elif not check_channel_owner(msg_channel, remover.u_id):
        raise AccessError(
            description=
            'You do not have the permission as you are not the owner or admin of this channel!'
        )
    # no exception raised, then remove the message
    msg_channel.messages.remove(found_msg)
    return {}
Ejemplo n.º 3
0
def msg_edit(token, msg_id, new_msg):
    global data
    editor = user_from_token(token)
    found_msg = find_msg(msg_id)
    msg_channel = find_channel(found_msg.in_channel)
    # iter3 update: if the new msg is empty, delete the message
    if new_msg == '':
        msg_remove(token, msg_id)
    elif len(new_msg) > 1000:
        raise ValueError(description='Message is more than 1000 words!')
    elif found_msg.sender != editor.u_id:
        raise AccessError(
            description=
            'You do not have the permission to edit this message as you are not the sender!'
        )
    elif not check_channel_owner(msg_channel, editor.u_id):
        raise AccessError(
            description=
            'You do not have the permission as you are not the owner or admin of this channel or Slackr!'
        )
    # edit the message if no exceptions raiseds
    found_msg.message = new_msg
    return {}
Ejemplo n.º 4
0
def channel_messages(token, channel_id, start):
    global data

    # get the current user
    curr_user = user_from_token(token)

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)

    # raise AccessError if authorised user isn't in channel
    if curr_user.u_id not in data['channels'][index].members:
        raise AccessError(description = 'authorised user is not in channel')

    # raise ValueError if start is greater than no. of total messages
    no_total_messages = len(data['channels'][index].messages)
    if start > no_total_messages:
        raise ValueError(description = 'start is greater than no. of total messages')
    
    end = -1
    list_messages = []
    i = start

    for item in data['channels'][index].messages[i:]:
        message = {}
        message['message_id'] = item.message_id
        message['u_id'] = item.sender
        message['message'] = item.message
        message['time_created'] = item.create_time
        message['is_pinned'] = item.is_pinned
        message['reacts'] = []
        for react in item.reactions:
            reacter_list = []
            reacter_list.append(react.reacter)
            message['reacts'].append({'react_id': react.react_id, 'u_ids': reacter_list, 'is_this_user_reacted': (curr_user.u_id in item.reacted_user)})

        i = i + 1
        list_messages.append(message)
        if i == no_total_messages:
            end = -1
            break
        if i == (start + 50):
            end = i
            break
    
    return {
        'messages': list_messages,
        'start': start,
        'end': end
    }
Ejemplo n.º 5
0
def channel_add_owner(token, channel_id, u_id):
    global data

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)

    # check if user with u_id is already owner 
    if user_from_uid(u_id).u_id in data['channels'][index].owners:
        raise ValueError(description = 'User with u_id is already an owner')

    # check if authorised user is an owner of this channel
    if user_from_token(token).u_id not in data['channels'][index].owners:
        raise AccessError(description = 'Authorised user not an owner of this channel')
    
    data['channels'][index].owners.append(u_id)

    return {}
Ejemplo n.º 6
0
def channel_remove_owner(token, channel_id, u_id):
    global data

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)

    # raise ValueError if u_id is not an owner
    if u_id not in data['channels'][index].owners:
        raise ValueError(description = 'u_id is not an owner of the channel')

    # raise AccessError if token is not an owner of this channel
    if user_from_token(token).u_id not in data['channels'][index].owners:
        raise AccessError(description = 'authorised user is not an owner of this channel')
    
    data['channels'][index].owners.remove(u_id)

    return {}
Ejemplo n.º 7
0
def check_in_channel(u_id, channel):
    # in_channel = False
    # for user_id in channel.owners: # search owners list
    #     if user_id == u_id:
    #         in_channel = True
    # # if in_channel == False:
    # #     for acc in data["channels"][channel_index].admins: # search admins list
    # #         if token == acc.token:
    # #             in_channel = True
    # if in_channel == False:
    #     for acc in channel.members: # search members list
    #         if token == acc.token:
    #             in_channel = True
    in_channel = check_channel_owner(channel, u_id)
    if in_channel == False:
        in_channel = check_channel_member(channel, u_id)
    if in_channel == False:  # if the user is not in the channel, raise an error
        raise AccessError(description="You are currently not in this channel.")
Ejemplo n.º 8
0
def channel_invite(token, channel_id, u_id):
    global data

    # raise AccessError if authorised user not in channel
    # check if channel_id is part of User's in_channel list
    acct = user_from_token(token)
    if (channel_id in acct.in_channel) == False:
        raise AccessError(description = 'authorised user is not in channel')

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)
    data['channels'][index].members.append(u_id)

    # add channel to user's list of channels 
    acct = user_from_uid(u_id)
    acct.in_channel.append(channel_id)

    return {}
Ejemplo n.º 9
0
def msg_send(token, msg, chan_id):
    global data
    sending_time = datetime.now().replace(tzinfo=timezone.utc).timestamp()
    sender = user_from_token(token)
    current_channel = find_channel(chan_id)
    if len(msg) > 1000:
        raise ValueError(description='Message is more than 1000 words!')
    elif check_channel_member(current_channel, sender.u_id) == False:
        raise AccessError(
            description=
            'You have not joined this channel yet, please join first!')
    else:
        # generate an unique id
        data['message_count'] += 1
        msg_id = data['message_count']
        # no exceptions raised, then add(send) the message to the current channel
        current_channel.messages.insert(
            0, Mesg(sender.u_id, sending_time, msg, msg_id, chan_id, False))
    return {'message_id': msg_id}
Ejemplo n.º 10
0
def channel_join(token, channel_id):
    global data

    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)
    acct = user_from_token(token)
    # raise AccessError if channel is Private & authorised user is not an admin
    if data['channels'][index].is_public == False:
        # check if authorised user is an admin
        valid = 0
        if acct.perm_id < perm_member:
            valid = 1
            data['channels'][index].owners.append(acct.u_id)
        if valid == 0:
            raise AccessError(description = 'authorised user is not an admin of private channel')
    
    acct.in_channel.append(channel_id)
    data['channels'][index].members.append(acct.u_id)
    return {}
Ejemplo n.º 11
0
def msg_unpin(token, msg_id):
    global data
    unpinner = user_from_token(token)
    found_msg = find_msg(msg_id)
    msg_channel = find_channel(found_msg.in_channel)
    if check_channel_member(msg_channel, unpinner.u_id) == False:
        raise AccessError(
            description=
            'You can not unpin the message as you are not a member of the channel'
        )
    elif found_msg.is_pinned == False:
        raise ValueError(description='The message is already unpinned!')
    elif not check_slackr_admin(unpinner):
        raise ValueError(
            description=
            'You can not unpin the message as you are not an Admin of the channel'
        )
    # unpin the message if no exceptions raised
    found_msg.is_pinned = False
    return {}
Ejemplo n.º 12
0
def channel_leave(token, channel_id):
    global data
    # raise ValueError if channel_id doesn't exist (channel_index)
    index = channel_index(channel_id)
    acct = user_from_token(token)
    
    # raise AccessError if authorised user not in channel
    if (channel_id in acct.in_channel) == False:
        raise AccessError('authorised user is not in channel')

    for j in data['channels'][index].members:
        if j == acct.u_id:
            # use .remove() instead of .pop() as .remove() takes in the actual element
            data['channels'][index].members.remove(j) 
    
    for j in data['channels'][index].owners:
        if j == acct.u_id:
            data['channels'][index].owners.remove(j)

    return {}
Ejemplo n.º 13
0
def user_from_uid(u_id):
    global data
    for acc in data['accounts']:
        if acc.u_id == u_id:
            return acc
    raise AccessError(description='u_id does not exist for any user')
Ejemplo n.º 14
0
def user_from_token(token):
    global data
    for acc in data['accounts']:
        if acc.token == token:
            return acc
    raise AccessError(description='token does not exist for any user')