Ejemplo n.º 1
0
def get_group_chat_message(group_chat_id, message_id):
    """Return a group chat message resource."""
    member = database_repository.get_group_chat_member(group_chat_id,
                                                       g.current_user.id)
    if not member:
        return {
            "error": "User is not a member of this group chat"
        }, HTTPStatus.UNAUTHORIZED
    chat_message = database_repository.get_chat_message(
        group_chat_id, message_id, MessageType.GROUP_CHAT)
    if not chat_message:
        return {"error": "Group chat message not found"}, HTTPStatus.NOT_FOUND
    return chat_message, HTTPStatus.OK
Ejemplo n.º 2
0
def react_to_chat_message(reaction_data, reaction_schema):
    """Add a new reaction to a group chat message."""
    chat_message = database_repository.get_chat_message(
        reaction_data["chat_id"], reaction_data["message_id"],
        reaction_data["message_type"])
    if not chat_message:
        emit("error", json.dumps({"error": "Chat message not found"}))
    elif not g.current_user.in_room(chat_message.chat_id):
        emit("error", json.dumps({"error": "User has not joined the chat"}))
    else:
        reaction = Reaction(g.current_user.id, reaction_data["reaction_type"])
        chat_message.add_reaction(reaction)
        database_repository.add_chat_message(chat_message)
        emit(
            "new_chat_message_reaction",
            reaction_schema.dumps(reaction),
            room=chat_message.chat_id,
        )
def get_private_chat_message(private_chat_id, message_id):
    """Return a private chat message resource."""
    private_chat = database_repository.get_private_chat(private_chat_id)
    if not private_chat:
        return {"error": "Private chat not found"}, HTTPStatus.NOT_FOUND
    if not private_chat.is_member(g.current_user.id):
        return (
            {
                "error": "User is not a member of this private chat"
            },
            HTTPStatus.UNAUTHORIZED,
        )
    chat_message = database_repository.get_chat_message(
        private_chat_id, message_id, MessageType.PRIVATE_CHAT)
    if not chat_message:
        return {
            "error": "Private chat message not found"
        }, HTTPStatus.NOT_FOUND
    return chat_message, HTTPStatus.OK
Ejemplo n.º 4
0
def delete_chat_message(message_data, message_schema):
    """Delete an existing chat message."""
    chat_message = database_repository.get_chat_message(
        message_data["_chat_id"], message_data["_id"],
        message_data["message_type"])
    if not chat_message:
        emit("error", json.dumps({"error": "Chat message not found"}))
    elif chat_message.user_id != g.current_user.id:
        emit("error",
             json.dumps({"error": "User is not the sender of this message"}))
    elif not g.current_user.in_room(chat_message.chat_id):
        emit("error", json.dumps({"error": "User has not joined the chat"}))
    else:
        database_repository.remove_chat_message(chat_message)
        emit("chat_message_deleted",
             json.dumps({
                 "message_id": chat_message.id,
                 "message_type": message_data["message_type"].name
             }),
             room=chat_message.chat_id)
Ejemplo n.º 5
0
def update_chat_message(message_data, message_schema):
    """Update the content of a chat message."""
    chat_message = database_repository.get_chat_message(
        message_data["_chat_id"], message_data["_id"],
        message_data["message_type"])
    if not chat_message:
        emit("error", json.dumps({"error": "Chat message not found"}))
    elif chat_message.user_id != g.current_user.id:
        emit("error",
             json.dumps({"error": "User is not the sender of this message"}))
    elif not g.current_user.in_room(chat_message.chat_id):
        emit("error", json.dumps({"error": "User has not joined the chat"}))
    else:
        chat_message.edit(message_data["_content"])
        database_repository.add_chat_message(chat_message)
        if chat_message.message_type == MessageType.PRIVATE_CHAT:
            schema = PrivateChatMessageSchema()
        else:
            schema = GroupChatMessageSchema()
        emit("chat_message_editted",
             schema.dumps(chat_message),
             room=chat_message.chat_id)
Ejemplo n.º 6
0
def unreact_to_chat_message(reaction_data, reaction_schema):
    """Remove a reaction from a chat message."""
    chat_message = database_repository.get_chat_message(
        reaction_data["chat_id"], reaction_data["message_id"],
        reaction_data["message_type"])
    if not chat_message:
        emit("error", json.dumps({"error": "Chat message not found"}))
    elif not g.current_user.in_room(chat_message.chat_id):
        emit("error", json.dumps({"error": "User has not joined the chat"}))
    else:
        reaction = chat_message.remove_reaction(g.current_user.id)
        if not reaction:
            emit(
                "error",
                json.dumps(
                    {"error": "User has not yet reacted to this message"}))
        else:
            database_repository.add_chat_message(chat_message)
            emit(
                "removed_chat_message_reaction",
                reaction_schema.dumps(reaction),
                room=chat_message.chat_id,
            )