Beispiel #1
0
def send_message(room_id):
    """
    Send message function
        POST json format:
            {"message": message}
        Response 200 if success
        if the room is not exist, return 400
        if the is not online now, return 406
        if the user is not in this room, return 406
    """
    sender = g.user
    print "sender is %s" % sender
    if not online_users.is_online(sender):
        return login_first()
    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    if not room_manager.is_in_room(room_id, sender.id):
        return not_in_room_by_id(room_id)
    info = get_json_post_data()
    msg = info.get('message', None)
    if msg is None:
        return Response(status=406)
    room_manager.send_message(sender.id, room_id, msg)
    return Response("successful send message to room %s" % room.title, 200)
Beispiel #2
0
def login():
    """
    Login function
        POST json format:
            {'username_or_mail': username_or_mail, 'password': password}
        Response json format:
            {'user_id': user_id}
        If password is wrong or the user is not exist, return error code 400
    """
    info = get_json_post_data()
    if info is None:
        return no_post_data
    name_or_mail = info.get("username_or_mail", None)
    pwd = info.get("password", None)
    if name_or_mail is None or pwd is None:
        return Response(status=406)
    user_list = User.query.filter(or_(User.username == name_or_mail, User.email == name_or_mail))
    user = user_list.first()
    ip = request.remote_addr
    if user is None or not user.check_password(pwd):
        return user_not_exist_by_name(name_or_mail)
    else:
        online_users.add_user(ip, user)
        talk_manager.add_user(user)
        # g.user = user  # store the logined user in g
        return jsonify(user_id=user.id)
Beispiel #3
0
def create_room():
    """
    Create Room function
        POST json format:
            {"room_name": room_name}
        Response json format:
            {"room_name": room_name, "room_id": room_id}
        If the room for name is already exist, return 406
    """
    creator = g.user
    info = get_json_post_data()
    if info is None:
        return no_post_data
    name = info.get('room_name', None)
    if name is None:
        return Response(status=406)
    room_list = ChatRoom.query.filter_by(title=name).all()
    if len(room_list) > 0:
        return Response("room %s was existed" % name, 406)
    # create the room
    room = ChatRoom(name, creator.id)
    db.session.add(room)
    db.session.commit()

    # and let creator join this room
    room_manager.join_room(room.id, creator.id)
    return jsonify(room_name=room.title, room_id=room.id)
Beispiel #4
0
def register():
    """
    Register function
        POST json format:
            {'username': username, 'password': password, 'email': email}
        Response json format:
            {'user_id': user_id}
        If the user was already existed, return error code 406
    """
    info = get_json_post_data()
    if info is None:
        return no_post_data
    name = info.get("username", None)
    pwd = info.get("password", None)
    mail = info.get("email", None)

    if name is None or pwd is None or mail is None:
        return Response(status=406)

    # determine the user if exist
    u = User.query.filter_by(username=name).all()
    if len(u) > 0:
        return Response("user %s has already existed" % name, status=406)
    else:
        new_user = User(username=name, email=mail, password=pwd)
        db.session.add(new_user)
        db.session.commit()
        return jsonify(user_id=new_user.id)