changed "no channel" to be a success response

This commit is contained in:
bill bittner 2017-10-17 15:09:43 -07:00
parent d4be277ffa
commit cd0fa39f15
3 changed files with 26 additions and 9 deletions

View file

@ -8,6 +8,7 @@ const SERVE = 'SERVE';
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL';
function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => {
@ -148,16 +149,26 @@ module.exports = {
db
.getLongChannelId(channelName, channelId) // 1. get the long channel Id
.then(result => { // 2. get all claims for that channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
longChannelId = result;
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
})
.then(result => { // 3. get all Claim records for this channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
shortChannelId = result;
return db.getAllChannelClaims(longChannelId);
})
.then(allChannelClaims => { // 4. add extra data not available from Claim table
if (allChannelClaims) {
allChannelClaims.forEach(element => {
.then(result => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) {
resolve(NO_CHANNEL);
return;
}
if (result) {
result.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
@ -165,11 +176,11 @@ module.exports = {
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
});
}
return resolve({
resolve({
channelName,
longChannelId,
shortChannelId,
claims: allChannelClaims,
claims: result,
});
})
.catch(error => {

View file

@ -6,6 +6,8 @@ const config = require('config');
const db = {};
const logger = require('winston');
const NO_CHANNEL = 'NO_CHANNEL';
const database = config.get('Database.Database');
const username = config.get('Database.Username');
const password = config.get('Database.Password');
@ -88,7 +90,7 @@ function getLongChannelIdFromShortChannelId (channelName, channelId) {
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid Short Channel Id');
return resolve(NO_CHANNEL);
default: // note results must be sorted
return resolve(result[0].claimId);
}
@ -100,13 +102,14 @@ function getLongChannelIdFromShortChannelId (channelName, channelId) {
}
function getLongChannelIdFromChannelName (channelName) {
logger.debug(`getLongChannelIdFromChannelName(${channelName})`);
return new Promise((resolve, reject) => {
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 })
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid Channel Name');
return resolve(NO_CHANNEL);
default:
return resolve(result[0].claimId);
}

View file

@ -10,6 +10,7 @@ const CLAIM = 'CLAIM';
const CLAIM_ID_CHAR = ':';
const CHANNEL_CHAR = '@';
const CLAIMS_PER_PAGE = 10;
const NO_CHANNEL = 'NO_CHANNEL';
function isValidClaimId (claimId) {
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
@ -178,7 +179,9 @@ module.exports = (app) => {
getChannelContents(channelName, channelId)
// 2. respond to the request
.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', {
channelName : result.channelName,
longChannelId : result.longChannelId,
@ -190,7 +193,7 @@ module.exports = (app) => {
totalPages : 0,
totalResults : 0,
});
} else {
} else { // channel found, with claims
const totalPages = determineTotalPages(result.claims.length);
res.status(200).render('channel', {
channelName : result.channelName,