def send_notification(): """ Send notifications to corresponding users according to different situations. :return: None """ if request.environ.get('wsgi.websocket'): ws = request.environ['wsgi.websocket'] while True: try: message = ws.receive() try: message = json.loads(message) if message['signal'] == 'NOTIFY_LOGIN': if database_helper.find_sign_in_user( message['data'][1]): if message['data'][0] in active_connections.keys(): try: active_connections[message['data'] [0]].send('BYE') except WebSocketError: print("[WEB_SOCKET_ERROR] DUE_TO_REFRESH") active_connections[message['data'][0]] = ws for active_connection in active_connections.keys(): active_connections[active_connection].send( 'NEW_LOGIN') elif message['signal'] == 'NOTIFY_LOGOUT': del active_connections[message['data'][0]] for active_connection in active_connections.keys(): active_connections[active_connection].send( 'NEW_LOGOUT') elif message['signal'] == 'NOTIFY_POST': if database_helper.find_sign_in_user( message['data'][1]): try: active_connections[message['data'][0]].send( 'NEW_POST') except KeyError: pass else: pass except TypeError: print('[SEND_NOTIFICATION] JSON DECODE') except WebSocketError: print('[WEB_SOCKET_ERROR] CONNECTION IS ALREADY CLOSED') break return
def get_user_data_by_email(): """ :return: status 200 and user information (except his/her password) if get data successfully, otherwise status 400. """ token = request.args.get('token') email = request.args.get('email') result = database_helper.find_sign_in_user(token) if result: result = database_helper.find_user(email) if result: return jsonify({"status": 200, "result": result}) else: return jsonify({"status": 400}) else: return jsonify({"status": 400})
def get_user_data_by_token(): """ :return: status 200 and user information if get user data successfully, otherwise status 400. """ token = request.args.get('token') result = database_helper.find_sign_in_user(token) if result: return jsonify({ "data": { "email": result[0], "firstname": result[1], "familyname": result[2], "gender": result[3], "city": result[4], "country": result[5] }, "status": 200 }) else: return jsonify({"status": 400})