changed "no channel" to be a success response
This commit is contained in:
parent
d4be277ffa
commit
cd0fa39f15
3 changed files with 26 additions and 9 deletions
|
@ -8,6 +8,7 @@ const SERVE = 'SERVE';
|
||||||
const SHOW = 'SHOW';
|
const SHOW = 'SHOW';
|
||||||
const SHOWLITE = 'SHOWLITE';
|
const SHOWLITE = 'SHOWLITE';
|
||||||
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
|
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
function checkForLocalAssetByClaimId (claimId, name) {
|
function checkForLocalAssetByClaimId (claimId, name) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -148,16 +149,26 @@ module.exports = {
|
||||||
db
|
db
|
||||||
.getLongChannelId(channelName, channelId) // 1. get the long channel Id
|
.getLongChannelId(channelName, channelId) // 1. get the long channel Id
|
||||||
.then(result => { // 2. get all claims for that channel
|
.then(result => { // 2. get all claims for that channel
|
||||||
|
if (result === NO_CHANNEL) {
|
||||||
|
return NO_CHANNEL;
|
||||||
|
}
|
||||||
longChannelId = result;
|
longChannelId = result;
|
||||||
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
||||||
})
|
})
|
||||||
.then(result => { // 3. get all Claim records for this channel
|
.then(result => { // 3. get all Claim records for this channel
|
||||||
|
if (result === NO_CHANNEL) {
|
||||||
|
return NO_CHANNEL;
|
||||||
|
}
|
||||||
shortChannelId = result;
|
shortChannelId = result;
|
||||||
return db.getAllChannelClaims(longChannelId);
|
return db.getAllChannelClaims(longChannelId);
|
||||||
})
|
})
|
||||||
.then(allChannelClaims => { // 4. add extra data not available from Claim table
|
.then(result => { // 4. add extra data not available from Claim table
|
||||||
if (allChannelClaims) {
|
if (result === NO_CHANNEL) {
|
||||||
allChannelClaims.forEach(element => {
|
resolve(NO_CHANNEL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result) {
|
||||||
|
result.forEach(element => {
|
||||||
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
||||||
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
|
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
|
||||||
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
|
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
|
||||||
|
@ -165,11 +176,11 @@ module.exports = {
|
||||||
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
|
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return resolve({
|
resolve({
|
||||||
channelName,
|
channelName,
|
||||||
longChannelId,
|
longChannelId,
|
||||||
shortChannelId,
|
shortChannelId,
|
||||||
claims: allChannelClaims,
|
claims: result,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
|
@ -6,6 +6,8 @@ const config = require('config');
|
||||||
const db = {};
|
const db = {};
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
const database = config.get('Database.Database');
|
const database = config.get('Database.Database');
|
||||||
const username = config.get('Database.Username');
|
const username = config.get('Database.Username');
|
||||||
const password = config.get('Database.Password');
|
const password = config.get('Database.Password');
|
||||||
|
@ -88,7 +90,7 @@ function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
||||||
.then(result => {
|
.then(result => {
|
||||||
switch (result.length) {
|
switch (result.length) {
|
||||||
case 0:
|
case 0:
|
||||||
throw new Error('That is an invalid Short Channel Id');
|
return resolve(NO_CHANNEL);
|
||||||
default: // note results must be sorted
|
default: // note results must be sorted
|
||||||
return resolve(result[0].claimId);
|
return resolve(result[0].claimId);
|
||||||
}
|
}
|
||||||
|
@ -100,13 +102,14 @@ function getLongChannelIdFromShortChannelId (channelName, channelId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLongChannelIdFromChannelName (channelName) {
|
function getLongChannelIdFromChannelName (channelName) {
|
||||||
|
logger.debug(`getLongChannelIdFromChannelName(${channelName})`);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
db
|
db
|
||||||
.sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
.sequelize.query(`SELECT claimId, amount, height FROM Certificate WHERE name = '${channelName}' ORDER BY effectiveAmount DESC, height ASC LIMIT 1;`, { type: db.sequelize.QueryTypes.SELECT })
|
||||||
.then(result => {
|
.then(result => {
|
||||||
switch (result.length) {
|
switch (result.length) {
|
||||||
case 0:
|
case 0:
|
||||||
throw new Error('That is an invalid Channel Name');
|
return resolve(NO_CHANNEL);
|
||||||
default:
|
default:
|
||||||
return resolve(result[0].claimId);
|
return resolve(result[0].claimId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ const CLAIM = 'CLAIM';
|
||||||
const CLAIM_ID_CHAR = ':';
|
const CLAIM_ID_CHAR = ':';
|
||||||
const CHANNEL_CHAR = '@';
|
const CHANNEL_CHAR = '@';
|
||||||
const CLAIMS_PER_PAGE = 10;
|
const CLAIMS_PER_PAGE = 10;
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
function isValidClaimId (claimId) {
|
function isValidClaimId (claimId) {
|
||||||
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
|
||||||
|
@ -178,7 +179,9 @@ module.exports = (app) => {
|
||||||
getChannelContents(channelName, channelId)
|
getChannelContents(channelName, channelId)
|
||||||
// 2. respond to the request
|
// 2. respond to the request
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (!result.claims) {
|
if (result === NO_CHANNEL) { // no channel found
|
||||||
|
res.status(200).json('no channel found');
|
||||||
|
} else if (!result.claims) { // channel found, but no claims
|
||||||
res.status(200).render('channel', {
|
res.status(200).render('channel', {
|
||||||
channelName : result.channelName,
|
channelName : result.channelName,
|
||||||
longChannelId : result.longChannelId,
|
longChannelId : result.longChannelId,
|
||||||
|
@ -190,7 +193,7 @@ module.exports = (app) => {
|
||||||
totalPages : 0,
|
totalPages : 0,
|
||||||
totalResults : 0,
|
totalResults : 0,
|
||||||
});
|
});
|
||||||
} else {
|
} else { // channel found, with claims
|
||||||
const totalPages = determineTotalPages(result.claims.length);
|
const totalPages = determineTotalPages(result.claims.length);
|
||||||
res.status(200).render('channel', {
|
res.status(200).render('channel', {
|
||||||
channelName : result.channelName,
|
channelName : result.channelName,
|
||||||
|
|
Loading…
Add table
Reference in a new issue