updated no matching channel or claim to be success cases

This commit is contained in:
bill bittner 2017-10-17 15:50:35 -07:00
parent cd0fa39f15
commit 0a62df1d76
3 changed files with 48 additions and 37 deletions

View file

@ -9,6 +9,7 @@ const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => {
@ -80,7 +81,7 @@ function getAssetByLongClaimId (fullClaimId, name) {
// insert a record in the File table & Update Claim table
return db.File.create(fileRecord);
})
.then(fileRecordResults => {
.then(() => {
logger.debug('File record successfully updated');
resolve(fileRecord);
})
@ -109,13 +110,15 @@ module.exports = {
getAssetByClaim (claimName, claimId) {
logger.debug('getting asset by claim');
return new Promise((resolve, reject) => {
// 1. get the long claim id
db
.getLongClaimId(claimName, claimId)
db.getLongClaimId(claimName, claimId) // 1. get the long claim id
// 2. get the claim Id
.then(longClaimId => {
logger.debug('long claim id = ', longClaimId);
resolve(getAssetByLongClaimId(longClaimId, claimName));
.then(result => {
if (result === NO_CLAIM || !result) {
resolve(NO_CLAIM);
return;
}
logger.debug('long claim id = ', result);
resolve(getAssetByLongClaimId(result, claimName));
})
.catch(error => {
reject(error);
@ -125,17 +128,21 @@ module.exports = {
getAssetByChannel (channelName, channelId, claimName) {
logger.debug('getting asset by channel');
return new Promise((resolve, reject) => {
// 1. get the long channel id
db
.getLongChannelId(channelName, channelId)
// 2. get the claim Id
.then(longChannelId => {
return db.getClaimIdByLongChannelId(longChannelId, claimName);
db.getLongChannelId(channelName, channelId) // 1. get the long channel id
.then(result => { // 2. get the claim Id
if (result === NO_CHANNEL) {
resolve(result);
return;
}
return db.getClaimIdByLongChannelId(result, claimName);
})
// 3. get the asset by this claim id and name
.then(claimId => {
logger.debug('asset claim id = ', claimId);
resolve(getAssetByLongClaimId(claimId, claimName));
.then(result => { // 3. get the asset by this claim id and name
logger.debug('asset claim id =', result);
if (result === NO_CHANNEL || result === NO_CLAIM) {
resolve(result);
return;
}
resolve(getAssetByLongClaimId(result, claimName));
})
.catch(error => {
reject(error);

View file

@ -7,6 +7,7 @@ const db = {};
const logger = require('winston');
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const database = config.get('Database.Database');
const username = config.get('Database.Username');
@ -54,7 +55,7 @@ function getLongClaimIdFromShortClaimId (name, shortId) {
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid Short Claim Id');
return resolve(NO_CLAIM);
default: // note results must be sorted
return resolve(result[0].claimId);
}
@ -258,7 +259,7 @@ db['getClaimIdByLongChannelId'] = (channelId, claimName) => {
.then(result => {
switch (result.length) {
case 0:
throw new Error('There is no such claim for that channel');
return resolve(NO_CLAIM);
default:
return resolve(result[0].claimId);
}
@ -290,23 +291,23 @@ db['getAllChannelClaims'] = (channelId) => {
db['getLongClaimId'] = (claimName, claimId) => {
logger.debug(`getLongClaimId (${claimName}, ${claimId})`);
if (claimId && (claimId.length === 40)) {
if (claimId && (claimId.length === 40)) { // if a full claim id is provided
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);
return getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided
} else {
return getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided
}
};
db['getLongChannelId'] = (channelName, channelId) => {
logger.debug(`getLongChannelId (${channelName}, ${channelId})`);
if (channelId && (channelId.length === 40)) { // full channel id
if (channelId && (channelId.length === 40)) { // if a full channel id is provided
return new Promise((resolve, reject) => resolve(channelId));
} else if (channelId && channelId.length < 40) { // short channel id
} else if (channelId && channelId.length < 40) { // if a short channel id is provided
return getLongChannelIdFromShortChannelId(channelName, channelId);
} else {
return getLongChannelIdFromChannelName(channelName); // no channelId provided
return getLongChannelIdFromChannelName(channelName); // if no channel id provided
}
};

View file

@ -11,6 +11,7 @@ const CLAIM_ID_CHAR = ':';
const CHANNEL_CHAR = '@';
const CLAIMS_PER_PAGE = 10;
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
function isValidClaimId (claimId) {
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
@ -88,7 +89,7 @@ module.exports = (app) => {
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
let identifier = params.identifier;
let name = params.name;
let claimType;
let claimOrChannel;
let channelName = null;
let claimId = null;
let channelId = null;
@ -125,7 +126,7 @@ module.exports = (app) => {
// parse identifier for whether it is a channel, short url, or claim_id
if (identifier.charAt(0) === '@') {
channelName = identifier;
claimType = CHANNEL;
claimOrChannel = CHANNEL;
const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1);
@ -135,18 +136,20 @@ module.exports = (app) => {
} else {
claimId = identifier;
logger.debug('claim id =', claimId);
claimType = CLAIM;
claimOrChannel = CLAIM;
}
// 1. retrieve the asset and information
getAsset(claimType, channelName, channelId, name, claimId)
getAsset(claimOrChannel, channelName, channelId, name, claimId)
// 2. serve or show
.then(fileInfo => {
logger.debug('file info found:', fileInfo);
if (!fileInfo) {
res.status(200).render('noClaims');
} else {
return serveOrShowAsset(fileInfo, fileExtension, method, headers, originalUrl, ip, res);
.then(result => {
if (result === NO_CLAIM) {
res.status(200).json({success: true, message: 'no matching claims were found'}); // res.status(200).render('noClaims');
return;
} else if (result === NO_CHANNEL) {
res.status(200).json({success: true, message: 'no matching channel was found'});
return;
}
return serveOrShowAsset(result, fileExtension, method, headers, originalUrl, ip, res);
})
// 3. update the file
.then(fileInfoForUpdate => {
@ -180,7 +183,7 @@ module.exports = (app) => {
// 2. respond to the request
.then(result => {
if (result === NO_CHANNEL) { // no channel found
res.status(200).json('no channel found');
res.status(200).json({ success: true, message: 'no matching channel found' });
} else if (!result.claims) { // channel found, but no claims
res.status(200).render('channel', {
channelName : result.channelName,