Пример #1
0
def pin(token, message_id):
    ''' Given a message within a channel, mark it as "pinned" to
    be given special display treatment by the frontend '''
    data = getData()
    user_id = getUserFromToken(token)
    user_dict = data['users'][user_id]
    msg_id = find_message(message_id)

    if msg_id is None:
        raise Value_Error("Message_Id is not a valid message")

    msg_dict = data['messages'][msg_id]

    if user_dict['permission'] == 3:
        raise Value_Error("The authorised user is not an admin")

    if not inChannel(token, msg_dict['channel_id']):
        raise AccessError(
            "The authorised user is not a member of the channel that the message is within"
        )

    if msg_dict['is_pinned'] is True:
        raise Value_Error(
            f"Message with ID message_id: {message_id} already pinned")

    msg_dict['is_pinned'] = True
    save(data)
    return {}
Пример #2
0
def uploadphoto(token, img_url, x_start, y_start, x_end, y_end):
    ''' Given a URL of an image on the internet, crops the image within
    bounds (x_start, y_start) and (x_end, y_end). Position (0,0) is the top left. '''
    if urllib.request.urlopen(img_url).getcode() != 200:
        raise Value_Error("img_url is returns an HTTP status other than 200.")

    if not img_url.lower().endswith('.jpg'):
        raise Value_Error("img_url is returns an HTTP status other than 200.")

    img_addr = './server/user_image/' + str(token) + '.jpg'
    urllib.request.urlretrieve(img_url, img_addr)
    imageObject = Image.open(img_addr)
    width, height = imageObject.size

    if x_end not in range(width + 1) or y_end not in range(
            height +
            1) or x_start not in range(x_end) or y_start not in range(y_end):
        os.remove(img_addr)
        raise Value_Error(
            "any of x_start, y_start, x_end, y_end are not within the dimensions of the image at the URL."
        )

    cropped = imageObject.crop((x_start, y_start, x_end, y_end))
    cropped.save(img_addr)
    data = getData()
    user_id = getUserFromToken(token)
    data['users'][user_id]['profile_img_url'] = '/user_image?file=' + str(
        token) + '.jpg'
    save(data)
    return {}
Пример #3
0
def unreact(token, message_id, react_id):
    ''' Given a message within a channel the authorised user is
    part of, remove a "react" to that particular message '''
    data = getData()
    user_id = getUserFromToken(token)
    user_dict = data['users'][user_id]
    msg_dict = data['messages'][find_message(message_id)]
    reacts_list = msg_dict['reacts']
    channel_id = msg_dict['channel_id']
    '''Check if the user is in this particular channel_id of the message '''
    user_channel_list = user_dict['user_channel']
    error_flag = 1
    for i in user_channel_list:
        if i['channel_id'] == channel_id:
            error_flag = 0

    if error_flag == 1:
        raise Value_Error(
            "Not a valid message within a channel that the authorised user has joined"
        )

    if react_id != 1:
        raise Value_Error("Not a valid react_id")

    reacts_list = msg_dict['reacts']

    if user_id not in reacts_list[int(react_id) - 1]['u_ids']:
        raise Value_Error(
            "Message with ID message_id does not contain an active React with ID react_id"
        )

    reacts_list[int(react_id) - 1]['u_ids'].remove(user_id)
    save(data)
    return {}
Пример #4
0
def standup_send(token, channel_id, message):
    '''Sending a message to get buffered in the standup queue,
    assuming a standup is currently active'''
    data = getData()
    user_id = getUserFromToken(token)
    user = data['users'][user_id]
    right_channel_index = find_channel(channel_id)

    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")

    right_channel = data['channels_list'][right_channel_index]

    if len(message) > 1000:
        raise Value_Error("Message is more than 1000 characters")

    if right_channel['standup']['finish_time'] < dt.utcnow():
        raise Value_Error(
            'An active standup is not currently running in this channel')

    if not inChannel(token, channel_id):
        raise AccessError(
            'The authorised user is not a member of the channel that the message is within'
        )

    message_id = right_channel['standup']['message_id']
    msg_id = find_message(message_id)
    old_message = data['messages'][msg_id]['message']
    old_message += str(user['handle']) + ': ' + message + ' '
    edit(token, message_id, old_message)

    # update data after edit
    data = getData()
    save(data)
    return {}
