예제 #1
0
파일: view.py 프로젝트: ragnraok/Athena
def launch_talk(talk_to_user_id):
    """
    Launch a new talk function,
    for current logined user want to talk to user for _user_id
        POST:
            no data
        Response 200 if success
        If the user is not exist return error code 400
        If the user is not both online, return 406
    """
    user = g.user
    if user is None:
        pass
    talk_to_user = User.query.get(talk_to_user_id)
    if talk_to_user is None:
        return user_not_exist_by_id(talk_to_user_id)
    if user.id not in online_users.get_online_users():
        return Response("User %s is not online now" % user.username, 406)
    if talk_to_user_id not in online_users.get_online_users():
        return Response("User %s is not online now" % talk_to_user.username, 406)
    talk_manager.new_talk(user.id, talk_to_user_id)
    return Response("launch talk to %s" % talk_to_user.username, 200)
예제 #2
0
파일: view.py 프로젝트: ragnraok/Athena
def logout():
    """
    Logout function
        POST:
            no data
        Response 200 if success
        If user is not exist, return 400
        If user is not online, return 406
    """
    user = g.user
    if user.id not in online_users.get_online_users():
        return Response("user %s is not online now" % user.username, 406)
    # delete user in online list, talk_manager
    del g.user
    online_users.delete_user(user)
    talk_manager.delete_user(user.id)
    room_manager.exit_all_room(user.id)
    return Response("logout success", 200)
예제 #3
0
파일: view.py 프로젝트: ragnraok/Athena
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)