150 resovle channels updates #156
4 changed files with 328 additions and 313 deletions
|
@ -1,7 +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 { resolveAgainstClaimTable, serveFile, showFile, showFileLite, getShortClaimIdFromLongClaimId, getClaimIdByLongChannelId, getAllChannelClaims, getLongChannelId, getShortChannelIdFromLongChannelId, getLongClaimId } = require('../helpers/serveHelpers.js');
|
const { serveFile, showFile, showFileLite } = require('../helpers/serveHelpers.js');
|
||||||
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
||||||
|
|
||||||
const SERVE = 'SERVE';
|
const SERVE = 'SERVE';
|
||||||
|
@ -59,34 +59,35 @@ function getAssetByLongClaimId (fullClaimId, name) {
|
||||||
}
|
}
|
||||||
logger.debug('no local file found for this name and claimId');
|
logger.debug('no local file found for this name and claimId');
|
||||||
// 2. if no local claim, resolve and get the claim
|
// 2. if no local claim, resolve and get the claim
|
||||||
resolveAgainstClaimTable(name, fullClaimId)
|
db
|
||||||
.then(resolveResult => {
|
.resolveClaim(name, fullClaimId)
|
||||||
logger.debug('resolve result >> ', resolveResult);
|
.then(resolveResult => {
|
||||||
// if no result, return early (claim doesn't exist or isn't free)
|
logger.debug('resolve result >> ', resolveResult);
|
||||||
if (!resolveResult) {
|
// if no result, return early (claim doesn't exist or isn't free)
|
||||||
return resolve(null);
|
if (!resolveResult) {
|
||||||
}
|
return resolve(null);
|
||||||
let fileRecord = {};
|
}
|
||||||
// get the claim
|
let fileRecord = {};
|
||||||
lbryApi.getClaim(`${name}#${fullClaimId}`)
|
// get the claim
|
||||||
.then(getResult => {
|
lbryApi.getClaim(`${name}#${fullClaimId}`)
|
||||||
logger.debug('getResult >>', getResult);
|
.then(getResult => {
|
||||||
fileRecord = createFileRecord(resolveResult);
|
logger.debug('getResult >>', getResult);
|
||||||
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
|
fileRecord = createFileRecord(resolveResult);
|
||||||
// insert a record in the File table & Update Claim table
|
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
|
||||||
return db.File.create(fileRecord);
|
// insert a record in the File table & Update Claim table
|
||||||
})
|
return db.File.create(fileRecord);
|
||||||
.then(fileRecordResults => {
|
})
|
||||||
logger.debug('File record successfully updated');
|
.then(fileRecordResults => {
|
||||||
resolve(fileRecord);
|
logger.debug('File record successfully updated');
|
||||||
|
resolve(fileRecord);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -99,34 +100,36 @@ module.exports = {
|
||||||
logger.debug('getting asset by claim');
|
logger.debug('getting asset by claim');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// 1. get the long claim id
|
// 1. get the long claim id
|
||||||
getLongClaimId(claimName, claimId) // here
|
db
|
||||||
// 2. get the claim Id
|
.getLongClaimId(claimName, claimId)
|
||||||
.then(longClaimId => {
|
// 2. get the claim Id
|
||||||
logger.debug('long claim id = ', longClaimId);
|
.then(longClaimId => {
|
||||||
resolve(getAssetByLongClaimId(longClaimId, claimName));
|
logger.debug('long claim id = ', longClaimId);
|
||||||
})
|
resolve(getAssetByLongClaimId(longClaimId, claimName));
|
||||||
.catch(error => {
|
})
|
||||||
reject(error);
|
.catch(error => {
|
||||||
});
|
reject(error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getAssetByChannel (channelName, channelId, claimName) {
|
getAssetByChannel (channelName, channelId, claimName) {
|
||||||
logger.debug('getting asset by channel');
|
logger.debug('getting asset by channel');
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// 1. get the long channel id
|
// 1. get the long channel id
|
||||||
getLongChannelId(channelName, channelId)
|
db
|
||||||
// 2. get the claim Id
|
.getLongChannelId(channelName, channelId)
|
||||||
.then(longChannelId => {
|
// 2. get the claim Id
|
||||||
return getClaimIdByLongChannelId(longChannelId, claimName);
|
.then(longChannelId => {
|
||||||
})
|
return db.getClaimIdByLongChannelId(longChannelId, claimName);
|
||||||
// 3. get the asset by this claim id and name
|
})
|
||||||
.then(claimId => {
|
// 3. get the asset by this claim id and name
|
||||||
logger.debug('asset claim id = ', claimId);
|
.then(claimId => {
|
||||||
resolve(getAssetByLongClaimId(claimId, claimName));
|
logger.debug('asset claim id = ', claimId);
|
||||||
})
|
resolve(getAssetByLongClaimId(claimId, claimName));
|
||||||
.catch(error => {
|
})
|
||||||
reject(error);
|
.catch(error => {
|
||||||
});
|
reject(error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getChannelContents (channelName, channelId) {
|
getChannelContents (channelName, channelId) {
|
||||||
|
@ -134,32 +137,33 @@ module.exports = {
|
||||||
let longChannelId;
|
let longChannelId;
|
||||||
let shortChannelId;
|
let shortChannelId;
|
||||||
// 1. get the long channel Id
|
// 1. get the long channel Id
|
||||||
getLongChannelId(channelName, channelId)
|
db
|
||||||
// 2. get all claims for that channel
|
.getLongChannelId(channelName, channelId)
|
||||||
.then(result => {
|
// 2. get all claims for that channel
|
||||||
longChannelId = result;
|
.then(result => {
|
||||||
return getShortChannelIdFromLongChannelId(channelName, longChannelId);
|
longChannelId = result;
|
||||||
})
|
return db.getShortChannelIdFromLongChannelId(channelName, longChannelId);
|
||||||
// 3. get all Claim records for this channel
|
})
|
||||||
.then(result => {
|
// 3. get all Claim records for this channel
|
||||||
shortChannelId = result;
|
.then(result => {
|
||||||
return getAllChannelClaims(longChannelId);
|
shortChannelId = result;
|
||||||
})
|
return db.getAllChannelClaims(longChannelId);
|
||||||
// 4. add extra data not available from Claim table
|
})
|
||||||
.then(allChannelClaims => {
|
// 4. add extra data not available from Claim table
|
||||||
if (allChannelClaims) {
|
.then(allChannelClaims => {
|
||||||
allChannelClaims.forEach(element => {
|
if (allChannelClaims) {
|
||||||
element['channelName'] = channelName;
|
allChannelClaims.forEach(element => {
|
||||||
element['longChannelId'] = longChannelId;
|
element['channelName'] = channelName;
|
||||||
element['shortChannelId'] = shortChannelId;
|
element['longChannelId'] = longChannelId;
|
||||||
element['fileExtension'] = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
element['shortChannelId'] = shortChannelId;
|
||||||
});
|
element['fileExtension'] = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
||||||
}
|
});
|
||||||
return resolve(allChannelClaims);
|
}
|
||||||
})
|
return resolve(allChannelClaims);
|
||||||
.catch(error => {
|
})
|
||||||
reject(error);
|
.catch(error => {
|
||||||
});
|
reject(error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
serveOrShowAsset (fileInfo, extension, method, headers, originalUrl, ip, res) {
|
serveOrShowAsset (fileInfo, extension, method, headers, originalUrl, ip, res) {
|
||||||
|
@ -181,21 +185,22 @@ module.exports = {
|
||||||
showFileLite(fileInfo, res);
|
showFileLite(fileInfo, res);
|
||||||
return fileInfo;
|
return fileInfo;
|
||||||
case SHOW:
|
case SHOW:
|
||||||
return getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
return db
|
||||||
.then(shortId => {
|
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
||||||
fileInfo['shortId'] = shortId;
|
.then(shortId => {
|
||||||
return resolveAgainstClaimTable(fileInfo.name, fileInfo.claimId);
|
fileInfo['shortId'] = shortId;
|
||||||
})
|
return db.resolveClaim(fileInfo.name, fileInfo.claimId);
|
||||||
.then(resolveResult => {
|
})
|
||||||
fileInfo['title'] = resolveResult.title;
|
.then(resolveResult => {
|
||||||
fileInfo['description'] = resolveResult.description;
|
fileInfo['title'] = resolveResult.title;
|
||||||
showFile(fileInfo, res);
|
fileInfo['description'] = resolveResult.description;
|
||||||
return fileInfo;
|
showFile(fileInfo, res);
|
||||||
})
|
return fileInfo;
|
||||||
.catch(error => {
|
})
|
||||||
logger.error('throwing serve/show error...');
|
.catch(error => {
|
||||||
throw error;
|
logger.error('throwing serve/show error...');
|
||||||
});
|
throw error;
|
||||||
|
});
|
||||||
default:
|
default:
|
||||||
logger.error('I did not recognize that method');
|
logger.error('I did not recognize that method');
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const db = require('../models');
|
|
||||||
|
|
||||||
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
|
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
|
||||||
return {
|
return {
|
||||||
|
@ -10,107 +9,6 @@ function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug(`finding long channel id for ${channelName}:${channelId}`);
|
|
||||||
// get the long channel Id
|
|
||||||
db.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
logger.debug('result >>', result);
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
throw new Error('That is an invalid Short Channel Id');
|
|
||||||
default: // note results must be sorted
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLongChannelIdFromChannelName (channelName) {
|
|
||||||
// select the top top channel id
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug(`finding long channel id for ${channelName}`);
|
|
||||||
// get the long channel Id
|
|
||||||
db.sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY amount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
logger.debug('result >>', result);
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
throw new Error('That is an invalid Channel Name');
|
|
||||||
default:
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLongClaimIdFromShortClaimId (name, shortId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug('getting claim_id from short url');
|
|
||||||
// use the daemon to check for claims list
|
|
||||||
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:
|
|
||||||
return reject(new Error('That is an invalid Short Claim Id'));
|
|
||||||
default: // note results must be sorted
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTopFreeClaimIdByClaimName (name) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
db.sequelize.query(`SELECT claimId 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].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function sortResult (result, longId) {
|
|
||||||
let claimIndex;
|
|
||||||
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
|
||||||
let shortIdLength = 0;
|
|
||||||
// find the index of this certificate
|
|
||||||
claimIndex = result.findIndex(element => {
|
|
||||||
return element.claimId === longId;
|
|
||||||
});
|
|
||||||
if (claimIndex < 0) { throw new Error('claimid not found in possible sorted list') }
|
|
||||||
// get an array of all certificates with lower height
|
|
||||||
let possibleMatches = result.slice(0, claimIndex);
|
|
||||||
// remove certificates with the same prefixes until none are left.
|
|
||||||
while (possibleMatches.length > 0) {
|
|
||||||
shortIdLength += 1;
|
|
||||||
shortId = longId.substring(0, shortIdLength);
|
|
||||||
possibleMatches = possibleMatches.filter(element => {
|
|
||||||
return (element.claimId.substring(0, shortIdLength) === shortId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// return the short Id
|
|
||||||
logger.debug('short channel id ===', shortId);
|
|
||||||
return shortId;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
serveFile ({ fileName, fileType, filePath }, res) {
|
serveFile ({ fileName, fileType, filePath }, res) {
|
||||||
logger.info(`serving file ${fileName}`);
|
logger.info(`serving file ${fileName}`);
|
||||||
|
@ -144,124 +42,4 @@ module.exports = {
|
||||||
const openGraphInfo = createOpenGraphInfo(fileInfo);
|
const openGraphInfo = createOpenGraphInfo(fileInfo);
|
||||||
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
|
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
|
||||||
},
|
},
|
||||||
getLongClaimId (claimName, claimId) { // read the various inputs and decide how to return the long claim id
|
|
||||||
if (claimId && (claimId.length === 40)) {
|
|
||||||
return new Promise((resolve, reject) => resolve(claimId));
|
|
||||||
} else if (claimId && claimId.length < 40) {
|
|
||||||
return getLongClaimIdFromShortClaimId(claimName, claimId); // need to create this function
|
|
||||||
} else { // if no claim id provided
|
|
||||||
return getTopFreeClaimIdByClaimName(claimName);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
getShortClaimIdFromLongClaimId (claimId, claimName) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug('finding short channel id');
|
|
||||||
db.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${claimName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return reject(new Error('That is an invalid claim name'));
|
|
||||||
default:
|
|
||||||
return resolve(sortResult(result, claimId));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getAllFreeClaims (name) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
db.sequelize.query(`SELECT name, claimId, outpoint, height, address 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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
resolveAgainstClaimTable (name, claimId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
db.sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description 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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getClaimIdByLongChannelId (channelId, claimName) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug(`finding claim id for claim "${claimName}" from channel "${channelId}"`);
|
|
||||||
db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${claimName}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return reject(new Error('There is no such claim for that channel'));
|
|
||||||
default:
|
|
||||||
return resolve(result[0].claimId);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getAllChannelClaims (channelId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug(`finding all claims in channel "${channelId}"`);
|
|
||||||
db.sequelize.query(`SELECT name, claimId, outpoint, height, address, contentType, title, description, license FROM Claim WHERE certificateId = '${channelId}' ORDeR BY height DESC;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return resolve(null);
|
|
||||||
default:
|
|
||||||
return resolve(result);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
getLongChannelId (channelName, channelId) {
|
|
||||||
if (channelId && (channelId.length === 40)) { // full channel id
|
|
||||||
return new Promise((resolve, reject) => resolve(channelId));
|
|
||||||
} else if (channelId && channelId.length < 40) { // short channel id
|
|
||||||
return getLongChannelIdFromShortChannelId(channelName, channelId);
|
|
||||||
} else {
|
|
||||||
return getLongChannelIdFromChannelName(channelName);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
getShortChannelIdFromLongChannelId (channelName, longChannelId) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
logger.debug('finding short channel id');
|
|
||||||
db.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
|
|
||||||
.then(result => {
|
|
||||||
switch (result.length) {
|
|
||||||
case 0:
|
|
||||||
return reject(new Error('That is an invalid channel name'));
|
|
||||||
default:
|
|
||||||
return resolve(sortResult(result, longChannelId));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
233
models/index.js
233
models/index.js
|
@ -17,6 +17,102 @@ const sequelize = new Sequelize(connectionUri, {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function sortResult (result, longId) {
|
||||||
|
let claimIndex;
|
||||||
|
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
||||||
|
let shortIdLength = 0;
|
||||||
|
// find the index of this certificate
|
||||||
|
claimIndex = result.findIndex(element => {
|
||||||
|
return element.claimId === longId;
|
||||||
|
});
|
||||||
|
if (claimIndex < 0) { throw new Error('claimid not found in possible sorted list') }
|
||||||
|
// get an array of all certificates with lower height
|
||||||
|
let possibleMatches = result.slice(0, claimIndex);
|
||||||
|
// remove certificates with the same prefixes until none are left.
|
||||||
|
while (possibleMatches.length > 0) {
|
||||||
|
shortIdLength += 1;
|
||||||
|
shortId = longId.substring(0, shortIdLength);
|
||||||
|
possibleMatches = possibleMatches.filter(element => {
|
||||||
|
return (element.claimId.substring(0, shortIdLength) === shortId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// return the short Id
|
||||||
|
logger.debug('short channel id ===', shortId);
|
||||||
|
return shortId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLongClaimIdFromShortClaimId (name, shortId) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
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:
|
||||||
|
return reject(new Error('That is an invalid Short Claim Id'));
|
||||||
|
default: // note results must be sorted
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTopFreeClaimIdByClaimName (name) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId 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].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' AND claimId LIKE '${channelId}%' ORDER BY height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
throw new Error('That is an invalid Short Channel Id');
|
||||||
|
default: // note results must be sorted
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLongChannelIdFromChannelName (channelName) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY amount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
throw new Error('That is an invalid Channel Name');
|
||||||
|
default:
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
sequelize
|
sequelize
|
||||||
.authenticate()
|
.authenticate()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -57,7 +153,8 @@ db['upsert'] = (Model, values, condition, tableName) => {
|
||||||
logger.debug(`creating "${values.name}" "${values.claimId}" in db.${tableName}`);
|
logger.debug(`creating "${values.name}" "${values.claimId}" in db.${tableName}`);
|
||||||
return Model.create(values);
|
return Model.create(values);
|
||||||
}
|
}
|
||||||
}).catch(function (error) {
|
})
|
||||||
|
.catch(function (error) {
|
||||||
logger.error('Sequelize findOne error', error);
|
logger.error('Sequelize findOne error', error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -70,4 +167,138 @@ db['getRecentClaims'] = () => {
|
||||||
return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT });
|
return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
db['getShortClaimIdFromLongClaimId'] = (claimId, claimName) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
logger.debug('finding short channel id');
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${claimName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return reject(new Error('That is an invalid claim name'));
|
||||||
|
default:
|
||||||
|
return resolve(sortResult(result, claimId));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getShortChannelIdFromLongChannelId'] = (channelName, longChannelId) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
logger.debug('finding short channel id');
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return reject(new Error('That is an invalid channel name'));
|
||||||
|
default:
|
||||||
|
return resolve(sortResult(result, longChannelId));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getAllFreeClaims'] = (name) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT name, claimId, outpoint, height, address 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['resolveClaim'] = (name, claimId) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT name, claimId, outpoint, height, address, title, description 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getClaimIdByLongChannelId'] = (channelId, claimName) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
logger.debug(`finding claim id for claim "${claimName}" from channel "${channelId}"`);
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${claimName}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return reject(new Error('There is no such claim for that channel'));
|
||||||
|
default:
|
||||||
|
return resolve(result[0].claimId);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getAllChannelClaims'] = (channelId) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
logger.debug(`finding all claims in channel "${channelId}"`);
|
||||||
|
db
|
||||||
|
.sequelize.query(`SELECT name, claimId, outpoint, height, address, contentType, title, description, license FROM Claim WHERE certificateId = '${channelId}' ORDeR BY height DESC;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
|
.then(result => {
|
||||||
|
switch (result.length) {
|
||||||
|
case 0:
|
||||||
|
return resolve(null);
|
||||||
|
default:
|
||||||
|
return resolve(result);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getLongClaimId'] = (claimName, claimId) => {
|
||||||
|
if (claimId && (claimId.length === 40)) {
|
||||||
|
return new Promise((resolve, reject) => resolve(claimId));
|
||||||
|
} else if (claimId && claimId.length < 40) {
|
||||||
|
return getLongClaimIdFromShortClaimId(claimName, claimId); // need to create this function
|
||||||
|
} else { // if no claim id provided
|
||||||
|
return getTopFreeClaimIdByClaimName(claimName);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
db['getLongChannelId'] = (channelName, channelId) => {
|
||||||
|
if (channelId && (channelId.length === 40)) { // full channel id
|
||||||
|
return new Promise((resolve, reject) => resolve(channelId));
|
||||||
|
} else if (channelId && channelId.length < 40) { // short channel id
|
||||||
|
return getLongChannelIdFromShortChannelId(channelName, channelId);
|
||||||
|
} else {
|
||||||
|
return getLongChannelIdFromChannelName(channelName);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = db;
|
module.exports = db;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const errorHandlers = require('../helpers/errorHandlers.js');
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
||||||
const { getAllFreeClaims } = require('../helpers/serveHelpers.js');
|
const db = require('../models');
|
||||||
const { postToStats, getStatsSummary, getTrendingClaims, getRecentClaims } = require('../controllers/statsController.js');
|
const { postToStats, getStatsSummary, getTrendingClaims, getRecentClaims } = require('../controllers/statsController.js');
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
|
@ -63,7 +63,8 @@ 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
|
||||||
getAllFreeClaims(params.name)
|
db
|
||||||
|
.getAllFreeClaims(params.name)
|
||||||
.then(orderedFreeClaims => {
|
.then(orderedFreeClaims => {
|
||||||
if (!orderedFreeClaims) {
|
if (!orderedFreeClaims) {
|
||||||
res.status(307).render('noClaims');
|
res.status(307).render('noClaims');
|
||||||
|
|
Loading…
Reference in a new issue