2018-08-08 01:15:34 +02:00
"use strict" ;
// P A C K A G E S
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" ) ;
// V A R I A B L E S
2018-09-27 18:11:26 +02:00
const randomString = local ( "app/helpers/random-string" ) ;
2018-08-08 22:20:53 +02:00
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" ) ;
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" ,
"wallet_send"
] ;
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" ;
body . bid = 0.0001 ; // Hardcoded publish amount
2018-08-08 01:15:34 +02:00
body . description = dataDetails . description ;
body . language = dataDetails . language ;
body . license = dataDetails . license ;
2018-09-28 21:09:45 +02:00
body . name = dataDetails . name + randomString ( 10 ) ; // underscores are not allowed?
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
body . file _path = dataDetails . file _path ; // just base64 string
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" >
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 > o n o u r b l o c k c h a i n e x p l o r e r ! P l e a s e n o t e t h a t i t m a y t a k e a c o u p l e m i n u t e s f o r t h e t r a n s a c t i o n t o b e c o n f i r m e d .
< br / > < br / >
You can also check out your meme 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 LBRY " > Spee . ch < / a > !
< / 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-10-09 20:28:58 +02:00
/ *
https : //open.lbry.io/Yet--another-test-733dd99009#59b368ed4b616108fe27d308fa8e22602acc762f
https : //open.lbry.io/claim_show#adee1be89febd3d89f51581601bca52d75a710a8
2018-09-28 21:09:45 +02:00
2018-10-09 20:28:58 +02:00
You are generating the claim name , so you 'd know it. it would be https://open.lbry.io/claim-name#claim_id (claim id comes from the return). You can also run a claim_show with the claim id parameter to get name if that' s easier . The spee . ch link would just be https : //spee.ch/claimid/claimname
* /
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 < / h 3 >
2018-09-28 21:09:45 +02:00
$ { explorerNotice }
2018-08-18 00:22:19 +02:00
< pre > < code class = "language-json" > $ { renderedCode } < / c o d e > < / p r e >
` ),
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
if ( resolveMethod === "wallet_send" ) {
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 < / h 3 >
< pre > < code class = "language-text" > Tipping creators not in the whitelist for this example is not allowed . < / c o d e > < / p r e >
` ),
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" ;
body . amount = "0.01" ; // 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 > o n o u r b l o c k c h a i n e x p l o r e r ! P l e a s e n o t e t h a t i t m a y t a k e a c o u p l e m i n u t e s f o r t h e t r a n s a c t i o n t o b e c o n f i r m e d . < / p > < b r / >
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 < / h 3 >
2018-09-28 21:09:45 +02:00
$ { explorerNotice }
2018-08-08 22:20:53 +02:00
< pre > < code class = "language-json" > $ { renderedCode } < / c o d e > < / p r e >
` ),
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 ) ;
} ) ;
} ) ;
} ;