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