Allow external channels to load via chainquery

This commit is contained in:
Shawn 2018-10-30 15:53:29 -05:00
parent a4b02b9f4b
commit 86a693b4f4
3 changed files with 39 additions and 37 deletions

View file

@ -1,28 +1,25 @@
const db = require('../../../../models');
const db = require('server/models');
const chainquery = require('chainquery');
const getChannelData = (channelName, channelClaimId) => {
return new Promise((resolve, reject) => {
let longChannelClaimId;
// 1. get the long channel Id (make sure channel exists)
db.Certificate
.getLongChannelId(channelName, channelClaimId)
.then(fullClaimId => {
longChannelClaimId = fullClaimId;
return db
.Certificate
.getShortChannelIdFromLongChannelId(fullClaimId, channelName);
})
.then(shortChannelClaimId => {
resolve({
channelName,
longChannelClaimId,
shortChannelClaimId,
});
})
.catch(error => {
reject(error);
});
});
const getChannelData = async (channelName, channelClaimId) => {
let longChannelClaimId = await chainquery.claim.queries.getLongClaimId(channelName, channelClaimId).catch(() => false);
if(!longChannelClaimId) {
// Allow an error to throw here if this fails
longChannelClaimId = await db.Certificate.getLongChannelId(channelName, channelClaimId);
}
let shortChannelClaimId = await chainquery.claim.queries.getShortClaimIdFromLongClaimId(longChannelClaimId, channelName).catch(() => false);
if(!shortChannelClaimId) {
shortChannelClaimId = await db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName);
}
return {
channelName,
longChannelClaimId,
shortChannelClaimId,
};
};
module.exports = getChannelData;

View file

@ -1,5 +1,6 @@
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const db = require('../../../../models');
const { handleErrorResponse } = require('server/controllers/utils/errorHandlers.js');
const db = require('server/models');
const chainquery = require('chainquery');
/*
@ -7,14 +8,18 @@ route to get a short channel id from long channel Id
*/
const channelShortIdRoute = ({ ip, originalUrl, params }, res) => {
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
res.status(200).json(shortId);
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
const channelShortIdRoute = async ({ ip, originalUrl, params }, res) => {
try {
let shortId = await chainquery.claim.queries.getShortClaimIdFromLongClaimId(params.longId, params.name).catch(() => false);
if(!shortId) {
shortId = await db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name);
}
res.status(200).json(shortId);
} catch (error) {
handleErrorResponse(originalUrl, ip, error, res);
}
};
module.exports = channelShortIdRoute;

View file

@ -1,9 +1,9 @@
const db = require('../../../../models');
const db = require('server/models');
const chainquery = require('chainquery');
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const { handleErrorResponse } = require('server/controllers/utils/errorHandlers.js');
const getClaimId = require('../../../utils/getClaimId.js');
const getClaimId = require('server/controllers/utils/getClaimId.js');
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';