Merge pull request #146 from lbryio/claim-table

Claim table
This commit is contained in:
Bill Bittner 2017-08-18 15:19:29 -07:00 committed by GitHub
commit 59619e394f
16 changed files with 410 additions and 351 deletions

View file

@ -1,37 +1,29 @@
# spee.ch # spee.ch
spee.ch is a single-serving site that reads and publishes images to and from the [LBRY](https://lbry.io/) blockchain. spee.ch is a single-serving site that reads and publishes images and videos to and from the [LBRY](https://lbry.io/) blockchain.
## how to run this repository locally ## how to run this repository locally
* start lbry
* install the [`lbry`](https://github.com/lbryio/lbry) daemon (0.14.2rc2 or higher)
* start the `lbry` daemon
* start mysql * start mysql
* install mysql * install mysql
* create a database called `lbry` * create a database called `lbry`
* save your connection uri somewhere handy (you will need it when you start the server) * save your connection uri somewhere handy (you will need it when you start the server)
* the uri should be in the form `mysql://user:pass@host:port/dbname` * the uri should be in the form `mysql://user:pass@host:port/dbname`
* clone this repo * start lbrynet daemon
* customize `config/develpment.json` by replacing the value of `Database.DownloadDirectory` with a string representing the local path where you want uploaded files to be stored. * install the [`lbry`](https://github.com/lbryio/lbry) daemon
* run `npm install` * start the `lbry` daemon
* to start the server, from your command line run `node speech.js` while passing three environmental variables: your lbry wallet address (`LBRY_CLAIM_ADDRESS`), your mysql connection uri (`MYSQL_CONNECTION_STRING`), and the environment to run (`NODE_ENV`). * start spee.ch-sync
* i.e. `LBRY_CLAIM_ADDRESS=<your wallet address here> MYSQL_CONNECTION_STRING=<your connection uri here> NODE_ENV=development node speech.js` * install and run this [`speech-sync`](https://github.com/billbitt/spee.ch-sync) tool
* e.g. `LBRY_CLAIM_ADDRESS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MYSQL_CONNECTION_STRING=mysql://root:XXXXXX@localhost:3306/lbry NODE_ENV=development node speech.js` * start spee.ch
* To run hot, use `nodemon` instead of `node` * clone this repo
* run `npm install`
* to start the server, from your command line run `node speech.js` while passing three environmental variables:
* (1) your lbry wallet address (`LBRY_CLAIM_ADDRESS`),
* (2) your mysql connection uri (`MYSQL_CONNECTION_STRING`),
* (3) the environment to run (`NODE_ENV`).
* i.e. `LBRY_CLAIM_ADDRESS=<your wallet address here> MYSQL_CONNECTION_STRING=<your connection uri here> NODE_ENV=development node speech.js`
* e.g. `LBRY_CLAIM_ADDRESS=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX MYSQL_CONNECTION_STRING=mysql://root:XXXXXX@localhost:3306/lbry NODE_ENV=development node speech.js`
* To run hot, use `nodemon` instead of `node`
* visit [localhost:3000](http://localhost:3000) * visit [localhost:3000](http://localhost:3000)
## site navigation
* spee.ch
* To publish a file, navigate to the homepage.
* spee.ch/< the name of the claim >
* To view the file with the largest bid at a claim.
* E.g. spee.ch/doitlive.
* spee.ch/< the name of the claim >/< the claim_id >
* To view a specific file at a claim
* E.g. spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5
* spee.ch/< the name of the claim >/all
* To view a batch of files at a claim
* E.g. spee.ch/doitlive/all
## API ## API
#### GET #### GET

View file

@ -3,96 +3,51 @@ const db = require('../models');
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const publishHelpers = require('../helpers/publishHelpers.js'); const publishHelpers = require('../helpers/publishHelpers.js');
function checkNameAvailability (name) {
return new Promise((resolve, reject) => {
// find any records where the name is used
db.File
.findAll({ where: { name } })
.then(result => {
if (result.length >= 1) {
// filter out any results that were not published from a spee.ch wallet address
lbryApi
.getWalletList()
.then((walletList) => {
const filteredResult = result.filter((claim) => {
return walletList.includes(claim.address);
});
if (filteredResult.length >= 1) {
resolve(false);
} else {
resolve(true);
}
})
.catch((error) => {
reject(error);
});
} else {
resolve(true);
}
})
.catch(error => {
reject(error);
});
});
};
module.exports = { module.exports = {
publish (publishParams, fileName, fileType) { publish (publishParams, fileName, fileType) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let publishResults = {};
// 1. make sure the name is available // 1. make sure the name is available
checkNameAvailability(publishParams.name) publishHelpers.checkNameAvailability(publishParams.name)
// 2. publish the file
.then(result => { .then(result => {
if (result === true) { if (result === true) {
// 2. publish the file return lbryApi.publishClaim(publishParams);
lbryApi
.publishClaim(publishParams)
.then(result => {
logger.info(`Successfully published ${fileName}`, result);
// 3. update old record or create new one (update is in case the claim has been published before by this daemon)
db.upsert(
db.File,
{
name : publishParams.name,
claimId : result.claim_id,
address : publishParams.claim_address,
outpoint: `${result.txid}:${result.nout}`,
height : 0,
fileName,
filePath: publishParams.file_path,
fileType,
nsfw : publishParams.metadata.nsfw,
},
{
name : publishParams.name,
claimId: result.claim_id,
}
).then(() => {
// resolve the promise with the result from lbryApi.publishClaim;
resolve(result);
})
.catch(error => {
logger.error('Sequelize findOne error', error);
// reject the promise
reject(error);
});
})
.catch(error => {
// delete the local file
publishHelpers.deleteTemporaryFile(publishParams.file_path);
// reject the promise
reject(error);
});
} else { } else {
const err = new Error('That name has already been claimed by spee.ch. Please choose a new claim name.'); return new Error('That name has already been claimed by spee.ch. Please choose a new claim name.');
reject(err);
} }
}) })
// 3. upsert File record (update is in case the claim has been published before by this daemon)
.then(result => {
let fileRecord;
let upsertCriteria;
publishResults = result;
logger.info(`Successfully published ${fileName}`, publishResults);
fileRecord = {
name : publishParams.name,
claimId : publishResults.claim_id,
address : publishParams.claim_address,
outpoint: `${publishResults.txid}:${publishResults.nout}`,
height : 0,
fileName,
filePath: publishParams.file_path,
fileType,
nsfw : publishParams.metadata.nsfw,
};
upsertCriteria = {
name : publishParams.name,
claimId: publishResults.claim_id,
};
return Promise.all([db.upsert(db.File, fileRecord, upsertCriteria, 'File'), db.upsert(db.Claim, fileRecord, upsertCriteria, 'Claim')]);
})
.then((fileRecordResults, claimRecordResults) => {
logger.debug('File and Claim records successfully created');
resolve(publishResults); // resolve the promise with the result from lbryApi.publishClaim;
})
.catch(error => { .catch(error => {
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
reject(error); reject(error);
}); });
}); });
}, },
checkNameAvailability (name) {
return checkNameAvailability(name);
},
}; };

View file

@ -1,9 +1,7 @@
const lbryApi = require('../helpers/lbryApi.js'); const lbryApi = require('../helpers/lbryApi.js');
const db = require('../models'); const db = require('../models');
const logger = require('winston'); const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable } = require('../helpers/serveHelpers.js');
const isFreeClaim = require('../helpers/functions/isFreeClaim.js');
const serveHelpers = require('../helpers/serveHelpers.js');
function checkForLocalAssetByClaimId (claimId, name) { function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -35,44 +33,47 @@ function formatGetResultsToFileInfo ({ name, claim_id, outpoint, file_name, down
} }
function getAssetByClaimId (fullClaimId, name) { function getAssetByClaimId (fullClaimId, name) {
logger.debug('...getting asset by claim Id...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 1. check locally for claim // 1. check locally for claim
checkForLocalAssetByClaimId(fullClaimId, name) checkForLocalAssetByClaimId(fullClaimId, name)
.then(dataValues => { .then(dataValues => {
// 2. if a result was found, resolve the result // if a result was found, return early with the result
if (dataValues) { if (dataValues) {
logger.debug('found a local file for this claimId'); logger.debug('found a local file for this name and claimId');
resolve(dataValues); return resolve(dataValues);
// 2. if not found locally, make a get request }
} else { logger.debug('no local file found for this name and claimId');
logger.debug('no local file for this claimId'); // 2. if no local claim, resolve and get the claim
// 3. resolve the claim resolveAgainstClaimTable(name, fullClaimId)
lbryApi.resolveUri(`${name}#${fullClaimId}`) .then(resolveResult => {
.then(resolveResult => { logger.debug('resolve result >> ', resolveResult);
// if the claim is free and public, then get it // if no result, return early (claim doesn't exist or isn't free)
if (resolveResult.claim && isFreeClaim(resolveResult.claim)) { if (!resolveResult) {
lbryApi.getClaim(`${name}#${fullClaimId}`) return resolve(null);
.then(getResult => { }
let fileInfo = formatGetResultsToFileInfo(getResult); let fileRecord = {};
fileInfo['address'] = resolveResult.claim.address; // get the claim
fileInfo['height'] = resolveResult.claim.height; lbryApi.getClaim(`${name}#${fullClaimId}`)
// insert a record in the File table .then(getResult => {
db.File.create(fileInfo); logger.debug('getResult >>', getResult);
// resolve the promise fileRecord = formatGetResultsToFileInfo(getResult);
resolve(fileInfo); fileRecord['address'] = (resolveResult.address || 0);
}) fileRecord['height'] = resolveResult.height;
.catch(error => { // insert a record in the File table & Update Claim table
reject(error); return db.File.create(fileRecord);
}); })
// if not, resolve with no claims .then(fileRecordResults => {
} else { logger.debug('File record successfully updated');
resolve(null); resolve(fileRecord);
}
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
}); });
} })
.catch(error => {
reject(error);
});
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
@ -82,19 +83,22 @@ function getAssetByClaimId (fullClaimId, name) {
module.exports = { module.exports = {
getAssetByChannel (channelName, name) { getAssetByChannel (channelName, name) {
logger.debug('...getting asset by channel...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// temporarily throw error // temporarily throw error
reject(new Error('channel names are not currently supported')); reject(new Error('channel names are not currently supported'));
// get the claim id // get the claim id
// get teh asset by claim Id // get the asset by claim Id
}); });
}, },
getAssetByShortId: function (shortId, name) { getAssetByShortId: function (shortId, name) {
logger.debug('...getting asset by short id...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the full claimId // get the full claimId
serveHelpers.getFullClaimIdFromShortId(shortId, name) getFullClaimIdFromShortId(shortId, name)
// get the asset by the claimId // get the asset by the claimId
.then(claimId => { .then(claimId => {
logger.debug('claim id =', claimId);
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByClaimId(claimId, name));
}) })
.catch(error => { .catch(error => {
@ -106,18 +110,19 @@ module.exports = {
return getAssetByClaimId(fullClaimId, name); return getAssetByClaimId(fullClaimId, name);
}, },
getAssetByName (name) { getAssetByName (name) {
logger.debug('...getting asset by claim name...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// 1. get a list of the free public claims // 1. get a list of the free public claims
getAllFreePublicClaims(name) getTopFreeClaim(name)
// 2. check locally for the top claim // 2. check locally for the top claim
.then(freePublicClaimList => { .then(topFreeClaim => {
// if no claims were found, return null // if no claims were found, return null
if (!freePublicClaimList) { if (!topFreeClaim) {
resolve(null); return resolve(null);
return;
} }
// parse the result // parse the result
const claimId = freePublicClaimList[0].claim_id; const claimId = topFreeClaim.claimId;
logger.debug('top free claim id =', claimId);
// get the asset // get the asset
resolve(getAssetByClaimId(claimId, name)); resolve(getAssetByClaimId(claimId, name));
}) })

View file

@ -146,15 +146,14 @@ module.exports = {
const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' '); const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' ');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// get the raw requests data // get the raw requests data
db.sequelize db.sequelize.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT })
.query(`SELECT COUNT(*), File.* FROM Request LEFT JOIN File ON Request.FileId = File.id WHERE FileId IS NOT NULL AND nsfw != 1 AND trendingEligible = 1 AND Request.createdAt > "${dateTime}" GROUP BY FileId ORDER BY COUNT(*) DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }) .then(results => {
.then(results => { resolve(results);
resolve(results); })
}) .catch(error => {
.catch(error => { logger.error('sequelize error', error);
logger.error('sequelize error', error); reject(error);
reject(error); });
});
}); });
}, },
}; };

View file

@ -1,61 +0,0 @@
const isFreeClaim = require('./isFreeClaim.js');
const lbryApi = require('../lbryApi.js');
const logger = require('winston');
function filterForFreePublicClaims (claimsListArray) {
logger.debug('filtering claims list for free, public claims');
if (!claimsListArray) {
return null;
}
const freePublicClaims = claimsListArray.filter(claim => {
if (!claim.value) {
return false;
}
return isFreeClaim(claim);
});
return freePublicClaims;
}
function orderClaims (claimsListArray) {
logger.debug('ordering the free public claims');
claimsListArray.sort((a, b) => {
if (a.amount === b.amount) {
return a.height < b.height;
} else {
return a.amount < b.amount;
}
});
return claimsListArray;
}
module.exports = (claimName) => {
return new Promise((resolve, reject) => {
// make a call to the daemon to get the claims list
lbryApi
.getClaimsList(claimName)
.then(({ claims }) => {
logger.debug(`Number of claims: ${claims.length}`);
// return early if no claims were found
if (claims.length === 0) {
logger.debug('exiting due to lack of claims');
resolve(null);
return;
}
// filter the claims to return only free, public claims
const freePublicClaims = filterForFreePublicClaims(claims);
// return early if no free, public claims were found
if (!freePublicClaims || freePublicClaims.length === 0) {
logger.debug('exiting due to lack of free or public claims');
resolve(null);
return;
}
// order the claims
const orderedPublicClaims = orderClaims(freePublicClaims);
// resolve the promise
resolve(orderedPublicClaims);
})
.catch(error => {
reject(error);
});
});
};

View file

@ -1,11 +0,0 @@
const logger = require('winston');
module.exports = ({ value }) => {
if (!value.stream.metadata.fee || value.stream.metadata.fee.amount === 0) {
logger.debug('isFreeClaim?', true);
return true;
} else {
logger.debug('isFreePublicClaim?', false);
return false;
}
};

View file

@ -61,7 +61,7 @@ module.exports = {
}); });
}); });
}, },
getClaimsList (claimName) { getClaimList (claimName) {
logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`); logger.debug(`lbryApi >> Getting claim_list for "${claimName}"`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
@ -79,6 +79,7 @@ module.exports = {
}, },
resolveUri (uri) { resolveUri (uri) {
logger.debug(`lbryApi >> Resolving URI for "${uri}"`); logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
// console.log('resolving uri', uri);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post('http://localhost:5279/lbryapi', { .post('http://localhost:5279/lbryapi', {
@ -93,6 +94,7 @@ module.exports = {
} }
}) })
.catch(error => { .catch(error => {
console.log('error with resolve', error);
reject(error); reject(error);
}); });
}); });

View file

@ -1,6 +1,8 @@
const logger = require('winston'); const logger = require('winston');
const config = require('config'); const config = require('config');
const fs = require('fs'); const fs = require('fs');
const db = require('../models');
const { getWalletList } = require('./lbryApi.js');
module.exports = { module.exports = {
validateFile (file, name, license, nsfw) { validateFile (file, name, license, nsfw) {
@ -90,4 +92,34 @@ module.exports = {
logger.debug(`successfully deleted ${filePath}`); logger.debug(`successfully deleted ${filePath}`);
}); });
}, },
checkNameAvailability (name) {
return new Promise((resolve, reject) => {
// find any records where the name is used
db.File.findAll({ where: { name } })
.then(result => {
if (result.length >= 1) {
// filter out any results that were not published from a spee.ch wallet address
getWalletList()
.then((walletList) => {
const filteredResult = result.filter((claim) => {
return walletList.includes(claim.address);
});
if (filteredResult.length >= 1) {
resolve(false);
} else {
resolve(true);
}
})
.catch((error) => {
reject(error);
});
} else {
resolve(true);
}
})
.catch(error => {
reject(error);
});
});
},
}; };

View file

@ -1,13 +1,12 @@
const logger = require('winston'); const logger = require('winston');
const db = require('../models'); const db = require('../models');
const lbryApi = require('./lbryApi');
function determineShortClaimId (claimId, height, claimList) { function determineShortClaimId (claimId, height, claimList) {
logger.debug('determining short url based on claim id and claim list'); logger.debug('determining short url based on claim id and claim list');
logger.debug('claimlist starting length:', claimList.length); logger.debug('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists // remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
return claim.claim_id !== claimId; return claim.claimId !== claimId;
}); });
logger.debug('claim list length without this claim:', claimList.length); logger.debug('claim list length without this claim:', claimList.length);
// If there are no other claims, return the first letter of the claim id... // If there are no other claims, return the first letter of the claim id...
@ -21,7 +20,7 @@ function determineShortClaimId (claimId, height, claimList) {
while (claimList.length !== 0) { while (claimList.length !== 0) {
i++; i++;
claimList = claimList.filter(claim => { claimList = claimList.filter(claim => {
const otherClaimIdSegmentToCompare = claim.claim_id.substring(0, i); const otherClaimIdSegmentToCompare = claim.claimId.substring(0, i);
const thisClaimIdSegmentToCompare = claimId.substring(0, i); const thisClaimIdSegmentToCompare = claimId.substring(0, i);
logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?'); logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?');
return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare); return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare);
@ -35,7 +34,7 @@ function determineShortClaimId (claimId, height, claimList) {
return claimId.substring(0, 1); return claimId.substring(0, 1);
} }
const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => { const allMatchingClaimsAtLastMatch = claimListCopy.filter(claim => {
return (claim.claim_id.substring(0, lastMatchIndex) === lastMatch); return (claim.claimId.substring(0, lastMatchIndex) === lastMatch);
}); });
// for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character // for those that share the longest shared prefix: see which came first in time. whichever is earliest, the others take the extra character
const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => { const sortedMatchingClaims = allMatchingClaimsAtLastMatch.sort((a, b) => {
@ -49,29 +48,6 @@ function determineShortClaimId (claimId, height, claimList) {
} }
} }
function checkLocalDbForClaims (name, shortId) {
return db.File
.findAll({
where: {
name,
claimId: { $like: `${shortId}%` },
},
})
.then(records => {
logger.debug('number of local search results:', records.length);
if (records.length === 0) {
return records;
}
const localClaims = records.map(record => { // format the data to match what lbry daemon would have returned
return { name: record.dataValues.name, claim_id: record.dataValues.claimId, height: record.dataValues.height };
});
return localClaims;
})
.catch(error => {
return error;
});
}
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) { function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
return { return {
embedUrl : `https://spee.ch/embed/${claimId}/${name}`, embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
@ -118,36 +94,13 @@ module.exports = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url'); logger.debug('getting claim_id from short url');
// use the daemon to check for claims list // use the daemon to check for claims list
lbryApi.getClaimsList(name) db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND claimId LIKE '${shortId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
.then(({ claims }) => { .then(result => {
logger.debug('Number of claims from getClaimsList:', claims.length); switch (result.length) {
// if no claims were found, check locally for possible claims
if (claims.length === 0) {
return checkLocalDbForClaims(name, shortId);
} else {
return claims;
}
})
// handle the claims list
.then(claims => {
logger.debug('Claims ready for filtering');
const regex = new RegExp(`^${shortId}`);
const filteredClaimsList = claims.filter(claim => {
return regex.test(claim.claim_id);
});
switch (filteredClaimsList.length) {
case 0: case 0:
reject(new Error('That is an invalid short url')); return reject(new Error('That is an invalid Short Id'));
break; default: // note results must be sorted
case 1: return resolve(result[0].claimId);
resolve(filteredClaimsList[0].claim_id);
break;
default:
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
return a.height > b.height;
});
resolve(sortedClaimsList[0].claim_id);
break;
} }
}) })
.catch(error => { .catch(error => {
@ -158,13 +111,66 @@ module.exports = {
getShortIdFromClaimId (claimId, height, name) { getShortIdFromClaimId (claimId, height, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
logger.debug('finding short claim id from full claim id'); logger.debug('finding short claim id from full claim id');
// get a list of all the claims db.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${name}' ORDER BY claimId;`, { type: db.sequelize.QueryTypes.SELECT })
lbryApi.getClaimsList(name) .then(result => {
// find the smallest possible unique url for this claim switch (result.length) {
.then(({ claims }) => { case 0:
const shortId = determineShortClaimId(claimId, height, claims); return reject(new Error('That is an invalid Claim Id'));
logger.debug('short claim id ===', shortId); default: // note results must be sorted
resolve(shortId); const shortId = determineShortClaimId(claimId, height, result);
logger.debug('short claim id ===', shortId);
return resolve(shortId);
}
})
.catch(error => {
reject(error);
});
});
},
getAllFreeClaims (name) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result);
}
})
.catch(error => {
reject(error);
});
});
},
getTopFreeClaim (name) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC LIMIT 1`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
default:
return resolve(result[0]);
}
})
.catch(error => {
reject(error);
});
});
},
resolveAgainstClaimTable (name, claimId) {
return new Promise((resolve, reject) => {
db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' AND claimId = '${claimId}'`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return resolve(null);
case 1:
return resolve(result[0]);
default:
return new Error('more than one entry matches that name and claimID');
}
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);

144
models/claim.js Normal file
View file

@ -0,0 +1,144 @@
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => {
const Claim = sequelize.define(
'Claim',
{
address: {
type : STRING,
default: null,
},
amount: {
type : STRING,
default: null,
},
claimId: {
type : STRING,
default: null,
},
claimSequence: {
type : INTEGER,
default: null,
},
decodedClaim: {
type : BOOLEAN,
default: null,
},
depth: {
type : INTEGER,
default: null,
},
effectiveAmount: {
type : STRING,
default: null,
},
hasSignature: {
type : BOOLEAN,
default: null,
},
height: {
type : STRING,
default: null,
},
hex: {
type : TEXT('long'),
default: null,
},
name: {
type : STRING,
default: null,
},
nout: {
type : INTEGER,
default: null,
},
txid: {
type : STRING,
default: null,
},
validAtHeight: {
type : STRING,
default: null,
},
outpoint: {
type : STRING,
default: null,
},
claimType: {
type : STRING,
default: null,
},
certificateId: {
type : STRING,
default: null,
},
author: {
type : STRING,
default: null,
},
description: {
type : TEXT('long'),
default: null,
},
language: {
type : STRING,
default: null,
},
license: {
type : STRING,
default: null,
},
licenseUrl: {
type : STRING,
default: null,
},
nsfw: {
type : BOOLEAN,
default: null,
},
preview: {
type : STRING,
default: null,
},
thumbnail: {
type : STRING,
default: null,
},
title: {
type : STRING,
default: null,
},
metadataVersion: {
type : STRING,
default: null,
},
contentType: {
type : STRING,
default: null,
},
source: {
type : STRING,
default: null,
},
sourceType: {
type : STRING,
default: null,
},
sourceVersion: {
type : STRING,
default: null,
},
streamVersion: {
type : STRING,
default: null,
},
valueVersion: {
type : STRING,
default: null,
},
},
{
freezeTableName: true,
}
);
return Claim;
};

