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 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
* start lbry
* install the [`lbry`](https://github.com/lbryio/lbry) daemon (0.14.2rc2 or higher)
* start the `lbry` daemon
* start mysql
* install mysql
* create a database called `lbry`
* 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`
* clone this repo
* 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.
* run `npm install`
* 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`).
* 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`
* start lbrynet daemon
* install the [`lbry`](https://github.com/lbryio/lbry) daemon
* start the `lbry` daemon
* start spee.ch-sync
* install and run this [`speech-sync`](https://github.com/billbitt/spee.ch-sync) tool
* start spee.ch
* 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)
## 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
#### GET

View file

@ -3,96 +3,51 @@ const db = require('../models');
const lbryApi = require('../helpers/lbryApi.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 = {
publish (publishParams, fileName, fileType) {
return new Promise((resolve, reject) => {
let publishResults = {};
// 1. make sure the name is available
checkNameAvailability(publishParams.name)
publishHelpers.checkNameAvailability(publishParams.name)
// 2. publish the file
.then(result => {
if (result === true) {
// 2. publish the file
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);
});
return lbryApi.publishClaim(publishParams);
} else {
const err = new Error('That name has already been claimed by spee.ch. Please choose a new claim name.');
reject(err);
return new Error('That name has already been claimed by spee.ch. Please choose a new claim name.');
}
})
// 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 => {
publishHelpers.deleteTemporaryFile(publishParams.file_path); // delete the local file
reject(error);
});
});
},
checkNameAvailability (name) {
return checkNameAvailability(name);
},
};

View file

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

View file

@ -146,15 +146,14 @@ module.exports = {
const dateTime = startDate.toISOString().slice(0, 19).replace('T', ' ');
return new Promise((resolve, reject) => {
// get the raw requests data
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 })
.then(results => {
resolve(results);
})
.catch(error => {
logger.error('sequelize error', error);
reject(error);
});
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 })
.then(results => {
resolve(results);
})
.catch(error => {
logger.error('sequelize error', 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}"`);
return new Promise((resolve, reject) => {
axios
@ -79,6 +79,7 @@ module.exports = {
},
resolveUri (uri) {
logger.debug(`lbryApi >> Resolving URI for "${uri}"`);
// console.log('resolving uri', uri);
return new Promise((resolve, reject) => {
axios
.post('http://localhost:5279/lbryapi', {
@ -93,6 +94,7 @@ module.exports = {
}
})
.catch(error => {
console.log('error with resolve', error);
reject(error);
});
});

View file

@ -1,6 +1,8 @@
const logger = require('winston');
const config = require('config');
const fs = require('fs');
const db = require('../models');
const { getWalletList } = require('./lbryApi.js');
module.exports = {
validateFile (file, name, license, nsfw) {
@ -90,4 +92,34 @@ module.exports = {
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 db = require('../models');
const lbryApi = require('./lbryApi');
function determineShortClaimId (claimId, height, claimList) {
logger.debug('determining short url based on claim id and claim list');
logger.debug('claimlist starting length:', claimList.length);
// remove this claim from the claim list, if it exists
claimList = claimList.filter(claim => {
return claim.claim_id !== claimId;
return claim.claimId !== claimId;
});
logger.debug('claim list length without this claim:', claimList.length);
// 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) {
i++;
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);
logger.debug('compare:', otherClaimIdSegmentToCompare, '===', thisClaimIdSegmentToCompare, '?');
return (otherClaimIdSegmentToCompare === thisClaimIdSegmentToCompare);
@ -35,7 +34,7 @@ function determineShortClaimId (claimId, height, claimList) {
return claimId.substring(0, 1);
}
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
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 }) {
return {
embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
@ -118,36 +94,13 @@ module.exports = {
return new Promise((resolve, reject) => {
logger.debug('getting claim_id from short url');
// use the daemon to check for claims list
lbryApi.getClaimsList(name)
.then(({ claims }) => {
logger.debug('Number of claims from getClaimsList:', claims.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) {
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(result => {
switch (result.length) {
case 0:
reject(new Error('That is an invalid short url'));
break;
case 1:
resolve(filteredClaimsList[0].claim_id);
break;
default:
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
return a.height > b.height;
});
resolve(sortedClaimsList[0].claim_id);
break;
return reject(new Error('That is an invalid Short Id'));
default: // note results must be sorted
return resolve(result[0].claimId);
}
})
.catch(error => {
@ -158,13 +111,66 @@ module.exports = {
getShortIdFromClaimId (claimId, height, name) {
return new Promise((resolve, reject) => {
logger.debug('finding short claim id from full claim id');
// get a list of all the claims
lbryApi.getClaimsList(name)
// find the smallest possible unique url for this claim
.then(({ claims }) => {
const shortId = determineShortClaimId(claimId, height, claims);
logger.debug('short claim id ===', shortId);
resolve(shortId);
db.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${name}' ORDER BY claimId;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return reject(new Error('That is an invalid Claim Id'));
default: // note results must be sorted
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 => {
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
.findOne({ where: condition })
.then(function (obj) {
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);
} 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);
}
}).catch(function (error) {

View file

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

View file

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

View file

@ -1,5 +1,5 @@
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');
module.exports = (app) => {
@ -48,14 +48,14 @@ module.exports = (app) => {
// route to display all free public claims at a given name
app.get('/:name/all', ({ ip, originalUrl, params }, res) => {
// get and render the content
getAllFreePublicClaims(params.name)
.then(orderedFreePublicClaims => {
if (!orderedFreePublicClaims) {
getAllFreeClaims(params.name)
.then(orderedFreeClaims => {
if (!orderedFreeClaims) {
res.status(307).render('noClaims');
return;
}
postToStats('show', originalUrl, ip, null, null, 'success');
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
res.status(200).render('allClaims', { claims: orderedFreeClaims });
})
.catch(error => {
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);

View file

@ -38,17 +38,16 @@ module.exports = (app, siofu, hostedContentPath) => {
// prepare the publish parameters
const publishParams = publishHelpers.createPublishParams(file.meta.name, file.pathName, file.meta.license, file.meta.nsfw);
// publish the file
publishController
.publish(publishParams, file.name, file.meta.type)
.then(result => {
postToStats('publish', '/', null, null, null, 'success');
socket.emit('publish-complete', { name: publishParams.name, result });
})
.catch(error => {
error = errorHandlers.handlePublishError(error);
postToStats('publish', '/', null, null, null, error);
socket.emit('publish-failure', error);
});
publishController.publish(publishParams, file.name, file.meta.type)
.then(result => {
postToStats('publish', '/', null, null, null, 'success');
socket.emit('publish-complete', { name: publishParams.name, result });
})
.catch(error => {
error = errorHandlers.handlePublishError(error);
postToStats('publish', '/', null, null, null, error);
socket.emit('publish-failure', error);
});
} else {
logger.error(`An error occurred in uploading the client's file`);
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>
{{#each claims}}
<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">
<ul style="list-style-type:none">
<li>claim: {{this.name}}</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>description: {{this.value.stream.metadata.description}}</li>
<li>license: {{this.value.stream.metadata.license}}</li>