lbry.tech/app/helpers/fetch-metadata.js

301 lines
8.8 KiB
JavaScript
Raw Normal View History

"use strict";
2018-11-30 21:46:22 +01:00
// I M P O R T S
2018-11-30 21:46:22 +01:00
import got from "got";
import prism from "prismjs";
import raw from "choo/html/raw";
import stringifyObject from "stringify-object";
2018-10-10 19:56:35 +02:00
// U T I L S
2018-11-30 21:46:22 +01:00
import messageSlack from "./slack";
import publishMeme from "./publish-meme";
2019-02-05 00:42:52 +01:00
import randomString from "./random-string";
import { send } from "~socket";
2018-11-30 21:46:22 +01:00
import uploadImage from "./upload-image";
const allowedQueryMethods = [
"support_create",
"publish",
2019-02-05 00:42:52 +01:00
"resolve"
];
const approvedContentIdsForTipping = [
"3db81c073f82fd1bb670c65f526faea3b8546720",
"173412f5b1b7aa63a752e8832406aafd9f1ecb4e",
"2a7f5db2678177435b1dee6c9e38e035ead450b6",
"d81bac6d49b1f92e58c37a5f633a27a45b43405e",
"b4668c0bd096317b44c40738c099b6618095e75f",
"007789cc45cbb4255cf02ba77cbf84ca8e3d7561",
"1ac47b8b3def40a25850dc726a09ce23d09e7009",
"784b3c215a6f06b663fc1aa292bcb19f29c489bb",
"758dd6497cdfc401ae1f25984738d024d47b50af",
"8a7401b88d5ed0376d98f16808194d4dcb05b284"
];
const environment = process.env.NODE_ENV === "development" ?
"development" :
"production";
2018-11-30 21:46:22 +01:00
// 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
2018-11-30 21:46:22 +01:00
export default async(data, socket) => {
const body = {};
let apiRequestMethod = "";
let dataDetails = "";
2018-11-30 21:46:22 +01:00
let explorerNotice = "";
2019-07-12 00:09:36 +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
2019-07-12 00:09:36 +02:00
if (data.example === 3 && (!data.claim || !data.method)) return;
2018-08-09 16:38:42 +02:00
const claimAddress = data.claim;
const resolveMethod = data.method;
2018-11-30 21:46:22 +01:00
if (allowedQueryMethods.indexOf(resolveMethod) < 0) {
2019-02-05 00:42:52 +01:00
return send(socket, {
2018-11-30 21:46:22 +01:00
details: "Unallowed resolve method for tutorial",
message: "notification",
type: "error"
2019-02-05 00:42:52 +01:00
});
2018-11-30 21:46:22 +01:00
}
2018-11-30 21:46:22 +01:00
body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN;
body.method = resolveMethod;
2018-11-30 21:46:22 +01:00
switch(true) {
// T I P
// E X A M P L E
case resolveMethod === "support_create":
2018-11-30 21:46:22 +01:00
if (!approvedContentIdsForTipping.includes(claimAddress)) {
2019-02-05 00:42:52 +01:00
return send(socket, {
2018-11-30 21:46:22 +01:00
example: data.example,
html: raw(`
<h3>Response</h3>
<pre><code class="language-text">Tipping creators not in the whitelist for this example is not allowed.</code></pre>
`),
message: "show result",
selector: `#example${data.example}-result`
2019-02-05 00:42:52 +01:00
});
2018-11-30 21:46:22 +01:00
}
2018-11-30 21:46:22 +01:00
apiRequestMethod = "POST";
body.amount = "0.001"; // Hardcoded tip amount
body.claim_id = claimAddress;
body.tip = true;
2018-11-30 21:46:22 +01:00
break;
2018-08-18 00:22:19 +02:00
2018-11-30 21:46:22 +01:00
// P U B L I S H
// E X A M P L E
case resolveMethod === "publish":
apiRequestMethod = "PUT";
2018-08-18 00:22:19 +02:00
2018-11-30 21:46:22 +01:00
// Required for publishing
body.author = "lbry.tech";
body.bid = "0.001"; // Hardcoded publish amount
body.description = dataDetails.description;
body.languages = [dataDetails.language];
2018-11-30 21:46:22 +01:00
body.license = dataDetails.license;
body.name = dataDetails.name + "-" + randomString(10);
body.title = dataDetails.title;
body.tags = dataDetails.tags;
2018-11-30 21:46:22 +01:00
// 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 data.example === 2:
case memePublishResponse.result:
case memePublishResponse.result.outputs[0].address:
explorerNotice = memePublishMessaging(memePublishResponse);
2018-11-30 21:46:22 +01:00
break;
default:
break;
}
2018-08-18 00:22:19 +02:00
2018-11-30 21:46:22 +01:00
delete memePublishResponse.result.lbrytech_claim_name;
2018-11-30 21:46:22 +01:00
const renderedCode = prism.highlight(
stringifyObject(memePublishResponse, { indent: " ", singleQuotes: false }),
prism.languages.json,
"json"
);
2019-02-05 00:42:52 +01:00
return send(socket, {
2018-11-30 21:46:22 +01:00
example: data.example,
html: raw(`
<h3>Response</h3>
${explorerNotice}
<pre><code class="language-json">${renderedCode}</code></pre>
`),
message: "show result",
selector: `#example${data.example}-result`
2019-02-05 00:42:52 +01:00
});
2019-07-12 00:09:36 +02:00
} catch(memePublishError) {
2019-02-05 00:42:52 +01:00
send(socket, {
2018-10-06 22:53:01 +02:00
details: "Meme publish failed",
message: "notification",
type: "error"
2019-02-05 00:42:52 +01:00
});
2018-08-18 00:22:19 +02:00
if (process.env.NODE_ENV !== "development") {
2018-11-30 21:46:22 +01:00
messageSlack({
message: "```" + JSON.parse(JSON.stringify(memePublishError.error)) + "```",
pretext: "_Someone is going through the Playground after a response has been parsed_",
title: `DAEMON ERROR | ${environment}`
2018-11-30 21:46:22 +01:00
});
2018-08-18 00:22:19 +02:00
}
return;
}
2019-07-12 00:09:36 +02:00
} catch(imageUploadError) {
2019-02-05 00:42:52 +01:00
send(socket, {
2018-11-30 21:46:22 +01:00
details: "Image upload failed",
message: "notification",
type: "error"
2019-02-05 00:42:52 +01:00
});
2018-11-30 21:46:22 +01:00
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 | ${environment}`
2018-11-30 21:46:22 +01:00
});
}
2018-11-30 21:46:22 +01:00
return;
}
2018-11-30 21:46:22 +01:00
// R E S O L V E
// E X A M P L E
case resolveMethod === "resolve":
apiRequestMethod = "GET";
body.uri = claimAddress;
break;
2018-11-30 21:46:22 +01:00
default:
break;
}
2018-09-28 21:09:45 +02:00
// Q U E R Y
// D A E M O N
const queryOptions = {
body: body,
json: true,
method: apiRequestMethod
};
2018-11-30 21:46:22 +01:00
const queryUrl = process.env.NODE_ENV === "development" ?
`http://localhost:5200/${resolveMethod}` :
`https://${process.env.DAEMON_URL}/${resolveMethod}`;
try {
const response = await got(queryUrl, queryOptions);
2018-09-28 21:09:45 +02:00
2018-11-30 21:46:22 +01:00
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(
stringifyObject(response.body, { indent: " ", singleQuotes: false }),
prism.languages.json,
"json"
);
2019-02-05 00:42:52 +01:00
return send(socket, {
example: data.example,
html: raw(`
<h3>Response</h3>
${explorerNotice}
<pre><code class="language-json">${renderedCode}</code></pre>
`),
message: "show result",
selector: `#example${data.example}-result`
2019-02-05 00:42:52 +01:00
});
}
2019-08-21 19:54:17 +02:00
// console.log("——");
// console.log(response.body.result[Object.keys(response.body.result)[0]]);
// console.log("——");
return response.body.result[Object.keys(response.body.result)[0]];
2019-07-12 00:09:36 +02:00
} catch(error) {
console.error(error);
2018-11-30 21:46:22 +01:00
messageSlack({
message: "```" + error + "```",
pretext: "_Someone is going through the Playground and the daemon is not running_",
title: `DAEMON ERROR | ${environment}`
2018-11-30 21:46:22 +01:00
});
}
};
2018-11-30 21:46:22 +01:00
// H E L P E R S
function memePublishMessaging(source) {
return `
<p class="playground__description success">
Nicely done, you've published to <code>lbry://${source.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.com/tx/${source.result.outputs[0].txid}?address=${source.result.outputs[0].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-11-30 21:46:22 +01:00
<br/><br/>
You can also check out your meme (once the transaction is confirmed) on <a href="https://open.lbry.com/${source.result.lbrytech_claim_name}#${source.result.outputs[0].claim_id}" rel="noopener noreferrer" target="_blank" title="Your meme, on LBRY">LBRY</a> or <a href="https://spee.ch/${source.result.outputs[0].claim_id}/${source.result.lbrytech_claim_name}" rel="noopener noreferrer" target="_blank" title="Your meme, on spee.ch">Spee.ch</a>!
2018-11-30 21:46:22 +01:00
</p>
<br/>
`;
}
function tipCompletionMessaging(source) {
return `
<p class="playground__description success">
If you want proof of the tip you just gave on behalf of LBRY, <a href="https://explorer.lbry.com/tx/${source.result.outputs[0].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.
2018-11-30 21:46:22 +01:00
</p><br/>
`;
}