"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"; // P R O G R A M export default (socket, action) => { if (typeof socket !== "object" && typeof action !== "object") return; switch(true) { case action.message === "fetch metadata": fetchMetadata(action, socket); break; case action.message === "landed on homepage": generateGitHubFeed(result => { socket.send(JSON.stringify({ html: result, message: "updated html", selector: "#github-feed" })); }); break; case action.message === "landed on playground": generateContent(1, result => { socket.send(JSON.stringify({ html: result, message: "updated html", selector: "#playground-loader" })); }); break; case action.message === "request for playground, example 1": generateContent(1, result => { socket.send(JSON.stringify({ 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 => { socket.send(JSON.stringify({ 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, method: "resolve", example: exampleNumber })); } Promise.all(rawContentCollection).then(collection => { for (const part of collection) { try { renderedContentCollection.push(`
${part.value.stream.metadata.title}
${part.channel_name}
`); } catch (err) { return; // TODO: Return nice error message } } renderedContentCollection.push(` `); displayTrendingContent(renderedContentCollection.join("")); }); }); } if (exampleNumber === 3) { const approvedUrls = [ "LBRY#3db81c073f82fd1bb670c65f526faea3b8546720", "correlation-can-imply-causation#173412f5b1b7aa63a752e8832406aafd9f1ecb4e", "thanos-is-the-protagonist-how-infinity#2a7f5db2678177435b1dee6c9e38e035ead450b6", "epic-arcade-mode-duos-nickatnyte-molt#d81bac6d49b1f92e58c37a5f633a27a45b43405e", "political-correctness-a-force-for-good-a#b4668c0bd096317b44c40738c099b6618095e75f", "10-secrets-hidden-inside-famous-logos#007789cc45cbb4255cf02ba77cbf84ca8e3d7561", "ever-wonder-how-bitcoin-and-other#1ac47b8b3def40a25850dc726a09ce23d09e7009", "bankrupt-pan-am#784b3c215a6f06b663fc1aa292bcb19f29c489bb", "minecraft-in-real-life-iron-man#758dd6497cdfc401ae1f25984738d024d47b50af", "ethan-shows-kyle-warframe-skyvault#8a7401b88d5ed0376d98f16808194d4dcb05b284" ]; const rawContentCollection = []; const renderedContentCollection = []; for (const url of approvedUrls) rawContentCollection.push(fetchMetadata({ claim: url, method: "resolve", example: exampleNumber })); return Promise.all(rawContentCollection).then(collection => { for (const part of collection) { if ( part && part.value && part.value.stream.metadata.thumbnail && part.channel_name ) { renderedContentCollection.push(`
${part.value.stream.metadata.title}
${part.channel_name}
`); } } renderedContentCollection.push(` `); displayTrendingContent(renderedContentCollection.join("")); }); } } function generateMemeCreator(socket) { const images = [ { alt: "Carl Sagan", // src: "https://spee.ch/4f6b953e605a602434246743fd246d3e1fd4f5fd/carlsagan2.jpg" src: "/assets/media/images/carlsagan2.jpg" }, { alt: "Doge", // src: "https://spee.ch/2f90f2d91441a4d33d3d4eb82bdfc4c56ec742c7/doge-meme.jpg" src: "/assets/media/images/doge-meme.jpg" }, { alt: "LBRY Logo With Green Background", // src: "https://spee.ch/40ac6818bbac87a208722bf4467653341d460908/lbry-green.png" src: "/assets/media/images/lbry-green.png" } ]; const memePlaceholderData = { bottomLine: { placeholder: "Top line", value: "that I made" }, description: { placeholder: "Description", value: "Check out this image I published to LBRY via lbry.tech" }, topLine: { placeholder: "Top line", value: "This is an example meme" }, title: { placeholder: "Title", value: "Dank Meme Supreme da Cheese" } }; const renderedImages = []; for (const image of images) renderedImages.push(`${image.alt}`); const memeCreator = html`
Unfortunately, it looks like canvas is not supported in your browser ${renderedImages}
Image Text
Metadata
`; return socket.send(JSON.stringify({ example: 2, html: memeCreator, message: "updated html", selector: "#playground-loader" })); } async function getTrendingContent() { try { const response = await got("https://api.lbry.io/file/list_trending"); return JSON.parse(response.body); // eslint-disable-line padding-line-between-statements } catch (error) { return error; } } function makeImageSourceSecure(url) { const originalUrl = new URL(url); if (originalUrl.protocol !== "https") return `https://${originalUrl.host}${originalUrl.pathname}`; return originalUrl.href; } async function newsletterSubscribe(data, socket) { const email = data.email; if (!validateEmail(email)) return socket.send(JSON.stringify({ class: "error", html: "Your email address is invalid", message: "updated html", selector: "#emailMessage" })); try { await got.post(`https://api.lbry.io/list/subscribe?email=${encodeURIComponent(email)}&tag=developer`); return socket.send(JSON.stringify({ html: "Thank you! Please confirm subscription in your inbox.", message: "updated html", selector: "#emailMessage" })); } catch (error) { const response = JSON.parse(error.body); if (!response.success) { messageSlack( "\n" + "> *NEWSLETTER ERROR:* ```" + response.error + "```" + "\n" + `> _Cause: ${email} interacted with the form_\n` ); return socket.send(JSON.stringify({ class: "error", html: response.error, message: "updated html", selector: "#emailMessage" })); } messageSlack( "\n" + "> *NEWSLETTER ERROR:* ```¯\\_(ツ)_/¯ This should be an unreachable error```" + "\n" + `> _Cause: ${email} interacted with the form_\n` ); return socket.send(JSON.stringify({ class: "error", html: "Something is terribly wrong", message: "updated html", selector: "#emailMessage" })); } } function validateEmail(email) { const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i; return emailRegex.test(String(email)); // eslint-disable-line padding-line-between-statements }