예제 #1
0
def message_edit(token, message_id, message):
    check_tokenlogin(token)

    auser_dic = get_user_from_token(token)
    # only the user who sent this message can edit the message
    check_message_length(message)

    m_c_dic = get_message_from_mid(message_id)
    check_validmessage(m_c_dic)
    message_dic = m_c_dic['message_dic']
    channel_dic = m_c_dic['channel_dic']

    if not helper_check_user_admin(auser_dic):
        check_permission_of_message(message_dic, channel_dic,
                                    auser_dic['u_id'])

    # edit the message
    # if the message is empty, delete this message
    if message == '':
        channel_dic['messages'].remove(message_dic)
        return {}

    message_dic['message'] = message

    return {}
예제 #2
0
def message_pin(token, message_id):
    # 1. authorise user should be in the channel
    # 2. the message should be valid
    # 3. the message should be unpinned
    check_tokenlogin(token)

    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    m_c_dic = get_message_from_mid(message_id)
    check_validmessage(m_c_dic)
    message_dic = m_c_dic['message_dic']
    channel_dic = m_c_dic['channel_dic']

    #check if the authorise user is in the channel
    check_auser_not_in_channel(auid, channel_dic['all_members'])
    check_user_admin(auser_dic)

    # cannot pin the message which is pinned
    if message_dic['is_pinned']:
        raise ValueError("Message with ID message_id is already pinned.")

    message_dic['is_pinned'] = True

    return {}
예제 #3
0
def channels_create(token, name, is_public):
    check_tokenlogin(token)

    data = getdata()
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    # check if the channel name valid
    if len(name) > 20:
        raise ValueError('channel name not valid')

    channel_id = -1
    for channel in data['channels']:
        channel_id = channel['channel_id']

    # channel id will be automaticlly updated with every creation
    channel_id = channel_id + 1

    channel_dic = {
        'channel_id': channel_id,
        'name': name,
        'is_public': is_public,
        'standup_messages': '',
        'standup_is_active': False,
        'standup_time_finish': None,
        'owner_members': [auid],
        'all_members': [auid],
        # the one who created this channel will be the owner of the channel
        'messages': []
    }

    # channel will be added into the channels list after creation
    data['channels'].append(channel_dic)

    return {'channel_id': channel_id}
예제 #4
0
def user_profile_sethandle(token, handle_str):
    check_tokenlogin(token)

    auser_dic = get_user_from_token(token)
    check_validhandle(handle_str)
    # the handle should be valid

    if not handle_is_unique(handle_str):
    # this handle has been used
        raise ValueError('handle is already used by another user')

    auser_dic['handle'] = handle_str
    return {}
예제 #5
0
def user_profile_setname(token, name_first, name_last):
    check_tokenlogin(token)
    # check the validation of the token

    auser_dic = get_user_from_token(token)

    # check if the name is valid
    check_validname(name_first, name_last)

    auser_dic['name_first'] = name_first
    auser_dic['name_last'] = name_last

    return {}
예제 #6
0
def users_all(token):
    check_tokenlogin(token)

    users = getdata()['users']

    users_profile = []

    for user in users:
        # do not get the uid 0 user, cause the first user with uid -1 just a sample
        if user['u_id'] == 0:
            continue

        this_profile = user_profile(token, user['u_id'])
        users_profile.append(this_profile)

    return {'users' : users_profile}
예제 #7
0
def message_send(token, channel_id, message):
    # only the user who is in the channel can send message
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    check_validchannel(channel_dic)
    check_auser_not_in_channel(auid, channel_dic['all_members'])
    check_message_length(message)

    c_messages = channel_dic['messages']
    m_id = helper_send_message(c_messages, message, auid)
    # append the m_id into the message list after sending

    return {'message_id': m_id}
예제 #8
0
def channels_list(token):
    check_tokenlogin(token)

    data = getdata()
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    channels = []
    # get the list of channels, which the token user in
    for channel in data['channels']:
        if auid in channel['all_members']:
            channels.append({
                'channel_id': channel['channel_id'],
                'name': channel['name']
            })

    return {'channels': channels}
예제 #9
0
def channel_details(token, channel_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)

    check_validchannel(channel_dic)
    # check_auser_not_in_channel (auser_dic['u_id'], channel_dic['all_members'])

    name = channel_dic['name']
    owner_members = get_members_details(channel_dic['owner_members'])
    all_members = get_members_details(channel_dic['all_members'])

    return {
        'name': name,
        'owner_members': owner_members,
        'all_members': all_members
    }
예제 #10
0
def search(token, query_str):
    check_tokenlogin(token)
    channels = getdata()['channels']
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    collection = []
    for channel in channels:
        # firstly search fot all the channels the user is in
        if auid in channel['all_members']:
            for message in channel['messages']:
                #then search for all the messages this user sent in a channel
                if query_str in message['message']:
                    # lastly append the message to the collection
                    collection.append(message)

    return {'messages': collection}
