Ejemplo n.º 1
0
def channel_addowner(token, channel_id, u_id):
    DATA = load()
    channelDict = DATA['channelDict']
    if channel_id_check(channel_id) == False:
        raise ValueError("channel_id is invalid")
    # already an owner
    getUserFromToken(token)
    u_id = int(u_id)
    channel_id = int(channel_id)
    for parts in channelDict:
        if parts['channel_id'] == channel_id and u_id in parts['channel_owner']:
            raise ValueError("user is already an owner in the channel")
    if not if_User_Owner(token, channel_id):
        raise AccessError(
            "the authorised user is not an owner of the slackr, or an owner of this channel"
        )
    if u_id not in channelDict[channel_id - 1]['channel_member']:
        raise AccessError("the user is not a member of this channel")
    for parts in channelDict:
        if parts['channel_id'] == channel_id:
            parts['channel_owner'].append(u_id)
            parts['channel_member'].remove(u_id)
    DATA['channelDict'] = channelDict
    save(DATA)
    return {}
Ejemplo n.º 2
0
def message_send(token, channel_id, message):
    u_id = getUserFromToken(token)
    DATA = load()
    messDict = DATA['messDict']
    channelDict = DATA['channelDict']
    if len(message) > 1000:
        raise ValueError("Message is more than 1000 characters")

    for cha in channelDict:
        if cha['channel_id'] == channel_id:
            if u_id not in cha['channel_owner'] and u_id not in cha[
                    'channel_member']:
                raise AccessError(
                    "The authorised user has not joined the channel they are trying to post to"
                )
    DATA['messID'] += 1
    mess = {
        'channel_id': int(channel_id),
        'message_id': int(DATA['messID']),
        'u_id': int(u_id),
        'message': message,
        'time_created': int(datetime.now().timestamp()),
        'reacts': [{
            'react_id': None,
            'u_ids': [],
            'is_this_user_reacted': False
        }],
        'is_pinned': False
    }

    mess_id = int(mess['message_id'])
    messDict.append(mess)
    DATA['messDict'] = messDict
    save(DATA)
    return int(mess_id)
Ejemplo n.º 3
0
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end):
    response = requests.get(img_url)
    if response.status_code != 200:
        raise ValueError('url corrupted')
    img = Image.open(urllib. request. urlopen(img_url))
    width, height = img.size
    if img == -1:
        raise ValueError("image does not exist")
    if x_end == x_start or y_end == y_start:
        raise ValueError("incorrect range")
    if int(x_end) > width or int(y_end) > height or int(x_start) > width or int(y_start) > height:
        raise ValueError('Out of bound')
    if int(x_end) < 0 or int(y_end) < 0 or int(x_start) < 0 or int(y_start) < 0:
        raise ValueError('Out of bound')
    if img.format != "JPEG" and img.format != "JPG":
        raise ValueError("Image uploaded is not a JPG")
    cropped = img.crop((int(x_start), int(y_start), int(x_end), int(y_end)))
    uid = getUserFromToken(token)
    cropped = cropped.save('frontend/prebundle/profile_image/' + str(uid) + '.jpg')
    DATA = load()
    userDict = DATA['userDict']
    # port = request.host
    # port = request.url_root
    for user in userDict:
        if int(uid) == int(user['u_id']):
            # user['profile_img_url'] = str(port) + "frontend/prebundle/static/" + str(id) + '.jpg'
            user['profile_img_url'] = 'frontend/prebundle/profile_image/' + str(uid) + '.jpg'

            print(user['profile_img_url'])
    DATA['userDict'] = userDict
    save(DATA)
    return {}
Ejemplo n.º 4
0
def message_pin(token, message_id):
    u_id = getUserFromToken(token)
    u_id = int(u_id)
    message_id = int(message_id)
    DATA = load()
    messDict = DATA['messDict']

    found = False
    for mess in messDict:
        if mess['message_id'] == message_id:
            if mess['is_pinned']:
                raise ValueError('already pinned')
            channelID = mess['channel_id']
            message = mess
            found = True
            break
    if not found:
        raise ValueError("Invalid message_id")
    if not if_User_Owner(token, channelID):
        raise ValueError('The authorised user is not an admin')

    if not is_in_channel(u_id, channelID):
        raise AccessError(
            'message_id is not a valid message within a channel that the authorised user has joined'
        )
    message['is_pinned'] = True
    DATA['messDict'] = messDict
    save(DATA)

    return {}
