示例#1
0
def edit_message(message_id: str, edit: str, id_token: str) -> Dict:
    """ Edits the message iff the user wrote the message
    """

    if not message_exists(message_id):
        return {'message': 'Error: Message does not exist.'}, 401

    db = fb.database()
    message_info = db.child('messages').child(message_id).get().val()
    message = MessageModel.from_ordered_dict(message_info)

    #Get info from token and validate token
    try:
        author_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    author = db.child('uids').child(author_firebase_uid).get().val()

    if author != message.author:
        return {'message': 'Error: User can not edit message.'}, 401

    #Editting the message
    message.message = edit
    message.is_editted = True

    result = {message_id: message.get_raw_info()}
    emitChatUpdate(message.chat_id, 'MESSAGE_UPDATE', result)

    return db.child('messages').update(result), 200
示例#2
0
def leave_chat(chat_id: str, id_token: str) -> Dict:
    """ Leave a chat
    """

    if not chat_exists(chat_id):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Check if receiver exists
    if not user_exists(receiver):
        return get_user_not_found_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    #Check if user is in the chat
    db = fb.database()
    if not user_has_access(chat_id, token_user):
        return get_no_access_to_chat_error()

    db = fb.database()
    db.child('users').child(token_user).child('chats').child(chat_id).remove()
    db = fb.database()
    db.child('chats').child(chat_id).child('members').child(
        token_user).remove()
示例#3
0
def create_chat(name: str, id_token: str) -> Dict:
    """ Creates a chat using a chat name and the initial_member who made it. 
    Requires an idToken for verification
    """

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    initial_member = db.child('uids').child(
        token_user_firebase_uid).get().val()

    database, status = get_user_database(initial_member)
    if 400 <= status < 500:
        return database, status

    chat_uuid = uuid.uuid4()
    chat_uuid = str(chat_uuid.int)

    chat = ChatModel(chat_uuid, name, [initial_member], {})

    database.child('chats').update({chat_uuid: True})
    db.child("chats").update(chat.get_raw_info())

    return db.child("chats").update(chat.get_raw_info()), 201
示例#4
0
def decline_chat_request(chat_id: str, id_token: str) -> Dict:
    """ Decline a chat request
    """

    if not chat_exists(chat_id):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    #Check if user has been invited to the chat
    if db.child('users').child(token_user).child('chat_requests').child(
            chat_id).get().val() is None:
        return {'message': 'Error: User has not been invited to chat'}, 401

    #Add user to chat
    db.child('users').child(token_user).child('chat_requests').child(
        chat_id).remove()

    return {'message': 'User has declined'}, 200
示例#5
0
def send_chat_request(chat_id: str, receiver: str, id_token: str) -> Dict:
    """ Sends the receiver a chat request iff the sender (id_token) is a part of the chat
    """

    if not chat_exists(chat_id):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Check if receiver exists
    if not user_exists(receiver):
        return get_user_not_found_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    #Check if user is in the chat
    db = fb.database()
    if not user_has_access(chat_id, token_user):
        return get_no_access_to_chat_error()

    #Add chat request to user
    db.child('users').child(receiver).child('chat_requests').update(
        {chat_id: True})

    #Notifying Websockets
    emitUserUpdate(receiver, "Chat_request_sent", chat_id)

    return {'message': 'Invite sent'}, 201
示例#6
0
def get_simple_chat(chat_uuid: str, id_token: str) -> Dict:
    """ Gets the simplified information for chats
    """

    if not chat_exists(chat_uuid):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    simple_info = {}

    #Format chat info
    db = fb.database()
    chat_name = db.child('chats').child(chat_uuid).child(
        'chat_name').get().val()

    simple_info['chat_name'] = chat_name

    return simple_info
