2017-06-14 00:39:38 +02:00
|
|
|
var path = require('path');
|
|
|
|
var axios = require('axios');
|
2017-06-14 02:13:38 +02:00
|
|
|
var lbryApi = require('./lbryApi');
|
2017-06-15 22:06:35 +02:00
|
|
|
var db = require("../models");
|
2017-06-14 00:39:38 +02:00
|
|
|
|
|
|
|
function filterForFreePublicClaims(claimsListArray){
|
2017-06-15 00:02:38 +02:00
|
|
|
//console.log("claims list:", claimsListArray)
|
2017-06-14 00:39:38 +02:00
|
|
|
if (!claimsListArray) {
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
var freePublicClaims = claimsListArray.filter(function(claim){
|
2017-06-15 00:02:38 +02:00
|
|
|
if (!claim.value){
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-14 00:39:38 +02:00
|
|
|
return (((claim.value.stream.metadata.license.indexOf('Public Domain') != -1) || (claim.value.stream.metadata.license.indexOf('Creative Commons') != -1)) &&
|
|
|
|
(!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee === 0));
|
|
|
|
});
|
|
|
|
return freePublicClaims;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isFreePublicClaim(claim){
|
2017-06-15 00:47:10 +02:00
|
|
|
console.log(">> isFreePublicClaim? claim:", claim);
|
2017-06-14 00:39:38 +02:00
|
|
|
if ((claim.value.stream.metadata.license === 'Public Domain' || claim.value.stream.metadata.license === 'Creative Commons') &&
|
|
|
|
(!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee.amount === 0)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function orderTopClaims(claimsListArray){
|
2017-06-15 00:47:10 +02:00
|
|
|
console.log(">> orderTopClaims");
|
2017-06-14 00:39:38 +02:00
|
|
|
claimsListArray.sort(function(claimA, claimB){
|
|
|
|
if (claimA.amount === claimB.amount){
|
|
|
|
return (claimA.height > claimB.height);
|
|
|
|
} else {
|
|
|
|
return (claimA.amount < claimB.amount);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return claimsListArray;
|
|
|
|
}
|
|
|
|
|
2017-06-15 00:02:38 +02:00
|
|
|
function getAllFreePublicClaims(claimName){
|
2017-06-14 02:13:38 +02:00
|
|
|
var deferred = new Promise(function(resolve, reject){
|
|
|
|
// make a call to the daemon to get the claims list
|
|
|
|
lbryApi.getClaimsList(claimName)
|
|
|
|
.then(function(data){
|
|
|
|
var claimsList = data.result.claims;
|
|
|
|
console.log(">> Number of claims:", claimsList.length)
|
|
|
|
// return early if no claims were found
|
|
|
|
if (claimsList.length === 0){
|
|
|
|
reject("NO_CLAIMS");
|
|
|
|
console.log("exiting due to lack of claims");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// filter the claims to return only free, public claims
|
|
|
|
var freePublicClaims = filterForFreePublicClaims(claimsList);
|
|
|
|
// return early if no free, public claims were found
|
|
|
|
if (!freePublicClaims || (freePublicClaims.length === 0)){
|
|
|
|
reject("NO_FREE_PUBLIC_CLAIMS");
|
|
|
|
console.log("exiting due to lack of free or public claims");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// order the claims
|
|
|
|
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
|
|
|
// resolve the promise
|
|
|
|
resolve(orderedPublicClaims);
|
|
|
|
}).catch(function(error){
|
|
|
|
console.log(">> 'claim_list' error");
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-14 00:39:38 +02:00
|
|
|
});
|
2017-06-14 02:13:38 +02:00
|
|
|
return deferred;
|
2017-06-14 00:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 21:29:02 +02:00
|
|
|
function getClaimAndHandleResponse(claimUri, resolve, reject){
|
|
|
|
lbryApi.getClaim(claimUri)
|
|
|
|
.then(function(result){
|
|
|
|
resolve({
|
|
|
|
file_name: result.file_name,
|
|
|
|
file_path: result.download_path,
|
|
|
|
file_type: result.mime_type
|
|
|
|
});
|
|
|
|
}).catch(function(error){
|
|
|
|
reject(error)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-14 00:39:38 +02:00
|
|
|
module.exports = {
|
|
|
|
getClaimBasedOnNameOnly: function(claimName){
|
|
|
|
var deferred = new Promise(function (resolve, reject){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> lbryHelpers >> getClaim BasedOnNameOnly:", claimName);
|
2017-06-15 23:37:19 +02:00
|
|
|
// get all free public claims
|
2017-06-14 02:13:38 +02:00
|
|
|
getAllFreePublicClaims(claimName)
|
|
|
|
.then(function(freePublicClaimList){
|
2017-06-16 21:29:02 +02:00
|
|
|
console.log(">> Decided on public claim id:", freePublicClaimList[0].claim_id);
|
|
|
|
var freePublicClaimOutpoint = freePublicClaimList[0].txid + ":" + freePublicClaimList[0].nout;
|
|
|
|
var freePublicClaimUri = freePublicClaimList[0].name + "#" + freePublicClaimList[0].claim_id;
|
2017-06-15 23:37:19 +02:00
|
|
|
// check to see if the file is available locally
|
2017-06-16 21:29:02 +02:00
|
|
|
db.File.findOne({where: { claim_id: freePublicClaimList[0].claim_id }})
|
2017-06-15 23:37:19 +02:00
|
|
|
.then(function(claim){
|
2017-06-16 21:29:02 +02:00
|
|
|
// if a found locally...
|
2017-06-15 23:37:19 +02:00
|
|
|
if (claim){
|
2017-06-16 21:29:02 +02:00
|
|
|
console.log(">> A matching claim_id was found locally");
|
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === freePublicClaimOutpoint){
|
|
|
|
console.log(">> Local outpoint matched");
|
|
|
|
resolve(claim.dataValues);
|
|
|
|
// if the outpoint's don't match, fetch updated claim
|
|
|
|
} else {
|
|
|
|
console.log(">> local outpoint did not match");
|
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
|
|
|
}
|
2017-06-15 23:37:19 +02:00
|
|
|
// ... otherwise use daemon to retrieve it
|
|
|
|
} else {
|
2017-06-16 21:29:02 +02:00
|
|
|
// 'get' the claim
|
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject)
|
2017-06-15 23:37:19 +02:00
|
|
|
}
|
2017-06-14 02:13:38 +02:00
|
|
|
}).catch(function(error){
|
2017-06-15 23:37:19 +02:00
|
|
|
reject(error);
|
2017-06-14 02:13:38 +02:00
|
|
|
});
|
|
|
|
}).catch(function(error){
|
2017-06-14 00:39:38 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-06-15 22:06:35 +02:00
|
|
|
getClaimBasedOnUri: function(claimName, claimId){
|
2017-06-14 00:39:38 +02:00
|
|
|
var deferred = new Promise(function (resolve, reject){
|
2017-06-15 22:06:35 +02:00
|
|
|
var uri = claimName + "#" + claimId;
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri);
|
2017-06-16 21:29:02 +02:00
|
|
|
// resolve the Uri
|
|
|
|
lbryApi.resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne()
|
|
|
|
.then(function(result){ // note should just be 'result' returned.
|
|
|
|
// get the outpoint
|
|
|
|
var resolvedOutpoint = result[uri].claim.txid + ":" + result[uri].claim.nout;
|
|
|
|
// check locally for the claim
|
|
|
|
db.File.findOne({where: { claim_id: claimId }})
|
|
|
|
.then(function(claim){
|
|
|
|
// if a found locally...
|
|
|
|
if (claim){
|
|
|
|
console.log(">> A matching claim_id was found locally");
|
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === resolvedOutpoint){
|
|
|
|
console.log(">> Local outpoint matched");
|
|
|
|
resolve(claim.dataValues);
|
|
|
|
// if the outpoint's don't match, fetch updated claim
|
|
|
|
} else {
|
|
|
|
console.log(">> Local outpoint did not match");
|
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
|
|
|
}
|
|
|
|
// ... otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// check to make sure it is free and public (note: no need for another resolve?)
|
|
|
|
if (isFreePublicClaim(result[uri].claim)){
|
2017-06-15 22:06:35 +02:00
|
|
|
// 'get' the claim
|
2017-06-16 21:29:02 +02:00
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
2017-06-15 22:06:35 +02:00
|
|
|
} else {
|
|
|
|
reject("NO_FREE_PUBLIC_CLAIMS");
|
|
|
|
}
|
2017-06-16 21:29:02 +02:00
|
|
|
}
|
|
|
|
}).catch(function(error){
|
|
|
|
reject(error)
|
|
|
|
});
|
2017-06-14 00:39:38 +02:00
|
|
|
}).catch(function(error){
|
2017-06-15 22:06:35 +02:00
|
|
|
reject(error);
|
2017-06-15 23:37:19 +02:00
|
|
|
});
|
2017-06-14 00:39:38 +02:00
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-06-16 21:29:02 +02:00
|
|
|
getAllClaims: function(claimName){
|
2017-06-14 02:13:38 +02:00
|
|
|
return getAllFreePublicClaims(claimName);
|
|
|
|
}
|
2017-06-14 00:39:38 +02:00
|
|
|
}
|