"use strict"; // P A C K A G E 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"); // 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"); const allowedQueryMethods = [ "publish", "resolve", "claim_tip" ]; const approvedContentIdsForTipping = [ "3db81c073f82fd1bb670c65f526faea3b8546720", "173412f5b1b7aa63a752e8832406aafd9f1ecb4e", "2a7f5db2678177435b1dee6c9e38e035ead450b6", "d81bac6d49b1f92e58c37a5f633a27a45b43405e", "b4668c0bd096317b44c40738c099b6618095e75f", "007789cc45cbb4255cf02ba77cbf84ca8e3d7561", "1ac47b8b3def40a25850dc726a09ce23d09e7009", "784b3c215a6f06b663fc1aa292bcb19f29c489bb", "758dd6497cdfc401ae1f25984738d024d47b50af", "8a7401b88d5ed0376d98f16808194d4dcb05b284" ]; loadLanguages(["json"]); // E X P O R T module.exports = exports = async(data, socket) => { let dataDetails = ""; 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" })); body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; // 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" })); 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; return publishMeme(body).then(publishResponse => { let explorerNotice = ""; if (publishResponse.error) { socket.send(JSON.stringify({ details: "Meme publish failed", message: "notification", type: "error" })); 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" ); } 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` })); }); }); } // R E S O L V E // E X A M P L E if (resolveMethod === "resolve") { apiRequestMethod = "GET"; body.uri = claimAddress; } // 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"; body.amount = "0.001"; // Hardcoded tip amount body.claim_id = claimAddress; } // Q U E R Y // D A E M O N const queryOptions = { body: body, json: true, method: apiRequestMethod }; 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.


`; if (socket) { const renderedCode = prism.highlight( stringifyObject(response.body, { 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` })); } 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" ); } };