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

View file

@ -7,6 +7,7 @@ const db = {};
const logger = require('winston'); const logger = require('winston');
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const database = config.get('Database.Database'); const database = config.get('Database.Database');
const username = config.get('Database.Username'); const username = config.get('Database.Username');
@ -54,7 +55,7 @@ function getLongClaimIdFromShortClaimId (name, shortId) {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
throw new Error('That is an invalid Short Claim Id'); return resolve(NO_CLAIM);
default: // note results must be sorted default: // note results must be sorted
return resolve(result[0].claimId); return resolve(result[0].claimId);
} }
@ -258,7 +259,7 @@ db['getClaimIdByLongChannelId'] = (channelId, claimName) => {
.then(result => { .then(result => {
switch (result.length) { switch (result.length) {
case 0: case 0:
throw new Error('There is no such claim for that channel'); return resolve(NO_CLAIM);
default: default:
return resolve(result[0].claimId); return resolve(result[0].claimId);
} }
@ -290,23 +291,23 @@ db['getAllChannelClaims'] = (channelId) => {
db['getLongClaimId'] = (claimName, claimId) => { db['getLongClaimId'] = (claimName, claimId) => {
logger.debug(`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)); return new Promise((resolve, reject) => resolve(claimId));
} else if (claimId && claimId.length < 40) { } else if (claimId && claimId.length < 40) {
return getLongClaimIdFromShortClaimId(claimName, claimId); // need to create this function return getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided
} else { // if no claim id provided } else {
return getTopFreeClaimIdByClaimName(claimName); return getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided
} }
}; };
db['getLongChannelId'] = (channelName, channelId) => { db['getLongChannelId'] = (channelName, channelId) => {
logger.debug(`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)); 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); return getLongChannelIdFromShortChannelId(channelName, channelId);
} else { } 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 CHANNEL_CHAR = '@';
const CLAIMS_PER_PAGE = 10; const CLAIMS_PER_PAGE = 10;
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
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));
@ -88,7 +89,7 @@ module.exports = (app) => {
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
let identifier = params.identifier; let identifier = params.identifier;
let name = params.name; let name = params.name;
let claimType; let claimOrChannel;
let channelName = null; let channelName = null;
let claimId = null; let claimId = null;
let channelId = null; let channelId = null;
@ -125,7 +126,7 @@ module.exports = (app) => {
// 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; channelName = identifier;
claimType = CHANNEL; claimOrChannel = CHANNEL;
const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR); const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
if (channelIdIndex !== -1) { if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1); channelId = channelName.substring(channelIdIndex + 1);
@ -135,18 +136,20 @@ module.exports = (app) => {
} else { } else {
claimId = identifier; claimId = identifier;
logger.debug('claim id =', claimId); logger.debug('claim id =', claimId);
claimType = CLAIM; claimOrChannel = CLAIM;
} }
// 1. retrieve the asset and information // 1. retrieve the asset and information
getAsset(claimType, channelName, channelId, name, claimId) getAsset(claimOrChannel, channelName, channelId, name, claimId)
// 2. serve or show // 2. serve or show
.then(fileInfo => { .then(result => {
logger.debug('file info found:', fileInfo); if (result === NO_CLAIM) {
if (!fileInfo) { res.status(200).json({success: true, message: 'no matching claims were found'}); // res.status(200).render('noClaims');
res.status(200).render('noClaims'); return;
} else { } else if (result === NO_CHANNEL) {
return serveOrShowAsset(fileInfo, fileExtension, method, headers, originalUrl, ip, res); 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 // 3. update the file
.then(fileInfoForUpdate => { .then(fileInfoForUpdate => {
@ -180,7 +183,7 @@ module.exports = (app) => {
// 2. respond to the request // 2. respond to the request
.then(result => { .then(result => {
if (result === NO_CHANNEL) { // no channel found 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 } 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,