def admin_userpermission_change(token, u_id, permission_id):
    fl = 1
    u_id = int(u_id)
    permission_id = int(permission_id)
    data = load()
    userDict = data['userDict']
    for user in userDict:
        if u_id == user['u_id']:
            fl = 0
    if fl == 1:
        raise ValueError('Wrong user id')
    if permission_id not in range(1, 4):
        raise ValueError('Unmatch permission id')

    opid = getUserFromToken(token)
    for user in userDict:
        if user['u_id'] == opid:
            if user['permission_id'] == 2 and permission_id == 1:
                raise AccessError(
                    'Permission Denied: Trying to give owner by admin')
            if user['permission_id'] == 3:
                raise AccessError('Permission Denied : member can not do this')
    for sb in userDict:
        if sb['u_id'] == u_id:
            if sb['permission_id'] == 1:
                raise AccessError(
                    'Permission Denied: Trying to change owner permission')
            sb['permission_id'] = permission_id
    data['userDict'] = userDict
    save(data)
    pass
Ejemplo n.º 6
0
def message_react(token, message_id, react_id):
    u_id = getUserFromToken(token)
    u_id = int(u_id)
    message_id = int(message_id)
    react_id = int(react_id)
    DATA = load()
    messDict = DATA['messDict']

    if react_id != 1:
        raise ValueError('React_id is not a valid React ID')
    is_mess = False
    for mess in messDict:
        if mess['message_id'] == message_id:
            channelID = mess['channel_id']
            message = mess
            is_mess = True
            break
    if not is_mess:
        raise ValueError('invalid message_id')
    if not is_in_channel(u_id, channelID):
        raise ValueError(
            'message_id is not a valid message within a channel that the authorised user has joined'
        )

    if u_id in message['reacts'][0]['u_ids']:
        raise ValueError(
            f'Message with ID {message_id} already contains an active React with ID {react_id}'
        )
    else:
        message['reacts'][0]['react_id'] = react_id
        message['reacts'][0]['u_ids'].append(u_id)
        DATA['messDict'] = messDict
        save(DATA)

    return {}
Ejemplo n.º 7
0
def standup_send(token, channel_id, message):
    channel_id = int(channel_id)
    data = load()
    channelDict = data['channelDict']
    userDict = data['userDict']

    opid = getUserFromToken(token)
    for user in userDict:
        if int(user['u_id']) == int(opid):
            name = user['first_name']
            break
    if len(message) > 1000:
        raise ValueError("Message too long")
    for ch in channelDict:
        if int(channel_id) == int(ch['channel_id']):
            if opid not in ch['channel_member'] and opid not in ch[
                    'channel_owner']:
                raise AccessError('You are not a member of this channel')
            if ch['standUp'] != True:
                raise ValueError(
                    'An active standup is not currently running in this channel'
                )
            append = str(name) + ': ' + str(message)
            if not ch['standlist']:
                ch['standlist'] = append
                data['channelDict'] = channelDict
                save(data)
                return {}
            else:
                ch['standlist'] += '\r\n' + append
                data['channelDict'] = channelDict
                save(data)
                return {}
    raise ValueError('Channel ID is not a valid channel')
Ejemplo n.º 8
0
def channel_join(token, channel_id):
    DATA = load()
    channelDict = DATA['channelDict']
    if channel_id_check(channel_id) == False:
        raise ValueError("channel_id is invalid")
    id = getUserFromToken(token)
    id = int(id)
    # private channel
    if channel_property_check(channel_id) == False:
        if channel_admin_check(token) == False:
            raise AccessError(
                "authorised user is not an admin when channel is private")
        else:
            for parts in channelDict:
                if parts['channel_id'] == int(channel_id):
                    parts['channel_owner'].append(id)
    # public channel
    else:
        for parts in channelDict:
            if parts['channel_id'] == int(channel_id):
                if channel_admin_check(token) == False:
                    if parts['channel_member'] == []:
                        parts['channel_member'] = [id]
                    else:
                        parts['channel_member'].append(id)
                else:
                    parts['channel_owner'].append(id)
    DATA['channelDict'] = channelDict
    save(DATA)
