From 1a81121b747eefa52e768189760223944d69ea30 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, 30 Nov 2018 14:46:22 -0600 Subject: [PATCH] 2.0.0 --- app/client.js | 11 +- app/components/api/header-blockchain.js | 2 +- app/components/api/header-sdk.js | 2 +- app/components/client/ecosystem-scripts.js | 10 +- app/components/client/glossary-scripts.js | 2 +- app/components/client/playground-scripts.js | 95 +++--- app/components/ecosystem.js | 43 --- app/components/ecosystem/index.js | 27 ++ .../ecosystem/module-applications.js | 2 +- app/components/ecosystem/module-lbry.js | 2 +- app/components/ecosystem/module-lbrycrd.js | 2 +- .../ecosystem/submodule-chainquery.js | 2 +- .../ecosystem/submodule-lighthouse.js | 2 +- .../ecosystem/submodule-reflector.js | 2 +- app/components/ecosystem/submodule-wallet.js | 2 +- app/components/edit-link.js | 2 +- app/components/email-subscribe.js | 2 +- app/components/footer.js | 7 +- app/components/glossary-toc.js | 4 +- app/components/head.js | 2 +- app/components/link-grid.js | 4 +- app/components/markdown.js | 9 +- app/components/mission-statement.js | 2 +- app/components/navigation.js | 2 +- app/components/overview.js | 39 +++ app/components/playground.js | 2 +- app/components/resources-link-grid.js | 6 +- app/components/wrapper.js | 33 +- app/helpers/fetch-metadata.js | 322 ++++++++++-------- app/helpers/github.js | 8 +- app/helpers/publish-meme.js | 15 +- app/helpers/random-string.js | 4 +- app/helpers/slack.js | 43 ++- app/helpers/upload-image.js | 15 +- app/sockets.js | 34 +- app/views/404.js | 4 +- app/views/api.js | 81 ++--- app/views/home.js | 7 +- app/views/redirect.js | 9 +- documents/overview.md | 2 +- package.json | 19 +- server.js | 12 +- 42 files changed, 488 insertions(+), 407 deletions(-) delete mode 100644 app/components/ecosystem.js create mode 100644 app/components/ecosystem/index.js create mode 100644 app/components/overview.js diff --git a/app/client.js b/app/client.js index 1bf2802..8bae0e8 100755 --- a/app/client.js +++ b/app/client.js @@ -2,19 +2,17 @@ -// P A C K A G E S +// I M P O R T S import async from "choo-async"; import asyncHtml from "choo-async/html"; import choo from "choo"; -import devtools from "choo-devtools"; -import { require as local } from "app-root-path"; import ssr from "choo-ssr"; // U T I L S -const head = local("/app/components/head").default; -const wrapper = local("/app/components/wrapper").default; +import head from "./components/head"; +import wrapper from "./components/wrapper"; @@ -23,8 +21,6 @@ const wrapper = local("/app/components/wrapper").default; function main() { const app = async(choo()); - if (process.env.NODE_ENV !== "production") app.use(devtools()); - const page = view => ( shell( ssr.head( @@ -53,6 +49,7 @@ if (typeof window !== "undefined") main(); // E X P O R T module.exports = exports = main; +// export default main(); diff --git a/app/components/api/header-blockchain.js b/app/components/api/header-blockchain.js index 34a9a85..ec05019 100644 --- a/app/components/api/header-blockchain.js +++ b/app/components/api/header-blockchain.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; diff --git a/app/components/api/header-sdk.js b/app/components/api/header-sdk.js index 4ac685c..5e4dd50 100644 --- a/app/components/api/header-sdk.js +++ b/app/components/api/header-sdk.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; diff --git a/app/components/client/ecosystem-scripts.js b/app/components/client/ecosystem-scripts.js index 07914c7..8fd87f5 100644 --- a/app/components/client/ecosystem-scripts.js +++ b/app/components/client/ecosystem-scripts.js @@ -1,4 +1,4 @@ -/* global document, localStorage */ "use strict"; +"use strict"; /* global document, localStorage */ @@ -199,18 +199,14 @@ for (const module of mainModules) { if ( module[Object.keys(module)] === "true" && document.querySelector(`.ecosystem__module.${Object.keys(module)} h2 span`) - ) { - document.querySelector(`.ecosystem__module.${Object.keys(module)} h2 span`).click(); - } + ) document.querySelector(`.ecosystem__module.${Object.keys(module)} h2 span`).click(); } for (const subModule of subModules) { if ( subModule[Object.keys(subModule)] === "true" && document.querySelector(`.ecosystem__submodule.${Object.keys(subModule)} h3`) - ) { - document.querySelector(`.ecosystem__submodule.${Object.keys(subModule)} h3`).click(); - } + ) document.querySelector(`.ecosystem__submodule.${Object.keys(subModule)} h3`).click(); } diff --git a/app/components/client/glossary-scripts.js b/app/components/client/glossary-scripts.js index cdccd8b..8973286 100644 --- a/app/components/client/glossary-scripts.js +++ b/app/components/client/glossary-scripts.js @@ -1,4 +1,4 @@ -/* global document */ "use strict"; +"use strict"; /* global document */ diff --git a/app/components/client/playground-scripts.js b/app/components/client/playground-scripts.js index cf9af68..99f4bf9 100644 --- a/app/components/client/playground-scripts.js +++ b/app/components/client/playground-scripts.js @@ -1,4 +1,4 @@ -/* global document, navigator, send, window */ "use strict"; +"use strict"; /* global document, navigator, send, window */ @@ -6,10 +6,10 @@ initializePlayground(); -if (window.location.href.search && window.location.href.split("?url=")[1]) { // pre-fill example one if search parameter exists +// pre-fill example one if search parameter exists +if (window.location.href.search && window.location.href.split("?url=")[1]) { const searchParameter = window.location.href.split("?url=")[1]; - - fetchMetadata(1, searchParameter); + fetchMetadata(1, searchParameter); // eslint-disable-line padding-line-between-statements } @@ -34,17 +34,19 @@ document.querySelector("body").addEventListener("click", event => { }); document.getElementById("fetch-claim-uri").addEventListener("keyup", event => { - const key = event.keyCode ? event.keyCode : event.which; + const key = event.keyCode ? + event.keyCode : + event.which; switch(true) { - case (document.querySelector("[data-example='1']").classList.contains("active")): + case document.querySelector("[data-example='1']").classList.contains("active"): if ( key === 13 && document.getElementById("fetch-claim-uri").value.length > 0 ) fetchMetadata(1, document.getElementById("fetch-claim-uri").value); break; - case (document.querySelector("[data-example='3']").classList.contains("active")): + case document.querySelector("[data-example='3']").classList.contains("active"): if ( key === 13 && document.getElementById("fetch-claim-uri").value.length > 0 @@ -79,7 +81,7 @@ function clearCanvas(canvas) { ctx.restore(); } -function detectLanguageAndUpdate() { // eslint-disable-line +function detectLanguageAndUpdate() { // eslint-disable-line no-unused-vars const compare = (array1, array2) => array2.filter(value => array2.indexOf(value)); // compare two arrays and get match(es) const memeLocaleObject = document.getElementById("meme-language").children; const memeLocales = []; @@ -115,8 +117,8 @@ function debounce(func, wait, immediate) { const callNow = immediate && !timeout; clearTimeout(timeout); - timeout = setTimeout(later, wait); + if (callNow) func.apply(context, args); }; } @@ -136,8 +138,6 @@ function initializePlayground() { }, 300); } - - function fetchMetadata(exampleNumber, data) { if (!exampleNumber) return; @@ -151,17 +151,7 @@ function fetchMetadata(exampleNumber, data) { })); document.getElementById("fetch-claim-uri").value = data; - - document.getElementById("playground-results").innerHTML = ` -

