From 2182428e2ca61bb7ec4df7e8ca14ae73121ad33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9D=E3=83=BC=E3=83=AB=20=E3=82=A6=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=83=96?= Date: Thu, 21 Feb 2019 13:28:09 -0600 Subject: [PATCH 1/2] Trying to fix this --- app/components/client/devprogram-scripts.js | 56 +++++++++++- app/components/playground.js | 2 +- app/dist/scripts/sockets.js | 7 +- app/sockets.js | 99 +++++++-------------- documents/developer-program.md | 3 +- 5 files changed, 92 insertions(+), 75 deletions(-) diff --git a/app/components/client/devprogram-scripts.js b/app/components/client/devprogram-scripts.js index 0017425..3bea3c1 100644 --- a/app/components/client/devprogram-scripts.js +++ b/app/components/client/devprogram-scripts.js @@ -1,4 +1,4 @@ -"use strict"; /* global document, history, send, window */ +"use strict"; /* global document, fetch, history, send, window */ @@ -12,7 +12,7 @@ document.getElementById("get-started").onclick = event => { if (window.location.search.includes("?code=")) { document.querySelector("developer-program").innerHTML = ` -
+ @@ -34,13 +34,63 @@ if (window.location.search.includes("?code=")) { } if (document.getElementById("creditsAcquire")) { + document.getElementById("walletAddress").addEventListener("keyup", event => { + const key = event.keyCode ? event.keyCode : event.which; + + if (key === 13) + document.getElementById("creditsAcquire").click(); + }); + document.getElementById("creditsAcquire").onclick = () => { send({ address: document.getElementById("walletAddress").value, code: document.getElementById("oauthCode").value, - message: "verify github auth" + message: "verify github token" }); document.querySelector("developer-program").innerHTML = "

Awaiting response from LBRY server...

"; }; } + +async function syncWithApi(data) { // eslint-disable-line no-unused-vars + const address = data.address; + const code = data.code; + + if (code === null) + document.querySelector("developer-program").innerHTML = + "

There was an issue with accessing GitHub's API. Please try again later.

"; + + try { + // let result = await got(`https://${apiUrl}/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${data.address}`, { json: true }); + + let result = await fetch(`https://api.lbry.io/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${address}`, { + headers: { + "Content-Type": "application/json" + }, + mode: "no-cors" + }).then(response => { + console.log("————— response"); // eslint-disable-line no-console + console.log(response); // eslint-disable-line no-console + return response; + }); + + console.log("————— result"); // eslint-disable-line no-console + console.log(result); // eslint-disable-line no-console + // result = result.body.data; + + // document.querySelector("developer-program").innerHTML = + // `

Success! Your wallet has been credited with ${result.reward_amount} LBC.

We have a great reference for the LBRY SDK here to help you get started.

You can see proof of this transaction on our Blockain Explorer.

`; + } catch(error) { + console.log(error); // eslint-disable-line no-console + + if (!error.body) { + document.querySelector("developer-program").innerHTML = + "

LBRY API is down. Please try again later.

"; + } + + else { + document.querySelector("developer-program").innerHTML = + "

You have already claimed this reward. This reward is limited to ONE per person. Your enthusiasm is appreciated.

"; + } + } +} diff --git a/app/components/playground.js b/app/components/playground.js index f0a60d7..b4eb3f3 100644 --- a/app/components/playground.js +++ b/app/components/playground.js @@ -42,7 +42,7 @@ function example1() { `; } -function navigation() { // TODO: Save tutorial position to localStorage +function navigation() { return dedent`
  • console.log("WebSocket connection established"); // eslint-disable-line no-console ws.onmessage = socket => { - const data = JSON.parse(socket.data); + let data = JSON.parse(socket.data); switch(true) { + case data.message === "github token status": + data = data.data; + syncWithApi(data); // eslint-disable-line no-undef + break; + case data.message === "notification": // TODO: Make work with appending so multiple notifications can be sent document.getElementById("flash-container").innerHTML = `
    ${data.details}
    `; diff --git a/app/sockets.js b/app/sockets.js index 63ec516..d61639f 100644 --- a/app/sockets.js +++ b/app/sockets.js @@ -13,9 +13,16 @@ import fetchMetadata from "@helper/fetch-metadata"; import { generateGitHubFeed } from "@helper/github"; import messageSlack from "@helper/slack"; -let apiUrl = process.env.REWARD_URL; -let githubAppId = process.env.GITHUB_APP_ID; -let githubAppSecret = process.env.GITHUB_APP_SECRET; +// let apiUrl = process.env.REWARD_URL; +// let apiUrl = process.env.REWARD_URL_TEST; + +// let githubAppId = process.env.GITHUB_APP_ID; +// let githubAppSecret = process.env.GITHUB_APP_SECRET; + +let githubAppId = process.env.GITHUB_APP_ID_TEST; +let githubAppSecret = process.env.GITHUB_APP_SECRET_TEST; + + // P R O G R A M @@ -28,8 +35,8 @@ export default (socket, action) => { getGitHubUserToken(socket); break; - case action.message === "verify github auth": - syncWithApi(action, socket); + case action.message === "verify github token": + verifyGitHubToken(action, socket); break; case action.message === "fetch metadata": @@ -418,79 +425,33 @@ export function send(transport, data) { return transport.send(JSON.stringify(data)); } -async function syncWithApi(data, socket) { - const tokenResponse = await verifyGitHubToken(data.code); - - if (tokenResponse === null) { - return send(socket, { - html: "

    There was an issue with accessing GitHub's API. Please try again later.

    ", - message: "updated html", - selector: "developer-program" - }); - } - - try { - let result = await got(`https://${apiUrl}/reward/new?github_token=${tokenResponse}&reward_type=github_developer&wallet_address=${data.address}`, { json: true }); - - // TEMPORARY - messageSlack({ - message: tokenResponse, - title: "DEVELOPER PROGRAM TOKEN RESPONSE" - }); - - messageSlack({ - message: data.address, - title: "DEVELOPER PROGRAM ADDRESS" - }); - // TEMPORARY - - result = result.body.data; - - return send(socket, { - html: `

    Success! Your wallet has been credited with ${result.reward_amount} LBC.

    We have a great reference for the LBRY SDK here to help you get started.

    You can see proof of this transaction on our Blockain Explorer.

    `, - message: "updated html", - selector: "developer-program" - }); - } catch(error) { - if (!error.body) { - return send(socket, { - html: "

    LBRY API is down. Please try again later.

    ", - message: "updated html", - selector: "developer-program" - }); - } - - console.log(error); // eslint-disable-line no-console - const err = error.body; - - // TEMPORARY - messageSlack({ - message: JSON.stringify(err), - title: "DEVELOPER PROGRAM ERROR" - }); - // TEMPORARY - - return send(socket, { - html: "

    You have already claimed this reward. This reward is limited to ONE per person. Your enthusiasm is appreciated.

    ", - message: "updated html", - selector: "developer-program" - }); - } -} - function validateEmail(email) { const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i; return emailRegex.test(String(email)); // eslint-disable-line padding-line-between-statements } -async function verifyGitHubToken(code) { +async function verifyGitHubToken(data, socket) { + const code = data.code; + try { let result = await got.post(`https://github.com/login/oauth/access_token?client_id=${githubAppId}&client_secret=${githubAppSecret}&code=${code}`, { json: true }); - result = result.body; - return result.access_token; + const response = { + address: data.address, + code: result.body.access_token + }; + + return send(socket, { + data: response, + message: "github token status" + }); } catch(verificationError) { console.log(verificationError.body); // eslint-disable-line no-console - return null; + + return send(socket, { + details: verificationError.body, + message: "notification", + type: "error" + }); } } diff --git a/documents/developer-program.md b/documents/developer-program.md index 3972c1d..71f1403 100644 --- a/documents/developer-program.md +++ b/documents/developer-program.md @@ -9,4 +9,5 @@ To qualify you must: - have a GitHub account that is at least 90 days old and - have an active commit history -This program will be active on 2019.02.21. Stay tuned! +### Claim LBC + -- 2.45.2 From 358ee3efae3e3e0d20d4a9cc7f24c12a719e966a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9D=E3=83=BC=E3=83=AB=20=E3=82=A6=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=83=96?= Date: Fri, 22 Feb 2019 11:41:50 -0600 Subject: [PATCH 2/2] This is now working locally --- app/components/client/devprogram-scripts.js | 53 +++++++++------------ app/sockets.js | 11 ++--- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/app/components/client/devprogram-scripts.js b/app/components/client/devprogram-scripts.js index 3bea3c1..d8f4827 100644 --- a/app/components/client/devprogram-scripts.js +++ b/app/components/client/devprogram-scripts.js @@ -52,7 +52,7 @@ if (document.getElementById("creditsAcquire")) { }; } -async function syncWithApi(data) { // eslint-disable-line no-unused-vars +function syncWithApi(data) { // eslint-disable-line no-unused-vars const address = data.address; const code = data.code; @@ -60,37 +60,30 @@ async function syncWithApi(data) { // eslint-disable-line no-unused-vars document.querySelector("developer-program").innerHTML = "

    There was an issue with accessing GitHub's API. Please try again later.

    "; - try { - // let result = await got(`https://${apiUrl}/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${data.address}`, { json: true }); + fetch(`https://api.lbry.io/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${address}`) + .then(response => response.json()) + .then(result => { + switch(true) { + case !result.success: + case result.error === "this reward is limited to 1 per person": + document.querySelector("developer-program").innerHTML = + "

    You have already claimed this reward. This reward is limited to ONE per person. Your enthusiasm is appreciated.

    "; + break; - let result = await fetch(`https://api.lbry.io/reward/new?github_token=${code}&reward_type=github_developer&wallet_address=${address}`, { - headers: { - "Content-Type": "application/json" - }, - mode: "no-cors" - }).then(response => { - console.log("————— response"); // eslint-disable-line no-console - console.log(response); // eslint-disable-line no-console - return response; - }); + case result.success: + result = result.data; + document.querySelector("developer-program").innerHTML = + `

    Success! Your wallet has been credited with ${result.reward_amount} LBC.

    We have a great reference for the LBRY SDK here to help you get started.

    You can see proof of this transaction on our Blockain Explorer.

    `; + break; - console.log("————— result"); // eslint-disable-line no-console - console.log(result); // eslint-disable-line no-console - // result = result.body.data; - - // document.querySelector("developer-program").innerHTML = - // `

    Success! Your wallet has been credited with ${result.reward_amount} LBC.

    We have a great reference for the LBRY SDK here to help you get started.

    You can see proof of this transaction on our Blockain Explorer.

    `; - } catch(error) { - console.log(error); // eslint-disable-line no-console - - if (!error.body) { + default: + console.log(data); // eslint-disable-line no-console + break; + } + }) + .catch(() => { + // Idk what the error would be (probably a 500) so let's just have this message document.querySelector("developer-program").innerHTML = "

    LBRY API is down. Please try again later.

    "; - } - - else { - document.querySelector("developer-program").innerHTML = - "

    You have already claimed this reward. This reward is limited to ONE per person. Your enthusiasm is appreciated.

    "; - } - } + }); } diff --git a/app/sockets.js b/app/sockets.js index d61639f..c528c7c 100644 --- a/app/sockets.js +++ b/app/sockets.js @@ -13,14 +13,11 @@ import fetchMetadata from "@helper/fetch-metadata"; import { generateGitHubFeed } from "@helper/github"; import messageSlack from "@helper/slack"; -// let apiUrl = process.env.REWARD_URL; -// let apiUrl = process.env.REWARD_URL_TEST; +const githubAppId = process.env.GITHUB_APP_ID; +const githubAppSecret = process.env.GITHUB_APP_SECRET; -// let githubAppId = process.env.GITHUB_APP_ID; -// let githubAppSecret = process.env.GITHUB_APP_SECRET; - -let githubAppId = process.env.GITHUB_APP_ID_TEST; -let githubAppSecret = process.env.GITHUB_APP_SECRET_TEST; +// const githubAppId = process.env.GITHUB_APP_ID_TEST; +// const githubAppSecret = process.env.GITHUB_APP_SECRET_TEST; -- 2.45.2