Пример #5
0
def search(token, query_str):
    '''Given a query string, return a collection of
    message_list that match the query'''
    if query_str == '':
        return {'messages': []}

    data = getData()
    user_id = getUserFromToken(token)
    user = data['users'][user_id]

    search_list = []
    for user_channel in user['user_channel']:
        for message_info in data['messages']:
            if int(message_info['channel_id']) == int(
                    user_channel['channel_id']) and int(
                        message_info['u_id']) == user_id:
                if str(query_str) in str(message_info['message']):
                    message_info['time_created'] = message_info[
                        'time_created'].replace(
                            tzinfo=timezone.utc).timestamp()
                    for reaction in message_info['reacts']:
                        reaction['is_this_user_reacted'] = (
                            user_id in reaction['u_ids'])
                    search_list.append(message_info)
    return {'messages': search_list}
Пример #6
0
def unpin(token, message_id):
    ''' Given a message within a channel, remove it's mark as unpinned '''
    data = getData()
    user_id = getUserFromToken(token)
    user_dict = data['users'][user_id]
    msg_id = find_message(message_id)

    if msg_id is None:
        raise Value_Error("Message_Id is not a valid message")

    msg_dict = data['messages'][msg_id]

    if user_dict['permission'] is 3:
        raise Value_Error(
            "Message with message_id was not sent by the authorised user making this request and The authorised user is not an admin or owner of this channel or the slackr"
        )

    if not inChannel(token, msg_dict['channel_id']):
        raise AccessError("you need to be in the channel to pin the message")

    if msg_dict['is_pinned'] is False:
        raise Value_Error(
            f"Message with ID message_id: {message_id} already unpinned")

    msg_dict['is_pinned'] = False
    save(data)
    return {}
Пример #7
0
def create(token, name, is_public):
    ''' Creates a new channel with that name that is either
    a public or private channel '''
    data = getData()
    user_id = getUserFromToken(token)

    if user_id not in range(len(data['users'])):
        raise Value_Error("the token doesn't exist")

    if len(name) > 20:
        raise Value_Error("the name is longer than 20 character")

    user = data['users'][user_id]
    user_info = user_detail(user_id, user['name_first'], user['name_last'], user['profile_img_url'])

    ch_id = len(data['channels_list'])
    data['channels_list'].append({'channel': {'channel_id': ch_id, 'name': name},
                                  'owners': [user_info],
                                  'members': [user_info],
                                  'is_public': is_public,
                                  'standup': {'finish_time': dt.utcnow(), 'message_id': None}})
    user['user_channel'].append({'channel_id': ch_id, 'name': name})

    save(data)
    return {'channel_id': ch_id}
Пример #8
0
def sendlater(token, channel_id, message, time_sent):
    ''' Send a message from authorised_user to the channel specified
    by channel_id automatically at a specified time in the future '''
    data = getData()
    user_id = getUserFromToken(token)
    user = data['users'][user_id]
    right_channel_index = find_channel(channel_id)

    if type(time_sent) != dt:
        time_sent = dt.utcfromtimestamp(int(time_sent))

    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")

    message_id = get_new_message_id()

    if len(message) > 1000:
        raise Value_Error("Message is more than 1000 characters")

    if not inChannel(token, channel_id):
        raise AccessError(
            "the authorised user has not joined the channel they are trying to post to"
        )

    now = dt.utcnow()
    if now > time_sent:
        raise Value_Error("Time sent is a time in the past")

    data = add_message(channel_id, message_id, user['u_id'], message,
                       time_sent)

    save(data)
    return {'message_id': message_id}
Пример #9
0
def edit(token, message_id, message):
    ''' Given a message, update it's text with new text '''
    data = getData()
    user_dict = data['users'][getUserFromToken(token)]
    msg_dict = data['messages'][find_message(message_id)]

    if msg_dict['u_id'] != user_dict['u_id'] and user_dict[
            'permission'] != 1 and user_dict['permission'] != 2:
        raise AccessError("Unauthorised user making edit request")

    msg_dict['message'] = message
    save(data)
    return {}
Пример #10
0
def logout(token):
    ''' Given an active token, invalidates the taken to log the user out.
    If a valid token is given, and the user is successfully logged out,
    it returns true, otherwise false. '''
    data = getData()
    user_id = getUserFromToken(token)

    if user_id in range(len(
            data['users'])) and data['users'][user_id]['loggedIn'] == True:
        data['users'][user_id]['loggedIn'] = False
        save(data)
        return {'is_success': True}
    else:
        return {'is_success': False}