Ejemplo n.º 9
0
def auth_login(email, password):
    DATA = load()
    userDict = DATA['userDict']
    # check email
    regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
    if not re.search(regex, email):
        raise ValueError("Invalid Email")

    # checking if this email has been registered
    found = False
    for user in userDict:
        if user['email'] == str(email):
            found = True
            break
    if not found:
        raise ValueError("Email entered doesn't belong to a user")

    # logging in
    for user in userDict:
        if user['email'] == email:
            if user['password'] == hashPassword(password):
                user['online'] = True
                DATA['userDict'] = userDict
                save(DATA)
                return {
                    'u_id': user['u_id'],
                    'token': generateToken(user['u_id'])
                }
            else:
                raise ValueError("Username or password incorrect")
Ejemplo n.º 10
0
def auth_passwordreset_request(email):
    DATA = load()
    userDict = DATA['userDict']
    for user in userDict:
        if user['email'] == email:
            user['reset_code'] = int(generateResetCode())
            DATA['userDict'] = userDict
            save(DATA)
            return str(user['reset_code'])
    raise ValueError('This email has not been registered')
Ejemplo n.º 11
0
def auth_logout(token):
    DATA = load()
    userDict = DATA['userDict']

    u_id = getUserFromToken(token)
    for user in userDict:
        if user['u_id'] == u_id and user['online']:
            user['online'] = False
            DATA['userDict'] = userDict
            save(DATA)
            return True
    return False
Ejemplo n.º 12
0
def auth_passwordreset_reset(reset_code, new_password):
    DATA = load()
    userDict = DATA['userDict']
    #incorrect password
    if len(new_password) < 5:
        raise ValueError("New password is not valid")
    if len(str(reset_code)) != 6:
        raise ValueError("reset_code is not valid")

    for user in userDict:
        if int(user['reset_code']) == int(reset_code):
            user['password'] = hashPassword(new_password)
            user['reset_code'] = 0
            DATA['userDict'] = userDict
            save(DATA)
            return {}
    raise ValueError("reset_code is not valid")
Ejemplo n.º 13
0
def send(channel_id, token):
    channel_id = int(channel_id)
    data = load()
    channelDict = data['channelDict']
    for channel in channelDict:
        if int(channel_id) == channel['channel_id']:
            message_send(token, channel_id, str(channel['standlist']))

    data = load()
    channelDict = data['channelDict']
    for channel in channelDict:
        if int(channel_id) == channel['channel_id']:
            channel['standUp'] = False
            channel['standtime'] = None
            channel['standlist'] == ''
            data['channelDict'] = channelDict
            save(data)
            return
Ejemplo n.º 14
0
def user_profile_sethandle(token, handle_str):
    opid = getUserFromToken(token)
    DATA = load()
    userDict = DATA['userDict']
    if len(handle_str) <= 3:
        raise ValueError('handle too short')
    if len(handle_str) >= 20:
        raise ValueError('handle too long')
    for user in userDict:
        if user['handle'] == handle_str:
            raise ValueError('handle already used')

    for user in userDict:
        if opid == user['u_id']:
            user['handle'] = handle_str
            DATA['userDict'] = userDict
            save(DATA)
            return
Ejemplo n.º 15
0
def channel_leave(token, channel_id):
    id = getUserFromToken(token)
    id = int(id)
    channel_id = int(channel_id)
    if channel_id_check(channel_id) == False:
        raise ValueError("channel_id is invalid")
    if not is_in_channel(id, channel_id):
        raise ValueError("user is not a member of channel")
    DATA = load()
    channelDict = DATA['channelDict']
    for cha in channelDict:
        if cha['channel_id'] == channel_id:
            if id in cha['channel_owner']:
                cha['channel_owner'].remove(id)
            else:
                cha['channel_member'].remove(id)
    DATA['channelDict'] = channelDict
    save(DATA)
Ejemplo n.º 16
0
def channels_create(token, name, is_public):
    DATA = load()
    channelDict = DATA['channelDict']
    if len(name) > 20:
        raise ValueError("Name is more than 20 characters long")
    user_id = getUserFromToken(token)
    if channelDict == []:
        dic = {
            'channel_id': 1,
            'name': name,
            'channel_creater': int(user_id),
            'channel_member': [],
            'channel_owner': [int(user_id)],
            'is_public': is_public,
            'standUp': False,
            'standlist': '',
            'standtime': None
        }
        channelDict.append(dic)
        DATA['channelDict'] = channelDict
        save(DATA)
        return dic['channel_id']
    else:
        # if same name
        for parts in channelDict:
            if parts['name'] == name:
                raise ValueError("this name was already used")
        count = len(channelDict) + 1
        dic = {
            'channel_id': int(count),
            'name': name,
            'channel_creater': int(user_id),
            'channel_member': [],
            'channel_owner': [int(user_id)],
            'is_public': is_public,
            'standUp': False,
            'standlist': '',
            'standtime': None
        }
        channelDict.append(dic)
        DATA['channelDict'] = channelDict
        save(DATA)
        return int(dic['channel_id'])
