2018-08-08 01:15:34 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P A C K A G E S
|
|
|
|
|
2018-10-10 19:56:35 +02:00
|
|
|
const loadLanguages = require("prismjs/components/");
|
2018-08-08 01:15:34 +02:00
|
|
|
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");
|
|
|
|
|
2018-10-10 19:56:35 +02:00
|
|
|
// U T I L S
|
2018-08-08 01:15:34 +02:00
|
|
|
|
2018-10-10 19:56:35 +02:00
|
|
|
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");
|
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",
|
2018-11-21 18:53:17 +01:00
|
|
|
"claim_tip"
|
2018-08-08 01:15:34 +02:00
|
|
|
];
|
|
|
|
|
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({
|
2018-10-06 22:53:01 +02:00
|
|
|
details: "Unallowed resolve method for tutorial",
|
|
|
|
message: "notification",
|
|
|
|
type: "error"
|
2018-08-08 01:15:34 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
2018-11-21 18:53:17 +01:00
|
|
|
body.bid = "0.001"; // Hardcoded publish amount
|
2018-08-08 01:15:34 +02:00
|
|
|
body.description = dataDetails.description;
|
|
|
|
body.language = dataDetails.language;
|
|
|
|
body.license = dataDetails.license;
|
2018-10-10 23:15:43 +02:00
|
|
|
body.name = dataDetails.name + "-" + randomString(10);
|
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
|
2018-10-10 23:15:43 +02:00
|
|
|
body.file_path = dataDetails.file_path;
|
2018-08-18 00:22:19 +02:00
|
|
|
|
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({
|
2018-10-06 22:53:01 +02:00
|
|
|
details: "Image upload failed",
|
|
|
|
message: "notification",
|
|
|
|
type: "error"
|
2018-08-18 00:22:19 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
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-10-09 20:28:58 +02:00
|
|
|
let explorerNotice = "";
|
|
|
|
|
2018-08-18 00:22:19 +02:00
|
|
|
if (publishResponse.error) {
|
|
|
|
socket.send(JSON.stringify({
|
2018-10-06 22:53:01 +02:00
|
|
|
details: "Meme publish failed",
|
|
|
|
message: "notification",
|
|
|
|
type: "error"
|
2018-08-18 00:22:19 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
logSlackError(
|
|
|
|
"\n" +
|
|
|
|
"> *DAEMON ERROR:* ```" + JSON.parse(JSON.stringify(publishResponse.error)) + "```" + "\n" +
|
2018-10-03 22:27:13 +02:00
|
|
|
"> _Cause: Someone is going through the Playground after a response has been parsed_\n"
|
2018-08-18 00:22:19 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-09 20:28:58 +02:00
|
|
|
if (
|
|
|
|
publishResponse.result &&
|
|
|
|
publishResponse.result.claim_address
|
|
|
|
) explorerNotice = `
|
|
|
|
<p class="playground__description success">
|
2018-10-10 18:39:50 +02:00
|
|
|
Nicely done, you've published to <code>lbry://${publishResponse.result.lbrytech_claim_name}</code>.
|
|
|
|
<br/><br/>
|
|
|
|
To see Proof of Work (lol) that your meme is on the LBRY blockchain, <a href="https://explorer.lbry.io/address/${publishResponse.result.claim_address}" rel="noopener noreferrer" target="_blank" title="Your meme, on our blockchain explorer">check it out</a> on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed.
|
2018-10-09 20:28:58 +02:00
|
|
|
<br/><br/>
|
2018-10-10 01:03:00 +02:00
|
|
|
You can also check out your meme (once the transaction is confirmed) on <a href="https://open.lbry.io/${publishResponse.result.lbrytech_claim_name}#${publishResponse.result.claim_id}" rel="noopener noreferrer" target="_blank" title="Your meme, on LBRY">LBRY</a> or <a href="https://spee.ch/${publishResponse.result.claim_id}/${publishResponse.result.lbrytech_claim_name}" rel="noopener noreferrer" target="_blank" title="Your meme, on spee.ch">Spee.ch</a>!
|
2018-10-09 20:28:58 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<br/>
|
|
|
|
`;
|
|
|
|
|
|
|
|
delete publishResponse.result.lbrytech_claim_name;
|
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
const renderedCode = prism.highlight(
|
|
|
|
stringifyObject(publishResponse, { indent: " ", singleQuotes: false }),
|
|
|
|
prism.languages.json,
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
|
2018-08-18 00:22:19 +02:00
|
|
|
return socket.send(JSON.stringify({
|
2018-10-06 22:53:01 +02:00
|
|
|
example: data.example,
|
|
|
|
html: raw(`
|
2018-08-18 00:22:19 +02:00
|
|
|
<h3>Response</h3>
|
2018-09-28 21:09:45 +02:00
|
|
|
${explorerNotice}
|
2018-08-18 00:22:19 +02:00
|
|
|
<pre><code class="language-json">${renderedCode}</code></pre>
|
|
|
|
`),
|
2018-10-06 22:53:01 +02:00
|
|
|
message: "show result",
|
|
|
|
selector: `#example${data.example}-result`
|
2018-08-18 00:22:19 +02:00
|
|
|
}));
|
|
|
|
});
|
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
|
|
|
|
2018-11-21 18:53:17 +01:00
|
|
|
if (resolveMethod === "claim_tip") {
|
2018-09-27 22:46:59 +02:00
|
|
|
const approvedIds = [
|
|
|
|
"3db81c073f82fd1bb670c65f526faea3b8546720",
|
|
|
|
"173412f5b1b7aa63a752e8832406aafd9f1ecb4e",
|
2018-09-27 23:14:58 +02:00
|
|
|
"2a7f5db2678177435b1dee6c9e38e035ead450b6",
|
2018-09-27 22:46:59 +02:00
|
|
|
"d81bac6d49b1f92e58c37a5f633a27a45b43405e",
|
|
|
|
"b4668c0bd096317b44c40738c099b6618095e75f",
|
|
|
|
"007789cc45cbb4255cf02ba77cbf84ca8e3d7561",
|
|
|
|
"1ac47b8b3def40a25850dc726a09ce23d09e7009",
|
|
|
|
"784b3c215a6f06b663fc1aa292bcb19f29c489bb",
|
|
|
|
"758dd6497cdfc401ae1f25984738d024d47b50af",
|
|
|
|
"8a7401b88d5ed0376d98f16808194d4dcb05b284"
|
|
|
|
];
|
|
|
|
|
|
|
|
if (!approvedIds.includes(claimAddress)) {
|
|
|
|
return socket.send(JSON.stringify({
|
2018-10-06 22:53:01 +02:00
|
|
|
example: data.example,
|
|
|
|
html: raw(`
|
2018-09-27 22:46:59 +02:00
|
|
|
<h3>Response</h3>
|
|
|
|
<pre><code class="language-text">Tipping creators not in the whitelist for this example is not allowed.</code></pre>
|
|
|
|
`),
|
2018-10-06 22:53:01 +02:00
|
|
|
message: "show result",
|
|
|
|
selector: `#example${data.example}-result`
|
2018-09-27 22:46:59 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-08-14 22:15:27 +02:00
|
|
|
apiRequestMethod = "POST";
|
|
|
|
|
2018-11-21 18:53:17 +01:00
|
|
|
body.amount = "0.001"; // Hardcoded tip amount
|
2018-08-10 00:17:47 +02:00
|
|
|
body.claim_id = claimAddress;
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
return new Promise((resolve, reject) => { // eslint-disable-line
|
|
|
|
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" +
|
2018-10-03 22:27:13 +02:00
|
|
|
"> _Cause: Someone is going through the Playground_\n"
|
2018-08-14 22:15:27 +02:00
|
|
|
);
|
|
|
|
}
|
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" +
|
2018-10-03 22:27:13 +02:00
|
|
|
"> _Cause: Someone is going through the Playground after a response has been parsed_\n"
|
2018-08-14 22:15:27 +02:00
|
|
|
);
|
|
|
|
}
|
2018-08-08 01:15:34 +02:00
|
|
|
|
|
|
|
return resolve(body.error);
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:09:45 +02:00
|
|
|
let explorerNotice = "";
|
|
|
|
|
2018-08-15 00:43:32 +02:00
|
|
|
if (
|
2018-09-28 21:09:45 +02:00
|
|
|
data.example === 3 &&
|
2018-08-15 00:43:32 +02:00
|
|
|
body.result &&
|
|
|
|
body.result.txid
|
|
|
|
) explorerNotice = `
|
2018-10-03 22:27:13 +02:00
|
|
|
<p class="playground__description success">If you want proof of the tip you just gave on behalf of LBRY, <a href="https://explorer.lbry.io/tx/${body.result.txid}" rel="noopener noreferrer" target="_blank" title="Your tip, on our blockchain explorer">check it out</a> on our blockchain explorer! Please note that it may take a couple minutes for the transaction to be confirmed.</p><br/>
|
2018-08-14 22:15:27 +02:00
|
|
|
`;
|
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
if (socket) {
|
2018-09-28 21:09:45 +02:00
|
|
|
const renderedCode = prism.highlight(
|
|
|
|
stringifyObject(body, { indent: " ", singleQuotes: false }),
|
|
|
|
prism.languages.json,
|
|
|
|
"json"
|
|
|
|
);
|
2018-08-08 22:20:53 +02:00
|
|
|
|
2018-08-08 01:15:34 +02:00
|
|
|
return socket.send(JSON.stringify({
|
2018-10-06 22:53:01 +02:00
|
|
|
example: data.example,
|
|
|
|
html: raw(`
|
2018-08-08 22:20:53 +02:00
|
|
|
<h3>Response</h3>
|
2018-09-28 21:09:45 +02:00
|
|
|
${explorerNotice}
|
2018-08-08 22:20:53 +02:00
|
|
|
<pre><code class="language-json">${renderedCode}</code></pre>
|
|
|
|
`),
|
2018-10-06 22:53:01 +02:00
|
|
|
message: "show result",
|
|
|
|
selector: `#example${data.example}-result`
|
2018-08-08 01:15:34 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(body.result[Object.keys(body.result)[0]].claim);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|