Slight refactoring and replaced request modules with got
This commit is contained in:
parent
09543fcf1d
commit
9d47aec6c5
6 changed files with 162 additions and 175 deletions
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
// P A C K A G E S
|
// P A C K A G E S
|
||||||
|
|
||||||
|
const got = require("got");
|
||||||
const loadLanguages = require("prismjs/components/");
|
const loadLanguages = require("prismjs/components/");
|
||||||
const local = require("app-root-path").require;
|
const local = require("app-root-path").require;
|
||||||
const prism = require("prismjs");
|
const prism = require("prismjs");
|
||||||
const raw = require("choo/html/raw");
|
const raw = require("choo/html/raw");
|
||||||
const request = require("request-promise-native");
|
|
||||||
const stringifyObject = require("stringify-object");
|
const stringifyObject = require("stringify-object");
|
||||||
|
|
||||||
// U T I L S
|
// U T I L S
|
||||||
|
@ -18,13 +18,32 @@ const logSlackError = local("/app/helpers/slack");
|
||||||
const publishMeme = local("/app/helpers/publish-meme");
|
const publishMeme = local("/app/helpers/publish-meme");
|
||||||
const uploadImage = local("/app/helpers/upload-image");
|
const uploadImage = local("/app/helpers/upload-image");
|
||||||
|
|
||||||
|
const allowedQueryMethods = [
|
||||||
|
"publish",
|
||||||
|
"resolve",
|
||||||
|
"claim_tip"
|
||||||
|
];
|
||||||
|
|
||||||
|
const approvedContentIdsForTipping = [
|
||||||
|
"3db81c073f82fd1bb670c65f526faea3b8546720",
|
||||||
|
"173412f5b1b7aa63a752e8832406aafd9f1ecb4e",
|
||||||
|
"2a7f5db2678177435b1dee6c9e38e035ead450b6",
|
||||||
|
"d81bac6d49b1f92e58c37a5f633a27a45b43405e",
|
||||||
|
"b4668c0bd096317b44c40738c099b6618095e75f",
|
||||||
|
"007789cc45cbb4255cf02ba77cbf84ca8e3d7561",
|
||||||
|
"1ac47b8b3def40a25850dc726a09ce23d09e7009",
|
||||||
|
"784b3c215a6f06b663fc1aa292bcb19f29c489bb",
|
||||||
|
"758dd6497cdfc401ae1f25984738d024d47b50af",
|
||||||
|
"8a7401b88d5ed0376d98f16808194d4dcb05b284"
|
||||||
|
];
|
||||||
|
|
||||||
loadLanguages(["json"]);
|
loadLanguages(["json"]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// E X P O R T
|
// E X P O R T
|
||||||
|
|
||||||
module.exports = exports = (data, socket) => {
|
module.exports = exports = async(data, socket) => {
|
||||||
let dataDetails = "";
|
let dataDetails = "";
|
||||||
|
|
||||||
if (data.example === 1 && !data.claim || !data.method) return;
|
if (data.example === 1 && !data.claim || !data.method) return;
|
||||||
|
@ -32,28 +51,25 @@ module.exports = exports = (data, socket) => {
|
||||||
if (data.example === 2) dataDetails = data.data; // file upload
|
if (data.example === 2) dataDetails = data.data; // file upload
|
||||||
if (data.example === 3 && !data.claim || !data.method) return;
|
if (data.example === 3 && !data.claim || !data.method) return;
|
||||||
|
|
||||||
const allowedMethods = [
|
|
||||||
"publish",
|
|
||||||
"resolve",
|
|
||||||
"claim_tip"
|
|
||||||
];
|
|
||||||
|
|
||||||
const body = {};
|
const body = {};
|
||||||
const claimAddress = data.claim;
|
const claimAddress = data.claim;
|
||||||
const resolveMethod = data.method;
|
const resolveMethod = data.method;
|
||||||
let apiRequestMethod = "";
|
let apiRequestMethod = "";
|
||||||
|
|
||||||
if (allowedMethods.indexOf(resolveMethod) < 0) return socket.send(JSON.stringify({
|
if (allowedQueryMethods.indexOf(resolveMethod) < 0) return socket.send(JSON.stringify({
|
||||||
details: "Unallowed resolve method for tutorial",
|
details: "Unallowed resolve method for tutorial",
|
||||||
message: "notification",
|
message: "notification",
|
||||||
type: "error"
|
type: "error"
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; // access_token
|
body.authorization = process.env.LBRY_DAEMON_ACCESS_TOKEN; // access_token
|
||||||
body.method = resolveMethod;
|
body.method = resolveMethod;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// P U B L I S H
|
||||||
|
// E X A M P L E
|
||||||
|
|
||||||
if (resolveMethod === "publish") {
|
if (resolveMethod === "publish") {
|
||||||
apiRequestMethod = "PUT";
|
apiRequestMethod = "PUT";
|
||||||
|
|
||||||
|
@ -149,26 +165,19 @@ module.exports = exports = (data, socket) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// R E S O L V E
|
||||||
|
// E X A M P L E
|
||||||
|
|
||||||
if (resolveMethod === "resolve") {
|
if (resolveMethod === "resolve") {
|
||||||
apiRequestMethod = "GET";
|
apiRequestMethod = "GET";
|
||||||
body.uri = claimAddress;
|
body.uri = claimAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resolveMethod === "claim_tip") {
|
// T I P
|
||||||
const approvedIds = [
|
// E X A M P L E
|
||||||
"3db81c073f82fd1bb670c65f526faea3b8546720",
|
|
||||||
"173412f5b1b7aa63a752e8832406aafd9f1ecb4e",
|
|
||||||
"2a7f5db2678177435b1dee6c9e38e035ead450b6",
|
|
||||||
"d81bac6d49b1f92e58c37a5f633a27a45b43405e",
|
|
||||||
"b4668c0bd096317b44c40738c099b6618095e75f",
|
|
||||||
"007789cc45cbb4255cf02ba77cbf84ca8e3d7561",
|
|
||||||
"1ac47b8b3def40a25850dc726a09ce23d09e7009",
|
|
||||||
"784b3c215a6f06b663fc1aa292bcb19f29c489bb",
|
|
||||||
"758dd6497cdfc401ae1f25984738d024d47b50af",
|
|
||||||
"8a7401b88d5ed0376d98f16808194d4dcb05b284"
|
|
||||||
];
|
|
||||||
|
|
||||||
if (!approvedIds.includes(claimAddress)) {
|
if (resolveMethod === "claim_tip") {
|
||||||
|
if (!approvedContentIdsForTipping.includes(claimAddress)) {
|
||||||
return socket.send(JSON.stringify({
|
return socket.send(JSON.stringify({
|
||||||
example: data.example,
|
example: data.example,
|
||||||
html: raw(`
|
html: raw(`
|
||||||
|
@ -188,50 +197,32 @@ module.exports = exports = (data, socket) => {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => { // eslint-disable-line
|
// Q U E R Y
|
||||||
request({
|
// D A E M O N
|
||||||
|
|
||||||
|
const queryOptions = {
|
||||||
body: body,
|
body: body,
|
||||||
json: true,
|
json: true,
|
||||||
method: apiRequestMethod,
|
method: apiRequestMethod
|
||||||
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 Playground_\n"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(error);
|
const queryUrl = `${process.env.NODE_ENV === "development" ? `http://localhost:5200/${resolveMethod}` : `https://${process.env.DAEMON_URL}/${resolveMethod}`}`;
|
||||||
}
|
|
||||||
|
|
||||||
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 Playground after a response has been parsed_\n"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(body.error);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await got(queryUrl, queryOptions);
|
||||||
let explorerNotice = "";
|
let explorerNotice = "";
|
||||||
|
|
||||||
if (
|
if (
|
||||||
data.example === 3 &&
|
data.example === 3 &&
|
||||||
body.result &&
|
response.body.result &&
|
||||||
body.result.txid
|
response.body.result.txid
|
||||||
) explorerNotice = `
|
) explorerNotice = `
|
||||||
<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/>
|
<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/${response.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/>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
if (socket) {
|
if (socket) {
|
||||||
const renderedCode = prism.highlight(
|
const renderedCode = prism.highlight(
|
||||||
stringifyObject(body, { indent: " ", singleQuotes: false }),
|
stringifyObject(response.body, { indent: " ", singleQuotes: false }),
|
||||||
prism.languages.json,
|
prism.languages.json,
|
||||||
"json"
|
"json"
|
||||||
);
|
);
|
||||||
|
@ -248,7 +239,12 @@ module.exports = exports = (data, socket) => {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(body.result[Object.keys(body.result)[0]].claim);
|
return response.body.result[Object.keys(response.body.result)[0]].claim;
|
||||||
});
|
} catch (error) {
|
||||||
});
|
logSlackError(
|
||||||
|
"\n" +
|
||||||
|
"> *DAEMON ERROR:* ```" + error + "```" + "\n" +
|
||||||
|
"> _Cause: Someone is going through the Playground_\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,21 +4,30 @@
|
||||||
|
|
||||||
// P A C K A G E
|
// P A C K A G E
|
||||||
|
|
||||||
const request = require("request-promise-native");
|
const got = require("got");
|
||||||
|
|
||||||
|
// U T I L
|
||||||
|
|
||||||
|
const queryUrl = `${process.env.NODE_ENV === "development" ? "http://localhost:5200/publish" : `https://${process.env.DAEMON_URL}/publish`}`;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// E X P O R T
|
// E X P O R T
|
||||||
|
|
||||||
module.exports = exports = publishMetadata => new Promise((resolve, reject) => request({ // eslint-disable-line
|
module.exports = exports = async(publishMetadata) => {
|
||||||
|
const options = {
|
||||||
body: {
|
body: {
|
||||||
authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN,
|
authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN,
|
||||||
metadata: publishMetadata
|
metadata: publishMetadata
|
||||||
},
|
},
|
||||||
json: true,
|
json: true,
|
||||||
method: "PUT",
|
method: "PUT"
|
||||||
url: `${process.env.NODE_ENV === "development" ? "http://localhost:5200/publish" : `https://${process.env.DAEMON_URL}/publish` }`
|
};
|
||||||
}, (error, response, body) => {
|
|
||||||
if (error) resolve(error);
|
try {
|
||||||
resolve(body);
|
const response = await got(queryUrl, options);
|
||||||
}));
|
return response.body; // eslint-disable-line padding-line-between-statements
|
||||||
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// V A R I A B L E S
|
// U T I L S
|
||||||
|
|
||||||
let Slack;
|
let Slack;
|
||||||
let slack;
|
let slack;
|
||||||
|
|
|
@ -4,23 +4,30 @@
|
||||||
|
|
||||||
// P A C K A G E
|
// P A C K A G E
|
||||||
|
|
||||||
const request = require("request-promise-native");
|
const got = require("got");
|
||||||
|
|
||||||
|
// U T I L
|
||||||
|
|
||||||
|
const queryUrl = `${process.env.NODE_ENV === "development" ? "http://localhost:5200/image" : `https://${process.env.DAEMON_URL}/image` }`;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// E X P O R T
|
// E X P O R T
|
||||||
|
|
||||||
module.exports = exports = imageSource => new Promise((resolve, reject) => { // eslint-disable-line
|
module.exports = exports = async(imageSource) => {
|
||||||
return request({
|
const options = {
|
||||||
body: {
|
body: {
|
||||||
authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN,
|
authorization: process.env.LBRY_DAEMON_ACCESS_TOKEN,
|
||||||
image: imageSource
|
image: imageSource
|
||||||
},
|
},
|
||||||
json: true,
|
json: true,
|
||||||
method: "POST",
|
method: "POST"
|
||||||
url: `${process.env.NODE_ENV === "development" ? "http://localhost:5200/image" : `https://${process.env.DAEMON_URL}/image` }`
|
};
|
||||||
}, (error, response, body) => {
|
|
||||||
if (error) resolve(error);
|
try {
|
||||||
resolve(body);
|
const response = await got(queryUrl, options);
|
||||||
});
|
return response.body; // eslint-disable-line padding-line-between-statements
|
||||||
});
|
} catch (error) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
100
app/sockets.js
100
app/sockets.js
|
@ -4,9 +4,9 @@
|
||||||
|
|
||||||
// P A C K A G E S
|
// P A C K A G E S
|
||||||
|
|
||||||
|
const got = require("got");
|
||||||
const html = require("choo/html");
|
const html = require("choo/html");
|
||||||
const local = require("app-root-path").require;
|
const local = require("app-root-path").require;
|
||||||
const request = require("request-promise-native");
|
|
||||||
|
|
||||||
// U T I L S
|
// U T I L S
|
||||||
|
|
||||||
|
@ -301,17 +301,13 @@ function generateMemeCreator(socket) {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTrendingContent() {
|
async function getTrendingContent() {
|
||||||
return new Promise((resolve, reject) => { // eslint-disable-line
|
try {
|
||||||
request({
|
const response = await got("https://api.lbry.io/file/list_trending");
|
||||||
method: "GET",
|
return JSON.parse(response.body); // eslint-disable-line padding-line-between-statements
|
||||||
url: "https://api.lbry.io/file/list_trending"
|
} catch (error) {
|
||||||
}, (error, response, body) => {
|
return error;
|
||||||
if (error || !JSON.parse(body)) resolve("Issue fetching content"); // error
|
}
|
||||||
body = JSON.parse(body);
|
|
||||||
resolve(body);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeImageSourceSecure(url) {
|
function makeImageSourceSecure(url) {
|
||||||
|
@ -323,7 +319,7 @@ function makeImageSourceSecure(url) {
|
||||||
return originalUrl.href;
|
return originalUrl.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
function newsletterSubscribe(data, socket) {
|
async function newsletterSubscribe(data, socket) {
|
||||||
const email = data.email;
|
const email = data.email;
|
||||||
|
|
||||||
if (!validateEmail(email)) return socket.send(JSON.stringify({
|
if (!validateEmail(email)) return socket.send(JSON.stringify({
|
||||||
|
@ -333,68 +329,48 @@ function newsletterSubscribe(data, socket) {
|
||||||
selector: "#emailMessage"
|
selector: "#emailMessage"
|
||||||
}));
|
}));
|
||||||
|
|
||||||
return new Promise((resolve, reject) => request({
|
try {
|
||||||
method: "POST",
|
await got.post(`https://api.lbry.io/list/subscribe?email=${encodeURIComponent(email)}&tag=developer`);
|
||||||
url: `https://api.lbry.io/list/subscribe?email=${encodeURIComponent(email)}&tag=developer`
|
|
||||||
}).then(body => {
|
return socket.send(JSON.stringify({
|
||||||
if (!body || !JSON.parse(body)) {
|
html: "Thank you! Please confirm subscription in your inbox.",
|
||||||
|
message: "updated html",
|
||||||
|
selector: "#emailMessage"
|
||||||
|
}));
|
||||||
|
} catch (error) {
|
||||||
|
const response = JSON.parse(error.body);
|
||||||
|
|
||||||
|
if (!response.success) {
|
||||||
|
logSlackError(
|
||||||
|
"\n" +
|
||||||
|
"> *NEWSLETTER ERROR:* ```" + response.error + "```" + "\n" +
|
||||||
|
`> _Cause: ${email} interacted with the form_\n`
|
||||||
|
);
|
||||||
|
|
||||||
|
return socket.send(JSON.stringify({
|
||||||
|
class: "error",
|
||||||
|
html: response.error,
|
||||||
|
message: "updated html",
|
||||||
|
selector: "#emailMessage"
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
logSlackError(
|
logSlackError(
|
||||||
"\n" +
|
"\n" +
|
||||||
"> *NEWSLETTER ERROR:* ```¯\\_(ツ)_/¯ This should be an unreachable error```" + "\n" +
|
"> *NEWSLETTER ERROR:* ```¯\\_(ツ)_/¯ This should be an unreachable error```" + "\n" +
|
||||||
`> _Cause: ${email} interacted with the form_\n`
|
`> _Cause: ${email} interacted with the form_\n`
|
||||||
);
|
);
|
||||||
|
|
||||||
return resolve(socket.send(JSON.stringify({
|
return socket.send(JSON.stringify({
|
||||||
class: "error",
|
class: "error",
|
||||||
html: "Something is terribly wrong",
|
html: "Something is terribly wrong",
|
||||||
message: "updated html",
|
message: "updated html",
|
||||||
selector: "#emailMessage"
|
selector: "#emailMessage"
|
||||||
})));
|
|
||||||
}
|
|
||||||
|
|
||||||
body = JSON.parse(body);
|
|
||||||
|
|
||||||
if (!body.success) {
|
|
||||||
logSlackError(
|
|
||||||
"\n" +
|
|
||||||
"> *NEWSLETTER ERROR:* ```" + JSON.parse(JSON.stringify(body.error)) + "```" + "\n" +
|
|
||||||
`> _Cause: ${email} interacted with the form_\n`
|
|
||||||
);
|
|
||||||
|
|
||||||
return reject(socket.send(JSON.stringify({
|
|
||||||
class: "error",
|
|
||||||
html: body.error,
|
|
||||||
message: "updated html",
|
|
||||||
selector: "#emailMessage"
|
|
||||||
})));
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(socket.send(JSON.stringify({
|
|
||||||
html: "Thank you! Please confirm subscription in your inbox.",
|
|
||||||
message: "updated html",
|
|
||||||
selector: "#emailMessage"
|
|
||||||
})));
|
|
||||||
})
|
|
||||||
.catch(welp => {
|
|
||||||
if (welp.statusCode === 409) {
|
|
||||||
logSlackError(
|
|
||||||
"\n" +
|
|
||||||
"> *NEWSLETTER ERROR:* ```" + JSON.parse(JSON.stringify(welp.error)) + "```" + "\n" +
|
|
||||||
`> _Cause: ${email} interacted with the form_\n`
|
|
||||||
);
|
|
||||||
|
|
||||||
return resolve(socket.send(JSON.stringify({
|
|
||||||
class: "error",
|
|
||||||
html: "You have already subscribed!",
|
|
||||||
message: "updated html",
|
|
||||||
selector: "#emailMessage"
|
|
||||||
})));
|
|
||||||
}
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function validateEmail(email) {
|
function validateEmail(email) {
|
||||||
const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i;
|
const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i;
|
||||||
|
return emailRegex.test(String(email)); // eslint-disable-line padding-line-between-statements
|
||||||
return emailRegex.test(String(email));
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
"fastify-ws": "^1.0.0",
|
"fastify-ws": "^1.0.0",
|
||||||
"front-matter": "^3.0.1",
|
"front-matter": "^3.0.1",
|
||||||
"fs-exists-sync": "^0.1.0",
|
"fs-exists-sync": "^0.1.0",
|
||||||
|
"got": "^9.3.2",
|
||||||
"graceful-fs": "^4.1.15",
|
"graceful-fs": "^4.1.15",
|
||||||
"heroku-ssl-redirect": "0.0.4",
|
"heroku-ssl-redirect": "0.0.4",
|
||||||
"make-fetch-happen": "^4.0.1",
|
"make-fetch-happen": "^4.0.1",
|
||||||
|
@ -34,8 +35,6 @@
|
||||||
"markdown-it-anchor": "^5.0.2",
|
"markdown-it-anchor": "^5.0.2",
|
||||||
"prismjs": "^1.15.0",
|
"prismjs": "^1.15.0",
|
||||||
"redis": "^2.8.0",
|
"redis": "^2.8.0",
|
||||||
"request": "^2.88.0",
|
|
||||||
"request-promise-native": "^1.0.5",
|
|
||||||
"slack-node": "^0.2.0",
|
"slack-node": "^0.2.0",
|
||||||
"socket.io": "^2.1.1",
|
"socket.io": "^2.1.1",
|
||||||
"stringify-object": "^3.3.0"
|
"stringify-object": "^3.3.0"
|
||||||
|
|
Loading…
Reference in a new issue