From ab45fce4ff6d648a4b7f5d752bb578f57bb3eb12 Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Sun, 26 Feb 2017 14:59:44 -0500 Subject: [PATCH] it works? --- controller/Controller.class.php | 5 +- controller/Session.class.php | 3 +- controller/action/DeveloperActions.class.php | 95 +++++++++---------- lib/thirdparty/LBRY.class.php | 2 +- .../developer/_formCreditsPublish.php | 2 +- view/template/developer/_formNew.php | 20 ---- .../developer/_formNewDeveloperReward.php | 22 +++++ view/template/developer/_quickstartApi.php | 2 +- .../template/developer/_quickstartCredits.php | 6 +- .../template/developer/_quickstartInstall.php | 4 +- view/template/developer/developer-program.php | 31 ------ view/template/developer/quickstart.php | 1 + web/js/quickstart.js | 59 ++++++++++++ web/scss/_form.scss | 6 ++ 14 files changed, 144 insertions(+), 114 deletions(-) delete mode 100644 view/template/developer/_formNew.php create mode 100644 view/template/developer/_formNewDeveloperReward.php delete mode 100644 view/template/developer/developer-program.php create mode 100644 web/js/quickstart.js diff --git a/controller/Controller.class.php b/controller/Controller.class.php index d63841cd..cfff2bd7 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -89,10 +89,9 @@ class Controller $router->get(['/ios', 'get-ios'], 'DownloadActions::executeGet'); $router->get('/roadmap', 'ContentActions::executeRoadmap'); + $router->post('/quickstart/auth', 'DeveloperActions::executeQuickstartAuth'); $router->get('/quickstart/{step}?', 'DeveloperActions::executeQuickstart'); - $router->get('/developer-program', 'DeveloperActions::executeDeveloperProgram'); - $router->post('/developer-program/post', 'DeveloperActions::executeDeveloperProgramPost'); - $router->get('/developer-program/callback', 'DeveloperActions::executeDeveloperProgramGithubCallback'); + $router->get('/quickstart/github/callback', 'DeveloperActions::executeQuickstartGithubCallback'); $router->get(['/press-kit.zip', 'press-kit'], 'ContentActions::executePressKit'); diff --git a/controller/Session.class.php b/controller/Session.class.php index 148600b8..ee1e9e50 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -7,8 +7,9 @@ class Session 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_DEVELOPER_RETURN_URL_SUCCESS = 'developer_return_url_success', + KEY_GITHUB_ACCESS_TOKEN = 'github_access_token', KEY_LIST_SUB_ERROR = 'list_error', KEY_USER_CULTURE = 'user_culture'; diff --git a/controller/action/DeveloperActions.class.php b/controller/action/DeveloperActions.class.php index 92a2d59d..5a2dfea4 100644 --- a/controller/action/DeveloperActions.class.php +++ b/controller/action/DeveloperActions.class.php @@ -47,21 +47,14 @@ class DeveloperActions extends Actions ]; } - public static function executeDeveloperProgram() - { - return ['developer/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 prepareFormNewPartial(array $vars) + public static function prepareFormNewDeveloperRewardPartial(array $vars) { + $sendToGithub = !Session::get(Session::KEY_GITHUB_ACCESS_TOKEN); return $vars + [ '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) + 'sendToGithub' => $sendToGithub, + 'apiUrl' => LBRY::getApiUrl('/user/new_github') ]; } @@ -70,68 +63,66 @@ class DeveloperActions extends Actions return $vars + [ '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() + public static function executeQuickstartAuth() { - $walletAddress = trim(Request::getPostParam('wallet')); - Session::set(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, $walletAddress); + Session::set(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS, trim(Request::getPostParam('wallet_address'))); - if (!$walletAddress) + if (Request::getPostParam('returnUrl')) { - 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::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid wallet address.'); - } - else - { - if (!Config::get('github_developer_credits_client_id')) - { - throw new Exception('no github client id'); - } - - $githubParams = [ - 'client_id' => Config::get('github_developer_credits_client_id'), - 'redirect_uri' => Request::getHostAndProto() . '/developer-program/callback', - 'scope' => 'user:email public_repo', - 'allow_signup' => false - ]; - return Controller::redirect('https://github.com/login/oauth/authorize?' . http_build_query($githubParams)); + Session::set(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, Request::getPostParam('returnUrl')); } - return Controller::redirect(Request::getReferrer('/quickstart/credits')); + if (!Config::get('github_developer_credits_client_id')) + { + throw new Exception('no github client id'); + } + + $gitHubParams = [ + 'client_id' => Config::get('github_developer_credits_client_id'), + 'redirect_uri' => Request::getHostAndProto() . '/quickstart/github/callback', + 'scope' => 'user:email public_repo', + 'allow_signup' => false + ]; + + return Controller::redirect('https://github.com/login/oauth/authorize?' . http_build_query($gitHubParams)); } - public static function executeDeveloperProgramGithubCallback() + public static function executeQuickstartGithubCallback() { $code = Request::getParam('code'); - $walletAddress = Session::get(Session::KEY_DEVELOPER_CREDITS_WALLET_ADDRESS); - if (!$walletAddress) - { - Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Your wallet address disappeared while authenticated with GitHub.'); - } - elseif (!$code) + if (!$code) { Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'This does not appear to be a valid response from GitHub.'); } else { - $newUserUrl = LBRY::getApiUrl('/user/new_developer'); - $lbryApiResponseData = Curl::post($newUserUrl, [ - 'github_code' => $code + $authResponseData = Curl::post('https://github.com/login/oauth/access_token', [ + 'code' => $code, + 'client_id' => Config::get('github_developer_credits_client_id'), + 'client_secret' => Config::get('github_developer_credits_client_secret') ], [ -// 'json_response' => true + 'headers' => ['Accept: application/json'], + 'json_response' => true ]); - echo '
';
-      print_r($lbryApiResponseData);
-      die('omg');
+
+      if (!$authResponseData || !isset($authResponseData['access_token']))
+      {
+        Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'Request to GitHub failed.');
+      }
+      elseif (isset($authResponseData['error_description']))
+      {
+        Session::setFlash(Session::KEY_DEVELOPER_CREDITS_ERROR, 'GitHub replied: ' . $authResponseData['error_description']);
+      }
+      else
+      {
+        Session::set(Session::KEY_GITHUB_ACCESS_TOKEN, $authResponseData['access_token']);
+      }
     }
 
