moved getShortChannelId and getShortClaimId to class methods
This commit is contained in:
parent
829ac91985
commit
d6be5c4dbe
8 changed files with 105 additions and 70 deletions
|
@ -114,7 +114,7 @@ module.exports = {
|
|||
return new Promise((resolve, reject) => {
|
||||
db.getLongClaimId(claimName, claimId) // 1. get 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) {
|
||||
logger.debug('resolving NO_CLAIM');
|
||||
resolve(NO_CLAIM);
|
||||
|
@ -161,7 +161,7 @@ module.exports = {
|
|||
return NO_CHANNEL;
|
||||
}
|
||||
longChannelId = result;
|
||||
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
||||
})
|
||||
.then(result => { // 3. get all Claim records for this channel
|
||||
if (result === NO_CHANNEL) {
|
||||
|
@ -216,7 +216,7 @@ module.exports = {
|
|||
showFileLite(fileInfo, res);
|
||||
return fileInfo;
|
||||
case SHOW:
|
||||
return db
|
||||
return db.Claim
|
||||
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
||||
.then(shortId => {
|
||||
fileInfo['shortId'] = shortId;
|
||||
|
|
|
@ -28,7 +28,7 @@ module.exports = {
|
|||
.then(channel => {
|
||||
userInfo['channelName'] = channel.channelName;
|
||||
userInfo['channelClaimId'] = channel.channelClaimId;
|
||||
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
|
|
|
@ -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 }) => {
|
||||
const Certificate = sequelize.define(
|
||||
'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;
|
||||
};
|
||||
|
|
|
@ -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 }) => {
|
||||
const Claim = sequelize.define(
|
||||
'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;
|
||||
};
|
||||
|
|
|
@ -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) {
|
||||
return new Promise((resolve, reject) => {
|
||||
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 });
|
||||
};
|
||||
|
||||
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) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
db
|
||||
|
|
|
@ -40,7 +40,7 @@ module.exports = new PassportLocalStrategy(
|
|||
.then(channel => {
|
||||
userInfo['channelName'] = channel.channelName;
|
||||
userInfo['channelClaimId'] = channel.channelClaimId;
|
||||
return db.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(channel.channelClaimId, channel.channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
|
|
|
@ -54,7 +54,7 @@ module.exports = new PassportLocalStrategy(
|
|||
})
|
||||
.then(() => {
|
||||
logger.verbose('user and certificate successfully associated');
|
||||
return db.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
|
||||
return db.Certificate.getShortChannelIdFromLongChannelId(userInfo.channelClaimId, userInfo.channelName);
|
||||
})
|
||||
.then(shortChannelId => {
|
||||
userInfo['shortChannelId'] = shortChannelId;
|
||||
|
|
|
@ -147,7 +147,8 @@ module.exports = (app) => {
|
|||
// route to get a short claim id from long claim Id
|
||||
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
|
||||
// serve content
|
||||
db.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
||||
db.Claim
|
||||
.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
||||
.then(shortId => {
|
||||
res.status(200).json(shortId);
|
||||
})
|
||||
|
@ -159,7 +160,7 @@ module.exports = (app) => {
|
|||
// route to get a short channel id from long channel Id
|
||||
app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => {
|
||||
// serve content
|
||||
db.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
||||
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
||||
.then(shortId => {
|
||||
logger.debug('sending back short channel id', shortId);
|
||||
res.status(200).json(shortId);
|
||||
|
|
Loading…
Reference in a new issue