Short urls 2 #93
1 changed files with 21 additions and 17 deletions
|
@ -2,6 +2,22 @@ const logger = require('winston');
|
||||||
// const db = require('../../models');
|
// const db = require('../../models');
|
||||||
const { getClaimsList } = require('./lbryApi');
|
const { getClaimsList } = require('./lbryApi');
|
||||||
|
|
||||||
|
function determineShortUrl (claimId, claimList) {
|
||||||
|
let shortUrl = claimId.substring(0, 1);
|
||||||
|
let i = 1;
|
||||||
|
claimList = claimList.filter(claim => { // filter out this exact claim id
|
||||||
|
return claim.claim_id !== claimId;
|
||||||
|
});
|
||||||
|
while (claimList.length !== 0) { // filter out matching claims until none or left
|
||||||
|
shortUrl = claimId.substring(0, i);
|
||||||
|
claimList = claimList.filter(claim => {
|
||||||
|
return (claim.claim_id.substring(0, i) === shortUrl);
|
||||||
|
});
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (shortUrl);
|
||||||
|
}
|
||||||
|
|
||||||
function getClaimIdByShortUrl (name, shortUrl) {
|
function getClaimIdByShortUrl (name, shortUrl) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
getClaimsList(name)
|
getClaimsList(name)
|
||||||
|
@ -40,18 +56,7 @@ function getShortUrlByClaimId (name, claimId) {
|
||||||
getClaimsList(name)
|
getClaimsList(name)
|
||||||
// find the smallest possible unique url for this claim
|
// find the smallest possible unique url for this claim
|
||||||
.then(({ claims }) => {
|
.then(({ claims }) => {
|
||||||
let shortUrl = claimId.substring(0, 1);
|
const shortUrl = determineShortUrl(claimId, claims);
|
||||||
let i = 1;
|
|
||||||
claims = claims.filter(claim => { // filter out this exact claim id
|
|
||||||
return claim.claim_id !== claimId;
|
|
||||||
});
|
|
||||||
while (claims.length !== 0) { // filter out matching claims until none or left
|
|
||||||
shortUrl = claimId.substring(0, i);
|
|
||||||
claims = claims.filter(claim => {
|
|
||||||
return (claim.claim_id.substring(0, i) === shortUrl);
|
|
||||||
});
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
resolve(shortUrl);
|
resolve(shortUrl);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -110,11 +115,7 @@ module.exports = {
|
||||||
getClaimIdByShortUrl(name, url)
|
getClaimIdByShortUrl(name, url)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
claimId = result;
|
claimId = result;
|
||||||
return getShortUrlByClaimId(name, claimId);
|
resolve({claimId, shortUrl: url});
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
shortUrl = result;
|
|
||||||
resolve({claimId, shortUrl});
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -125,4 +126,7 @@ module.exports = {
|
||||||
});
|
});
|
||||||
return deferred;
|
return deferred;
|
||||||
},
|
},
|
||||||
|
determineShortUrl (claimId, claimList) {
|
||||||
|
return determineShortUrl(claimId, claimList);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue