updated teh name/all/ route

This commit is contained in:
bill bittner 2017-05-31 17:00:13 -07:00
parent 0bb0a89f17
commit 6098537c12
3 changed files with 31 additions and 79 deletions

View file

@ -102,46 +102,40 @@ module.exports = {
})
.then(function (response) {
console.log(">> 'claim_list' success");
var claimsList = response.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 orderedPublcClaims = orderTopClaims(freePublicClaims);
// create the uri for the first (selected) claim
console.log(">> ordered free public claims");
var freePublicClaimUri = "lbry://" + orderedPublcClaims[0].name + "#" + orderedPublcClaims[0].claim_id;
console.log(">> your free public claim URI:", freePublicClaimUri);
// fetch the image to display
getClaimWithUri(freePublicClaimUri, resolve, reject);
})
.catch(function(error){
console.log(">> 'claim_list' error:", error);
// reject the promise with an approriate message
if (error.code === "ECONNREFUSED"){
reject("Connection refused. The daemon may not be running.")
} else {
} else if (error.response.data.error) {
reject(error.response.data.error);
} else {
reject(error);
};
return;
});
@ -175,34 +169,41 @@ module.exports = {
).then(function (response) {
console.log(">> 'claim_list' success");
console.log(">> Number of claims:", response.data.result.claims.length)
console.log(">> 'claim_list' success");
var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found
if (response.data.result.claims.length === 0){
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
if (claimsList.length === 0){
reject("NO_CLAIMS");
console.log("exiting due to lack of claims");
return;
}
// filter the claims to return free, public claims
var freePublicClaims = filterForFreePublicClaims(response.data.result.claims);
// 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)){
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
reject("NO_FREE_PUBLIC_CLAIMS");
console.log("exiting due to lack of free or public claims");
return;
}
console.log(">> Number of free public claims:", freePublicClaims.length);
// order the claims
var orderedPublicClaims = orderTopClaims(freePublicClaims);
var orderedPublcClaims = orderTopClaims(freePublicClaims);
// serve the response
/*
to do: rather than returning json, serve a page of all these claims
*/
res.status(200).send(orderedPublicClaims);
resolve(orderedPublicClaims);
}).catch(function(error){
console.log(">> 'claim_list' error:", error.response.data);
// serve the response
res.status(500).send(JSON.stringify({msg: "An error occurred while finding the claim list.", err: error.response.data.error.message}));
console.log(">> 'claim_list' error:", error);
if (error.code === "ECONNREFUSED"){
reject("Connection refused. The daemon may not be running.")
} else if (error.response.data.error) {
reject(error.response.data.error);
} else {
reject(error);
};
})
});
return deffered;
return deferred;
}
}

View file

@ -26,14 +26,6 @@
<br/>
Name: <input type="text" id="publish-name" name="name" value="name"/>
<br/>
Title: <input id="publish-title" type="text" name="title" value="title"/>
<br/>
Description: <input id="publish-description" type="text" name="description" value="I love spee.ch!"/>
<br/>
Author: <input type="text" id="publish-author" name="author" value="author"/>
<br/>
Language: <input type="text" id="publish-language" name="language" value="en"/>
<br/>
License: <select type="text" id="publish-license" name="license" value="license">
<option value="Creative Commons">Creative Commons</option>
<option value="Public Domain">Public Domain</option>
@ -122,10 +114,6 @@
event.preventDefault();
var fileName = document.getElementById('publish-name').value;
var description = document.getElementById('publish-description').value;
var title = document.getElementById('publish-title').value;
var author = document.getElementById('publish-author').value;
var language = document.getElementById('publish-language').value;
var license = document.getElementById('publish-license').value;
var nsfw = (document.getElementById('publish-nsfw').value.toLowerCase() === 'true');
@ -140,10 +128,6 @@
uploadReader.onload = function(readerEvent){
socket.emit('upload', {
'name': fileName,
'description': description,
'title': title,
'author': author,
'language': language,
'license': license,
'nsfw': nsfw,
'data': readerEvent.target.result

View file

@ -6,27 +6,6 @@ var multipartMiddleware = multipart();
var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.js');
// // helper functions
// function createPublishObject(req){
// var publishObject = {
// "method":"publish",
// "params": {
// "name": req.body.name,
// "file_path": req.files.file.path,
// "bid": 0.1,
// "metadata": {
// "description": req.body.description,
// "title": req.body.title,
// "author": req.body.author,
// "language": req.body.language,
// "license": req.body.license,
// "nsfw": (req.body.nsfw.toLowerCase() === "true")
// }
// }
// };
// return publishObject;
// }
// routes to export
module.exports = function(app){
// route to fetch one free public claim
@ -34,35 +13,21 @@ module.exports = function(app){
console.log(" >> GET request on favicon.ico");
res.sendFile(path.join(__dirname, '../public', 'favicon.ico'));
});
// // route to publish a new claim
// app.post("/publish", multipartMiddleware, function(req, res){
// console.log(" >> POST request on /publish");
// // build the data needed to publish the file
// var publishObject = createPublishObject(req);
// console.log("publish", publishObject);
// // post the task to the que
// queueApi.addNewTaskToQueue(JSON.stringify({
// type: 'publish',
// data: publishObject
// }));
// // respond to the client that the task has been queued
// res.status(200).sendFile(path.join(__dirname, '../public', 'publishingClaim.html'));
// });
// route to fetch one free public claim
app.get("/:name/all", function(req, res){
var name = req.params.name;
console.log(">> GET request on /" + name + " (all)");
lbryApi.serveAllClaims(name, res);
// create promise
var promise = lbryApi.getAllClaims(name);
// handle the promise resolve
promise.then(function(orderedFreePublicClaims){
console.log("/name/all promise success.")
res.status(200).send(orderedFreePublicClaims);
return;
})
// handle the promise rejection
.catch(function(error){
console.log("/name promise error:", error);
console.log("/name/all/ promise error:", error);
// handle the error
if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
@ -72,13 +37,12 @@ module.exports = function(app){
return;
};
})
});
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
var uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
// create promise
var promise = lbryApi.getClaimBasedOnUri(uri);
// handle the promise resolve
promise.then(function(filePath){
@ -99,10 +63,12 @@ module.exports = function(app){
};
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
var name = req.params.name;
console.log(">> GET request on /" + name);
// create promise
var promise = lbryApi.getClaimBasedOnNameOnly(name);
// handle the promise resolve
promise.then(function(filePath){
@ -123,6 +89,7 @@ module.exports = function(app){
};
});
});
// route for the home page
app.get("/", function(req, res){
res.status(200).sendFile(path.join(__dirname, '../public', 'index.html'));