Publishing to the blockchain works now

This commit is contained in:
ポール ウェッブ 2018-08-17 17:22:19 -05:00
parent 801068f200
commit 84d69d6ba8
5 changed files with 107 additions and 419 deletions

View file

@ -15,6 +15,7 @@ const stringifyObject = require("stringify-object");
const randomString = local("/helpers/random-string");
const loadLanguages = require("prismjs/components/");
const logSlackError = local("/helpers/slack");
const publishMeme = local("/helpers/publish-meme");
const uploadImage = local("/helpers/upload-image");
loadLanguages(["json"]);
@ -56,43 +57,81 @@ module.exports = exports = (data, socket) => {
if (resolveMethod === "publish") {
apiRequestMethod = "PUT";
body.bid = 0.001; // Hardcoded publish amount
// Required for publishing
body.author = "lbry.tech";
body.bid = 0.0001; // Hardcoded publish amount
body.description = dataDetails.description;
body.file_path = dataDetails.file_path; // just base64 string
body.language = dataDetails.language;
body.license = dataDetails.license;
body.name = dataDetails.name.replace(/\s/g, "") + randomString(10);
body.name = dataDetails.name.replace(/\s/g, "-") + randomString(10); // underscores are not allowed?
body.nsfw = dataDetails.nsfw;
body.title = dataDetails.title;
// Gotta let the blockchain know what to save
body.file_path = dataDetails.file_path; // just base64 string
return uploadImage(body.file_path).then(uploadResponse => {
// if (uploadResponse.status !== "ok") return;
// console.log("————— RESPONSE");
// console.log(uploadResponse);
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;
body.filename = uploadResponse.filename;
// 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 => {
// console.log("————— ERROR");
// console.log(uploadError);
return publishMeme(body).then(publishResponse => {
let explorerNotice = "";
socket.send(JSON.stringify({
"details": "Image upload failed",
"message": "notification",
"type": "error"
}));
if (publishResponse.error) {
socket.send(JSON.stringify({
"details": "Meme publish 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"
);
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;
return;
}
if (
publishResponse.result &&
publishResponse.result.txid
) explorerNotice = `
<p>If you want proof of the tip you just gave, <a href="https://explorer.lbry.io/tx/${publishResponse.result.txid}" target="_blank" title="Your tip, on our blockchain explorer" rel="noopener noreferrer">check it out</a> on our blockchain explorer!</p>
`;
const renderedCode = prism.highlight(stringifyObject(publishResponse, { indent: " ", singleQuotes: false }), prism.languages.json, "json");
return socket.send(JSON.stringify({
"html": raw(`
<h3>Response</h3>
${explorerNotice}
<pre><code class="language-json">${renderedCode}</code></pre>
<script>$("#temp-loader").hide();</script>
`),
"message": "updated html",
"selector": `#example${data.example}-result`
}));
});
});
}

26
helpers/publish-meme.js Normal file
View file

@ -0,0 +1,26 @@
"use strict";
// P A C K A G E
const request = require("request-promise-native");
// E X P O R T
module.exports = exports = publishMetadata => new Promise((resolve, reject) => { // eslint-disable-line
return request({
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://daemon.lbry.tech/publish" }`
}, (error, response, body) => {
if (error) resolve(error);
resolve(body);
});
});

View file

@ -21,7 +21,6 @@ module.exports = exports = imageSource => new Promise((resolve, reject) => { //
url: `${process.env.NODE_ENV === "development" ? "http://localhost:5200/image" : "https://daemon.lbry.tech/image" }`
}, (error, response, body) => {
if (error) resolve(error);
body = JSON.parse(body);
resolve(body);
});
});

View file

@ -1,376 +0,0 @@
"use strict";
// P A C K A G E
const fs = require("graceful-fs");
const html = require("choo-async/html");
const raw = require("nanohtml/raw");
// E X P O R T
module.exports = exports = () => async () => html`
<div class="hook" id="hook">
<nav class="hook__navigation" id="hook-navigation">
<div class="inner-wrap"> <!--/ TODO: Save tutorial position to localStorage /-->
<button class="hook__navigation__step" data-action="tour, step 1" type="button">
<span class="number">1</span>
Resolve a claim
</button>
<button class="hook__navigation__step" data-action="tour, step 2" type="button">
<span class="number">2</span>
Publish content
</button>
<button class="hook__navigation__step" data-action="tour, step 3" type="button">
<span class="number">3</span>
Support with LBC
</button>
</div>
</nav>
${step1()}
${step2()}
${step3()}
</div>
<script>${raw(fs.readFileSync("./views/partials/tour-scripts.js", "utf-8"))}</script>
`;
function step1() {
/**
Step 1 loading steps:
- exampleCode !== ""
<pre><code class="bash"><span v-html="highlight('bash', exampleCode)"></span></code></pre>
- isLoading
<div class="loader"></div>
- jsonData
<p style="text-align: center;">Success! Here is the response for <strong>lbry://{{ address }}</strong>:</p>
<pre><code class="json"><span v-html="highlight('json', jsonData)"></span></code></pre>
<a href="#" class="__button-black" data-action="go to step 2" title="Proceed to step two">Go to next step</a>
- TODO:
[] Create message for error
[ ] Scroll to top of page when selecting a claim
*/
return html`
<section class="hook__page" id="step1-page">
<header class="hook__page__hero">
<div class="inner-wrap">
<h1>Learn the LBRY protocol by examples</h1>
<p>Let's start by getting the associated metadata for a claim.</p>
<div class="hook__page__hero__claim">
<span>lbry://</span><input id="fetch-claim-uri" placeholder="&thinsp;Claim URI goes here" type="text"/>
<button class="button" data-action="execute claim" type="button">Execute</button>
</div>
</div>
</header>
<div class="hook__page__content inner-wrap" id="step1-placeholder"></div>
<div class="hook__page__content inner-wrap" id="step1-selections">
<p style="text-align: center;">&hellip;or select a live example from below</p>
<div class="hook__page__content__card">
<img data-action="choose claim" data-claim-id="itsadisaster" src="https://spee.ch/0654f2e2322ccfefa02a956d252df9ac7611d8b0/placeholder-itsadisaster.jpeg">
<div data-action="choose claim" data-claim-id="itsadisaster">
<h4>It's a Disaster</h4>
<p class="account">Anonymous</p>
</div>
</div>
<div class="hook__page__content__card">
<img data-action="choose claim" data-claim-id="unbubbled1-1" src="https://spee.ch/b1bd330e048fc22dc7bf941c33dd8245eef492c1/unbubbled.png">
<div data-action="choose claim" data-claim-id="unbubbled1-1">
<h4>Unbubbled with Jamie King, Ep1.1 &mdash; Bitcoin, Boom or Bust</h4>
<p class="account">@Unbubbled</p>
</div>
</div>
<div class="hook__page__content__card">
<img data-action="choose claim" data-claim-id="fortnite-top-stream-moments-nickatnyte" src="https://spee.ch/9880947df41af880bc19724ceddd1cce957a07e2/placeholder-fortninte.jpeg">
<div data-action="choose claim" data-claim-id="fortnite-top-stream-moments-nickatnyte">
<h4>FORTNITE TOP STREAM MOMENTS &mdash; Nickatnyte &amp; GamingwithMolt</h4>
<p class="account">@nickatnyte</p>
</div>
</div>
<div class="hook__page__content__card">
<img data-action="choose claim" data-claim-id="six" src="https://spee.ch/a3b8258478ad88954f42f6ac3427eab380720f60/placeholder-lbrymine.png">
<div data-action="choose claim" data-claim-id="six">
<h4>LBRY Coin (LBC) GPU Miner for AMD and NVIDIA</h4>
<p class="account">Anonymous</p>
</div>
</div>
</div>
</section>
`;
}
function step2() {
/**
Step 2 loading steps:
- exampleCode !== ''
<pre style="clear: both;"><code class="bash"><span v-html="highlight('bash', exampleCode)"></span></code></pre>
- isLoading
<div class="loader"></div>
- jsonData
<p style="text-align: center;">Success!<br/>
<a class="__button-black" v-bind:href="'http://explorer.lbry.io/tx/'+txid">See the transaction on explorer.lbry.io</a>
</p>
<p style="text-align: center;">Here is the raw response:</p>
<pre><code class="json"><span v-html="highlight('json', jsonData)"></span></code></pre>
Meme submission process:
- `PUT` request to `http://daemon.lbry.tech/images.php`:
- headers: "Content-Type": "text/plain"
- qs: access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN
- body: document.getElementById("meme-canvas").toDataURL("image/jpeg", 0.6)
- response should be parsed as JSON
- //
- socket emit "fetch metadata":
- bid: 0.001, // hard-coded on the back-end
- description: component.description,
- file_path: uploadResponse.body.filename,
- language: component.language,
- license: component.license,
- method: "publish",
- name: component.title,
- nsfw: component.nsfw,
- title: component.title
- socket emit error from back-end if any field is missing
Process after submitting meme:
- isLoading appears
- exampleCode and jsonData replace `#step2-placeholder` contents
- next button should exist
Issues:
- image isn't uploaded to spee.ch
- response is blank
- response should have link to upload on Blockchain Explorer
- NSFW flag should work
*/
const images = [
{
alt: "Carl Sagan",
src: "/assets/media/images/carlsagan2.jpg"
},
{
alt: "Doge",
src: "/assets/media/images/doge-meme.jpg"
},
{
alt: "LBRY Logo With Green Background",
src: "/assets/media/images/lbry-green.png"
}
];
const memePlaceholderData = {
bottomLine: {
placeholder: "Top line",
value: "that I made"
},
description: {
placeholder: "Description",
value: "Check out this image I published to LBRY via lbry.tech"
},
topLine: {
placeholder: "Top line",
value: "This is an example meme"
},
title: {
placeholder: "Title",
value: "Dank Meme Supreme da Cheese"
}
};
const renderedImages = [];
for (const image of images) {
renderedImages.push(`<img alt="${image.alt}" class="hook__page__content__meme__thumbnail" src="${image.src}"/>`);
}
return html`
<section class="hook__page" id="step2-page" style="display: none;">
<header class="hook__page__hero">
<div class="inner-wrap">
<h1>Publish your content on the blockchain</h1>
<p>Upload an image to the blockchain and you are able to view it on the <a href="http://explorer.lbry.io" rel="noopener noreferrer" target="_blank" title="LBRY Blockchain Explorer">LBRY Blockchain Explorer</a>.</p>
</div>
</header>
<div class="hook__page__content inner-wrap">
<div class="hook__page__content__meme left">
<img alt="Base image for LBRY meme creator" id="base-image" style="height: 0; visibility: hidden;"/>
<canvas id="meme-canvas" height="300" width="400">Unfortunately, it looks like canvas is <strong>not supported</strong> in your browser</canvas>
${renderedImages}
</div>
<form class="hook__page__content__meme right">
<h2>Image Text</h2>
<fieldset>
<label for="meme-top-line">Top line</label>
<input id="meme-top-line" name="meme-top-line" placeholder="${memePlaceholderData.topLine.placeholder}" spellcheck="false" type="text" value="${memePlaceholderData.topLine.value}" required/>
</fieldset>
<fieldset>
<label for="meme-bottom-line">Bottom line</label>
<input id="meme-bottom-line" name="meme-bottom-line" placeholder="${memePlaceholderData.bottomLine.placeholder}" spellcheck="false" type="text" value="${memePlaceholderData.bottomLine.value}" required/>
</fieldset>
<h2 class="__metadata">Metadata</h2>
<fieldset>
<label for="meme-title">Title</label>
<input id="meme-title" name="meme-title" placeholder="${memePlaceholderData.title.placeholder}" spellcheck="false" type="text" value="${memePlaceholderData.title.value}" required/>
</fieldset>
<fieldset>
<label for="meme-description">Description</label>
<textarea id="meme-description" name="meme-description" placeholder="${memePlaceholderData.description.placeholder}" spellcheck="false" type="text" required>${memePlaceholderData.description.value}</textarea>
</fieldset>
<fieldset>
<label for="meme-language">Language</label>
<select id="meme-language" name="meme-language">
<option value="ar">Arabic</option>
<option value="zh">Chinese (Mandarin)</option>
<option value="en">English</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="it">Italian</option>
<option value="jp">Japanese</option>
<option value="ru">Russian</option>
<option value="es">Spanish</option>
<option value="">Not specified</option>
</select>
</fieldset>
<fieldset>
<label for="meme-license">License</label>
<select id="meme-license" name="meme-license" required>
<option value="Public Domain">Public Domain</option>
<option value="Creative Commons Attribution 4.0 International">Creative Commons Attribution 4.0 International</option>
<option value="Creative Commons Attribution-ShareAlike 4.0 International">Creative Commons Attribution-ShareAlike 4.0 International</option>
<option value="Creative Commons Attribution-NoDerivatives 4.0 International">Creative Commons Attribution-NoDerivatives 4.0 International</option>
<option value="Creative Commons Attribution-NonCommercial 4.0 International">Creative Commons Attribution-NonCommercial 4.0 International</option>
<option value="Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International</option>
<option value="Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International</option>
<option value="None">None</option>
</select>
</fieldset>
<fieldset>
<label><input id="meme-nsfw-flag" name="nsfw" type="checkbox"/>NSFW</label>
</fieldset>
<fieldset>
<button data-action="upload image" class="__button-black" type="button">Submit</button>
</fieldset>
</form>
</div>
<div class="hook__page__content inner-wrap" id="step2-placeholder"></div>
</section>
`;
}
function step3() {
/**
Step 3 loading steps:
- exampleCode !== ''
<pre><code class="bash"><span v-html="highlight('bash', exampleCode)"></span></code></pre>
- isLoading
<div class="loader"></div>
- jsonData
<p style="text-align: center;">Success! Here is the response:</p>
<pre><code class="json"><span v-html="highlight('json', jsonData)"></span></code></pre>
*/
return html`
<section class="hook__page" id="step3-page" style="display: none;">
<header class="hook__page__hero">
<div class="inner-wrap">
<h1>Support your favorite content creators with LBRY</h1>
<p>Send LBRY coins to claim addresses and the owner will receive it in their wallet.</p>
<p>To send LBC to someone, you need either their wallet address or claim ID. You can get claim IDs by using the resolve method in <a href="#" v-on:click.prevent.stop="goTo(1)" title="Go back to step one">the first step</a>, or you can use the examples below.</p>
<div class="hook__page__hero__support">
<input type="text" v-model="claimId" placeholder="Claim ID goes here"/>
<input type="number" v-model="amount" disabled title="You can set this value to any amount of LBC in your wallet, but for demonstration purposes we have hardcoded it to 0.01"/><span>LBC</span>
<a href="#" v-on:click.prevent="send" class="button" title="Execute claim">Execute</a>
</div>
</div>
</header>
<div class="hook__page__content inner-wrap" id="step3-placeholder"></div>
<div class="hook__page__content inner-wrap" id="step3-selections">
<p style="text-align: center;">Click on any of the claims below to support them!</p>
<div class="hook__page__content__card"> <!--/ fbdcd44a97810522d23d5f1335b8ca04be9d776c /-->
<img data-action="choose claim" data-claim-id="itsadisaster" src="https://spee.ch/0654f2e2322ccfefa02a956d252df9ac7611d8b0/placeholder-itsadisaster.jpeg">
<div data-action="choose claim" data-claim-id="itsadisaster">
<h4>It's a Disaster</h4>
<p class="account">Anonymous</p>
</div>
</div>
<div class="hook__page__content__card"> <!--/ de7f7fa33e8d879b2bae7238d2bdf827a39f9301 /-->
<img data-action="choose claim" data-claim-id="unbubbled1-1" src="https://spee.ch/b1bd330e048fc22dc7bf941c33dd8245eef492c1/unbubbled.png">
<div data-action="choose claim" data-claim-id="unbubbled1-1">
<h4>Unbubbled with Jamie King, Ep1.1 &mdash; Bitcoin, Boom or Bust</h4>
<p class="account">@Unbubbled</p>
</div>
</div>
<div class="hook__page__content__card"> <!--/ 5b7c7a202201033d99e1be2930d290c127c0f4fe /-->
<img data-action="choose claim" data-claim-id="fortnite-top-stream-moments-nickatnyte" src="https://spee.ch/9880947df41af880bc19724ceddd1cce957a07e2/placeholder-fortninte.jpeg">
<div data-action="choose claim" data-claim-id="fortnite-top-stream-moments-nickatnyte">
<h4>FORTNITE TOP STREAM MOMENTS &mdash; Nickatnyte &amp; GamingwithMolt</h4>
<p class="account">@nickatnyte</p>
</div>
</div>
<div class="hook__page__content__card"> <!--/ a1372cf5523885f5923237bfe522f02f5f054362 /-->
<img data-action="choose claim" data-claim-id="six" src="https://spee.ch/a3b8258478ad88954f42f6ac3427eab380720f60/placeholder-lbrymine.png">
<div data-action="choose claim" data-claim-id="six">
<h4>LBRY Coin (LBC) GPU Miner for AMD and NVIDIA</h4>
<p class="account">Anonymous</p>
</div>
</div>
</div>
</section>
`;
}

View file

@ -43,8 +43,8 @@ $("body").on("click", "[data-action]", event => {
$(".tour__sidebar__example").removeClass("active");
$(".tour__sidebar__example:nth-child(1)").addClass("active");
$("#tour-loader").empty().show(); // .html("");
$("#tour-results").empty().show(); // .html("");
$("#tour-loader").empty().show();
$("#tour-results").empty().show();
send(JSON.stringify({
"message": `request for ${data.action}`
@ -63,8 +63,8 @@ $("body").on("click", "[data-action]", event => {
$(".tour__sidebar__example").removeClass("active");
$(".tour__sidebar__example:nth-child(2)").addClass("active");
$("#tour-loader").empty().show(); // .html("");
$("#tour-results").empty().show(); // .html("");
$("#tour-loader").empty().show();
$("#tour-results").empty().show();
send(JSON.stringify({
"message": `request for ${data.action}`
@ -83,8 +83,8 @@ $("body").on("click", "[data-action]", event => {
$(".tour__sidebar__example").removeClass("active");
$(".tour__sidebar__example:nth-child(3)").addClass("active");
$("#tour-loader").empty().show(); // .html("");
$("#tour-results").empty().show(); // .html("");
$("#tour-loader").empty().show();
$("#tour-results").empty().show();
send(JSON.stringify({
"message": `request for ${data.action}`
@ -108,8 +108,8 @@ $("body").on("click", ".tour__content__meme__canvas__thumbnail", event => {
updateCanvas(event.currentTarget);
});
$("#fetch-claim-uri").on("keyup", function (e) {
const key = e.keyCode ? e.keyCode : e.which;
$("#fetch-claim-uri").on("keyup", event => {
const key = event.keyCode ? event.keyCode : event.which;
if (key === 13 && $("#fetch-claim-uri").val()) fetchMetadata(1, $("#fetch-claim-uri").val());
});
@ -189,6 +189,16 @@ curl --header <span class="token string">"Content-Type: application/json"</span>
"example": exampleNumber
}));
$("#tour-results").html(`
<pre><code class="language-bash">
<span class="token comment"># This will be updated soon</span>
</code></pre>
<div class="loader" id="temp-loader"></div>
<div id="example2-result"></div>
`);
$("#tour-loader").hide();
break;
case 3:
@ -230,16 +240,6 @@ function getMemeInfo() { // TODO: Error handling
title: $("#meme-title").val()
};
/*
info.description = $("#meme-description").val();
info.file_path = $("#meme-canvas")[0].toDataURL("image/jpeg", 0.6);
info.language = $("#meme-language").val();
info.license = $("#meme-license").val();
info.name = $("#meme-title").val();
info.nsfw = $("#meme-nsfw-flag")[0].checked;
info.title = $("#meme-title").val();
*/
return info;
}