def get_friends(): """ Get your friends list function GET: no data Response json format: {'friends': [{'user_id': user_id, 'username': username, 'ip_addr': ip_addr}, ....] } ip_addr is None when user is not online now return 400 if user is not exist """ user = g.user if user is None: pass friend_list = user.get_friends() data_list = [] for friend_id in friend_list: friend = User.query.get(friend_id) if friend is not None: ip_addr = online_users.get_online_user_ip(friend) data_list.append({"user_id": friend_id, "username": friend.username, "ip_addr": ip_addr or ""}) return jsonify(friends=data_list)
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)