-    return Controller::redirect(Request::getReferrer('/quickstart/credits'));
+    return Controller::redirect(Session::get(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, '/quickstart/credits'));
   }
 }
diff --git a/lib/thirdparty/LBRY.class.php b/lib/thirdparty/LBRY.class.php
index 8eeed0b7..3caf74d3 100644
--- a/lib/thirdparty/LBRY.class.php
+++ b/lib/thirdparty/LBRY.class.php
@@ -14,6 +14,6 @@ class LBRY
       'cache' => 3600, //one hour
       'json_response' => true
     ]);
-    return $response['rate'];
+    return $response['data']['lbc_usd'] ?? 0;
   }
 }
\ No newline at end of file
diff --git a/view/template/developer/_formCreditsPublish.php b/view/template/developer/_formCreditsPublish.php
index b632b655..4ec14101 100644
--- a/view/template/developer/_formCreditsPublish.php
+++ b/view/template/developer/_formCreditsPublish.php
@@ -1,4 +1,4 @@
-
+

Receive Credits

diff --git a/view/template/developer/_formNew.php b/view/template/developer/_formNew.php deleted file mode 100644 index d7b72721..00000000 --- a/view/template/developer/_formNew.php +++ /dev/null @@ -1,20 +0,0 @@ - -

Receive Credits

- -
- -
- -
- -
- -
-
-
- -
-
We require a GitHub account to prevent abuse. This will record your email (no spam) and mark you as interested in the lbry repo. - No GitHub account? No problem! Join our Slack channel and post an introduction in #tech.
-
\ No newline at end of file diff --git a/view/template/developer/_formNewDeveloperReward.php b/view/template/developer/_formNewDeveloperReward.php new file mode 100644 index 00000000..4d4f1522 --- /dev/null +++ b/view/template/developer/_formNewDeveloperReward.php @@ -0,0 +1,22 @@ + + lbry.quickstartForm('#form-new-developer-reward', ""); + +
+

Receive Credits

+
+
+
+ +
+ +
+
+
+ + + +
+
We require a GitHub account to prevent abuse. This will record your email (no spam) and mark you as interested in the lbry repo. + No GitHub account? No problem! Join our Slack channel and post an introduction in #tech.
+
\ No newline at end of file diff --git a/view/template/developer/_quickstartApi.php b/view/template/developer/_quickstartApi.php index 385df56b..a690a4d6 100644 --- a/view/template/developer/_quickstartApi.php +++ b/view/template/developer/_quickstartApi.php @@ -3,7 +3,7 @@ When running, the LBRY daemon provides a JSON-RPC server running at https://localhost:5279/lbryapi.

- It can be accessed via cURL or any other utility capable of making HTTPS GET and POST requests. So, basically anything, including possibly your toaster. + It can be accessed by any utility capable of making HTTPS GET and POST requests, such as cURL or possibly your toaster.

To verify the LBRY daemon is running correctly, let's try looking up a name: diff --git a/view/template/developer/_quickstartCredits.php b/view/template/developer/_quickstartCredits.php index 50d71085..2945f187 100644 --- a/view/template/developer/_quickstartCredits.php +++ b/view/template/developer/_quickstartCredits.php @@ -4,8 +4,10 @@ $curl 'http://localhost:5279/lbryapi' --data '{"method":"wallet_new_address","params":[]}' ["bbFxRyWCFRkA9YcuuZD8nE7XTLUxYnddTs"]

