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" ) ;
const raw = require ( "nanohtml/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-08-11 00:29:06 +02:00
const randomString = local ( "/helpers/random-string" ) ;
2018-08-08 22:20:53 +02:00
const loadLanguages = require ( "prismjs/components/" ) ;
2018-08-08 01:15:34 +02:00
const logSlackError = local ( "/helpers/slack" ) ;
const uploadImage = local ( "/helpers/upload-image" ) ;
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-08 01:15:34 +02:00
if ( allowedMethods . indexOf ( resolveMethod ) < 0 ) return socket . send ( JSON . stringify ( {
"details" : "Unallowed resolve method for tutorial" ,
"message" : "notification" ,
"type" : "error"
} ) ) ;
body . access _token = process . env . LBRY _DAEMON _ACCESS _TOKEN ;
body . method = resolveMethod ;
if ( resolveMethod === "publish" ) {
2018-08-11 00:29:06 +02:00
body . bid = 0.001 ; // Hardcoded publish amount
2018-08-08 01:15:34 +02:00
body . description = dataDetails . description ;
body . file _path = process . env . LBRY _DAEMON _IMAGES _PATH + dataDetails . file _path ; // TODO: Fix the internal image path in daemon (original comment, check to see if still true)
body . language = dataDetails . language ;
body . license = dataDetails . license ;
2018-08-11 00:29:06 +02:00
body . name = dataDetails . name . replace ( /\s/g , "" ) + randomString ( 10 ) ;
2018-08-08 01:15:34 +02:00
body . nsfw = dataDetails . nsfw ;
body . title = dataDetails . title ;
2018-08-11 00:29:06 +02:00
// TODO: Forget all this and upload to spee.ch
2018-08-08 01:15:34 +02:00
return uploadImage ( body . file _path ) . then ( uploadResponse => {
if ( uploadResponse . status !== "ok" ) return ;
body . file _path = uploadResponse . filename ;
2018-08-11 00:29:06 +02:00
body . filename = uploadResponse . filename ;
2018-08-08 01:15:34 +02:00
// Reference:
// https://github.com/lbryio/lbry.tech/blob/legacy/content/.vuepress/components/Tour/Step2.vue
// https://github.com/lbryio/lbry.tech/blob/legacy/server.js
} ) . catch ( uploadError => {
// component.isLoading = false;
// component.jsonData = JSON.stringify(uploadError, null, " ");
socket . send ( JSON . stringify ( {
"details" : "Image upload failed" ,
"message" : "notification" ,
"type" : "error"
} ) ) ;
logSlackError (
"\n" +
"> *DAEMON ERROR:* ```" + JSON . parse ( JSON . stringify ( uploadError ) ) + "```" + "\n" +
"> _Cause: Someone attempted to publish a meme via the Tour_\n"
) ;
return ;
} ) ;
}
2018-08-11 00:29:06 +02:00
if ( resolveMethod === "resolve" ) body . uri = claimAddress ;
2018-08-10 00:17:47 +02:00
if ( resolveMethod === "wallet_send" ) {
body . amount = "0.001" ; // Hardcoded tip amount
body . claim _id = claimAddress ;
}
2018-08-08 01:15:34 +02:00
return new Promise ( ( resolve , reject ) => { // eslint-disable-line
request ( {
url : "http://daemon.lbry.tech" ,
qs : body
} , ( error , response , body ) => {
if ( error ) {
logSlackError (
"\n" +
"> *DAEMON ERROR:* ```" + JSON . parse ( JSON . stringify ( error ) ) + "```" + "\n" +
"> _Cause: Someone is going through the Tour_\n"
) ;
return resolve ( error ) ;
}
body = JSON . parse ( body ) ;
if ( body . error && typeof body . error !== "undefined" ) {
logSlackError (
"\n" +
2018-08-10 00:17:47 +02:00
"> *DAEMON ERROR:* ```" + JSON . parse ( JSON . stringify ( body . error . message ) ) + "```" + "\n" +
"> _Cause: Someone is going through the Tour after a response has been parsed_\n"
2018-08-08 01:15:34 +02:00
) ;
return resolve ( body . error ) ;
}
if ( socket ) {
2018-08-08 22:20:53 +02:00
const renderedCode = prism . highlight ( stringifyObject ( body , { indent : " " , singleQuotes : false } ) , prism . languages . json , "json" ) ;
2018-08-08 01:15:34 +02:00
return socket . send ( JSON . stringify ( {
2018-08-08 22:20:53 +02:00
"html" : raw ( `
< h3 > Response < / h 3 >
< pre > < code class = "language-json" > $ { renderedCode } < / c o d e > < / p r e >
` ),
2018-08-08 01:15:34 +02:00
"message" : "updated html" ,
2018-08-10 00:17:47 +02:00
"selector" : ` #example ${ data . example } -result `
2018-08-08 01:15:34 +02:00
} ) ) ;
}
return resolve ( body . result [ Object . keys ( body . result ) [ 0 ] ] . claim ) ;
} ) ;
} ) ;
} ;