Beispiel #1
0
def get_room_users(room_id):
    """
    Get room's all online users informations function
        GET:
            no data
        Response json format:
            {'users':
                [{'user_id': user_id,
                  'username': username}, ...]
            }
        if room is not exist, return 400
    """
    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    user_list = room_manager.get_room_online_users(room_id)
    if user_list is None:
        return jsonify(users=[])
    data = []
    for uid in user_list:
        user = User.query.get(uid)
        data.append(
            {'user_id': user.id,
             'username': user.username}
        )
    return jsonify(users=data)
Beispiel #2
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 #3
0
def get_room_user_num(room_id):
    """
    Get room online user number function
        GET:
            no data
        Response json format:
            {'user_num': user_num}
        if the room is not exist, return 400
    """
    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    user_num = room_manager.get_room_online_user_num(room_id)
    return jsonify(user_num=user_num)
Beispiel #4
0
def get_room_info(room_id):
    """
    Get room information
        GET:
            no data
        Response json format:
            {'room_id': room_id, 'room_name': room_name, 'room_launcher_id': launcher_id,
            'room_launcher_name': room_launcher_name}
        return 400 if room is not exist
    """
    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    launcher = User.query.get(room.launcher_id)
    return jsonify(room_id=room.id, room_name=room.title,
                  room_launcher_id=launcher.id, room_launcher_name=launcher.username)
Beispiel #5
0
def update_message(room_id):
    """
    Update message function, a long polling interface,
    the user must ensure 'Keep-Alive' in 'Connection' field of
    HTTP header
        POST:
            no data
        Response json format:
            {'messages':
                [{'msg_id': msg_id,
                'sender_id': sender_id,
                'sender_name': sender_name,
                'msg': msg,
                'send_time': YYYY/MM/DD,HH:MM:SS}, ...]
            }, the lastest 40 messages(if have enough messages)
        if the room is not exist, return 400
        if the 'Keep-Alive' is not in 'Connection' field, return 406
        if the user is not in this room, return 406
    """
    connection = request.headers.get('Connection', None)
    if connection is None or connection.lower() != 'keep-alive':
        return add_keep_alive()

    room = ChatRoom.query.get(room_id)
    if room is None:
        return room_not_exist_by_id(room_id)
    update_user = g.user
    if not room_manager.is_in_room(room_id, update_user.id):
        return not_in_room_by_id(room_id)
    room_manager.update_message(room_id)
    # construct the message json
    msgs = room.records.order_by(desc(ChatRoomRecord.send_time)).all()
    if len(msgs) > 40:
        msgs = msgs[:40]
    data = []
    for m in msgs:
        sender = User.query.get(m.sender_id)
        if sender is not None:
            data.append(
                {'msg_id': m.id,
                 'sender_id': m.sender_id,
                 'sender_name': sender.username,
                 'msg': m.record,
                 'send_time': m.send_time.strftime("%Y/%m/%d,%H:%M:%S")}
            )
    return jsonify(messages=data)
Beispiel #6
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)
Beispiel #7
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)