added Claim table with modelwrapper for special functions

This commit is contained in:
bill bittner 2017-08-10 09:47:59 -07:00
parent 965ae1c360
commit dcaa561e8f
5 changed files with 242 additions and 4 deletions

View file

@ -4,6 +4,7 @@ const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreeClaim = require('../helpers/functions/isFreeClaim.js');
const serveHelpers = require('../helpers/serveHelpers.js');
const { createClaimEntryFromLbryResolve } = require('../helpers/claimModelWrapper.js');
function checkForLocalAssetByClaimId (claimId, name) {
return new Promise((resolve, reject) => {
@ -39,7 +40,7 @@ function getAssetByClaimId (fullClaimId, name) {
// 1. check locally for claim
checkForLocalAssetByClaimId(fullClaimId, name)
.then(dataValues => {
// 2. if a result was found, resolve the result
// 2. if a result was found, return the result
if (dataValues) {
logger.debug('found a local file for this claimId');
resolve(dataValues);
@ -49,15 +50,19 @@ function getAssetByClaimId (fullClaimId, name) {
// 3. resolve the claim
lbryApi.resolveUri(`${name}#${fullClaimId}`)
.then(resolveResult => {
// if the claim is free and public, then get it
// if the claim is free...
if (resolveResult.claim && isFreeClaim(resolveResult.claim)) {
// get the claim
lbryApi.getClaim(`${name}#${fullClaimId}`)
.then(getResult => {
// logger.debug('getResult >>', getResult);
let fileInfo = formatGetResultsToFileInfo(getResult);
fileInfo['address'] = resolveResult.claim.address;
fileInfo['height'] = resolveResult.claim.height;
// insert a record in the File table
db.File.create(fileInfo);
// insert a record in the Claim table
createClaimEntryFromLbryResolve(resolveResult.claim);
// resolve the promise
resolve(fileInfo);
})

View file

@ -0,0 +1,97 @@
const logger = require('winston');
const db = require('../models');
module.exports = {
getLocalClaimsList (name) {
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) {
console.log('there was a result');
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;
});
},
createClaimEntryFromLbryResolve (claim) {
logger.debug('db.Claim >> creating claim entry from lbry resolve');
// 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
const searchCriteria = { name: claimData.name, claimId: claimData.claimId };
// create entry in db
db.upsert(db.Claim, claimData, searchCriteria)
.then(() => {
logger.debug('successfully added data to db.Claim');
})
.catch(error => {
logger.error('Sequelize findOne error', error);
});
},
};

136
models/claim.js Normal file
View file

@ -0,0 +1,136 @@
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, ARRAY, DECIMAL, DOUBLE }) => {
const Claim = sequelize.define(
'Claim',
{
address: {
type : STRING,
allowNull: false,
},
amount: {
type : STRING,
allowNull: false,
},
claimId: {
type : STRING,
allowNull: false,
},
claimSequence: {
type : INTEGER,
allowNull: false,
},
decodedClaim: {
type : BOOLEAN,
allowNull: false,
},
depth: {
type : INTEGER,
allowNull: false,
},
effectiveAmount: {
type : STRING,
allowNull: false,
},
hasSignature: {
type : BOOLEAN,
default: false,
},
height: {
type : STRING,
default: '0',
},
hex: {
type : TEXT('long'),
allowNull: false,
},
name: {
type : STRING,
allowNull: false,
},
nout: {
type : INTEGER,
allowNull: false,
},
txid: {
type : STRING,
allowNull: false,
},
validAtHeight: {
type : STRING,
default: null,
},
outpoint: {
type : STRING,
allowNull: false,
},
claimType: {
type : STRING,
allowNull: false,
},
author: {
type : STRING,
default: null,
},
description: {
type : STRING,
default: null,
},
language: {
type : STRING,
default: null,
},
licenseUrl: {
type : STRING,
default: null,
},
nsfw: {
type : BOOLEAN,
default: null,
},
preview: {
type : STRING,
default: null,
},
thumbnail: {
type : STRING,
default: null,
},
title: {
type : STRING,
default: null,
},
metadataVersion: {
type : STRING,
default: null,
},
contentType: {
type : STRING,
default: null,
},
source: {
type : STRING,
default: null,
},
sourceType: {
type : STRING,
default: null,
},
sourceVersion: {
type : STRING,
default: null,
},
streamVersion: {
type : STRING,
default: null,
},
valueVersion: {
type : STRING,
default: null,
},
},
{
freezeTableName: true,
}
);
return Claim;
};

View file

@ -132,7 +132,7 @@ app.set('view engine', 'handlebars');
// start the server
db.sequelize
.sync() // sync sequelize
.sync({force: true}) // sync sequelize
.then(() => { // get the download directory from the daemon
logger.info('Retrieving daemon download directory');
return getDownloadDirectory();

View file

@ -5,7 +5,7 @@
<p>These are all the free, public assets at that claim. You can publish more at <a href="/">spee.ch</a>.</p>
{{#each claims}}
<div class="all-claims-item">
<img class="all-claims-img" src="/{{this.claim_id}}/{{this.name}}/" />
<img class="all-claims-img" src="/{{this.claim_id}}/{{this.name}}.test" />
<div class="all-claims-details">
<ul style="list-style-type:none">
<li>claim: {{this.name}}</li>