Enter this address in the form below and we'll send you 50 credits.

-
- +
+ Request::getRelativeUri() . '#new-developer' + ]) ?>

Next, confirm you've received your credits by calling wallet_balance:

$curl 'http://localhost:5279/lbryapi' --data '{"method":"wallet_balance"}' diff --git a/view/template/developer/_quickstartInstall.php b/view/template/developer/_quickstartInstall.php index bbacaec4..a26baf1d 100644 --- a/view/template/developer/_quickstartInstall.php +++ b/view/template/developer/_quickstartInstall.php @@ -29,5 +29,5 @@

While running, the daemon will provide a JSON-RPC interface on localhost. We'll learn how to interact with that next.

macOS and Windows do not currently bundle the daemon separately. Just launch the full app and the API will still be available. This will be resolved in v0.9.
- */ ?> -
The first time you run the daemon, it must catch-up with most recent blockheaders. This can take a few minutes.
\ No newline at end of file +
The first time you run the daemon, it must catch-up with most recent blockheaders. This can take a few minutes.
+ */ ?> \ No newline at end of file diff --git a/view/template/developer/developer-program.php b/view/template/developer/developer-program.php deleted file mode 100644 index 116e809f..00000000 --- a/view/template/developer/developer-program.php +++ /dev/null @@ -1,31 +0,0 @@ - - - - false, 'isAbsolute' => false]) ?> -
-
-

Developer Program

-

All developers with a GitHub account prior to January 31st, 2017 are eligible for free credits.

-

To claim your credits, enter a wallet address in the form below and authenticate with GitHub.

-

- We will store your GitHub username and email address, but nothing else. -

-
-

Receive Credits

- -
- -
- -
- -
- -
-
- -
-
-
- \ No newline at end of file diff --git a/view/template/developer/quickstart.php b/view/template/developer/quickstart.php index a3356c3f..48d6d030 100644 --- a/view/template/developer/quickstart.php +++ b/view/template/developer/quickstart.php @@ -1,4 +1,5 @@ + false, 'isAbsolute' => false]) ?> diff --git a/web/js/quickstart.js b/web/js/quickstart.js new file mode 100644 index 00000000..9e783349 --- /dev/null +++ b/web/js/quickstart.js @@ -0,0 +1,59 @@ +lbry.quickstartForm = function (selector, apiUrl) { + var form = $(selector), + accessToken = form.find(':input[name="access_token"]').val(), + walletAddressInput = form.find(':input[name="wallet_address"]'), + submitButton = form.find(':input[type="submit"]'), + isSubmitting = false; + + function resetFormState() { + isSubmitting = false; + walletAddressInput.attr('readonly', null); + submitButton.val(submitButton.data('submitLabel')).attr('disabled', null); + } + + if (window.localStorage.getItem("quickstartFormSuccessHtml")) { + form.find('.notice-success').html(window.localStorage.getItem("quickstartFormSuccessHtml")).show(); + form.find('.form-row, .submit-row').hide(); + } else if (accessToken) { + form.submit(function (event) { + if (isSubmitting) { + return false; + } + + form.find('.notice-success, .notice-error').hide(); + + if (!walletAddressInput.val()) { + resetFormState(); + form.find('.notice-error').html("Please supply a wallet address.").show(); + return false; + } + + event.preventDefault(); + + walletAddressInput.attr('readonly', 'readonly'); + submitButton.val(submitButton.data('submittingLabel')).attr('disabled', 'disabled'); + + $.post(apiUrl, { + access_token: accessToken, + wallet_address: walletAddressInput.val() + }) + .done(function (responseData) { + var data = responseData.data; + var anchor = $(''); + anchor.attr("href", "https://explorer.lbry.io/tx/" + data.TransactionHash); + anchor.html(data.TransactionHash) + form.find('.notice-success') + .html(data.RewardAmount + " credits sent in transaction ") + .append(anchor) + .show(); + window.localStorage.setItem("quickstartFormSuccessHtml", form.find('.notice-success').html()); + }) + .fail(function (xhr) { + var responseData = $.parseJSON(xhr.responseText); + form.find('.notice-error').html(responseData.error.length ? responseData.error : "Something went wrong. Please email grin@lbry.io").show(); + }) + .always(resetFormState); + }) + form.submit(); + } +} \ No newline at end of file diff --git a/web/scss/_form.scss b/web/scss/_form.scss index 4754d778..1194d479 100644 --- a/web/scss/_form.scss +++ b/web/scss/_form.scss @@ -104,6 +104,9 @@ input[type="date"] { border: 1px solid #ccc; box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1); transition: all 0.2s linear; + &[readonly] { + background-color: #bbb; + } } textarea:focus, @@ -126,6 +129,9 @@ input[type="checkbox"] { width: auto; } +input[type="submit"][disabled] { + cursor: auto; +} .mail-submit, .invite-submit {