Пример #11
0
def sethandle(token, handle):
    ''' Update the authorised user's handle (i.e. display name) '''
    data = getData()
    user_id = getUserFromToken(token)

    for user in data['users']:
        if user['handle'] == handle:
            raise Value_Error("handle is already used by another user")

    if len(handle) < 3 or len(handle) > 20:
        raise Value_Error("handle_str must be between 3 and 20 characters")

    data['users'][user_id]['handle'] = handle

    save(data)
    return {}
Пример #12
0
def all_user(token):
    ''' return all user '''
    data = getData()
    user_id = getUserFromToken(token)

    if user_id not in range(len(data['users'])):
        return

    all_list = []
    for user in data['users']:
        user_detail = user_info(user['u_id'], user['email'],
                                user['name_first'], user['name_last'],
                                user['handle'], user['profile_img_url'])
        all_list.append(user_detail)

    return {'users': all_list}
Пример #13
0
def setname(token, name_first, name_last):
    '''Update the authorised user's first and last name'''
    data = getData()
    user_id = getUserFromToken(token)

    if len(name_first) not in range(1, 51):
        raise Value_Error(
            "name_first is not between 1 and 50 characters in length")
    if len(name_last) not in range(1, 51):
        raise Value_Error(
            "name_last is not between 1 and 50 characters in length")

    data['users'][user_id]['name_first'] = name_first
    data['users'][user_id]['name_last'] = name_last

    save(data)
    return {}
Пример #14
0
def setemail(token, email):
    ''' Update the authorised user's email address '''
    data = getData()
    user_id = getUserFromToken(token)

    for user in data['users']:
        if user['email'] == email:
            raise Value_Error(
                "Email address is already being used by another user")

    if (re.search(CHECK_EMAIL, email)):
        data['users'][user_id]['email'] = email
    else:
        raise Value_Error("Email entered is not a valid email")

    save(data)
    return {}
Пример #15
0
def permission(token, u_id, permission_id):
    ''' Given a User by their user ID, set their permissions to
    new permissions described by permission_id '''
    data = getData()
    if permission_id not in range(0, 4):
        raise Value_Error("permission_id does not refer to a value permission")

    if u_id not in range(len(data['users'])):
        raise Value_Error(f"u_id: {u_id} does not refer to a valid user.")

    user_id = getUserFromToken(token)
    admin_or_owner = data['users'][user_id]
    if admin_or_owner['permission'] == 3:
        raise Value_Error("The authorised user is not an admin or owner")

    data['users'][int(u_id)]['permission'] = permission_id
    save(data)
    return {}
Пример #16
0
def send(token, channel_id, message):
    ''' Send a message from authorised_user to the
    channel specified by channel_id '''
    data = getData()
    user_id = getUserFromToken(token)
    user = data['users'][user_id]
    message_id = get_new_message_id()
    if len(message) > 1000:
        raise Value_Error("Message is more than 1000 characters")

    if not inChannel(token, channel_id):
        raise AccessError(
            "the authorised user has not joined the channel they are trying to post to"
        )

    data = add_message(channel_id, message_id, user['u_id'], message,
                       dt.utcnow())

    save(data)
    return {'message_id': message_id}
Пример #17
0
def leave(token, channel_id):
    ''' Given a channel ID, the user removed as a member of this channel '''
    data = getData()
    user_id = getUserFromToken(token)
    right_channel_index = find_channel(channel_id)
    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")
    right_channel = data['channels_list'][right_channel_index]

    # remove channel from user's channel list
    if right_channel['channel'] in data['users'][user_id]['user_channel']:
        data['users'][user_id]['user_channel'].remove(right_channel['channel'])

    # remove user from channel's member list
    for member in right_channel['members']:
        if data['users'][user_id]['u_id'] == member['u_id'] and member in right_channel['members']:
            right_channel['members'].remove(member)
            break

    save(data)
    return {}
Пример #18
0
def remove(token, message_id):
    ''' Given a message_id for a message, this
    message is removed from the channel '''
    data = getData()
    user_id = getUserFromToken(token)
    user = data['users'][user_id]
    message_index = find_message(message_id)

    if message_index is None:
        raise Value_Error(f"Message (based on ID) no longer exists")
    message = data['messages'][message_index]

    if user['u_id'] != message['u_id'] and int(
            user['permission']) not in range(1, 3):
        raise Value_Error(
            "Message with message_id was not sent by the authorised user making this request and The authorised user is not an admin or owner of this channel or the slackr"
        )

    data['messages'].remove(message)
    save(data)
    return {}