View file

@ -37,15 +37,15 @@ Object.keys(db).forEach(modelName => {
} }
}); });
db['upsert'] = (Model, values, condition) => { db['upsert'] = (Model, values, condition, tableName) => {
return Model return Model
.findOne({ where: condition }) .findOne({ where: condition })
.then(function (obj) { .then(function (obj) {
if (obj) { // update if (obj) { // update
logger.silly(`updating ${values.name}:${values.claimId} in File db`); logger.debug(`updating "${values.name}" "${values.claimId}" in db.${tableName}`);
return obj.update(values); return obj.update(values);
} else { // insert } else { // insert
logger.silly(`creating ${values.name}:${values.claimId} in File db`); logger.debug(`creating "${values.name}" "${values.claimId}" in db.${tableName}`);
return Model.create(values); return Model.create(values);
} }
}).catch(function (error) { }).catch(function (error) {

View file

@ -35,6 +35,7 @@
"mysql2": "^1.3.5", "mysql2": "^1.3.5",
"nodemon": "^1.11.0", "nodemon": "^1.11.0",
"sequelize": "^4.1.0", "sequelize": "^4.1.0",
"sleep": "^5.1.1",
"socket.io": "^2.0.1", "socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0", "socketio-file-upload": "^0.6.0",
"universal-analytics": "^0.4.13", "universal-analytics": "^0.4.13",

View file

@ -1,9 +1,9 @@
const logger = require('winston'); const logger = require('winston');
const multipart = require('connect-multiparty'); const multipart = require('connect-multiparty');
const multipartMiddleware = multipart(); const multipartMiddleware = multipart();
const publishController = require('../controllers/publishController.js'); const { publish } = require('../controllers/publishController.js');
const lbryApi = require('../helpers/lbryApi.js'); const { getClaimList, resolveUri } = require('../helpers/lbryApi.js');
const { createPublishParams, validateFile } = require('../helpers/publishHelpers.js'); const { createPublishParams, validateFile, checkNameAvailability } = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../helpers/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
@ -13,47 +13,44 @@ module.exports = (app, hostedContentPath) => {
// google analytics // google analytics
sendGoogleAnalytics('SERVE', headers, ip, originalUrl); sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
// serve the content // serve the content
lbryApi getClaimList(params.name)
.getClaimsList(params.name) .then(claimsList => {
.then(claimsList => { postToStats('serve', originalUrl, ip, null, null, 'success');
postToStats('serve', originalUrl, ip, null, null, 'success'); res.status(200).json(claimsList);
res.status(200).json(claimsList); })
}) .catch(error => {
.catch(error => { errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); });
});
}); });
// route to check whether spee.ch has published to a claim // route to check whether spee.ch has published to a claim
app.get('/api/isClaimAvailable/:name', ({ ip, originalUrl, params }, res) => { app.get('/api/isClaimAvailable/:name', ({ ip, originalUrl, params }, res) => {
// send response // send response
publishController checkNameAvailability(params.name)
.checkNameAvailability(params.name) .then(result => {
.then(result => { if (result === true) {
if (result === true) { res.status(200).json(true);
res.status(200).json(true); } else {
} else { logger.debug(`Rejecting publish request because ${params.name} has already been published via spee.ch`);
logger.debug(`Rejecting publish request because ${params.name} has already been published via spee.ch`); res.status(200).json(false);
res.status(200).json(false); }
} })
}) .catch(error => {
.catch(error => { res.status(500).json(error);
res.status(500).json(error); });
});
}); });
// route to run a resolve request on the daemon // route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => { app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => {
// google analytics // google analytics
sendGoogleAnalytics('SERVE', headers, ip, originalUrl); sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
// serve content // serve content
lbryApi resolveUri(params.uri)
.resolveUri(params.uri) .then(resolvedUri => {
.then(resolvedUri => { postToStats('serve', originalUrl, ip, null, null, 'success');
postToStats('serve', originalUrl, ip, null, null, 'success'); res.status(200).json(resolvedUri);
res.status(200).json(resolvedUri); })
}) .catch(error => {
.catch(error => { errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); });
});
}); });
// route to run a publish request on the daemon // route to run a publish request on the daemon
@ -79,14 +76,13 @@ module.exports = (app, hostedContentPath) => {
const fileType = file.type; const fileType = file.type;
const publishParams = createPublishParams(name, filePath, license, nsfw); const publishParams = createPublishParams(name, filePath, license, nsfw);
// publish the file // publish the file
publishController publish(publishParams, fileName, fileType)
.publish(publishParams, fileName, fileType) .then(result => {
.then(result => { postToStats('publish', originalUrl, ip, null, null, 'success');
postToStats('publish', originalUrl, ip, null, null, 'success'); res.status(200).json(result);
res.status(200).json(result); })
}) .catch(error => {
.catch(error => { errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res); });
});
}); });
}; };

