2017-08-10 18:47:59 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
const db = require('../models');
|
|
|
|
|
|
|
|
module.exports = {
|
2017-08-15 22:48:42 +02:00
|
|
|
getLocalClaimList (name) {
|
2017-08-10 18:47:59 +02:00
|
|
|
logger.debug(`db.Claim >> Getting claim_list for "${name}"`);
|
|
|
|
return db.Claim.findAll({ name })
|
|
|
|
.then(result => {
|
|
|
|
logger.debug('db.claim result length', result.length);
|
|
|
|
if (result.length >= 1) {
|
|
|
|
result = result.map(claim => {
|
|
|
|
return claim.dataValues;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
resolveLocalUri (name, claimId) {
|
|
|
|
logger.debug(`db.Claim >> Resolving "${name}#${claimId}"`);
|
|
|
|
db.Claim.findAll({ name, claimId })
|
|
|
|
.then(result => {
|
|
|
|
if (result) {
|
|
|
|
if (result.length > 1) {
|
|
|
|
result = result.sort((a, b) => {
|
|
|
|
return (a.dataValues.height < b.dataValues.height);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result[0].dataValues;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
},
|
2017-08-15 22:48:42 +02:00
|
|
|
updateLocalClaimRecordFromResolve (claim) {
|
|
|
|
logger.debug('db.Claim >> creating claim entry from lbry resolve results');
|
2017-08-10 18:47:59 +02:00
|
|
|
// parse the resolved data
|
|
|
|
let claimData = {};
|
|
|
|
claimData['address'] = claim.address;
|
|
|
|
claimData['amount'] = claim.amount;
|
|
|
|
claimData['claimId'] = claim.claim_id;
|
|
|
|
claimData['claimSequence'] = claim.claim_sequence;
|
|
|
|
claimData['decodedClaim'] = claim.decoded_claim;
|
|
|
|
claimData['depth'] = claim.depth;
|
|
|
|
claimData['effectiveAmount'] = claim.effective_amount;
|
|
|
|
claimData['hasSignature'] = claim.has_signature;
|
|
|
|
claimData['height'] = claim.height;
|
|
|
|
claimData['hex'] = claim.hex;
|
|
|
|
claimData['name'] = claim.name;
|
|
|
|
claimData['nout'] = claim.nout;
|
|
|
|
claimData['txid'] = claim.txid;
|
|
|
|
claimData['validAtHeight'] = claim.valid_at_height;
|
|
|
|
claimData['outpoint'] = `${claim.txid}:${claim.nout}`;
|
|
|
|
if (claim.value) {
|
|
|
|
claimData['claimType'] = claim.value.claimType;
|
|
|
|
if (claim.value.stream) {
|
|
|
|
if (claim.value.stream.metadata) {
|
|
|
|
claimData['author'] = claim.value.stream.metadata.author;
|
|
|
|
claimData['description'] = claim.value.stream.metadata.description;
|
|
|
|
claimData['language'] = claim.value.stream.metadata.language;
|
|
|
|
claimData['licenseUrl'] = claim.value.stream.metadata.licenseUrl;
|
|
|
|
claimData['nsfw'] = claim.value.stream.metadata.nsfw;
|
|
|
|
claimData['preview'] = claim.value.stream.metadata.preview;
|
|
|
|
claimData['thumbnail'] = claim.value.stream.metadata.thumbnail;
|
|
|
|
claimData['title'] = claim.value.stream.metadata.title;
|
|
|
|
claimData['metadataVersion'] = claim.value.stream.metadata.version;
|
|
|
|
}
|
|
|
|
if (claim.value.stream.source) {
|
|
|
|
claimData['contentType'] = claim.value.stream.source.contentType;
|
|
|
|
claimData['source'] = claim.value.stream.source.source;
|
|
|
|
claimData['sourceType'] = claim.value.stream.source.sourceType;
|
|
|
|
claimData['sourceVersion'] = claim.value.stream.source.version;
|
|
|
|
}
|
|
|
|
claimData['streamVersion'] = claim.value.stream.version;
|
|
|
|
}
|
|
|
|
claimData['valueVersion'] = claim.value.version;
|
|
|
|
}
|
|
|
|
// create search criteria
|
2017-08-15 22:48:42 +02:00
|
|
|
const updateCriteria = { name: claimData.name, claimId: claimData.claimId };
|
2017-08-10 18:47:59 +02:00
|
|
|
// create entry in db
|
2017-08-15 22:48:42 +02:00
|
|
|
db.Claim.update(claimData, updateCriteria)
|
|
|
|
.then((rows, result) => {
|
|
|
|
logger.debug('successfully updated record in db.Claim');
|
2017-08-10 18:47:59 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-08-15 22:48:42 +02:00
|
|
|
logger.error('db.Claim.update error', error);
|
2017-08-10 18:47:59 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|