"use strict";
// I M P O R T S
import got from "got";
import html from "choo/html";
// U T I L S
import apiPage from "~view/api";
import fetchMetadata from "~helper/fetch-metadata";
import { generateGitHubFeed } from "~helper/github";
import messageSlack from "~helper/slack";
import { URL } from "url";
// const githubAppId = process.env.GITHUB_APP_ID;
// const githubAppSecret = process.env.GITHUB_APP_SECRET;
const githubAppId = process.env.GITHUB_APP_ID_TEST;
const githubAppSecret = process.env.GITHUB_APP_SECRET_TEST;
// P R O G R A M
export default async(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 token":
verifyGitHubToken(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;
case action.message === "view different documentation version":
send(socket, {
element: "div",
html: await apiPage({
params: {
wildcard: action.version.split("-")[0]
},
tag: action.version.split("-")[1]
}),
message: "replace html",
parentElement: "main",
selector: ".__slate"
});
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) {
if (part && part.value.tags && part.value.tags.includes("mature"))
continue;
if (part === undefined)
continue;
try {
renderedContentCollection.push(`