-# With the LBRY app/daemon running locally, you can use this in your Terminal
-curl --header "Content-Type: application/json" --data '{ "method": "resolve", "params": { "uri": "${data}" }}' http://localhost:5279    
-        
- -
-
- `; - + document.getElementById("playground-results").innerHTML = playgroundResponseForExample1(data); document.getElementById("playground-loader").style.display = "none"; break; @@ -173,16 +163,7 @@ curl --header "Content-Type: application/json" example: exampleNumber })); - document.getElementById("playground-results").innerHTML = ` -

-# With the LBRY app/daemon running locally, you can use this in your Terminal
-curl --header "Content-Type: application/json" --data '{ "method": "publish", "params": { "name": "${getMemeInfo().name}", "file_path": "ABSOLUTE_PATH_TO_MEDIA_ON_YOUR_COMPUTER", "bid": "0.001", "metadata": { "description": "${getMemeInfo().description}", "title": "${getMemeInfo().title}", "language": "${getMemeInfo().language}", "license": "${getMemeInfo().license}", "nsfw": ${getMemeInfo().nsfw} }}}' http://localhost:5279    
-        
- -
-
- `; - + document.getElementById("playground-results").innerHTML = playgroundResponseForExample2(getMemeInfo()); document.getElementById("playground-loader").style.display = "none"; break; @@ -195,17 +176,7 @@ curl --header "Content-Type: application/json" })); document.getElementById("fetch-claim-uri").value = data; - - document.getElementById("playground-results").innerHTML = ` -

-# With the LBRY app/daemon running locally, you can use this in your Terminal
-curl --header "Content-Type: application/json" --data '{ "method": "claim_tip", "params": { "amount": "0.001", "claim_id": "${data}" }}' http://localhost:5279    
-        
- -
-
- `; - + document.getElementById("playground-results").innerHTML = playgroundResponseForExample3(data); document.getElementById("playground-loader").style.display = "none"; break; @@ -226,6 +197,42 @@ function getMemeInfo() { // TODO: Error handling }; } +function playgroundResponseForExample1(source) { + return ` +

+# With the LBRY app/daemon running locally, you can use this in your Terminal
+curl --header "Content-Type: application/json" --data '{ "method": "resolve", "params": { "uri": "${source}" }}' http://localhost:5279    
+
+ +
+
+ `; +} + +function playgroundResponseForExample2(source) { + return ` +

