def set_username_endpoint(): """ set users username """ user_id, auth_token = extract_headers(request) if user_id is None: raise InvalidUsage('invalid payload') print('setting username for userid %s' % user_id) # dont serve users with no phone number if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user %s from getting tasks: phone not verified' % user_id) return jsonify(status='denied'), status.HTTP_403_FORBIDDEN try: payload = request.get_json(silent=True) username = payload.get('username', None) if username is None: raise InvalidUsage('bad-request') except Exception as e: print(e) raise InvalidUsage('bad-request') if set_username(user_id, username): return jsonify(status='ok') else: return jsonify(status='failed')
def report_transaction_api(): """ store a given transaction in the database """ user_id, auth_token = extract_headers(request) if user_id is None: raise InvalidUsage('invalid payload') if not user_exists(user_id): raise InvalidUsage( 'report_transaction_api: user_id %s does not exist. aborting' % user_id) print('getting picture for user_id %s' % user_id) # don't serve users with no phone number if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user %s from reporting transactions' % user_id) return jsonify(status='denied'), status.HTTP_403_FORBIDDEN transaction = request.get_json(silent=True) transaction['user_id'] = user_id if report_transaction(transaction): return jsonify(status='ok') else: raise InvalidUsage('failed to add picture')
def get_pictures_summery_endpoint(): """ return a list of shown pictures and tips sum for each""" user_id, auth_token = extract_headers(request) if user_id is None: raise InvalidUsage('invalid payload') print('getting picture-summery for userid %s' % user_id) # dont serve users with no phone number if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user %s from getting tasks: phone not verified' % user_id) return jsonify(status='denied'), status.HTTP_403_FORBIDDEN return jsonify(summery=get_pictures_summery(user_id))
def get_block_user_endpoint(): """ return user's block list """ user_id, auth_token = extract_headers(request) if user_id is None: raise InvalidUsage('invalid payload') print('getting block for userid %s' % user_id) # dont serve users with no phone number if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user %s from getting tasks: phone not verified' % user_id) return jsonify(status='denied'), status.HTTP_403_FORBIDDEN return jsonify(get_user_blocked_users(user_id))
def get_next_picture(): """returns current picture for user""" user_id, auth_token = extract_headers(request) if user_id is None: raise InvalidUsage('invalid payload') print('getting picture for userid %s' % user_id) # dont serve users with no phone number if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user %s from getting tasks: phone not verified' % user_id) return jsonify(reason='denied'), status.HTTP_403_FORBIDDEN picture = get_picture_for_user(user_id) print('picture returned for user %s: %s' % (user_id, picture)) if picture.get('blocked', False): return jsonify(error="blocked_user") return jsonify(picture=picture)
def authorize(user_id): if config.AUTH_TOKEN_ENFORCED and not is_user_authenticated(user_id): print( 'user %s is not authenticated. rejecting results submission request' % user_id) increment_metric('rejected-on-auth') return 'auth-failed' if config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): print('blocking user (%s) results - didnt pass phone_verification' % user_id) return 'user_phone_not_verified' if is_userid_blacklisted(user_id): print('blocked user_id %s from booking goods - user_id blacklisted' % user_id) return 'denied' return 'authorized'
def onboard_user(): """creates a wallet for the user and deposits some xlms there""" # input sanity try: user_id, auth_token = extract_headers(request) public_address = request.get_json(silent=True).get( 'public_address', None) if None in (public_address, user_id): raise InvalidUsage('bad-request') except Exception as e: raise InvalidUsage('bad-request') # block users with an older version from onboarding. and send them a push message if should_block_user_by_client_version(user_id): print( 'blocking + deactivating user %s on onboarding with older version' % user_id) # send_please_upgrade_push_2([user_id]) # and also, deactivate the user deactivate_user(user_id) abort(403) elif config.PHONE_VERIFICATION_REQUIRED and not is_user_phone_verified( user_id): raise InvalidUsage('user isnt phone verified') onboarded = is_onboarded(user_id) if onboarded is True: raise InvalidUsage('user already has an account and has been awarded') elif onboarded is None: raise InvalidUsage('no such user exists') else: # create an account, provided none is already being created lock = redis_lock.Lock(app.redis, 'address:%s' % public_address) if lock.acquire(blocking=False): try: if not active_account_exists(public_address): print('creating account with address %s and amount %s' % (public_address, config.STELLAR_INITIAL_ACCOUNT_BALANCE)) tx_id = create_account( public_address, config.STELLAR_INITIAL_ACCOUNT_BALANCE) if not tx_id: raise InternalError('failed to create account at %s' % public_address) elif not award_user(user_id, public_address): raise InternalError( 'unable to award user with %d Kin' % get_initial_reward()) elif not award_user(user_id, public_address): raise InternalError('unable to award user with %d Kin' % get_initial_reward()) except Exception as e: print('exception trying to create account:%s' % e) raise InternalError('unable to create account') finally: lock.release() else: raise InvalidUsage( 'already creating account for user_id: %s and address: %s' % (user_id, public_address)) increment_metric('user_onboarded') return jsonify(status='ok')