2017-06-17 02:48:51 +02:00
|
|
|
var lbryApi = require('../helpers/libraries/lbryApi.js');
|
2017-06-15 22:06:35 +02:00
|
|
|
var db = require("../models");
|
2017-06-14 00:39:38 +02:00
|
|
|
|
2017-06-17 02:48:51 +02:00
|
|
|
var getAllFreePublicClaims = require("../helpers/functions/getAllFreePublicClaims.js");
|
|
|
|
var isFreePublicClaim = require("../helpers/functions/isFreePublicClaim.js");
|
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 = {
|
2017-06-17 02:48:51 +02:00
|
|
|
getClaimByName: function(claimName){
|
2017-06-14 00:39:38 +02:00
|
|
|
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-17 02:48:51 +02:00
|
|
|
getClaimByClaimId: 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-14 02:13:38 +02:00
|
|
|
}
|
2017-06-14 00:39:38 +02:00
|
|
|
}
|