2018-08-08 01:15:34 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
|
|
|
|
const local = require("app-root-path").require;
|
2018-08-08 22:20:53 +02:00
|
|
|
const prism = require("prismjs");
|
2018-08-29 01:57:18 +02:00
|
|
|
const raw = require("choo/html/raw");
|
2018-08-08 01:15:34 +02:00
|
|
|
const request = require("request-promise-native");
|
|
|
|
const stringifyObject = require("stringify-object");
|
|
|
|
|
|
|
|
// V A R I A B L E S
|
|
|
|
|
2018-09-27 18:11:26 +02:00
|
|
|
const randomString = local("app/helpers/random-string");
|
2018-08-08 22:20:53 +02:00
|
|
|
const loadLanguages = require("prismjs/components/");
|
2018-09-27 18:11:26 +02:00
|
|
|
const logSlackError = local("app/helpers/slack");
|
|
|
|
const publishMeme = local("app/helpers/publish-meme");
|
|
|
|
const uploadImage = local("app/helpers/upload-image");
|
2018-08-08 01:15:34 +02:00
|
|
|
|
2018-08-08 22:20:53 +02:00
|
|
|
loadLanguages(["json"]);
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
// E X P O R T
|
|
|
|
|
|
|
|
module.exports = exports = (data, socket) => {
|
|
|
|
let dataDetails = "";
|
|
|
|
|
2018-08-10 00:17:47 +02:00
|
|
|
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;
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
const allowedMethods = [
|
|
|
|
"publish",
|
|
|
|
"resolve",
|
|
|
|
"wallet_send"
|
|
|
|
];
|
|
|
|
|
2018-08-09 16:38:42 +02:00
|
|
|
const body = {};
|
|
|
|
const claimAddress = data.claim;
|
|
|
|
const resolveMethod = data.method;
|
2018-08-14 22:15:27 +02:00
|
|
|
let apiRequestMethod = "";
|
2018-08-09 16:38:42 +02:00
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
if (allowedMethods.indexOf(resolveMethod) < 0) return socket.send(JSON.stringify({
|
|
|
|
"details": "Unallowed resolve method for tutorial",
|
|
|
|
"message": "notification",
|
|
|
|
"type": "error"
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-08-14 22:15:27 +02:00
|
|
|
body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; // access_token
|
2018-08-08 01:15:34 +02:00
|
|
|
body.method = resolveMethod;
|
|
|
|
|
|
|
|
if (resolveMethod === "publish") {
|
2018-08-14 22:15:27 +02:00
|
|
|
apiRequestMethod = "PUT";
|
|
|
|
|
2018-08-18 00:22:19 +02:00
|
|
|
// Required for publishing
|
|
|
|
body.author = "lbry.tech";
|
|
|
|
body.bid = 0.0001; // Hardcoded publish amount
|
2018-08-08 01:15:34 +02:00
|
|
|
body.description = dataDetails.description;
|
|
|
|
body.language = dataDetails.language;
|
|
|
|
body.license = dataDetails.license;
|
2018-08-18 00:22:19 +02:00
|
|
|
body.name = dataDetails.name.replace(/\s/g, "-") + randomString(10); // underscores are not allowed?
|
2018-08-08 01:15:34 +02:00
|
|
|
body.nsfw = dataDetails.nsfw;
|
|
|
|
body.title = dataDetails.title;
|
|
|
|
|
2018-08-18 00:22:19 +02:00
|
|
|
// Gotta let the blockchain know what to save
|
|
|
|
body.file_path = dataDetails.file_path; // just base64 string
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
return uploadImage(body.file_path).then(uploadResponse => {
|
2018-08-18 00:22:19 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
body.file_path = uploadResponse.filename;
|
2018-08-18 00:22:19 +02:00
|
|
|
|
|
|
|
return publishMeme(body).then(publishResponse => {
|
2018-09-27 20:00:34 +02:00
|
|
|
// let explorerNotice = "";
|
2018-08-18 00:22:19 +02:00
|
|
|
|
|
|
|
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 Tour after a response has been parsed_\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:00:34 +02:00
|
|
|
/*
|
2018-08-18 00:22:19 +02:00
|
|
|
if (
|
|
|
|
publishResponse.result &&
|
|
|
|
publishResponse.result.txid
|
|
|
|
) explorerNotice = `
|
|
|
|
<p>If you want proof of the tip you just gave, <a href="https://explorer.lbry.io/tx/${publishResponse.result.txid}" target="_blank" title="Your tip, on our blockchain explorer" rel="noopener noreferrer">check it out</a> on our blockchain explorer!</p>
|
|
|
|
`;
|
2018-09-27 20:00:34 +02:00
|
|
|
*/
|
2018-08-18 00:22:19 +02:00
|
|
|
|
|
|
|
const renderedCode = prism.highlight(stringifyObject(publishResponse, { indent: " ", singleQuotes: false }), prism.languages.json, "json");
|
|
|
|
|
|
|
|
return socket.send(JSON.stringify({
|
2018-09-27 20:00:34 +02:00
|
|
|
"example": data.example,
|
2018-08-18 00:22:19 +02:00
|
|
|
"html": raw(`
|
|
|
|
<h3>Response</h3>
|
2018-09-27 20:00:34 +02:00
|
|
|
<!--/ explorerNotice /-->
|
2018-08-18 00:22:19 +02:00
|
|
|
<pre><code class="language-json">${renderedCode}</code></pre>
|
|
|
|
`),
|
|
|
|
"message": "updated html",
|
|
|
|
"selector": `#example${data.example}-result`
|
|
|
|
}));
|
|
|
|
});
|
2018-08-08 01:15:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:15:27 +02:00
|
|
|
if (resolveMethod === "resolve") {
|
|
|
|
apiRequestMethod = "GET";
|
|
|
|
body.uri = claimAddress;
|
|
|
|
}
|
2018-08-10 00:17:47 +02:00
|
|
|
|
|
|
|
if (resolveMethod === "wallet_send") {
|
2018-08-14 22:15:27 +02:00
|
|
|
apiRequestMethod = "POST";
|
|
|
|
|
|
|
|
body.amount = "0.01"; // Hardcoded tip amount
|
2018-08-10 00:17:47 +02:00
|
|
|
body.claim_id = claimAddress;
|
|
|
|
}
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
return new Promise((resolve, reject) => { // eslint-disable-line
|
2018-09-27 20:00:34 +02:00
|
|
|
// let explorerNotice = "";
|
2018-08-14 22:15:27 +02:00
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
request({
|
2018-08-14 22:15:27 +02:00
|
|
|
body: body,
|
|
|
|
json: true,
|
|
|
|
method: apiRequestMethod,
|
2018-09-24 19:55:01 +02:00
|
|
|
url: `${process.env.NODE_ENV === "development" ? `http://localhost:5200/${resolveMethod}` : `https://${process.env.DAEMON_URL}/${resolveMethod}`}`
|
2018-08-08 01:15:34 +02:00
|
|
|
}, (error, response, body) => {
|
|
|
|
if (error) {
|
2018-08-14 22:15:27 +02:00
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
logSlackError(
|
|
|
|
"\n" +
|
|
|
|
"> *DAEMON ERROR:* ```" + JSON.parse(JSON.stringify(error)) + "```" + "\n" +
|
|
|
|
"> _Cause: Someone is going through the Tour_\n"
|
|
|
|
);
|
|
|
|
}
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
return resolve(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.error && typeof body.error !== "undefined") {
|
2018-08-14 22:15:27 +02:00
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
logSlackError(
|
|
|
|
"\n" +
|
|
|
|
"> *DAEMON ERROR:* ```" + JSON.parse(JSON.stringify(body.error.message)) + "```" + "\n" +
|
|
|
|
"> _Cause: Someone is going through the Tour after a response has been parsed_\n"
|
|
|
|
);
|
|
|
|
}
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
return resolve(body.error);
|
|
|
|
}
|
|
|
|
|
2018-09-27 20:00:34 +02:00
|
|
|
/*
|
2018-08-15 00:43:32 +02:00
|
|
|
if (
|
|
|
|
body.result &&
|
|
|
|
body.result.txid
|
|
|
|
) explorerNotice = `
|
2018-08-30 23:53:52 +02:00
|
|
|
<p>If you want proof of the tip you just gave on behalf of LBRY, <a href="https://explorer.lbry.io/tx/${body.result.txid}" target="_blank" title="Your tip, on our blockchain explorer" rel="noopener noreferrer">check it out</a> on our blockchain explorer!</p>
|
2018-08-14 22:15:27 +02:00
|
|
|
`;
|
2018-09-27 20:00:34 +02:00
|
|
|
*/
|
2018-08-14 22:15:27 +02:00
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
if (socket) {
|
2018-08-08 22:20:53 +02:00
|
|
|
const renderedCode = prism.highlight(stringifyObject(body, { indent: " ", singleQuotes: false }), prism.languages.json, "json");
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
return socket.send(JSON.stringify({
|
2018-09-27 20:00:34 +02:00
|
|
|
"example": data.example,
|
2018-08-08 22:20:53 +02:00
|
|
|
"html": raw(`
|
|
|
|
<h3>Response</h3>
|
2018-09-27 20:00:34 +02:00
|
|
|
<!--/ explorerNotice /-->
|
2018-08-08 22:20:53 +02:00
|
|
|
<pre><code class="language-json">${renderedCode}</code></pre>
|
|
|
|
`),
|
2018-08-08 01:15:34 +02:00
|
|
|
"message": "updated html",
|
2018-08-10 00:17:47 +02:00
|
|
|
"selector": `#example${data.example}-result`
|
2018-08-08 01:15:34 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(body.result[Object.keys(body.result)[0]].claim);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|