예제 #11
0
def channel_invite(token, channel_id, u_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    check_validchannel(channel_dic)

    members = channel_dic['all_members']
    check_auser_not_in_channel(auser_dic['u_id'], members)

    uid_dic = get_user_from_uid(u_id)
    # add this user in the channel if this user is not a member of the channel
    check_validuid(uid_dic)
    if u_id in members:
        raise ValueError("the invited user already in channel")
    members.append(u_id)
    return {}
예제 #12
0
def user_profile(token, u_id):
    check_tokenlogin(token)

    user_dic = get_user_from_uid(u_id)

    check_validuid(user_dic)

    #initialize the user dic
    profile_dic = {
        'u_id' : user_dic['u_id'],
        'email' : user_dic['email'],
        'name_first' : user_dic['name_first'],
        'name_last' : user_dic['name_last'],
        'handle_str' : user_dic['handle'],
        'profile_img_url' : user_dic['profile_img_url']
    }

    return profile_dic
예제 #13
0
def channel_removeowner(token, channel_id, u_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    check_validchannel(channel_dic)

    check_auser_permission_of_channel(auser_dic, channel_dic)
    # only the owner can remove an owner, remove oneself is okay
    if u_id not in channel_dic['owner_members']:
        raise ValueError("The user with uid is not a owner of this channel")

    check_only_owner(channel_dic)

    # add uid to the owner of the channel
    channel_dic['owner_members'].remove(u_id)
    return {}
예제 #14
0
def message_remove(token, message_id):
    check_tokenlogin(token)

    auser_dic = get_user_from_token(token)

    m_c_dic = get_message_from_mid(message_id)
    check_validmessage(m_c_dic)
    message_dic = m_c_dic['message_dic']
    channel_dic = m_c_dic['channel_dic']

    if not helper_check_user_admin(auser_dic):
        check_permission_of_message(message_dic, channel_dic,
                                    auser_dic['u_id'])

    # remove the message
    channel_dic['messages'].remove(message_dic)

    return {}
예제 #15
0
def channel_messages(token, channel_id, start):
    check_tokenlogin(token)

    #set up the initial value
    max = 50
    returned_least_recent = -1

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    check_validchannel(channel_dic)
    check_auser_not_in_channel(auser_dic['u_id'], channel_dic['all_members'])

    # check if the start is valid
    total_messages = len(channel_dic['messages'])
    return_messages = []

    if total_messages == 0:
        return {'messages': return_messages, 'start': start, 'end': -1}

    if start > total_messages:
        raise ValueError(
            'Start is greater than the total number of messages in the channel'
        )

    counter = start
    end = start + max

    # get the messages between start and end
    # if end > total, just get the messages between start and last message
    while counter < end and counter < total_messages:
        return_messages.append(channel_dic['messages'][counter])
        counter = counter + 1

    # 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.
    if start == total_messages:
        end = returned_least_recent

    # else return the counter of last message in the list of returned messages
    else:
        end = counter

    return {'messages': return_messages, 'start': start, 'end': end}
예제 #16
0
def message_react(token, message_id, react_id):
    # 1. authorise user should be in the channel
    # 2. the message should be valid
    # 3. the message should be unreacted
    check_tokenlogin(token)

    #get the information of the user
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    m_c_dic = get_message_from_mid(message_id)
    check_validmessage(m_c_dic)
    # check if the message is valid then get the message
    message_dic = m_c_dic['message_dic']
    channel_dic = m_c_dic['channel_dic']

    check_uid_not_in_channel(auid, channel_dic)
    check_validreact(react_id)

    m_reacts = message_dic['reacts']

    this_react = helper_check_react_exists(m_reacts, react_id)
    user_reacted = helper_is_the_user_reacted(message_dic, auid)
    # if there is no this react in the message
    # add a react
    if not this_react:
        m_reacts.append({
            'react_id': react_id,
            'u_ids': [auid],
            'is_this_user_reacted': user_reacted
        })
        return {}

    # if there is this react in the message
    if auid in this_react['u_ids']:
        raise ValueError(
            "Message already contains an active React with react_id from user."
        )

    this_react['u_ids'].append(auid)
    if not user_reacted:
        this_react['is_this_user_reacted'] = True

    return {}
예제 #17
0
def channel_addowner(token, channel_id, u_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    check_validchannel(channel_dic)

    check_auser_permission_of_channel(auser_dic, channel_dic)

    check_uid_not_in_channel(u_id, channel_dic)

    if u_id in channel_dic['owner_members']:
        raise ValueError(
            "The user with uid is already a owner of this channel")

    # add uid to the owner of the channel
    channel_dic['owner_members'].append(u_id)
    return {}
예제 #18
0
def channels_listall(token):
    check_tokenlogin(token)

    data = getdata()
    channels = []

    # get list of all channels
    # do not include the first channel in channel list
    # because in data structure, the first on is just a model
    first = True
    for channel in data['channels']:
        if not first:
            channels.append({
                'channel_id': channel['channel_id'],
                'name': channel['name']
            })
        first = False

    return {'channels': channels}
예제 #19
0
def user_profile_setemail(token, email):
    check_tokenlogin(token)

    users = getdata()['users']

    auser_dic = get_user_from_token(token)

    check_validemail(email)
    # check if the email is valid

    for user in users:
        if user['email'] == email:
            raise ValueError("email has been used.")

    if auser_dic['email'] == email:
        raise ValueError('No new email entered')

    auser_dic['email'] = email

    return {}
예제 #20
0
def channel_join(token, channel_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    # check error
    check_validchannel(channel_dic)

    auid = auser_dic['u_id']
    check_auser_in_channel(auid, channel_dic['all_members'])

    # when channel_id refers to a channel that is private
    # when the authorised user is not an admin
    if not channel_dic['is_public'] and not helper_check_user_admin(auser_dic):
        raise AccessError("channel is private")

    # add the auser to channel
    channel_dic['all_members'].append(auid)

    return {}
예제 #21
0
def channel_leave(token, channel_id):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)

    # check error
    check_validchannel(channel_dic)

    auser_uid = auser_dic['u_id']
    channel_all_members = channel_dic['all_members']
    check_auser_not_in_channel(auser_uid, channel_all_members)

    # the auser leave the channel
    channel_all_members.remove(auser_uid)
    owners = channel_dic['owner_members']
    # if auser is a owner of channel, remove auser from owner list of this channel
    if auser_uid in owners:
        check_only_owner(channel_dic)
        owners.remove(auser_uid)

    return {}
예제 #22
0
def message_sendlater(token, channel_id, message, time_send):
    check_tokenlogin(token)

    channel_dic = get_channel_from_cid(channel_id)
    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    check_validchannel(channel_dic)
    check_auser_not_in_channel(auid, channel_dic['all_members'])
    check_message_length(message)

    second = (convert_date(time_send) - datetime.utcnow()).total_seconds()
    if second < 0:
        raise ValueError("Time sent is a time in the past.")

    timer = CustomTimer(second, helper_send_message,
                        (channel_dic['messages'], message, auid))
    # timer = threading.Timer(second, message_send(token, channel_id, message))
    timer.start()
    m_id = timer.join()
    print(m_id)
    return {'message_id': m_id}
예제 #23
0
def admin_userpermission_change(token, u_id, permission_id):
    check_tokenlogin(token)
    permission_ids = get_permission_ids()
    auser_dic = get_user_from_token(token)
    user_dic = get_user_from_uid(u_id)

    check_validuid(user_dic)

    # 3 types of permissions: 1. owner  2. admin  3. member
    # only 1 and 2 can change permission
    owner_id = permission_ids['Owner']
    admin_id = permission_ids['Admin']
    member_id = permission_ids['Member']

    if permission_id not in (owner_id, admin_id, member_id):
        raise ValueError("permission_id does not refer to a value permission")

    if not helper_check_user_admin(auser_dic):
        raise AccessError("the authorised user is not an admin or owner")

    user_dic['permission_id'] = permission_id

    return {}
예제 #24
0
def message_unreact(token, message_id, react_id):
    # 1. authorise user should be in the channel
    # 2. the message should be valid
    # 3. the message should be reacted
    check_tokenlogin(token)

    auser_dic = get_user_from_token(token)
    auid = auser_dic['u_id']

    m_c_dic = get_message_from_mid(message_id)
    check_validmessage(m_c_dic)

    message_dic = m_c_dic['message_dic']
    channel_dic = m_c_dic['channel_dic']

    # when the user not in this channel
    check_uid_not_in_channel(auid, channel_dic)
    check_validreact(react_id)

    m_reacts = message_dic['reacts']

    this_react = helper_check_react_exists(m_reacts, react_id)

    # when the user didn't react to this message
    if not this_react or auid not in this_react['u_ids']:
        raise ValueError(
            "Message with message_id does not contain an active React with react_id from user."
        )

    user_reacted = helper_is_the_user_reacted(message_dic, auid)

    this_react['u_ids'].remove(auid)
    if not user_reacted:
        this_react['is_this_user_reacted'] = False

    return {}
예제 #25
0
def auth_logout(token):
    # if the token is vaild
    # check if token login
    check_tokenlogin(token)
    return{'is_success' : True}