added support for channel with full id

This commit is contained in:
bill bittner 2017-08-21 18:03:57 -07:00
parent a43836d639
commit 28ab926ef9
3 changed files with 54 additions and 22 deletions

View file

@ -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 { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable } = require('../helpers/serveHelpers.js'); const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannelId } = require('../helpers/serveHelpers.js');
function checkForLocalAssetByClaimId (claimId, name) { function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -82,14 +82,21 @@ function getAssetByClaimId (fullClaimId, name) {
} }
module.exports = { module.exports = {
getAssetByChannel (channelName, name) { getAssetByChannel (channelName, channelId, name) {
logger.debug('...getting asset by channel...'); if (channelId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// temporarily throw error getClaimIdByChannelId(channelId, name)
reject(new Error('channel names are not currently supported')); .then(claimId => {
// get the claim id logger.debug('claim id = ', claimId);
// get the asset by claim Id resolve(getAssetByClaimId(claimId, name));
})
.catch(error => {
reject(error);
}); });
});
} else {
return new Error('this is not supported yet');
}
}, },
getAssetByShortId: function (shortId, name) { getAssetByShortId: function (shortId, name) {
logger.debug('...getting asset by short id...'); logger.debug('...getting asset by short id...');

View file

@ -127,6 +127,24 @@ module.exports = {
}); });
}); });
}, },
getClaimIdByChannelId (channelId, name) {
return new Promise((resolve, reject) => {
logger.debug('finding claim id from channel id');
db.sequelize.query(`SELECT claimId FROM Claim WHERE name = '${name}' AND certificateId = '${channelId}' LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
return reject(new Error('That is an invalid Channel Id'));
default: // note results must be sorted
logger.debug('found result', result);
return resolve(result[0].claimId);
}
})
.catch(error => {
reject(error);
});
});
},
getAllFreeClaims (name) { getAllFreeClaims (name) {
return new Promise((resolve, reject) => { 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 }) db.sequelize.query(`SELECT * FROM Claim WHERE name = '${name}' ORDER BY amount DESC, height ASC`, { type: db.sequelize.QueryTypes.SELECT })

View file

@ -7,19 +7,20 @@ const SERVE = 'SERVE';
const SHOW = 'SHOW'; const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE'; const SHOWLITE = 'SHOWLITE';
const CHANNEL = 'CHANNEL'; const CHANNEL = 'CHANNEL';
const SHORTURL = 'SHORTURL'; const CLAIM_ID_SHORT = 'CLAIM_ID_SHORT';
const CLAIMID = 'CLAIMID'; const CLAIM_ID_LONG = 'CLAIM_ID_LONG';
const NAME = 'NAME'; const CLAIM_NAME = 'CLAIM_NAME';
const CHANNELID_INDICATOR = ':';
function getAsset (claimType, channelName, shortId, fullClaimId, name) { function getAsset (claimType, channelName, channelId, shortId, fullClaimId, name) {
switch (claimType) { switch (claimType) {
case CHANNEL: case CHANNEL:
return getAssetByChannel(channelName, name); return getAssetByChannel(channelName, channelId, name);
case SHORTURL: case CLAIM_ID_SHORT:
return getAssetByShortId(shortId, name); return getAssetByShortId(shortId, name);
case CLAIMID: case CLAIM_ID_LONG:
return getAssetByClaimId(fullClaimId, name); return getAssetByClaimId(fullClaimId, name);
case NAME: case CLAIM_NAME:
return getAssetByName(name); return getAssetByName(name);
default: default:
return new Error('that claim type was not found'); return new Error('that claim type was not found');
@ -89,6 +90,7 @@ module.exports = (app) => {
let channelName = null; let channelName = null;
let shortId = null; let shortId = null;
let fullClaimId = null; let fullClaimId = null;
let channelId = null;
let method; let method;
let extension; let extension;
// parse the name // parse the name
@ -121,24 +123,29 @@ module.exports = (app) => {
logger.debug('method =', method); logger.debug('method =', method);
// parse identifier for whether it is a channel, short url, or claim_id // parse identifier for whether it is a channel, short url, or claim_id
if (identifier.charAt(0) === '@') { if (identifier.charAt(0) === '@') {
channelName = identifier.substring(1); channelName = identifier;
logger.debug('channel name =', channelName); logger.debug('channel name =', channelName);
claimType = CHANNEL; claimType = CHANNEL;
const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR);
if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1);
channelName = channelName.substring(0, channelIdIndex);
}
} else if (identifier.length === 40) { } else if (identifier.length === 40) {
fullClaimId = identifier; fullClaimId = identifier;
logger.debug('full claim id =', fullClaimId); logger.debug('full claim id =', fullClaimId);
claimType = CLAIMID; claimType = CLAIM_ID_LONG;
} else if (identifier.length < 40) { } else if (identifier.length < 40) {
shortId = identifier; shortId = identifier;
logger.debug('short claim id =', shortId); logger.debug('short claim id =', shortId);
claimType = SHORTURL; claimType = CLAIM_ID_SHORT;
} else { } else {
logger.error('The URL provided could not be parsed'); logger.error('The URL provided could not be parsed');
res.send('that url is invalid'); res.send('that url is invalid');
return; return;
}; };
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(claimType, channelName, shortId, fullClaimId, name) getAsset(claimType, channelName, channelId, shortId, fullClaimId, name)
// 2. serve or show // 2. serve or show
.then(fileInfo => { .then(fileInfo => {
logger.debug('fileInfo', fileInfo); logger.debug('fileInfo', fileInfo);
@ -179,7 +186,7 @@ module.exports = (app) => {
logger.debug('claim name = ', name); logger.debug('claim name = ', name);
logger.debug('method =', method); logger.debug('method =', method);
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(NAME, null, null, null, name) getAsset(CLAIM_NAME, null, null, null, name)
// 2. serve or show // 2. serve or show
.then(fileInfo => { .then(fileInfo => {
if (!fileInfo) { if (!fileInfo) {