moved getShortChannelId and getShortClaimId to class methods

This commit is contained in:
bill bittner 2017-10-31 10:05:15 -07:00
parent 829ac91985
commit d6be5c4dbe
8 changed files with 105 additions and 70 deletions

View file

@ -114,7 +114,7 @@ module.exports = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db.getLongClaimId(claimName, claimId) // 1. get the long claim id db.getLongClaimId(claimName, claimId) // 1. get the long claim id
.then(result => { // 2. get the asset using the long claim id .then(result => { // 2. get the asset using the long claim id
logger.debug('getLongClaimId result:', result); logger.debug('long claim id ===', result);
if (result === NO_CLAIM) { if (result === NO_CLAIM) {
logger.debug('resolving NO_CLAIM'); logger.debug('resolving NO_CLAIM');
resolve(NO_CLAIM); resolve(NO_CLAIM);
@ -161,7 +161,7 @@ module.exports = {
return NO_CHANNEL; return NO_CHANNEL;
} }
longChannelId = result; longChannelId = result;
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName); return db.Certificate.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) { if (result === NO_CHANNEL) {
@ -216,7 +216,7 @@ module.exports = {
showFileLite(fileInfo, res); showFileLite(fileInfo, res);
return fileInfo; return fileInfo;
case SHOW: case SHOW:
return db return db.Claim
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name) .getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
.then(shortId => { .then(shortId => {
fileInfo['shortId'] = shortId; fileInfo['shortId'] = shortId;

View file

@ -28,7 +28,7 @@ module.exports = {
.then(channel => { .then(channel => {
userInfo['channelName'] = channel.channelName; userInfo['channelName'] = channel.channelName;
userInfo['channelClaimId'] = channel.channelClaimId; userInfo['channelClaimId'] = channel.channelClaimId;
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
}) })
.then(shortChannelId => { .then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId; userInfo['shortChannelId'] = shortChannelId;

View file

@ -1,3 +1,29 @@
const logger = require('winston');
function sortResult (result, longId) {
let claimIndex;
let shortId = longId.substring(0, 1); // default sort id is the first letter
let shortIdLength = 0;
// find the index of this certificate
claimIndex = result.findIndex(element => {
return element.claimId === longId;
});
if (claimIndex < 0) { throw new Error('channelId not found in possible sorted list') }
// get an array of all certificates with lower height
let possibleMatches = result.slice(0, claimIndex);
// remove certificates with the same prefixes until none are left.
while (possibleMatches.length > 0) {
shortIdLength += 1;
shortId = longId.substring(0, shortIdLength);
possibleMatches = possibleMatches.filter(element => {
return (element.claimId.substring(0, shortIdLength) === shortId);
});
}
// return the short Id
logger.debug('short channel id ===', shortId);
return shortId;
}
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => {
const Certificate = sequelize.define( const Certificate = sequelize.define(
'Certificate', 'Certificate',
@ -97,5 +123,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
}); });
}; };
Certificate.getShortChannelIdFromLongChannelId = function (longChannelId, channelName) {
logger.debug(`finding short channel id for ${channelName}:${longChannelId}`);
return new Promise((resolve, reject) => {
this
.findAll({
where: {name: channelName},
order: [['height', 'ASC']],
})
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid channel name');
default:
return resolve(sortResult(result, longChannelId));
}
})
.catch(error => {
reject(error);
});
});
};
return Certificate; return Certificate;
}; };

View file

@ -1,3 +1,29 @@
const logger = require('winston');
function sortResult (result, longId) {
let claimIndex;
let shortId = longId.substring(0, 1); // default sort id is the first letter
let shortIdLength = 0;
// find the index of this certificate
claimIndex = result.findIndex(element => {
return element.claimId === longId;
});
if (claimIndex < 0) { throw new Error('claimid not found in possible sorted list') }
// get an array of all certificates with lower height
let possibleMatches = result.slice(0, claimIndex);
// remove certificates with the same prefixes until none are left.
while (possibleMatches.length > 0) {
shortIdLength += 1;
shortId = longId.substring(0, shortIdLength);
possibleMatches = possibleMatches.filter(element => {
return (element.claimId.substring(0, shortIdLength) === shortId);
});
}
// return the short Id
logger.debug('short claim id ===', shortId);
return shortId;
}
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => { module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => {
const Claim = sequelize.define( const Claim = sequelize.define(
'Claim', 'Claim',
@ -154,5 +180,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, D
}); });
}; };
Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) {
return new Promise((resolve, reject) => {
logger.debug(`finding short claim id for ${claimId}#${claimId}`);
this
.findAll({
where: {name: claimName},
order: [['height', 'ASC']],
})
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid claim name');
default:
resolve(sortResult(result, claimId));
}
})
.catch(error => {
reject(error);
});
});
};
return Claim; return Claim;
}; };

