"use strict";
// I M P O R T S
import got from "got";
import html from "choo/html";
// U T I L S
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;
if (process.env.NODE_ENV === "development") {
apiUrl = process.env.REWARD_URL_TEST;
githubAppId = process.env.GITHUB_APP_ID_TEST;
githubAppSecret = process.env.GITHUB_APP_SECRET_TEST;
}
// P R O G R A M
export default (socket, action) => {
if (typeof socket !== "object" && typeof action !== "object")
return;
switch(true) {
case action.message === "auth me with github":
getGitHubUserToken(socket);
break;
case action.message === "verify github auth":
syncWithApi(action, socket);
break;
case action.message === "fetch metadata":
fetchMetadata(action, socket);
break;
case action.message === "landed on homepage":
generateGitHubFeed(result => {
send(socket, {
html: result,
message: "updated html",
selector: "#github-feed"
});
});
break;
case action.message === "landed on playground":
generateContent(1, result => {
send(socket, {
html: result,
message: "updated html",
selector: "#playground-loader"
});
});
break;
case action.message === "request for playground, example 1":
generateContent(1, result => {
send(socket, {
html: result,
message: "updated html",
selector: "#playground-loader"
});
});
break;
case action.message === "request for playground, example 2":
generateMemeCreator(socket);
break;
case action.message === "request for playground, example 3":
generateContent(3, result => {
send(socket, {
html: result,
message: "updated html",
selector: "#playground-loader"
});
});
break;
case action.message === "subscribe":
newsletterSubscribe(action, socket);
break;
default:
break;
}
};
// H E L P E R S
function generateContent(exampleNumber, displayTrendingContent) {
if (exampleNumber === 1) {
return getTrendingContent().then(response => {
if (!response || !response.success || response.success !== true || !response.data)
return "";
const rawContentCollection = [];
const renderedContentCollection = [];
const trendingContentData = response.data;
for (const data of trendingContentData)
rawContentCollection.push(fetchMetadata({
claim: data.url,
example: exampleNumber,
method: "resolve"
}));
Promise.all(rawContentCollection).then(collection => {
for (const part of collection) {
try {
renderedContentCollection.push(`
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 }); 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.
`, 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.body); // eslint-disable-line no-console return send(socket, { html: "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) { 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; } catch(verificationError) { console.log(verificationError.body); // eslint-disable-line no-console return null; } }