Ejemplo n.º 17
0
def message_remove(token, message_id):
    DATA = load()
    messDict = DATA['messDict']
    found = False

    for mess in messDict:
        if mess['message_id'] == int(message_id):
            channelID = int(mess['channel_id'])
            message = mess
            found = True
            break
    if not found:
        raise ValueError("Message (based on ID) no longer exists")
    if not is_owner(token, channelID) and not is_sender(token, message_id):
        raise AccessError('Unauthorised remove !')
    messDict.remove(message)
    DATA['messDict'] = messDict
    save(DATA)

    return {}
Ejemplo n.º 18
0
def user_profile_setmail(token, email):
    opid = getUserFromToken(token)

    DATA = load()
    userDict = DATA['userDict']
    regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
    if re.search(regex, email):
        pass
    else:
        raise ValueError("Invalid Email")

    for user in userDict:
        if user['email'] == email:
            raise ValueError("Email address is already used bt another user.")
    for user in userDict:
        if opid == user['u_id']:
            user['email'] = email
            DATA['userDict'] = userDict
            save(DATA)
            return
Ejemplo n.º 19
0
def user_profile_setname(token, name_first, name_last):
    opid = getUserFromToken(token)
    DATA = load()
    userDict = DATA['userDict']
    if len(name_first) > 50:
        raise ValueError('First name too long')
    if len(name_last) > 50:
        raise ValueError('Last name too long')
    if len(name_first) < 1:
        raise ValueError('First name too short')
    if len(name_last) < 1:
        raise ValueError('Last name too short')

    for user in userDict:
        if opid == user['u_id']:
            user['first_name'] = name_first
            user['last_name'] = name_last
            DATA['userDict'] = userDict
            save(DATA)
            return
Ejemplo n.º 20
0
def channel_invite(token, channel_id, u_id):
    if channel_id_check(int(channel_id)) == False:
        raise ValueError("channel_id is invalid")
    if u_id_check(u_id) == False:
        raise ValueError("u_id does not refer to a valid user")
    if auth_id_check(token, channel_id) == False:
        raise AccessError("Auth user is not a member of channel")
    if is_in_channel(u_id, channel_id):
        raise AccessError('The user you are inviting is already in this channel.')
    DATA = load()
    channelDict = DATA['channelDict']
    for parts in channelDict:
        if int(parts['channel_id']) == int(channel_id):
            # the user invite by owner is also a owner
            if if_slackr_owner(token) == True:
                parts['channel_owner'].append(int(u_id))
            else:
                parts['channel_member'].append(int(u_id))
    DATA['channelDict'] = channelDict
    save(DATA)
    return {}
Ejemplo n.º 21
0
def message_edit(token, message_id, message):
    u_id = getUserFromToken(token)
    u_id = int(u_id)
    message_id = int(message_id)

    DATA = load()
    messDict = DATA['messDict']
    for mess in messDict:
        if mess['message_id'] == message_id:
            channelID = mess['channel_id']
            mess_dict = mess
            break
    if not is_owner(token, channelID) and not is_sender(token, message_id):
        raise AccessError('Unauthorised edit !')
    if len(str(message)) == 0:
        DATA['messDict'].remove(mess_dict)
    else:
        mess_dict['message'] = message
    DATA['messDict'] = messDict
    save(DATA)

    return {}
Ejemplo n.º 22
0
def message_unpin(token, message_id):
    u_id = getUserFromToken(token)
    u_id = int(u_id)
    message_id = int(message_id)
    DATA = load()
    messDict = DATA['messDict']
    channelDict = DATA['channelDict']

    found = False
    for mess in messDict:
        if mess['message_id'] == message_id:
            if not mess['is_pinned']:
                raise ValueError('already unpinned')
            channelID = mess['channel_id']
            message = mess
            found = True
            break
    if not found:
        raise ValueError("Invalid message_id")
    if not if_User_Owner(token, channelID):
        raise ValueError('The authorised user is not an admin')

    for chan in channelDict:
        if chan['channel_id'] == channelID:
            if int(u_id) in chan['channel_member']:
                pass
            elif u_id in chan['channel_owner']:
                pass
            else:
                raise AccessError(
                    'message_id is not a valid message within a channel that the authorised user has joined'
                )
    message['is_pinned'] = False
    DATA['messDict'] = messDict
    DATA['channelDict'] = channelDict
    save(DATA)

    return {}