View file

@ -24,30 +24,6 @@ const sequelize = new Sequelize(database, username, password, {
}, },
}); });
function sortResult (result, longId) {
let claimIndex;
let shortId = longId.substring(0, 1); // default sort id is the first letter
let shortIdLength = 0;
// find the index of this certificate
claimIndex = result.findIndex(element => {
return element.claimId === longId;
});
if (claimIndex < 0) { throw new Error('claimid not found in possible sorted list') }
// get an array of all certificates with lower height
let possibleMatches = result.slice(0, claimIndex);
// remove certificates with the same prefixes until none are left.
while (possibleMatches.length > 0) {
shortIdLength += 1;
shortId = longId.substring(0, shortIdLength);
possibleMatches = possibleMatches.filter(element => {
return (element.claimId.substring(0, shortIdLength) === shortId);
});
}
// return the short Id
logger.debug('short channel id ===', shortId);
return shortId;
}
function getLongClaimIdFromShortClaimId (name, shortId) { function getLongClaimIdFromShortClaimId (name, shortId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db db
@ -176,44 +152,6 @@ db['getRecentClaims'] = () => {
return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT }); return db.sequelize.query(`SELECT * FROM File WHERE nsfw != 1 AND trendingEligible = 1 ORDER BY createdAt DESC LIMIT 25;`, { type: db.sequelize.QueryTypes.SELECT });
}; };
db['getShortClaimIdFromLongClaimId'] = (claimId, claimName) => {
return new Promise((resolve, reject) => {
logger.debug('finding short channel id');
db
.sequelize.query(`SELECT claimId, height FROM Claim WHERE name = '${claimName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid claim name');
default:
return resolve(sortResult(result, claimId));
}
})
.catch(error => {
reject(error);
});
});
};
db['getShortChannelIdFromLongChannelId'] = (longChannelId, channelName) => {
return new Promise((resolve, reject) => {
logger.debug(`finding short channel id for ${longChannelId} ${channelName}`);
db
.sequelize.query(`SELECT claimId, height FROM Certificate WHERE name = '${channelName}' ORDER BY height;`, { type: db.sequelize.QueryTypes.SELECT })
.then(result => {
switch (result.length) {
case 0:
throw new Error('That is an invalid channel name');
default:
return resolve(sortResult(result, longChannelId));
}
})
.catch(error => {
reject(error);
});
});
};
db['getAllFreeClaims'] = (name) => { db['getAllFreeClaims'] = (name) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
db db

View file

@ -40,7 +40,7 @@ module.exports = new PassportLocalStrategy(
.then(channel => { .then(channel => {
userInfo['channelName'] = channel.channelName; userInfo['channelName'] = channel.channelName;
userInfo['channelClaimId'] = channel.channelClaimId; userInfo['channelClaimId'] = channel.channelClaimId;
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName); return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
}) })
.then(shortChannelId => { .then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId; userInfo['shortChannelId'] = shortChannelId;

View file

@ -54,7 +54,7 @@ module.exports = new PassportLocalStrategy(
}) })
.then(() => { .then(() => {
logger.verbose('user and certificate successfully associated'); logger.verbose('user and certificate successfully associated');
return db.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName); return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
}) })
.then(shortChannelId => { .then(shortChannelId => {
userInfo['shortChannelId'] = shortChannelId; userInfo['shortChannelId'] = shortChannelId;

View file

@ -147,7 +147,8 @@ module.exports = (app) => {
// route to get a short claim id from long claim Id // route to get a short claim id from long claim Id
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => { app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
// serve content // serve content
db.getShortClaimIdFromLongClaimId(params.longId, params.name) db.Claim
.getShortClaimIdFromLongClaimId(params.longId, params.name)
.then(shortId => { .then(shortId => {
res.status(200).json(shortId); res.status(200).json(shortId);
}) })
@ -159,7 +160,7 @@ module.exports = (app) => {
// route to get a short channel id from long channel Id // route to get a short channel id from long channel Id
app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => { app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => {
// serve content // serve content
db.getShortChannelIdFromLongChannelId(params.longId, params.name) db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => { .then(shortId => {
logger.debug('sending back short channel id', shortId); logger.debug('sending back short channel id', shortId);
res.status(200).json(shortId); res.status(200).json(shortId);