View file

@ -1,5 +1,5 @@
const errorHandlers = require('../helpers/errorHandlers.js'); const errorHandlers = require('../helpers/errorHandlers.js');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const { getAllFreeClaims } = require('../helpers/serveHelpers.js');
const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js'); const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js');
module.exports = (app) => { module.exports = (app) => {
@ -48,14 +48,14 @@ module.exports = (app) => {
// route to display all free public claims at a given name // route to display all free public claims at a given name
app.get('/:name/all', ({ ip, originalUrl, params }, res) => { app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
// get and render the content // get and render the content
getAllFreePublicClaims(params.name) getAllFreeClaims(params.name)
.then(orderedFreePublicClaims => { .then(orderedFreeClaims => {
if (!orderedFreePublicClaims) { if (!orderedFreeClaims) {
res.status(307).render('noClaims'); res.status(307).render('noClaims');
return; return;
} }
postToStats('show', originalUrl, ip, null, null, 'success'); postToStats('show', originalUrl, ip, null, null, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); res.status(200).render('allClaims', { claims: orderedFreeClaims });
}) })
.catch(error => { .catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res); errorHandlers.handleRequestError('show', originalUrl, ip, error, res);

View file

@ -38,17 +38,16 @@ module.exports = (app, siofu, hostedContentPath) => {
// prepare the publish parameters // prepare the publish parameters
const publishParams = publishHelpers.createPublishParams(file.meta.name, file.pathName, file.meta.license, file.meta.nsfw); const publishParams = publishHelpers.createPublishParams(file.meta.name, file.pathName, file.meta.license, file.meta.nsfw);
// publish the file // publish the file
publishController publishController.publish(publishParams, file.name, file.meta.type)
.publish(publishParams, file.name, file.meta.type) .then(result => {
.then(result => { postToStats('publish', '/', null, null, null, 'success');
postToStats('publish', '/', null, null, null, 'success'); socket.emit('publish-complete', { name: publishParams.name, result });
socket.emit('publish-complete', { name: publishParams.name, result }); })
}) .catch(error => {
.catch(error => { error = errorHandlers.handlePublishError(error);
error = errorHandlers.handlePublishError(error); postToStats('publish', '/', null, null, null, error);
postToStats('publish', '/', null, null, null, error); socket.emit('publish-failure', error);
socket.emit('publish-failure', error); });
});
} else { } else {
logger.error(`An error occurred in uploading the client's file`); logger.error(`An error occurred in uploading the client's file`);
socket.emit('publish-failure', 'File uploaded, but with errors'); socket.emit('publish-failure', 'File uploaded, but with errors');

View file

@ -5,12 +5,12 @@
<p>These are all the free, public assets at that claim. You can publish more at <a href="/">spee.ch</a>.</p> <p>These are all the free, public assets at that claim. You can publish more at <a href="/">spee.ch</a>.</p>
{{#each claims}} {{#each claims}}
<div class="all-claims-item"> <div class="all-claims-item">
<img class="all-claims-img" src="/{{this.claim_id}}/{{this.name}}/" /> <img class="all-claims-img" src="/{{this.claimId}}/{{this.name}}.test" />
<div class="all-claims-details"> <div class="all-claims-details">
<ul style="list-style-type:none"> <ul style="list-style-type:none">
<li>claim: {{this.name}}</li> <li>claim: {{this.name}}</li>
<li>claim_id: {{this.claim_id}}</li> <li>claim_id: {{this.claim_id}}</li>
<li>link: <a href="/{{this.claim_id}}/{{this.name}}">spee.ch/{{this.name}}/{{this.claim_id}}</a></li> <li>link: <a href="/{{this.claimId}}/{{this.name}}">spee.ch/{{this.name}}/{{this.claimId}}</a></li>
<li>author: {{this.value.stream.metadata.author}}</li> <li>author: {{this.value.stream.metadata.author}}</li>
<li>description: {{this.value.stream.metadata.description}}</li> <li>description: {{this.value.stream.metadata.description}}</li>
<li>license: {{this.value.stream.metadata.license}}</li> <li>license: {{this.value.stream.metadata.license}}</li>