From 02bd91272d8e0b6b2f1909e5a3c07309de3ec921 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Mon, 30 Jan 2017 16:16:31 -0500 Subject: [PATCH] send credits --- controller/Session.class.php | 3 + .../action/AcquisitionActions.class.php | 89 ++++++++++++------- lib/tools/Lock.class.php | 70 +++++++++++++++ 3 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 lib/tools/Lock.class.php diff --git a/controller/Session.class.php b/controller/Session.class.php index 8d14fd2d..4291690f 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -6,6 +6,9 @@ class Session KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2', KEY_PREFINERY_USER_ID = 'prefinery_user_id', KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code', + KEY_DEVELOPER_CREDITS_ERROR = 'developer_credits_error', + KEY_DEVELOPER_CREDITS_SUCCESS = 'developer_credits_success', + KEY_DEVELOPER_CREDITS_WALLET_ADDRESS = 'developer_credits_wallet_address', KEY_LIST_SUB_ERROR = 'list_error', KEY_USER_CULTURE = 'user_culture'; diff --git a/controller/action/AcquisitionActions.class.php b/controller/action/AcquisitionActions.class.php index 17bda1e1..847d4d92 100644 --- a/controller/action/AcquisitionActions.class.php +++ b/controller/action/AcquisitionActions.class.php @@ -2,10 +2,7 @@ class AcquisitionActions extends Actions { - const DEVELOPER_REWARD = 250, - SESSION_KEY_DEVELOPER_CREDITS_ERROR = 'acquisition.developer-credits-error', - SESSION_KEY_DEVELOPER_CREDITS_SUCCESS = 'acquisition.developer-credits-success', - SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS = 'acquisition.developer-credits-wallet-address'; + const DEVELOPER_REWARD = 250; public static function executeThanks() { @@ -47,27 +44,25 @@ class AcquisitionActions extends Actions public static function executeDeveloperProgram() { - $vars = [ - 'defaultWalletAddress' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS), - 'error' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR), - 'success' => Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS) - ]; - Session::unsetKey(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS); - Session::unsetKey(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR); - return ['acquisition/developer-program', $vars]; + return ['acquisition/developer-program', [ + 'defaultWalletAddress' => Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS), + 'error' => Session::getFlash(Session::KEY_DEVELOPER_CREDITS_ERROR), + 'success' => Session::getFlash(Session::KEY_DEVELOPER_CREDITS_SUCCESS) + ]]; } public static function executeDeveloperProgramPost() { $walletAddress = trim(Request::getPostParam('wallet')); - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, $walletAddress); + Session::set(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, $walletAddress); + if (!$walletAddress) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Please provide a wallet address.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Please provide a wallet address.'); } elseif (!preg_match('/^b[1-9A-HJ-NP-Za-km-z]{33}$/', $walletAddress)) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid wallet address.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid wallet address.'); } else { @@ -90,15 +85,15 @@ class AcquisitionActions extends Actions public static function executeDeveloperProgramGithubCallback() { $code = Request::getParam('code'); - $walletAddress = Session::get(static::SESSION_KEY_DEVELOPER_CREDITS_WALLET_ADDRESS); + $walletAddress = Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS); if (!$walletAddress) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Your wallet address disappeared while authenticated with GitHub.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Your wallet address disappeared while authenticated with GitHub.'); } elseif (!$code) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.'); } else { @@ -112,11 +107,11 @@ class AcquisitionActions extends Actions ]); if (!$authResponseData || !isset($authResponseData['access_token'])) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.'); } elseif (isset($authResponseData['error_description'])) { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']); } else { @@ -126,21 +121,55 @@ class AcquisitionActions extends Actions 'json_response' => true ]); - if (date('Y-m', strtotime($userResponseData['created_at'])) > '2017-01') + if (date('Y-m-d', strtotime($userResponseData['created_at'])) > '2017-01-30') { - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_ERROR, 'This GitHub account is too recent.'); + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This GitHub account is too recent.'); } else { - // print_r($userResponseData); - /* - * TODO: send credits here, see success message below for relevant values - * (keep wallet address in success message) - */ + $lockName = 'github_dev_credits_write'; + $dataFile = ROOT_DIR . '/data/writeable/github_developer_credits'; - Session::set(static::SESSION_KEY_DEVELOPER_CREDITS_SUCCESS, - 'Send credits to GitHub user ' . $userResponseData['login'] . ' (' . $userResponseData['email'] . ') at wallet address ' . - $walletAddress); + $lock = Lock::getLock($lockName, true); // EXCLUSIVE LOCK. IF SENDING CREDITS IS SLOW, THIS COULD BLOCK USERS + + $existing = is_file($dataFile) ? json_decode(file_get_contents($dataFile), true) : []; + + if (isset($existing[$userResponseData['login']])) + { + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'You account already received credits.'); + } + else + { + list($code, $out, $err) = + Shell::exec('/usr/bin/lbrynet-cli send_amount_to_address amount=250 ' . escapeshellarg('address=' . $walletAddress)); + + if ($code != 0) + { + if (stripos($out, 'InsufficientFundsError') !== false) + { + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, + 'Our wallet is running low on funds. Please ping jeremy@lbry.io so he can refill it, then try again.'); + } + else + { + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, + 'Failed to send credits. This is an error on our side. Please email jeremy@lbry.io if it persists.'); + } + } + else + { + $existing[$userResponseData['login']] = [$userResponseData['email'], $walletAddress, date('Y-m-d H:i:s')]; + file_put_contents($dataFile, json_encode($existing)); + + Session::setFlash(Session::KEY_DEVELOPER_CREDITS_SUCCESS, + 'Send credits to GitHub user ' . $userResponseData['login'] . ' (' . $userResponseData['email'] . ') at wallet address ' . + $walletAddress); + + } + } + + Lock::freeLock($lock); + $lock = null; } } } diff --git a/lib/tools/Lock.class.php b/lib/tools/Lock.class.php new file mode 100644 index 00000000..4c5affbe --- /dev/null +++ b/lib/tools/Lock.class.php @@ -0,0 +1,70 @@ +