Sequelize queries #234
|
@ -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');
|
||||
I think Certificate is accurate because it is actually storing the certificate claims from the blockchain. We have a Channel table for the Channels that are created on spee.ch. It parallels how we have a Claim table for all the stream claims in the blockchain (i guess this table should be Streams to be fully parallel) and a File table for all the files held locally by spee.ch. I think Certificate is accurate because it is actually storing the certificate claims from the blockchain. We have a Channel table for the Channels that are created on spee.ch. It parallels how we have a Claim table for all the stream claims in the blockchain (i guess this table should be Streams to be fully parallel) and a File table for all the files held locally by spee.ch.
@billbitt got it @billbitt got it
|
||||
|
||||
function sortResult (result, longId) {
|
||||
let claimIndex;
|
||||
let shortId = longId.substring(0, 1); // default sort id is the first letter
|
||||
This function is called This function is called `sortResult` but it appears to return a short Id.
|
||||
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
|
||||
This function seems to contain code that could be shared with the other (This function has same naming problem as well.) This function seems to contain code that could be shared with the other `sortResult`.
(This function has same naming problem as well.)
|
||||
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) => {
|
||||
I'm guessing this code isn't actually missing a colon or it wouldn't work, but what's the difference here? I'm guessing this code isn't actually missing a colon or it wouldn't work, but what's the difference here?
the line break was accidental; difference was just adding Claim because getShortClaimId... is now a method of Claim the line break was accidental; difference was just adding Claim because getShortClaimId... is now a method of Claim
|
||||
// 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);
|
||||
|
|
Would it make sense for this to be
Channel
rather thanCertificate
?