Resolve channels #150
3 changed files with 54 additions and 22 deletions
|
@ -1,7 +1,7 @@
|
|||
const lbryApi = require('../helpers/lbryApi.js');
|
||||
const db = require('../models');
|
||||
const logger = require('winston');
|
||||
const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable } = require('../helpers/serveHelpers.js');
|
||||
const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannelId } = require('../helpers/serveHelpers.js');
|
||||
|
||||
I get a little confused sometimes on the line between controller and model responsibilities. But, yes, after looking at it and re-reading some MVC notes, I think that all the database related functions should go in the model. I rearranged as such. I get a little confused sometimes on the line between controller and model responsibilities. But, yes, after looking at it and re-reading some MVC notes, I think that all the database related functions should go in the model. I rearranged as such.
https://github.com/lbryio/spee.ch/commit/af046e9d3655a57bf9cd67d3a67a54d4d44ed802
|
||||
function checkForLocalAssetByClaimId (claimId, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@ -82,14 +82,21 @@ 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 the asset by claim Id
|
||||
});
|
||||
getAssetByChannel (channelName, channelId, name) {
|
||||
if (channelId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClaimIdByChannelId(channelId, name)
|
||||
.then(claimId => {
|
||||
logger.debug('claim id = ', claimId);
|
||||
resolve(getAssetByClaimId(claimId, name));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return new Error('this is not supported yet');
|
||||
}
|
||||
},
|
||||
getAssetByShortId: function (shortId, name) {
|
||||
logger.debug('...getting asset by short id...');
|
||||
|
|
|
@ -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) {
|
||||
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 })
|
||||
|
|
|
@ -7,19 +7,20 @@ const SERVE = 'SERVE';
|
|||
const SHOW = 'SHOW';
|
||||
const SHOWLITE = 'SHOWLITE';
|
||||
const CHANNEL = 'CHANNEL';
|
||||
const SHORTURL = 'SHORTURL';
|
||||
const CLAIMID = 'CLAIMID';
|
||||
const NAME = 'NAME';
|
||||
const CLAIM_ID_SHORT = 'CLAIM_ID_SHORT';
|
||||
const CLAIM_ID_LONG = 'CLAIM_ID_LONG';
|
||||
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) {
|
||||
case CHANNEL:
|
||||
return getAssetByChannel(channelName, name);
|
||||
case SHORTURL:
|
||||
return getAssetByChannel(channelName, channelId, name);
|
||||
case CLAIM_ID_SHORT:
|
||||
return getAssetByShortId(shortId, name);
|
||||
case CLAIMID:
|
||||
case CLAIM_ID_LONG:
|
||||
return getAssetByClaimId(fullClaimId, name);
|
||||
case NAME:
|
||||
case CLAIM_NAME:
|
||||
return getAssetByName(name);
|
||||
default:
|
||||
return new Error('that claim type was not found');
|
||||
|
@ -89,6 +90,7 @@ module.exports = (app) => {
|
|||
let channelName = null;
|
||||
let shortId = null;
|
||||
let fullClaimId = null;
|
||||
let channelId = null;
|
||||
let method;
|
||||
let extension;
|
||||
// parse the name
|
||||
|
@ -121,24 +123,29 @@ module.exports = (app) => {
|
|||
logger.debug('method =', method);
|
||||
// parse identifier for whether it is a channel, short url, or claim_id
|
||||
if (identifier.charAt(0) === '@') {
|
||||
channelName = identifier.substring(1);
|
||||
channelName = identifier;
|
||||
logger.debug('channel name =', channelName);
|
||||
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) {
|
||||
fullClaimId = identifier;
|
||||
logger.debug('full claim id =', fullClaimId);
|
||||
claimType = CLAIMID;
|
||||
claimType = CLAIM_ID_LONG;
|
||||
} else if (identifier.length < 40) {
|
||||
shortId = identifier;
|
||||
logger.debug('short claim id =', shortId);
|
||||
claimType = SHORTURL;
|
||||
claimType = CLAIM_ID_SHORT;
|
||||
} else {
|
||||
logger.error('The URL provided could not be parsed');
|
||||
res.send('that url is invalid');
|
||||
return;
|
||||
};
|
||||
// 1. retrieve the asset and information
|
||||
getAsset(claimType, channelName, shortId, fullClaimId, name)
|
||||
getAsset(claimType, channelName, channelId, shortId, fullClaimId, name)
|
||||
// 2. serve or show
|
||||
.then(fileInfo => {
|
||||
logger.debug('fileInfo', fileInfo);
|
||||
|
@ -179,7 +186,7 @@ module.exports = (app) => {
|
|||
logger.debug('claim name = ', name);
|
||||
logger.debug('method =', method);
|
||||
// 1. retrieve the asset and information
|
||||
getAsset(NAME, null, null, null, name)
|
||||
getAsset(CLAIM_NAME, null, null, null, name)
|
||||
// 2. serve or show
|
||||
.then(fileInfo => {
|
||||
if (!fileInfo) {
|
||||
|
|
Loading…
Reference in a new issue
This looks like it could use better organization. I'd be inclined to put a lot of these inside of the
modal
folder, but I'd suggest looking at what other conventions are used in this framework before doing that.