Ejemplo n.º 23
0
def message_unreact(token, message_id, react_id):
    u_id = getUserFromToken(token)
    u_id = int(u_id)
    message_id = int(message_id)
    react_id = int(react_id)
    DATA = load()
    messDict = DATA['messDict']
    if react_id != 1:
        raise ValueError('React_id is not a valid React ID')
    is_mess = False
    for mess in messDict:
        if mess['message_id'] == message_id:
            if mess['reacts'][0]['react_id'] == None:
                raise ValueError(
                    'Message with ID message_id does not contain an active React'
                )
            if mess['reacts'][0]['react_id'] != react_id:
                raise ValueError('React_id is not a valid React ID')
            channelID = mess['channel_id']
            message = mess
            is_mess = True
            break
    if not is_mess:
        raise ValueError('invalid message_id')
    if not is_in_channel(u_id, channelID):
        raise ValueError(
            'message_id is not a valid message within a channel that the authorised user has joined'
        )

    if u_id in message['reacts'][0]['u_ids']:
        message['reacts'][0]['u_ids'].remove(u_id)
    if len(message['reacts'][0]['u_ids']) == 0:
        message['reacts'][0]['react_id'] = None
    DATA['messDict'] = messDict
    save(DATA)

    return {}
Ejemplo n.º 24
0
def standup_start(token, channel_id, length):
    channel_id = int(channel_id)
    data = load()
    channelDict = data['channelDict']

    opid = getUserFromToken(token)
    for ch in channelDict:
        if int(channel_id) == ch['channel_id']:
            if opid not in ch['channel_member'] and opid not in ch[
                    'channel_owner']:
                raise AccessError('You are not a member of this channel')
            if ch['standUp'] == True:
                raise ValueError('this channel is already in standup')
            ch['standUp'] = True
            ch['standtime'] = showtime(length)
            time = ch['standtime']
            data['channelDict'] = channelDict
            save(data)

            timer = threading.Timer(int(length), send, [channel_id, token])
            timer.start()

            return {'time_finish': time}
    raise ValueError('incorrect channel id')
Ejemplo n.º 25
0
def auth_register(email, password, name_first, name_last):
    DATA = load()
    userDict = DATA['userDict']
    #check email
    regex = '^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$'
    if not re.search(regex, email):
        raise ValueError("Invalid Email")
    # Email already be used
    for user in userDict:
        if user['email'] == email:
            raise ValueError("Email address is already used bt another user.")
    # incorrect name
    if len(name_first) > 50 or len(name_first) < 1:
        raise ValueError("Firstname is needed between 1 and 50 characters.")
    if len(name_last) > 50 or len(name_last) < 1:
        raise ValueError("Lastname is needed between 1 and 50 characters.")
    # incorrect password
    if len(password) < 6:
        raise ValueError("Password should be at least 6 characters long")

    firstName = name_first.lower()
    lastName = name_last.lower()
    handle = firstName + lastName
    if len(handle) > 40:
        handle = handle[:20]

    newUser = {
        'first_name': firstName,
        'last_name': lastName,
        'email': email,
        'u_id': len(userDict) + 1,
        'permission_id': None,
        'handle': handle,
        'password': hashPassword(password),
        'online': True,
        'reset_code': 0,
        'profile_img_url': ''
    }

    # handling
    if handle_check(handle):
        handle = handle[3:len(handle)]
        for i in range(1, 999):
            if digit_check(i) == 1:
                new = "00" + str(i)
                if not handle_check(new + handle):
                    newUser['handle'] = new + handle
                    break

            elif digit_check(i) == 2:
                new = "0" + str(i)
                if not handle_check(new + handle):
                    newUser['handle'] = new + handle
                    break

            elif digit_check(i) == 3:
                new = str(i)
                if not handle_check(new + handle):
                    newUser['handle'] = new + handle
                    break

    if userDict == []:
        newUser['permission_id'] = 1
    else:
        newUser['permission_id'] = 3

    userDict.append(newUser)

    returned = {
        'u_id': newUser['u_id'],
        'token': generateToken(newUser['u_id'])
    }
    DATA['userDict'] = userDict
    save(DATA)
    return returned