consolidated the resolve-and-update code
This commit is contained in:
parent
0af0ffb2da
commit
232591fdb3
6 changed files with 104 additions and 103 deletions
|
@ -50,9 +50,10 @@ function upsert (Model, values, condition) {
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publish (name, fileName, filePath, fileType, license, nsfw, socket, visitor) {
|
publish (name, fileName, filePath, fileType, license, nsfw, socket, visitor) {
|
||||||
|
console.log('nsfw:', nsfw);
|
||||||
// validate nsfw
|
// validate nsfw
|
||||||
if (typeof nsfw === 'string') {
|
if (typeof nsfw === 'string') {
|
||||||
nsfw = (nsfw.toLowerCase() === 'on');
|
nsfw = (nsfw.toLowerCase() === 'true');
|
||||||
}
|
}
|
||||||
// update the client
|
// update the client
|
||||||
socket.emit('publish-status', 'Your image is being published (this might take a second)...');
|
socket.emit('publish-status', 'Your image is being published (this might take a second)...');
|
||||||
|
@ -60,14 +61,14 @@ module.exports = {
|
||||||
visitor.event('Publish Route', 'Publish Request', filePath).send();
|
visitor.event('Publish Route', 'Publish Request', filePath).send();
|
||||||
// create the publish object
|
// create the publish object
|
||||||
const publishParams = createPublishParams(name, filePath, license, nsfw);
|
const publishParams = createPublishParams(name, filePath, license, nsfw);
|
||||||
// get a promise to publish
|
// 1. publish the file
|
||||||
lbryApi
|
lbryApi
|
||||||
.publishClaim(publishParams, fileName, fileType)
|
.publishClaim(publishParams, fileName, fileType)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
logger.info(`Successfully published ${fileName}`, result);
|
logger.info(`Successfully published ${fileName}`, result);
|
||||||
// google analytics
|
// google analytics
|
||||||
visitor.event('Publish Route', 'Publish Success', filePath).send();
|
visitor.event('Publish Route', 'Publish Success', filePath).send();
|
||||||
// update old record of create new one
|
// 2. update old record of create new one (update is in case the claim has been published before by this daemon)
|
||||||
upsert(
|
upsert(
|
||||||
db.File,
|
db.File,
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,9 +4,71 @@ const logger = require('winston');
|
||||||
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
||||||
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
||||||
|
|
||||||
function getClaimAndHandleResponse (claimUri, height, resolve, reject) {
|
function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
|
||||||
|
logger.debug(`A mysql record was found for ${claimId}`);
|
||||||
|
logger.debug('initiating resolve on claim to check outpoint');
|
||||||
|
// 1. resolve claim
|
||||||
lbryApi
|
lbryApi
|
||||||
.getClaim(claimUri)
|
.resolveUri(uri)
|
||||||
|
.then(result => {
|
||||||
|
// logger.debug('resolved result:', result);
|
||||||
|
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
||||||
|
const resolvedHeight = result[uri].claim.height;
|
||||||
|
logger.debug('database outpoint:', localOutpoint);
|
||||||
|
logger.debug('resolved outpoint:', resolvedOutpoint);
|
||||||
|
// 2. if the outpoint's match, no further work needed
|
||||||
|
if (localOutpoint === resolvedOutpoint) {
|
||||||
|
logger.debug('local outpoint matched');
|
||||||
|
// 2. if the outpoints don't match, check the height
|
||||||
|
} else if (localHeight > resolvedHeight) {
|
||||||
|
logger.debug('local height was greater than resolved height');
|
||||||
|
// 2. get the resolved claim
|
||||||
|
} else {
|
||||||
|
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
||||||
|
getClaimAndUpdate(uri, resolvedHeight);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
logger.error(`error resolving "${uri}" >> `, error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getClaimAndUpdate (uri, height) {
|
||||||
|
// 1. get the claim
|
||||||
|
lbryApi
|
||||||
|
.getClaim(uri)
|
||||||
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||||
|
logger.debug(' Get returned outpoint: ', outpoint);
|
||||||
|
// 2. update the entry in db
|
||||||
|
db.File
|
||||||
|
.update({
|
||||||
|
outpoint,
|
||||||
|
height, // note: height is coming from 'resolve', not 'get'.
|
||||||
|
fileName: file_name,
|
||||||
|
filePath: download_path,
|
||||||
|
fileType: mime_type,
|
||||||
|
nsfw : metadata.stream.metadata.nsfw,
|
||||||
|
}, {
|
||||||
|
where: {
|
||||||
|
name,
|
||||||
|
claimId: claim_id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(result => {
|
||||||
|
logger.debug('successfully updated mysql record', result);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
logger.error('sequelize error', error);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
logger.error(`error while getting claim for ${uri} >> `, error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getClaimAndHandleResponse (uri, height, resolve, reject) {
|
||||||
|
lbryApi
|
||||||
|
.getClaim(uri)
|
||||||
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||||
// create entry in the db
|
// create entry in the db
|
||||||
logger.debug('creating new record in db');
|
logger.debug('creating new record in db');
|
||||||
|
@ -21,6 +83,9 @@ function getClaimAndHandleResponse (claimUri, height, resolve, reject) {
|
||||||
fileType: mime_type,
|
fileType: mime_type,
|
||||||
nsfw : metadata.stream.metadata.nsfw,
|
nsfw : metadata.stream.metadata.nsfw,
|
||||||
})
|
})
|
||||||
|
.then(result => {
|
||||||
|
logger.debug('successfully created mysql record', result);
|
||||||
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error('sequelize create error', error);
|
logger.error('sequelize create error', error);
|
||||||
});
|
});
|
||||||
|
@ -36,95 +101,29 @@ function getClaimAndHandleResponse (claimUri, height, resolve, reject) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveAndUpdateIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
|
|
||||||
logger.debug('initiating resolve on local record to check outpoint');
|
|
||||||
// 1. resolve claim
|
|
||||||
lbryApi
|
|
||||||
.resolveUri(uri)
|
|
||||||
.then(result => {
|
|
||||||
// logger.debug('resolved result:', result);
|
|
||||||
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
|
||||||
const resolvedHeight = result[uri].claim.height;
|
|
||||||
logger.debug('database outpoint:', localOutpoint);
|
|
||||||
logger.debug('resolved outpoint:', resolvedOutpoint);
|
|
||||||
// 2. if the outpoint's match, no further work needed
|
|
||||||
if (localOutpoint === resolvedOutpoint) {
|
|
||||||
logger.debug('local outpoint matched');
|
|
||||||
// 2. if the outpoints don't match, check the height
|
|
||||||
} else if (localHeight > resolvedHeight) {
|
|
||||||
logger.debug('local height was greater than resolve height');
|
|
||||||
// 2. get the resolved claim
|
|
||||||
} else {
|
|
||||||
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
|
||||||
getClaimAndUpdate(uri, claimName, claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
logger.error(`error resolving "${uri}" >> `, error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getClaimAndUpdate (uri, claimName, claimId) {
|
|
||||||
// 1. get the claim
|
|
||||||
lbryApi
|
|
||||||
.getClaim(uri)
|
|
||||||
.then(({ outpoint, file_name, download_path, mime_type }) => {
|
|
||||||
logger.debug('getClaim outpoint', outpoint);
|
|
||||||
// 2. update the entry in db
|
|
||||||
db.File
|
|
||||||
.update({
|
|
||||||
outpoint: outpoint,
|
|
||||||
fileName: file_name,
|
|
||||||
filePath: download_path,
|
|
||||||
fileType: mime_type,
|
|
||||||
}, {
|
|
||||||
where: {
|
|
||||||
name : claimName,
|
|
||||||
claimId: claimId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
logger.debug('successfully updated mysql record', result);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
logger.error('sequelize error', error);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
logger.error(`error while getting claim for ${uri}`, error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getClaimByName (claimName) {
|
getClaimByName (claimName) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
// 1. get all free public claims
|
// 1. get the top free, public claims
|
||||||
getAllFreePublicClaims(claimName)
|
getAllFreePublicClaims(claimName)
|
||||||
.then(freePublicClaimList => {
|
.then(freePublicClaimList => {
|
||||||
const claimId = freePublicClaimList[0].claim_id;
|
|
||||||
const name = freePublicClaimList[0].name;
|
const name = freePublicClaimList[0].name;
|
||||||
const outpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
|
const claimId = freePublicClaimList[0].claim_id;
|
||||||
const uri = `${name}#${claimId}`;
|
const uri = `${name}#${claimId}`;
|
||||||
const height = freePublicClaimList[0].height;
|
const height = freePublicClaimList[0].height;
|
||||||
// 2. check to see if the file is available locally
|
// 2. check to see if the file is available locally
|
||||||
db.File
|
db.File
|
||||||
.findOne({ where: { name: name, claimId: claimId } }) // note: consolidate for es6?
|
.findOne({ where: { name: name, claimId: claimId } }) // note: destructure for es6?
|
||||||
.then(claim => {
|
.then(claim => {
|
||||||
// 3. if a matching claim_id is found locally, serve it
|
// 3. if a matching claim_id is found locally, serve it
|
||||||
if (claim) {
|
if (claim) {
|
||||||
logger.debug(`A mysql record was found for ${claimId}`);
|
|
||||||
// trigger update if needed
|
// trigger update if needed
|
||||||
if (claim.dataValues.outpoint === outpoint) {
|
updateFileIfNeeded(uri, name, claimId, claim.dataValues.outpoint, claim.dataValues.height);
|
||||||
logger.debug(`local outpoint matched for ${claimId}`);
|
// serve the file
|
||||||
} else {
|
|
||||||
logger.debug(`local outpoint did not match for ${claimId}`);
|
|
||||||
getClaimAndUpdate(uri, name, claimId);
|
|
||||||
}
|
|
||||||
// return the claim
|
|
||||||
resolve(claim.dataValues);
|
resolve(claim.dataValues);
|
||||||
// 3. otherwise use daemon to retrieve it
|
// 3. otherwise use daemon to retrieve it
|
||||||
} else {
|
} else {
|
||||||
// 4. get the claim and serve it
|
// get the claim and serve it
|
||||||
getClaimAndHandleResponse(uri, height, resolve, reject);
|
getClaimAndHandleResponse(uri, height, resolve, reject);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -144,14 +143,13 @@ module.exports = {
|
||||||
const uri = `${claimName}#${claimId}`;
|
const uri = `${claimName}#${claimId}`;
|
||||||
// 1. check locally for the claim
|
// 1. check locally for the claim
|
||||||
db.File
|
db.File
|
||||||
.findOne({ where: { name: claimName, claimId: claimId } }) // note: consolidate for es6?
|
.findOne({ where: { name: claimName, claimId: claimId } }) // note: destructure?
|
||||||
.then(claim => {
|
.then(claim => {
|
||||||
// 2. if a match is found locally, serve it
|
// 2. if a match is found locally, serve it
|
||||||
if (claim) {
|
if (claim) {
|
||||||
logger.debug(`A mysql record was found for ${claimId}`);
|
|
||||||
// trigger an update if needed
|
// trigger an update if needed
|
||||||
resolveAndUpdateIfNeeded(uri, claimName, claimId, claim.dataValues.outpoint, claim.dataValues.outpoint); // ok to just start asynch function, or need to add to a task cue?
|
updateFileIfNeeded(uri, claimName, claimId, claim.dataValues.outpoint, claim.dataValues.outpoint);
|
||||||
// resolve and send
|
// serve the file
|
||||||
resolve(claim.dataValues);
|
resolve(claim.dataValues);
|
||||||
// 2. otherwise use daemon to retrieve it
|
// 2. otherwise use daemon to retrieve it
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -110,7 +110,7 @@ document.getElementById('publish-submit').addEventListener('click', function(eve
|
||||||
uploader.addEventListener('start', function(event){
|
uploader.addEventListener('start', function(event){
|
||||||
var name = document.getElementById('publish-name').value;
|
var name = document.getElementById('publish-name').value;
|
||||||
var license = document.getElementById('publish-license').value;
|
var license = document.getElementById('publish-license').value;
|
||||||
var nsfw = document.getElementById('publish-nsfw').value;
|
var nsfw = document.getElementById('publish-nsfw').checked;
|
||||||
event.file.meta.name = name;
|
event.file.meta.name = name;
|
||||||
event.file.meta.license = license;
|
event.file.meta.license = license;
|
||||||
event.file.meta.nsfw = nsfw;
|
event.file.meta.nsfw = nsfw;
|
||||||
|
|
|
@ -128,7 +128,7 @@ socket.on('publish-complete', function(msg){
|
||||||
{
|
{
|
||||||
text: 'Check out my meme creation on the LBRY blockchain!',
|
text: 'Check out my meme creation on the LBRY blockchain!',
|
||||||
url: 'https://spee.ch/' + directUrl,
|
url: 'https://spee.ch/' + directUrl,
|
||||||
hashtags: 'MemeFodder',
|
hashtags: 'LBRYMemeFodder',
|
||||||
via: 'lbryio'
|
via: 'lbryio'
|
||||||
})
|
})
|
||||||
.then( function( el ) {
|
.then( function( el ) {
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
<div class="col-md-12">
|
<div class="col-md-12">
|
||||||
<div class="card" id="publish">
|
<div class="card" id="publish">
|
||||||
<div class="card-title card-block grey lighten-1 white-text">
|
<div class="card-title card-block grey lighten-1 white-text">
|
||||||
<h2>#MemeFodder</h2>
|
<h2>#LBRYMemeFodder</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-block">
|
<div class="card-block">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-12">
|
<div class="col-md-12">
|
||||||
<h3>Congratulations, you found the /meme-fodder game</h3>
|
<h3>Congratulations, you found the /meme-fodder game</h3>
|
||||||
<p>Create a meme based on the current <i>lbry://meme-fodder</i> claims using the tool below. Got a masterpiece? <a href="https://twitter.com/hashtag/MemeFodder" target="_blank">Share it with the community</a> and see what they think!</p>
|
<p>Create a meme based on the current <i>lbry://meme-fodder</i> claims using the tool below. Got a masterpiece? <a href="https://twitter.com/hashtag/LBRYMemeFodder" target="_blank">Share it with the community</a> and see what they think!</p>
|
||||||
<p>Spee.ch/meme-fodder/play uses the free, public images at the claim <a href="lbry://meme-fodder">lbry://meme-fodder</a>. Want to put a different image on the chopping block? Go publish it!</p>
|
<p>Spee.ch/meme-fodder/play uses the free, public images at the claim <a href="lbry://meme-fodder">lbry://meme-fodder</a>. Want to put a different image on the chopping block? Go publish it!</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -13,23 +13,25 @@
|
||||||
<img id="image-preview" src="" height="200" alt="Image preview..."/>
|
<img id="image-preview" src="" height="200" alt="Image preview..."/>
|
||||||
</div>
|
</div>
|
||||||
<div id="publish-active-area" class="col-md-6">
|
<div id="publish-active-area" class="col-md-6">
|
||||||
<div class="md-form">
|
<form>
|
||||||
<input type="text" id="publish-name" value="Name" class="form-control">
|
<div class="md-form">
|
||||||
</div>
|
<input type="text" id="publish-name" value="Name" class="form-control">
|
||||||
<fieldset class="form-group">
|
</div>
|
||||||
License:
|
<fieldset class="form-group">
|
||||||
<select type="text" id="publish-license" name="license" value="license">
|
License:
|
||||||
<option value="Public Domain">Public Domain</option>
|
<select type="text" id="publish-license" name="license" value="license">
|
||||||
<option value="Creative Commons">Creative Commons</option>
|
<option value="Public Domain">Public Domain</option>
|
||||||
</select>
|
<option value="Creative Commons">Creative Commons</option>
|
||||||
</fieldset>
|
</select>
|
||||||
<fieldset class="form-group">
|
</fieldset>
|
||||||
<input type="checkbox" id="publish-nsfw">
|
<fieldset class="form-group">
|
||||||
<label for="publish-nsfw">NSFW</label>
|
<input type="checkbox" id="publish-nsfw">
|
||||||
</fieldset>
|
<label for="publish-nsfw">NSFW</label>
|
||||||
<p><i>By clicking 'Publish' I attest that I have read and agree to the <a href="https://lbry.io/termsofservice" target="_blank">LBRY terms of service</a>.</i></p>
|
</fieldset>
|
||||||
<button id="publish-submit">Publish</button>
|
<p><i>By clicking 'Publish' I attest that I have read and agree to the <a href="https://lbry.io/termsofservice" target="_blank">LBRY terms of service</a>.</i></p>
|
||||||
<button id="publish-reset">Reset</button>
|
<button id="publish-submit">Publish</button>
|
||||||
|
<button id="publish-reset">Reset</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue