Example #1
0
def channel_removeowner(token, channel_id, u_id):
    getUserFromToken(token)
    u_id = int(u_id)
    channel_id = int(channel_id)
    DATA = load()
    channelDict = DATA['channelDict']
    if channel_id_check(channel_id) == False:
        raise ValueError("channel_id is invalid")
    if if_User_Owner(token, channel_id) == False: 
        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_owner']:
        raise AccessError("user with user id u_id is not an owner of the channel")
    for parts in channelDict:
        if parts['channel_id'] == channel_id:
            parts['channel_owner'].remove(u_id)
            parts['channel_member'].append(u_id)
    DATA['channelDict'] = channelDict
    save(DATA)
    return {}
Example #2
0
def users_all(token):
    getUserFromToken(token)
    data = load()
    userDict = data['userDict']
    lis = []
    for user in userDict:
        dic = {
            'u_id': user['u_id'],
            'email': (user['email']),
            'name_first': (user['first_name']),
            'name_last': (user['last_name']),
            'handle_str': (user['handle']),
            'profile_img_url': user['profile_img_url']
        }
        lis.append(dic)

    return {
        'users': list(lis)
    }
Example #3
0
def get_messages(u_id, message_ids):
    data = load()
    messDict = data['messDict']
    mess = []
    for mID in list(message_ids):
        for message in messDict:
            if message['message_id'] == int(mID):
                mom = {
                    'message_id': message['message_id'],
                    'u_id': message['u_id'],
                    'message': message['message'],
                    'time_created': message['time_created'],
                    'reacts': message['reacts'],
                    'is_pinned': message['is_pinned']
                }
                if is_user_reacted(u_id, message['message_id']):
                    mom['reacts'][0]['is_this_user_reacted'] = True
                mess.append(mom)
                break
    return list(mess)
Example #4
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
Example #5
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
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 {}
Example #7
0
def search(token, query_str):
    data = load()
    messDict = data['messDict']
    channel_list = channels_list(token)['channels']
    cha_ids = []
    for cha in channel_list:
        cha_ids.append(cha['channel_id'])
    lis = []
    for meg in messDict:
        if meg['channel_id'] in cha_ids:
            if meg['message'] == query_str:
                mess = {
                    'message_id': meg['message_id'],
                    'u_id': meg['u_id'],
                    'message': meg['message'],
                    'time_created': meg['time_created'],
                    'reacts': meg['reacts'],
                    'is_pinned': meg['is_pinned']
                }
                lis.append(mess)
    return {'messages': lis}
Example #8
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 {}
def is_owner(token, channel_id):
    channel_id = int(channel_id)
    DATA = load()
    channelDict = DATA['channelDict']
    userDict = DATA['userDict']
    # get the user id from token
    uid = getUserFromToken(token)
    uid = int(uid)

    # slacker owner or admin
    for parts in userDict:
        if (parts['u_id'] == uid and
            (parts['permission_id'] == 1 or parts['permission_id'] == 2)):
            return True
    # find the channel and serach the owner
    for elements in channelDict:
        if elements['channel_id'] == channel_id:
            # if (elements['channel_creater'] == id):
            if uid in elements['channel_owner']:
                return True
    return False
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 {}
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 {}
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 {}
Example #13
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')
Example #14
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
Example #15
0
def test_auth_register_handleCombine():
    restart()
    auth_register('*****@*****.**', '123456', 'hongzhou', 'xuan')
    DATA = load()
    userDict = DATA['userDict']
    assert userDict[0]['handle'] == 'hongzhouxuan'