+# With the LBRY app/daemon running locally, you can use this in your Terminal
+curl --header "Content-Type: application/json" --data '{ "method": "publish", "params": { "name": "${source.name}", "file_path": "ABSOLUTE_PATH_TO_MEDIA_ON_YOUR_COMPUTER", "bid": "0.001", "metadata": { "description": "${source.description}", "title": "${source.title}", "language": "${source.language}", "license": "${source.license}", "nsfw": ${source.nsfw} }}}' http://localhost:5279    
+    
+ +
+
+ `; +} + +function playgroundResponseForExample3(source) { + return ` +

+# With the LBRY app/daemon running locally, you can use this in your Terminal
+curl --header "Content-Type: application/json" --data '{ "method": "claim_tip", "params": { "amount": "0.001", "claim_id": "${source}" }}' http://localhost:5279    
+    
+ +
+
+ `; +} + const handleExamples = debounce(event => { let exampleNumber; const data = event.dataset; @@ -340,7 +347,7 @@ const handleExamples = debounce(event => { } }, 10); -function initCanvas() { // eslint-disable-line +function initCanvas() { // eslint-disable-line no-unused-vars const canvas = document.getElementById("meme-canvas"); const canvasHeight = 600; const canvasWidth = 800; diff --git a/app/components/ecosystem.js b/app/components/ecosystem.js deleted file mode 100644 index 585b518..0000000 --- a/app/components/ecosystem.js +++ /dev/null @@ -1,43 +0,0 @@ -"use strict"; - - - -// P A C K A G E - -const local = require("app-root-path").require; - -// U T I L S - -const applications = local("/app/components/ecosystem/module-applications"); -const chainquery = local("/app/components/ecosystem/submodule-chainquery"); -const lbry = local("/app/components/ecosystem/module-lbry"); -const lbrycrd = local("/app/components/ecosystem/module-lbrycrd"); -const lighthouse = local("/app/components/ecosystem/submodule-lighthouse"); -const reflector = local("/app/components/ecosystem/submodule-reflector"); -const wallet = local("/app/components/ecosystem/submodule-wallet"); - - - -// E X P O R T - -export default () => { - return ` -
- - -
- ${lbrycrd()} - ${lbry()} - ${applications()} -
- - -
- `; -}; diff --git a/app/components/ecosystem/index.js b/app/components/ecosystem/index.js new file mode 100644 index 0000000..4b0be3c --- /dev/null +++ b/app/components/ecosystem/index.js @@ -0,0 +1,27 @@ +"use strict"; + + + +// U T I L S + +import applications from "./module-applications"; +import chainquery from "./submodule-chainquery"; +import lbry from "./module-lbry"; +import lbrycrd from "./module-lbrycrd"; +import lighthouse from "./submodule-lighthouse"; +import reflector from "./submodule-reflector"; +import wallet from "./submodule-wallet"; + + + +// E X P O R T S + +export { + applications, + chainquery, + lbry, + lbrycrd, + lighthouse, + reflector, + wallet +}; diff --git a/app/components/ecosystem/module-applications.js b/app/components/ecosystem/module-applications.js index 72574ba..2f8cdd0 100644 --- a/app/components/ecosystem/module-applications.js +++ b/app/components/ecosystem/module-applications.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `
× diff --git a/app/components/ecosystem/module-lbry.js b/app/components/ecosystem/module-lbry.js index 561bf8f..4d7f9fc 100644 --- a/app/components/ecosystem/module-lbry.js +++ b/app/components/ecosystem/module-lbry.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `
× diff --git a/app/components/ecosystem/module-lbrycrd.js b/app/components/ecosystem/module-lbrycrd.js index 21524ab..b80ab93 100644 --- a/app/components/ecosystem/module-lbrycrd.js +++ b/app/components/ecosystem/module-lbrycrd.js @@ -14,7 +14,7 @@ const markdown = local("/app/components/markdown").default; // E X P O R T -module.exports = exports = () => ` +export default () => `
× diff --git a/app/components/ecosystem/submodule-chainquery.js b/app/components/ecosystem/submodule-chainquery.js index 724b06f..0383df7 100644 --- a/app/components/ecosystem/submodule-chainquery.js +++ b/app/components/ecosystem/submodule-chainquery.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `

chainquery

diff --git a/app/components/ecosystem/submodule-lighthouse.js b/app/components/ecosystem/submodule-lighthouse.js index a3d46e1..17332a7 100644 --- a/app/components/ecosystem/submodule-lighthouse.js +++ b/app/components/ecosystem/submodule-lighthouse.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `

lighthouse

diff --git a/app/components/ecosystem/submodule-reflector.js b/app/components/ecosystem/submodule-reflector.js index 6f1a767..8507b89 100644 --- a/app/components/ecosystem/submodule-reflector.js +++ b/app/components/ecosystem/submodule-reflector.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `

reflector

diff --git a/app/components/ecosystem/submodule-wallet.js b/app/components/ecosystem/submodule-wallet.js index 47509b8..eba334e 100644 --- a/app/components/ecosystem/submodule-wallet.js +++ b/app/components/ecosystem/submodule-wallet.js @@ -4,7 +4,7 @@ // E X P O R T -module.exports = exports = () => ` +export default () => `

