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

222 lines
6.8 KiB
JavaScript
Raw Normal View History

"use strict";
// P A C K A G E S
const local = require("app-root-path").require;
const prism = require("prismjs");
2018-08-29 01:57:18 +02:00
const raw = require("choo/html/raw");
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");
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");
loadLanguages(["json"]);
// E X P O R T
module.exports = exports = (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 allowedMethods = [
"publish",
"resolve",
"wallet_send"
];
2018-08-09 16:38:42 +02:00
const body = {};
const claimAddress = data.claim;
const resolveMethod = data.method;
let apiRequestMethod = "";
2018-08-09 16:38:42 +02:00
if (allowedMethods.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;
if (resolveMethod === "publish") {
apiRequestMethod = "PUT";
2018-08-18 00:22:19 +02:00
// Required for publishing
body.author = "lbry.tech";
body.bid = 0.0001; // Hardcoded publish amount
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?
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
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;
}
body.file_path = uploadResponse.filename;
2018-08-18 00:22:19 +02:00
return publishMeme(body).then(publishResponse => {
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;
}
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>
<pre><code class="language-json">${renderedCode}</code></pre>
`),
"message": "updated html",
"selector": `#example${data.example}-result`
}));
});
});
}
if (resolveMethod === "resolve") {
apiRequestMethod = "GET";
body.uri = claimAddress;
}
if (resolveMethod === "wallet_send") {
const approvedIds = [
"3db81c073f82fd1bb670c65f526faea3b8546720",
"173412f5b1b7aa63a752e8832406aafd9f1ecb4e",
"d81bac6d49b1f92e58c37a5f633a27a45b43405e",
"b4668c0bd096317b44c40738c099b6618095e75f",
"007789cc45cbb4255cf02ba77cbf84ca8e3d7561",
"1ac47b8b3def40a25850dc726a09ce23d09e7009",
"784b3c215a6f06b663fc1aa292bcb19f29c489bb",
"758dd6497cdfc401ae1f25984738d024d47b50af",
"8a7401b88d5ed0376d98f16808194d4dcb05b284"
];
if (!approvedIds.includes(claimAddress)) {
return socket.send(JSON.stringify({
"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": "updated html",
"selector": `#example${data.example}-result`
}));
}
apiRequestMethod = "POST";
body.amount = "0.01"; // Hardcoded tip amount
body.claim_id = claimAddress;
}
return new Promise((resolve, reject) => { // eslint-disable-line
request({
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}`}`
}, (error, response, body) => {
if (error) {
if (process.env.NODE_ENV !== "development") {
logSlackError(
"\n" +
"> *DAEMON ERROR:* ```" + JSON.parse(JSON.stringify(error)) + "```" + "\n" +
"> _Cause: Someone is going through the Tour_\n"
);
}
return resolve(error);
}
if (body.error && typeof body.error !== "undefined") {
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"
);
}
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-09-27 20:00:34 +02:00
*/
if (socket) {
const renderedCode = prism.highlight(stringifyObject(body, { indent: " ", singleQuotes: false }), prism.languages.json, "json");
return socket.send(JSON.stringify({
2018-09-27 20:00:34 +02:00
"example": data.example,
"html": raw(`
<h3>Response</h3>
<pre><code class="language-json">${renderedCode}</code></pre>
`),
"message": "updated html",
"selector": `#example${data.example}-result`
}));
}
return resolve(body.result[Object.keys(body.result)[0]].claim);
});
});
};