Пример #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)
Пример #2
0
def join_room(room_id):
    """
    Join room function:
        POST:
            no data
        Response 200 if success
        If the room is not exist, return 400
        If the user is not online now, return 406
    """
    join_user = g.user
    if not online_users.is_online(join_user):
        return login_first()
    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    room_manager.join_room(room_id, join_user.id)
    # broadcast a message to all user
    room_manager.send_message(join_user.id, room_id, "%s join room!" % join_user.username)
    return Response("successful join room %s" % room.title, 200)
Пример #3
0
def exit_room(room_id):
    """
    Exit room function
        POST:
            no data
        Response 200 if success
        if the room is not exist, return 400
        if the user is not online now, return 406
        if the user is not in this room, return 406
    """
    exit_user = g.user
    if not online_users.is_online(exit_user):
        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, exit_user.id):
        return not_in_room_by_id(room_id)
    room_manager.exit_room(room_id, exit_user.id)

    # broadcast a message to all users in this room
    room_manager.send_message(exit_user.id, room_id, "%s exit room!" % exit_user.username)
    return Response("successful exit room %s" % room.title, 200)