diff --git a/package.json b/package.json index b839f32..6cdc3a3 100755 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "choo-devtools": "^2.5.1", "nodemon": "^1.18.3", "npm-run-all": "^4.1.3", - "sass": "^1.10.0", + "sass": "^1.10.1", "snazzy": "^7.1.1", "standardx": "^2.1.0", "updates": "^4.1.0" diff --git a/server.js b/server.js index 6a182e0..eac8484 100755 --- a/server.js +++ b/server.js @@ -135,6 +135,123 @@ start(); // H E L P E R S +function fetchMetadata(data, socket) { + let dataDetails = ""; + + if (data.step === 1 && !data.claim || !data.method) return; + if (data.step === 2 && !data.data) return; + if (data.step === 2) dataDetails = data.data; + + const claimAddress = data.claim; + const resolveMethod = data.method; + + const allowedClaims = [ + "fortnite-top-stream-moments-nickatnyte", + "itsadisaster", + "six", + "unbubbled1-1" + ]; + + const allowedMethods = [ + "publish", + "resolve", + "wallet_send" + ]; + + if (allowedMethods.indexOf(resolveMethod) < 0) return socket.send(JSON.stringify({ + "details": "Unallowed resolve method for tutorial", + "message": "notification", + "type": "error" + })); + + if (data.step === 1 && allowedClaims.indexOf(claimAddress) < 0) return socket.send(JSON.stringify({ + "details": "Invalid claim ID for tutorial", + "message": "notification", + "type": "error" + })); + + const body = {}; + + body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN; + body.method = resolveMethod; + if (data.step === 1) body.uri = claimAddress; + + if (resolveMethod === "publish") { + body.bid = 0.001; // Hardcoded publish amount + body.description = dataDetails.description; + body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + dataDetails.file_path; // TODO: Fix the internal image path in daemon (original comment, check to see if still true) + body.language = dataDetails.language; + body.license = dataDetails.license; + body.name = dataDetails.name; + body.nsfw = dataDetails.nsfw; + body.title = dataDetails.title; + + return uploadImage(body.file_path).then(uploadResponse => { + if (uploadResponse.status !== "ok") return; + + body.file_path = uploadResponse.filename; + body.method = resolveMethod; + + // console.log(body); + + return new Promise((resolve, reject) => { + request({ + qs: body, + url: "http://daemon.lbry.tech/images.php" + }, (error, response, body) => { + if (error) reject(error); + body = JSON.parse(body); + resolve(body); + }); + }); + }).catch(uploadError => { + // component.isLoading = false; + // component.jsonData = JSON.stringify(uploadError, null, " "); + + socket.send(JSON.stringify({ + "details": "Image upload failed", + "message": "notification", + "type": "error" + })); + + logSlackError("[daemon error]\n", "```" + JSON.stringify(uploadError) + "```"); + return; + }); + } + + return new Promise((resolve, reject) => { + request({ + url: "http://daemon.lbry.tech", + qs: body + }, (error, response, body) => { + if (error) { + reject(error); + logSlackError("[daemon error]\n", "```" + JSON.stringify(error) + "```"); + return; + } + + body = JSON.parse(body); + + if (typeof body.error !== "undefined") { + reject(body.error); + logSlackError("[daemon error]\n", "```" + JSON.stringify(body.error) + "```"); + return; + } + + socket.send(JSON.stringify({ + "html": html` +

Success! Here is the response for lbry://${claimAddress}:

