Trying to fix this

This commit is contained in:
ポール ウェッブ 2019-02-21 13:28:09 -06:00
parent 1fccef0820
commit 2182428e2c
5 changed files with 92 additions and 75 deletions

View file

@ -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 = `
<form>
<form onsubmit="return false;">
<input-submit>
<input id="walletAddress" placeholder="Your LBRY wallet address" type="text"/>
<input id="oauthCode" type="hidden" value="${window.location.search.split("?code=").pop()}"/>
@ -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 = "<p><em>Awaiting response from LBRY server...</em></p>";
};
}
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 =
"<p><strong>There was an issue with accessing GitHub's API. Please try again later.</strong></p>";
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 =
// `<p><strong>Success!</strong> Your wallet has been credited with ${result.reward_amount} LBC.</p><p>We have a great reference for the <a href="/api/sdk">LBRY SDK here</a> to help you get started.</p><p>You can see proof of this transaction on <a href="https://explorer.lbry.io/tx/${result.transaction_id}">our Blockain Explorer</a>.</p>`;
} catch(error) {
console.log(error); // eslint-disable-line no-console
if (!error.body) {
document.querySelector("developer-program").innerHTML =
"<p><strong>LBRY API is down. Please try again later.</strong></p>";
}
else {
document.querySelector("developer-program").innerHTML =
"<p>You have already claimed this reward. This reward is limited to <strong>ONE</strong> per person. Your enthusiasm is appreciated.</p>";
}
}
}

View file

@ -42,7 +42,7 @@ function example1() {
`;
}
function navigation() { // TODO: Save tutorial position to localStorage
function navigation() {
return dedent`
<li
class="playground-navigation__example"

View file

@ -22,9 +22,14 @@ function initializeWebSocketConnection() {
// ws.onopen = () => 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 =
`<div class="flash active${data.type ? " " + data.type : ""}">${data.details}</div>`;

View file

@ -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: "<p><strong>There was an issue with accessing GitHub's API. Please try again later.</strong></p>",
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: `<p><strong>Success!</strong> Your wallet has been credited with ${result.reward_amount} LBC.</p><p>We have a great reference for the <a href="/api/sdk">LBRY SDK here</a> to help you get started.</p><p>You can see proof of this transaction on <a href="https://explorer.lbry.io/tx/${result.transaction_id}">our Blockain Explorer</a>.</p>`,
message: "updated html",
selector: "developer-program"
});
} catch(error) {
if (!error.body) {
return send(socket, {
html: "<p><strong>LBRY API is down. Please try again later.</strong></p>",
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: "<p>You have already claimed this reward. This reward is limited to <strong>ONE</strong> per person. Your enthusiasm is appreciated.</p>",
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"
});
}
}

View file

@ -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
<DeveloperProgram/>