Пример #19
0
def removeowner(token, channel_id, u_id):
    ''' Remove user with user id u_id an owner of this channel '''
    data = getData()
    maker_id = getUserFromToken(token)
    maker = data['users'][maker_id]
    unlucky = data['users'][int(u_id)]
    right_channel_index = find_channel(channel_id)
    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")
    right_channel = data['channels_list'][right_channel_index]

    unlucky_detail = user_detail(unlucky['u_id'], unlucky['name_first'], unlucky['name_last'], unlucky['profile_img_url'])
    maker_detail = user_detail(maker['u_id'], maker['name_first'], maker['name_last'], maker['profile_img_url'])

    if (maker_detail not in right_channel['owners']) and (maker['permission'] != 1):
        raise AccessError("you don't have the right to access")

    if unlucky_detail in right_channel['owners']:
        if len(right_channel['owners']) > 1:
            right_channel['owners'].remove(unlucky_detail)

    save(data)
    return {}
Пример #20
0
def addowner(token, channel_id, u_id):
    ''' Make user with user id u_id an owner of this channel '''
    data = getData()
    maker_id = getUserFromToken(token)
    maker = data['users'][maker_id]
    joiner = data['users'][int(u_id)]
    right_channel_index = find_channel(channel_id)
    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")
    right_channel = data['channels_list'][right_channel_index]

    joiner_detail = user_detail(joiner['u_id'], joiner['name_first'], joiner['name_last'], joiner['profile_img_url'])
    maker_detail = user_detail(maker['u_id'], maker['name_first'], maker['name_last'], maker['profile_img_url'])

    if (maker_detail not in right_channel['owners']) and (maker['permission'] != 1):
        raise AccessError("you don't have the right to access")

    if joiner_detail in right_channel['owners']:
        raise Value_Error("the user id is already an owner")

    right_channel['owners'].append(joiner_detail)
    save(data)
    return {}
Пример #21
0
def messages(token, channel_id, start):
    '''Given a Channel with ID channel_id that the authorised user is part of,
    return up to 50 messages between index "start" and "start + 50".
    Message with index 0 is the most recent message in the channel.
    This function returns a new index "end" which is the value of "start + 50",
    or, if this function has returned the least recent messages in the channel,
    returns -1 in "end" to indicate there are no more messages to load after this return.'''
    data = getData()
    u_id = getUserFromToken(token)
    right_channel_index = find_channel(channel_id)
    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")
    right_channel = data['channels_list'][right_channel_index]
    now = dt.utcnow()
    message_list = []
    total_message = 0
    for message in data['messages']:
        if int(message['channel_id']) == int(channel_id) and now >= message['time_created']:
            tmp = message
            tmp['time_created'] = message['time_created'].replace(tzinfo=timezone.utc).timestamp()
            for reaction in tmp['reacts']:
                reaction['is_this_user_reacted'] = (u_id in reaction['u_ids'])
            message_list.append(tmp)
            total_message += 1
        if message['message'] == '':
            data['messages'].remove(message)

    if int(start) != 0 and int(start) >= total_message:
        raise Value_Error("start is greater than or equal to the total number of messages in the channel")

    if not inChannel(token, channel_id):
        raise AccessError("Authorised user is not a member of channel with channel_id")

    end = int(start) + 50
    if end > total_message:
        end = -1
    return {'messages': message_list[int(start):int(start)+50], 'start': int(start), 'end': end}
Пример #22
0
def join(token, channel_id):
    ''' Given a channel_id of a channel that the authorised
    user can join, adds them to that channel '''
    data = getData()
    joiner_id = getUserFromToken(token)
    joiner = data['users'][joiner_id]
    right_channel_index = find_channel(channel_id)
    if right_channel_index is None:
        raise Value_Error(f"Channel ID: {channel_id} is not a valid channel")
    right_channel = data['channels_list'][right_channel_index]

    if not right_channel['is_public']:
        raise AccessError("a private channel can only be joined by invitation")

    if right_channel['channel'] not in joiner['user_channel']:
        joiner['user_channel'].append(right_channel['channel'])

    joiner_info = user_detail(joiner['u_id'], joiner['name_first'], joiner['name_last'], joiner['profile_img_url'])

    if joiner_info not in right_channel['members']:
        right_channel['members'].append(joiner_info)

    save(data)
    return {}
Пример #23
0
def user_channel_list(token):
    ''' Provide a list of all channels (and their associated
    details) that the authorised user is part of '''
    data = getData()
    user_id = getUserFromToken(token)
    return {'channels': data['users'][user_id]['user_channel']}