Example #1
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)
Example #2
0
def check_new_talk():
    """
    Check new talk function, check if someone want to
    talk to you, please ensure 'Connection:Keep-Alive' for this
    connection(a long polling interface)
        GET:
            no data
        Response:
            if someone want to talk to you, return
            {'senders': [{'user_id': user_id, 'username': username,
            'user_ip': user_ip}, ....]}
        If the user is not exist, return 400
        If 'Keep-Alive' is not in the 'Connection' field, return 406
        If the user is not online, return 406
    """
    connection = request.headers.get('Connection', None)
    if connection is None or connection.lower() != 'keep-alive':
        #return Response("You should put \'Keep-Alive\' in \'Connection\' field", 406)
        return add_keep_alive()
    user = g.user
    if user is None:
        pass
    if user.id not in online_users.get_online_users():
        return Response("User %s is not online now" % user.username, 406)
    initiator_list = talk_manager.tell_to_talk(user.id)
    if initiator_list is not None:
        return_json = []
        for initiator_id in initiator_list:
            initiator = User.query.get(initiator_id)
            initiator_ip = online_users.get_online_user_ip(initiator)
            return_json.append(
                {'user_id': initiator.id,
                 'username': initiator.username,
                 'user_ip': initiator_ip}
            )
        return jsonify(senders=return_json)