wallet server

diff --git a/app/components/edit-link.js b/app/components/edit-link.js index 88d9342..9251edf 100644 --- a/app/components/edit-link.js +++ b/app/components/edit-link.js @@ -2,7 +2,7 @@ -// P A C K A G E S +// I M P O R T S import html from "choo/html"; import { require as local } from "app-root-path"; diff --git a/app/components/email-subscribe.js b/app/components/email-subscribe.js index b205998..1406cb6 100644 --- a/app/components/email-subscribe.js +++ b/app/components/email-subscribe.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; diff --git a/app/components/footer.js b/app/components/footer.js index 01ee284..fdae9bf 100644 --- a/app/components/footer.js +++ b/app/components/footer.js @@ -2,16 +2,17 @@ -// P A C K A G E S +// I M P O R T S import html from "choo/html"; import { require as local } from "app-root-path"; // U T I L S +import editLink from "./edit-link"; +import emailSubscribe from "./email-subscribe"; + const config = local("/config"); -const editLink = local("/app/components/edit-link").default; -const emailSubscribe = local("/app/components/email-subscribe").default; diff --git a/app/components/glossary-toc.js b/app/components/glossary-toc.js index e49fde5..b94d4e5 100644 --- a/app/components/glossary-toc.js +++ b/app/components/glossary-toc.js @@ -2,7 +2,7 @@ -// V A R I A B L E S +// U T I L S const idRegex = /(".*")/g; const numberRegex = /^[0-9]/g; @@ -13,7 +13,7 @@ const titleRegex = /(>.*<)/g; // E X P O R T -module.exports = exports = (state, emit, markdown) => { +export default (state, emit, markdown) => { const collectionOfTocElements = []; const tocElements = markdown.match(renderedHeaderRegex); diff --git a/app/components/head.js b/app/components/head.js index 3496530..3cd6834 100644 --- a/app/components/head.js +++ b/app/components/head.js @@ -2,7 +2,7 @@ -// P A C K A G E S +// I M P O R T S import html from "choo/html"; import { require as local } from "app-root-path"; diff --git a/app/components/link-grid.js b/app/components/link-grid.js index 561672e..0c821d0 100644 --- a/app/components/link-grid.js +++ b/app/components/link-grid.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; @@ -11,7 +11,7 @@ import html from "choo/html"; // E X P O R T export default links => { - const renderedLinks = links.map((link) => + const renderedLinks = links.map(link => returnLinkTemplate(link.title, link.description, link.destination, link.label)); return html` diff --git a/app/components/markdown.js b/app/components/markdown.js index ba48286..8ba9542 100644 --- a/app/components/markdown.js +++ b/app/components/markdown.js @@ -2,7 +2,7 @@ -// P A C K A G E S +// I M P O R T S import decamelize from "decamelize"; import exists from "fs-exists-sync"; @@ -13,12 +13,9 @@ import path from "path"; import raw from "choo/html/raw"; import { require as local } from "app-root-path"; -// V A R I A B L E +// U T I L S const numberRegex = /^[0-9]/g; - -// U T I L - const md = require("markdown-it")({ html: true, typographer: true @@ -67,7 +64,7 @@ function partialFinder(markdownBody) { const filename = decamelize(partial, "-").replace("<", "") .replace("/>", "") .trim(); - const fileExistsTest = exists(`./app/components/${filename}.js`); // `local` results in error if used here and file !exist + const fileExistsTest = exists(`./app/components/${filename}.js`); if (!fileExistsTest) markdownBody = markdownBody.replace(partial, ""); diff --git a/app/components/mission-statement.js b/app/components/mission-statement.js index 3a0fecb..34b4a78 100644 --- a/app/components/mission-statement.js +++ b/app/components/mission-statement.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; diff --git a/app/components/navigation.js b/app/components/navigation.js index e5e9fc7..64bad97 100644 --- a/app/components/navigation.js +++ b/app/components/navigation.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; diff --git a/app/components/overview.js b/app/components/overview.js new file mode 100644 index 0000000..2f523dc --- /dev/null +++ b/app/components/overview.js @@ -0,0 +1,39 @@ +"use strict"; + + + +// U T I L S + +import { + applications, + chainquery, + lbry, + lbrycrd, + lighthouse, + reflector, + wallet +} from "./ecosystem"; + + + +// E X P O R T + +export default () => ` +
+ + +
+ ${lbrycrd()} + ${lbry()} + ${applications()} +
+ + +
+`; diff --git a/app/components/playground.js b/app/components/playground.js index 37f6371..80a4e78 100644 --- a/app/components/playground.js +++ b/app/components/playground.js @@ -2,7 +2,7 @@ -// P A C K A G E S +// I M P O R T S import dedent from "dedent"; import html from "choo/html"; diff --git a/app/components/resources-link-grid.js b/app/components/resources-link-grid.js index 9bdf332..0db1251 100644 --- a/app/components/resources-link-grid.js +++ b/app/components/resources-link-grid.js @@ -2,13 +2,9 @@ -// P A C K A G E - -import { require as local } from "app-root-path"; - // U T I L -const linkGrid = local("/app/components/link-grid").default; +import linkGrid from "./link-grid"; diff --git a/app/components/wrapper.js b/app/components/wrapper.js index 2ef0690..8f7ef0a 100644 --- a/app/components/wrapper.js +++ b/app/components/wrapper.js @@ -2,30 +2,31 @@ -// P A C K A G E S +// I M P O R T import asyncHtml from "choo-async/html"; -import { require as local } from "app-root-path"; // U T I L S -const footer = local("/app/components/footer").default; -const navigation = local("/app/components/navigation").default; +import footer from "./footer"; +import navigation from "./navigation"; // E X P O R T -export default children => (state, emit) => asyncHtml` -
- +export default children => (state, emit) => { + return asyncHtml` +
+ - ${navigation(state.href)} - - ${children(state, emit)} - ${footer(state, emit)} -
-`; + ${navigation(state.href)} + + ${children.default(state, emit)} + ${footer(state, emit)} +
+ `; +}; diff --git a/app/helpers/fetch-metadata.js b/app/helpers/fetch-metadata.js index 6084655..7fe773a 100644 --- a/app/helpers/fetch-metadata.js +++ b/app/helpers/fetch-metadata.js @@ -2,21 +2,20 @@ -// P A C K A G E S +// I M P O R T S -const got = require("got"); -const loadLanguages = require("prismjs/components/"); -const local = require("app-root-path").require; -const prism = require("prismjs"); -const raw = require("choo/html/raw"); -const stringifyObject = require("stringify-object"); +import got from "got"; +import prism from "prismjs"; +import raw from "choo/html/raw"; +import stringifyObject from "stringify-object"; // U T I L S -const randomString = local("/app/helpers/random-string"); -const logSlackError = local("/app/helpers/slack"); -const publishMeme = local("/app/helpers/publish-meme"); -const uploadImage = local("/app/helpers/upload-image"); +import randomString from "./random-string"; +import messageSlack from "./slack"; + +import publishMeme from "./publish-meme"; +import uploadImage from "./upload-image"; const allowedQueryMethods = [ "publish", @@ -37,80 +36,122 @@ const approvedContentIdsForTipping = [ "8a7401b88d5ed0376d98f16808194d4dcb05b284" ]; -loadLanguages(["json"]); +// P A C K A G E + +const loadLanguages = require("prismjs/components/"); +loadLanguages(["json"]); // eslint-disable-line padding-line-between-statements // E X P O R T -module.exports = exports = async(data, socket) => { +export default async(data, socket) => { + const body = {}; + let apiRequestMethod = ""; let dataDetails = ""; + let explorerNotice = ""; if (data.example === 1 && !data.claim || !data.method) return; if (data.example === 2 && !data.data) return; if (data.example === 2) dataDetails = data.data; // file upload if (data.example === 3 && !data.claim || !data.method) return; - const body = {}; const claimAddress = data.claim; const resolveMethod = data.method; - let apiRequestMethod = ""; - if (allowedQueryMethods.indexOf(resolveMethod) < 0) return socket.send(JSON.stringify({ - details: "Unallowed resolve method for tutorial", - message: "notification", - type: "error" - })); + if (allowedQueryMethods.indexOf(resolveMethod) < 0) { + return socket.send(JSON.stringify({ + details: "Unallowed resolve method for tutorial", + message: "notification", + type: "error" + })); + } - body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; // access_token + body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; body.method = resolveMethod; - // P U B L I S H - // E X A M P L E - - if (resolveMethod === "publish") { - apiRequestMethod = "PUT"; - - // Required for publishing - body.author = "lbry.tech"; - body.bid = "0.001"; // Hardcoded publish amount - body.description = dataDetails.description; - body.language = dataDetails.language; - body.license = dataDetails.license; - body.name = dataDetails.name + "-" + randomString(10); - body.nsfw = dataDetails.nsfw; - body.title = dataDetails.title; - - // Gotta let the blockchain know what to save - body.file_path = dataDetails.file_path; - - return uploadImage(body.file_path).then(uploadResponse => { - if (!uploadResponse.status || uploadResponse.status !== "ok") { - socket.send(JSON.stringify({ - details: "Image upload failed", - message: "notification", - type: "error" + switch(true) { + // T I P + // E X A M P L E + case resolveMethod === "claim_tip": + if (!approvedContentIdsForTipping.includes(claimAddress)) { + return socket.send(JSON.stringify({ + example: data.example, + html: raw(` +

Response

+
Tipping creators not in the whitelist for this example is not allowed.
+ `), + message: "show result", + selector: `#example${data.example}-result` })); - - if (process.env.NODE_ENV !== "development") { - logSlackError( - "\n" + - "> *DAEMON ERROR:*\n" + - "> _Cause: Someone attempted to upload a meme to the web daemon_\n" - ); - } - - return; } - body.file_path = uploadResponse.filename; + apiRequestMethod = "POST"; + body.amount = "0.001"; // Hardcoded tip amount + body.claim_id = claimAddress; - return publishMeme(body).then(publishResponse => { - let explorerNotice = ""; + break; - if (publishResponse.error) { + + + // P U B L I S H + // E X A M P L E + case resolveMethod === "publish": + apiRequestMethod = "PUT"; + + // Required for publishing + body.author = "lbry.tech"; + body.bid = "0.001"; // Hardcoded publish amount + body.description = dataDetails.description; + body.language = dataDetails.language; + body.license = dataDetails.license; + body.name = dataDetails.name + "-" + randomString(10); + body.nsfw = dataDetails.nsfw; + body.title = dataDetails.title; + + // Gotta let the blockchain know what to save + body.file_path = dataDetails.file_path; + + try { + const imageUploadResponse = await uploadImage(body.file_path); + body.file_path = imageUploadResponse.filename; // eslint-disable-line padding-line-between-statements + + try { + const memePublishResponse = await publishMeme(body); + + switch(true) { + case memePublishResponse.result: + case memePublishResponse.result.claim_address: + explorerNotice = memePublishMessaging(memePublishResponse); + break; + + default: + break; + } + + delete memePublishResponse.result.lbrytech_claim_name; + + const renderedCode = prism.highlight( + stringifyObject(memePublishResponse, { indent: " ", singleQuotes: false }), + prism.languages.json, + "json" + ); + + return socket.send(JSON.stringify({ + example: data.example, + html: raw(` +

Response

+ ${explorerNotice} +
${renderedCode}
+ `), + message: "show result", + selector: `#example${data.example}-result` + })); + } + + catch(memePublishError) { socket.send(JSON.stringify({ details: "Meme publish failed", message: "notification", @@ -118,81 +159,49 @@ module.exports = exports = async(data, socket) => { })); if (process.env.NODE_ENV !== "development") { - logSlackError( - "\n" + - "> *DAEMON ERROR:* ```" + JSON.parse(JSON.stringify(publishResponse.error)) + "```" + "\n" + - "> _Cause: Someone is going through the Playground after a response has been parsed_\n" - ); + messageSlack({ + message: "```" + JSON.parse(JSON.stringify(memePublishError.error)) + "```", + pretext: "_Someone is going through the Playground after a response has been parsed_", + title: "DAEMON ERROR" + }); } return; } + } - if ( - publishResponse.result && - publishResponse.result.claim_address - ) explorerNotice = ` -

- Nicely done, you've published to lbry://${publishResponse.result.lbrytech_claim_name}. -

- To see Proof of Work (lol) that your meme is on the LBRY blockchain, check it out on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed. -

- You can also check out your meme (once the transaction is confirmed) on LBRY or Spee.ch! -

- -
- `; - - delete publishResponse.result.lbrytech_claim_name; - - const renderedCode = prism.highlight( - stringifyObject(publishResponse, { indent: " ", singleQuotes: false }), - prism.languages.json, - "json" - ); - - return socket.send(JSON.stringify({ - example: data.example, - html: raw(` -

Response

- ${explorerNotice} -
${renderedCode}
- `), - message: "show result", - selector: `#example${data.example}-result` + catch(imageUploadError) { + socket.send(JSON.stringify({ + details: "Image upload failed", + message: "notification", + type: "error" })); - }); - }); - } - // R E S O L V E - // E X A M P L E + if (process.env.NODE_ENV !== "development") { + messageSlack({ + message: "```" + imageUploadError.status + "```", + pretext: "_Someone attempted to upload a meme to the web daemon and it failed_", + title: "DAEMON ERROR" + }); + } - if (resolveMethod === "resolve") { - apiRequestMethod = "GET"; - body.uri = claimAddress; - } + return; + } - // T I P - // E X A M P L E - if (resolveMethod === "claim_tip") { - if (!approvedContentIdsForTipping.includes(claimAddress)) { - return socket.send(JSON.stringify({ - example: data.example, - html: raw(` -

Response

-
Tipping creators not in the whitelist for this example is not allowed.
- `), - message: "show result", - selector: `#example${data.example}-result` - })); - } - apiRequestMethod = "POST"; + // R E S O L V E + // E X A M P L E + case resolveMethod === "resolve": + apiRequestMethod = "GET"; + body.uri = claimAddress; - body.amount = "0.001"; // Hardcoded tip amount - body.claim_id = claimAddress; + break; + + + + default: + break; } @@ -206,19 +215,23 @@ module.exports = exports = async(data, socket) => { method: apiRequestMethod }; - const queryUrl = `${process.env.NODE_ENV === "development" ? `http://localhost:5200/${resolveMethod}` : `https://${process.env.DAEMON_URL}/${resolveMethod}`}`; + const queryUrl = process.env.NODE_ENV === "development" ? + `http://localhost:5200/${resolveMethod}` : + `https://${process.env.DAEMON_URL}/${resolveMethod}`; try { const response = await got(queryUrl, queryOptions); - let explorerNotice = ""; - if ( - data.example === 3 && - response.body.result && - response.body.result.txid - ) explorerNotice = ` -

If you want proof of the tip you just gave on behalf of LBRY, check it out on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed.


- `; + switch(true) { + case data.example === 3: + case response.body.result: + case response.body.result.txid: + explorerNotice = tipCompletionMessaging(response.body); + break; + + default: + break; + } if (socket) { const renderedCode = prism.highlight( @@ -240,11 +253,44 @@ module.exports = exports = async(data, socket) => { } return response.body.result[Object.keys(response.body.result)[0]].claim; - } catch (error) { - logSlackError( - "\n" + - "> *DAEMON ERROR:* ```" + error + "```" + "\n" + - "> _Cause: Someone is going through the Playground_\n" - ); + } + + catch(error) { + messageSlack({ + message: "```" + error + "```", + pretext: "_Someone is going through the Playground and the daemon is not running_", + title: "DAEMON ERROR" + }); } }; + + + +// H E L P E R S + +function memePublishMessaging(source) { + return ` +

+ Nicely done, you've published to lbry://${source.result.lbrytech_claim_name}. + +

+ + To see Proof of Work (lol) that your meme is on the LBRY blockchain, check it out on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed. + +

+ + You can also check out your meme (once the transaction is confirmed) on LBRY or Spee.ch! +

+ +
+ `; +} + +function tipCompletionMessaging(source) { + return ` +

+ If you want proof of the tip you just gave on behalf of LBRY, check it out on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed. +


+ `; +} + diff --git a/app/helpers/github.js b/app/helpers/github.js index bee1127..6c1cffc 100644 --- a/app/helpers/github.js +++ b/app/helpers/github.js @@ -12,7 +12,7 @@ const redis = require("redis"); // U T I L S -const logSlackError = local("/app/helpers/slack"); +const messageSlack = local("/app/helpers/slack"); const relativeDate = local("/app/modules/relative-date"); // R E D I S @@ -32,7 +32,7 @@ if (typeof process.env.REDISCLOUD_URL !== "undefined") { client.on("error", redisError => { process.env.NODE_ENV === "development" ? process.stdout.write(`\n${color.yellow("Unable to connect to Redis client.")}\nYou may be missing an .env file or your connection was reset.`) : - logSlackError( + messageSlack( "\n" + "> *REDIS ERROR:* ```" + JSON.parse(JSON.stringify(redisError)) + "```" + "\n" + "> _Cause: Someone is trying to run LBRY.tech locally without environment variables OR Heroku is busted_\n" @@ -342,7 +342,7 @@ function updateGithubFeed() { }, () => client.zremrangebyrank("events", 0, -51)); // Keep the latest 50 events }) .catch(err => { - logSlackError( + messageSlack( "\n" + "> *GITHUB FEED ERROR:* ```" + JSON.parse(JSON.stringify(err)) + "```" + "\n" + "> _Cause: GitHub feed refresh_\n" @@ -362,7 +362,7 @@ function refToBranch(ref) { // E X P O R T S -module.exports = exports = { +export { generateEvent, generateGitHubFeed, generateUrl, diff --git a/app/helpers/publish-meme.js b/app/helpers/publish-meme.js index 33a08c4..358273d 100644 --- a/app/helpers/publish-meme.js +++ b/app/helpers/publish-meme.js @@ -2,30 +2,31 @@ -// P A C K A G E +// I M P O R T -const got = require("got"); +import got from "got"; // U T I L -const queryUrl = `${process.env.NODE_ENV === "development" ? "http://localhost:5200/publish" : `https://${process.env.DAEMON_URL}/publish`}`; +const queryUrl = process.env.NODE_ENV === "development" ? + "http://localhost:5200/publish" : + `https://${process.env.DAEMON_URL}/publish`; // E X P O R T -module.exports = exports = async(publishMetadata) => { +export default async(publishMetadata) => { const options = { body: { authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN, metadata: publishMetadata }, - json: true, - method: "PUT" + json: true }; try { - const response = await got(queryUrl, options); + const response = await got.put(queryUrl, options); return response.body; // eslint-disable-line padding-line-between-statements } catch (error) { return error; diff --git a/app/helpers/random-string.js b/app/helpers/random-string.js index 33bc691..20911fe 100644 --- a/app/helpers/random-string.js +++ b/app/helpers/random-string.js @@ -4,13 +4,13 @@ // N A T I V E -const crypto = require("crypto"); +import crypto from "crypto"; // E X P O R T -module.exports = exports = len => { +export default len => { if (!Number.isFinite(len)) throw new TypeError("Expected a finite number"); return crypto.randomBytes(Math.ceil(len / 2)).toString("hex") .slice(0, len); diff --git a/app/helpers/slack.js b/app/helpers/slack.js index ef7cf30..a683da8 100644 --- a/app/helpers/slack.js +++ b/app/helpers/slack.js @@ -1,30 +1,41 @@ -"use strict"; require("dotenv").config(); +"use strict"; +// I M P O R T + +import { IncomingWebhook } from "@slack/client"; + // U T I L S -let Slack; -let slack; +require("dotenv").config(); -if (typeof process.env.SLACK_WEBHOOK_URL !== "undefined") { - Slack = require("slack-node"); - slack = new Slack(); - slack.setWebhook(process.env.SLACK_WEBHOOK_URL); -} +const environmentMessage = process.env.NODE_ENV === "development" ? + "\n_— in DEVELOPMENT_" : + "\n_— in PRODUCTION_"; + +const slackUrl = process.env.SLACK_WEBHOOK_URL || ""; +const slackWebhook = new IncomingWebhook(slackUrl); // P R O G R A M -module.exports = exports = text => { - if (typeof slack === "undefined") return; +export default ({ message, pretext, title }) => { + if (!slackUrl) return; + pretext = pretext + environmentMessage; - slack.webhook({ - channel: "dottech-errors", - username: "lbrytech-bot", - text: text - }, (err, response) => { // eslint-disable-line - // do nothing? + slackWebhook.send({ + attachments: [{ + mrkdwn_in: [ + "text", + "pretext" + ], + pretext: pretext || "", + text: message || "", + title: title || "" + }] + }, (error, response) => { // eslint-disable-line no-unused-vars + if (error) console.log(error); // eslint-disable-line no-console }); }; diff --git a/app/helpers/upload-image.js b/app/helpers/upload-image.js index b3cfc83..5239fe1 100644 --- a/app/helpers/upload-image.js +++ b/app/helpers/upload-image.js @@ -2,30 +2,31 @@ -// P A C K A G E +// I M P O R T -const got = require("got"); +import got from "got"; // U T I L -const queryUrl = `${process.env.NODE_ENV === "development" ? "http://localhost:5200/image" : `https://${process.env.DAEMON_URL}/image` }`; +const queryUrl = process.env.NODE_ENV === "development" ? + "http://localhost:5200/image" : + `https://${process.env.DAEMON_URL}/image`; // E X P O R T -module.exports = exports = async(imageSource) => { +export default async(imageSource) => { const options = { body: { authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN, image: imageSource }, - json: true, - method: "POST" + json: true }; try { - const response = await got(queryUrl, options); + const response = await got.post(queryUrl, options); return response.body; // eslint-disable-line padding-line-between-statements } catch (error) { return error; diff --git a/app/sockets.js b/app/sockets.js index 5463e58..f4ac927 100644 --- a/app/sockets.js +++ b/app/sockets.js @@ -2,31 +2,30 @@ -// P A C K A G E S +// I M P O R T S -const got = require("got"); -const html = require("choo/html"); -const local = require("app-root-path").require; +import got from "got"; +import html from "choo/html"; // U T I L S -const fetchMetadata = local("/app/helpers/fetch-metadata"); -const { generateGitHubFeed } = local("/app/helpers/github"); -const logSlackError = local("/app/helpers/slack"); +import fetchMetadata from "./helpers/fetch-metadata"; +import { generateGitHubFeed } from "./helpers/github"; +import messageSlack from "./helpers/slack"; // P R O G R A M -module.exports = exports = (socket, action) => { +export default (socket, action) => { if (typeof socket !== "object" && typeof action !== "object") return; switch(true) { - case (action.message === "fetch metadata"): + case action.message === "fetch metadata": fetchMetadata(action, socket); break; - case (action.message === "landed on homepage"): + case action.message === "landed on homepage": generateGitHubFeed(result => { socket.send(JSON.stringify({ html: result, @@ -36,7 +35,7 @@ module.exports = exports = (socket, action) => { }); break; - case (action.message === "landed on playground"): + case action.message === "landed on playground": generateContent(1, result => { socket.send(JSON.stringify({ html: result, @@ -46,7 +45,7 @@ module.exports = exports = (socket, action) => { }); break; - case (action.message === "request for playground, example 1"): + case action.message === "request for playground, example 1": generateContent(1, result => { socket.send(JSON.stringify({ html: result, @@ -56,11 +55,11 @@ module.exports = exports = (socket, action) => { }); break; - case (action.message === "request for playground, example 2"): + case action.message === "request for playground, example 2": generateMemeCreator(socket); break; - case (action.message === "request for playground, example 3"): + case action.message === "request for playground, example 3": generateContent(3, result => { socket.send(JSON.stringify({ html: result, @@ -70,12 +69,11 @@ module.exports = exports = (socket, action) => { }); break; - case (action.message === "subscribe"): + case action.message === "subscribe": newsletterSubscribe(action, socket); break; default: - console.log(action); // eslint-disable-line break; } }; @@ -341,7 +339,7 @@ async function newsletterSubscribe(data, socket) { const response = JSON.parse(error.body); if (!response.success) { - logSlackError( + messageSlack( "\n" + "> *NEWSLETTER ERROR:* ```" + response.error + "```" + "\n" + `> _Cause: ${email} interacted with the form_\n` @@ -355,7 +353,7 @@ async function newsletterSubscribe(data, socket) { })); } - logSlackError( + messageSlack( "\n" + "> *NEWSLETTER ERROR:* ```¯\\_(ツ)_/¯ This should be an unreachable error```" + "\n" + `> _Cause: ${email} interacted with the form_\n` diff --git a/app/views/404.js b/app/views/404.js index 147587d..328b8a0 100644 --- a/app/views/404.js +++ b/app/views/404.js @@ -2,7 +2,7 @@ -// P A C K A G E +// I M P O R T import html from "choo/html"; @@ -10,7 +10,7 @@ import html from "choo/html"; // E X P O R T -module.exports = exports = () => html` +export default () => html`