示例#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
示例#2
0
def get_chat(chat):
    current_chat = session.query(Chat).filter_by(id=chat.id).first()
    if not current_chat:
        current_chat = Chat(id=chat.id, chat_type=chat.type, title=chat.title)
        current_chat.create()
    return current_chat