Ejemplo n.º 1
0
def create_chat():
    """
    Creates a chat specified by a client

    Client must post json including:
        "chat_name": name for the chat
        "users": a list of users to add on creation of the chat
    """
    # TODO: Add authentication allowing any logged in user to use
    data = request.get_json()
    chat_name = data.get("chat_name")

    try:
        new_chat = Chat.create(name=chat_name)
    except Exception as e:
        return str(e), 400
    if data.get("users") is not None:
        try:
            for phone_number in data.get("users"):
                user = User.select().where(
                    User.phone_number == phone_number).get()
                Participation.create(chat=new_chat, user=user, rank="member")
        except Exception as e:
            return str(e), 400

    return jsonify(model_to_dict(new_chat)), 201
Ejemplo n.º 2
0
def add_user_to_chat(chat_id):
    """
    Adds a specified user to a chat identified by url "chat_id"

    Client must post json including:
        "is_admin": either 0 for member or 1 for admin
    Client must include in the json one of either:
        "user_id": the id of the user
        "phone_number": the phone number of the user
    """
    # TODO: Add authentication allowing any member of the chat to use
    data = request.get_json()
    user_id = data.get("user_id")
    phone_number = data.get("phone_number")
    is_admin = data.get("is_admin")

    if is_admin is None or (user_id is None and phone_number is None):
        return str("Bad Data"), 400
    try:
        Participation.create(chat=chat_id, user=user_id, is_admin=is_admin)
    except Exception as e:
        return str(e), 400
    return "", 204