+
${stringifyObject(body, { indent: "  ", singleQuotes: false })}
+ + + `, + "message": "updated html", + "selector": "#step1-result" + })); + }); + }); +} + function generateGitHubFeed(displayGitHubFeed) { if (typeof process.env.REDISCLOUD_URL !== "undefined") { client.zrevrange("events", 0, 9, (err, reply) => { @@ -173,86 +290,22 @@ function generateGitHubFeed(displayGitHubFeed) { } } - - -function uploadImage(imageSource, callback) { - // return upload response -} - -// TODO: Data parameter should include which step of tour is requesting metadata -function fetchMetadata(data, socket) { - if (data.step === 1 && !data.claim || !data.method) return; - - const claimAddress = data.claim; - const resolveMethod = data.method; - - const allowedClaims = [ - "fortnite-top-stream-moments-nickatnyte", - "itsadisaster", - "six", - "unbubbled1-1" - ]; - - const allowedMethods = [ - "publish", - "resolve", - "wallet_send" - ]; - - if (!allowedMethods.includes(resolveMethod)) return socket.send(JSON.stringify({ - "details": "Unallowed resolve method for tutorial", - "message": "notification", - "type": "error" - })); - - if (!allowedClaims.includes(claimAddress)) return socket.send(JSON.stringify({ - "details": "Invalid claim ID for tutorial", - "message": "notification", - "type": "error" - })); - - const body = {}; - - if (resolveMethod === "publish") { - body.bid = 0.001; // Hardcode the publish amount - - // Fix the internal image path in daemon - // body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + body.file_path; // TODO: needed for step 2, check for `file_path` - } - - body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN; - body.method = resolveMethod; - body.uri = claimAddress; - +function uploadImage(imageSource) { return new Promise((resolve, reject) => { request({ - url: "http://daemon.lbry.tech", - qs: body + body: imageSource, + headers: { + "Content-Type": "text/plain" + }, + method: "PUT", + qs: { + access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN + }, + url: "http://daemon.lbry.tech/images.php" }, (error, response, body) => { - if (error) { - reject(error); - logSlackError("[daemon error]\n", "```" + JSON.stringify(error) + "```"); - return; - } - + if (error) reject(error); body = JSON.parse(body); - - if (typeof body.error !== "undefined") { - reject(body.error); - logSlackError("[daemon error]\n", "```" + JSON.stringify(body.error) + "```"); - return; - } - - socket.send(JSON.stringify({ - "html": html` -

Success! Here is the response for lbry://${claimAddress}:

-
${stringifyObject(body, { indent: "  ", singleQuotes: false })}
- - - `, - "message": "updated html", - "selector": "#step1-result" - })); + resolve(body); }); }); } diff --git a/views/partials/tour-scripts.js b/views/partials/tour-scripts.js index 31fe07d..f1cc668 100644 --- a/views/partials/tour-scripts.js +++ b/views/partials/tour-scripts.js @@ -100,7 +100,6 @@ function fetchMetadata(stepNumber, data) { - Style code with highlightjs */ - console.log(typeof data); if (!stepNumber) return; switch(stepNumber) { @@ -128,7 +127,13 @@ function fetchMetadata(stepNumber, data) { break; case 2: - console.log(stepNumber, data); + send(JSON.stringify({ + "data": data, + "message": "fetch metadata", + "method": "publish", + "step": stepNumber + })); + break; default: @@ -136,21 +141,7 @@ function fetchMetadata(stepNumber, data) { } } -function getMemeInfo() { - /* - - description: component.description, - - file_path: uploadResponse.body.filename, - - language: component.language, - - license: component.license, - - name: component.title, - - nsfw: component.nsfw, - - title: component.title - - // Set on back-end - - bid: 0.001, - - method: "publish" - */ - +function getMemeInfo() { // TODO: Error handling const info = {}; info.description = $("#meme-description").val(); @@ -158,7 +149,7 @@ function getMemeInfo() { info.language = $("#meme-language").val(); info.license = $("#meme-license").val(); info.name = $("#meme-title").val(); - info.nsfw = $("#meme-nsfw-flag").val(); + info.nsfw = $("#meme-nsfw-flag")[0].checked; info.title = $("#meme-title").val(); return info; @@ -220,9 +211,3 @@ function updateCanvas(imageSource) { ctx.fillText($("#meme-top-line").val().toUpperCase(), canvasWidth / 2, 20); ctx.fillText($("#meme-bottom-line").val().toUpperCase(), canvasWidth / 2, (canvasHeight - 40)); } - - - -send(JSON.stringify({ // TODO: Remove this - "message": "Landed on Tour" -}));