示例#7
0
def delete_chat(chat_id: str, id_token: str) -> Dict:
    """ Deletes the chat if user is a member of the chat
    """

    if chat_id == get_default_chat_uid():
        return {
            "message": "You can not delete the default chat",
            "error": "CANT_DELETE_CHAT"
        }, 401

    if not chat_exists(chat_id):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    #Check if user is in the chat
    db = fb.database()
    if not user_has_access(chat_id, token_user):
        return get_no_access_to_chat_error()

    #Get chat
    chat_info = db.child('chats').child(chat_id).get().val()
    chat = ChatModel.from_ordered_dict(chat_id, chat_info)

    #Delete chat
    db = fb.database()
    db.child('chats').child(chat_id).remove()

    #Delete members
    for member in chat.members:
        db.child('users').child(member).child('chats').child(chat_id).remove()

    #Delete messages
    if chat.messages is not None:
        for message in chat.messages.keys():
            db.child('messages').child(message).remove()

    #Notifying websockets
    emitChatUpdate(chat_id, 'CHAT_DELETED', {})

    return {'message': 'Chat deleted'}, 200
示例#8
0
def get_chat(chat_uuid: str, id_token: str, message_limit: int) -> Dict:
    """ Gets and formats a chat from the database
    """

    if not chat_exists(chat_uuid):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    #Check if user is in the chat
    if not user_has_access(chat_uuid, token_user):
        return get_no_access_to_chat_error()

    #Grabbing messages with limit
    messages = []
    messages_ordered_dict = db.child('chats').child(chat_uuid).child(
        'messages').order_by_child('time').limit_to_last(
            message_limit).get().val()
    if messages_ordered_dict is not None and len(messages_ordered_dict) > 0:
        for message_id in list(messages_ordered_dict.keys()):
            raw_message = db.child('messages').child(message_id).get().val()
            message = MessageModel.from_ordered_dict(query_info=raw_message)

            formatted_massage = message.get_raw_info()

            author_display_name = db.child('users').child(
                message.author).child("display_name").get().val()
            formatted_massage["author_display_name"] = author_display_name

            messages.append(formatted_massage)

    #Format chat info
    db = fb.database()
    chat_info = db.child('chats').child(chat_uuid).get().val()
    chat_info['messages'] = messages
    return chat_info, 200
示例#9
0
def send_message(chat_uuid: str, message: str, id_token: str) -> Dict:
    """ Creates a message for the chat with the corresponding chat_uuid.
    The time should be in the millisecond time format.
    Requires an idToken for verification.
    """

    if not chat_exists(chat_uuid):
        return get_chat_not_found_error()

    #Get info from token and validate token
    try:
        token_user_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    token_user = db.child('uids').child(token_user_firebase_uid).get().val()

    if not user_exists(token_user):
        return get_user_not_found_error()

    message_uuid = str(uuid.uuid4().int)

    message_model = MessageModel(message_uuid, message, token_user,
                                 int(round(time.time() * 1000)), chat_uuid)

    #Add message uuid to chat
    db = fb.database()
    db.child('chats').child(chat_uuid).child('messages').update(
        {message_uuid: {
            "time": message_model.time
        }})

    #Message
    db = fb.database()
    result = db.child('messages').update(
        {message_uuid: message_model.get_raw_info()})

    #Notifying Websockets
    emitChatUpdate(chat_uuid, "MESSAGE_SENT", result)

    return result, 201
示例#10
0
def delete_message(message_id: str, id_token: str) -> Dict:
    """ Deletes a message only if the person deleting the message is the author
    """

    if not message_exists(message_id):
        return get_user_not_found_error()

    db = fb.database()
    message_info = db.child('messages').child(message_id).get().val()
    message = MessageModel.from_ordered_dict(message_info)

    #Get info from token and validate token
    try:
        author_firebase_uid = get_user_from_token(id_token)['user_id']
    except:
        return get_invalid_token_error()

    #Get uid from token
    db = fb.database()
    author = db.child('uids').child(author_firebase_uid).get().val()

    if author != message.author:
        return {'message': 'Error: User can not delete message.'}, 401

    #Delete message from chat
    if chat_exists(message.chat_id):
        db.child('chats').child(
            message.chat_id).child('messages').child(message_id).remove()

    #Delete message from messages
    db.child('messages').child(message_id).remove()

    #Inform websockets
    emitChatUpdate(message.chat_id, 'MESSAGE_DELETE', {})

    return {'message': 'Message deleted'}, 200