Block used claims #68
9 changed files with 73 additions and 22 deletions
|
@ -20,19 +20,20 @@ function upsert (Model, values, condition) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publish: (publishParams, fileName, fileType) => {
|
publish (publishParams, fileName, fileType) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
// 1. publish the file
|
// 1. publish the file
|
||||||
lbryApi
|
lbryApi
|
||||||
.publishClaim(publishParams)
|
.publishClaim(publishParams)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
logger.info(`Successfully published ${fileName}`, result);
|
logger.info(`Successfully published ${fileName}`, result);
|
||||||
// 2. update old record of create new one (update is in case the claim has been published before by this daemon)
|
// 2. update old record or create new one (update is in case the claim has been published before by this daemon)
|
||||||
upsert(
|
upsert(
|
||||||
db.File,
|
db.File,
|
||||||
{
|
{
|
||||||
name : publishParams.name,
|
name : publishParams.name,
|
||||||
claimId : result.claim_id,
|
claimId : result.claim_id,
|
||||||
|
address : publishParams.claim_address,
|
||||||
outpoint: `${result.txid}:${result.nout}`,
|
outpoint: `${result.txid}:${result.nout}`,
|
||||||
height : 0,
|
height : 0,
|
||||||
fileName,
|
fileName,
|
||||||
|
@ -63,13 +64,32 @@ module.exports = {
|
||||||
});
|
});
|
||||||
return deferred;
|
return deferred;
|
||||||
},
|
},
|
||||||
checkNameAvailability: (name) => {
|
checkNameAvailability (name) {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
// find any records where the name is used
|
// find any records where the name is used
|
||||||
db.File
|
db.File
|
||||||
.findAll({ where: { name } })
|
.findAll({ where: { name } })
|
||||||
.then(result => {
|
.then(result => {
|
||||||
resolve(result);
|
if (result.length >= 1) {
|
||||||
|
// filter out any results that were not published from a spee.ch wallet address
|
||||||
|
lbryApi
|
||||||
|
.getWalletList()
|
||||||
|
.then((walletList) => {
|
||||||
|
const filteredResult = result.filter((claim) => {
|
||||||
|
return walletList.includes(claim.address);
|
||||||
|
});
|
||||||
|
if (filteredResult.length >= 1) {
|
||||||
|
resolve(false);
|
||||||
|
} else {
|
||||||
|
resolve(true);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
resolve(true);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject(error);
|
reject(error);
|
||||||
|
|
|
@ -18,6 +18,7 @@ function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight
|
||||||
// logger.debug('resolved result:', result);
|
// logger.debug('resolved result:', result);
|
||||||
const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`;
|
const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`;
|
||||||
const resolvedHeight = result.claim.height;
|
const resolvedHeight = result.claim.height;
|
||||||
|
const resolvedAddress = result.claim.address;
|
||||||
logger.debug('database outpoint:', localOutpoint);
|
logger.debug('database outpoint:', localOutpoint);
|
||||||
logger.debug('resolved outpoint:', resolvedOutpoint);
|
logger.debug('resolved outpoint:', resolvedOutpoint);
|
||||||
// 2. if the outpoint's match, no further work needed
|
// 2. if the outpoint's match, no further work needed
|
||||||
|
@ -29,7 +30,7 @@ function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight
|
||||||
// 2. get the resolved claim
|
// 2. get the resolved claim
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
||||||
getClaimAndUpdate(uri, resolvedHeight);
|
getClaimAndUpdate(uri, resolvedAddress, resolvedHeight);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -37,7 +38,7 @@ function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClaimAndUpdate (uri, height) {
|
function getClaimAndUpdate (uri, address, height) {
|
||||||
// 1. get the claim
|
// 1. get the claim
|
||||||
lbryApi
|
lbryApi
|
||||||
.getClaim(uri)
|
.getClaim(uri)
|
||||||
|
@ -47,7 +48,8 @@ function getClaimAndUpdate (uri, height) {
|
||||||
db.File
|
db.File
|
||||||
.update({
|
.update({
|
||||||
outpoint,
|
outpoint,
|
||||||
height, // note: height is coming from 'resolve', not 'get'.
|
height, // note: height is coming from the 'resolve', not 'get'.
|
||||||
|
address, // note: address is coming from the 'resolve', not 'get'.
|
||||||
fileName: file_name,
|
fileName: file_name,
|
||||||
filePath: download_path,
|
filePath: download_path,
|
||||||
fileType: mime_type,
|
fileType: mime_type,
|
||||||
|
@ -70,7 +72,7 @@ function getClaimAndUpdate (uri, height) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClaimAndHandleResponse (uri, height, resolve, reject) {
|
function getClaimAndHandleResponse (uri, address, height, resolve, reject) {
|
||||||
lbryApi
|
lbryApi
|
||||||
.getClaim(uri)
|
.getClaim(uri)
|
||||||
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
||||||
|
@ -80,8 +82,9 @@ function getClaimAndHandleResponse (uri, height, resolve, reject) {
|
||||||
.create({
|
.create({
|
||||||
name,
|
name,
|
||||||
claimId : claim_id,
|
claimId : claim_id,
|
||||||
|
address, // note: comes from parent 'resolve,' not this 'get' call
|
||||||
outpoint,
|
outpoint,
|
||||||
height,
|
height, // note: comes from parent 'resolve,' not this 'get' call
|
||||||
fileName: file_name,
|
fileName: file_name,
|
||||||
filePath: download_path,
|
filePath: download_path,
|
||||||
fileType: mime_type,
|
fileType: mime_type,
|
||||||
|
@ -120,6 +123,7 @@ module.exports = {
|
||||||
const claimId = freePublicClaimList[0].claim_id;
|
const claimId = freePublicClaimList[0].claim_id;
|
||||||
const uri = `${name}#${claimId}`;
|
const uri = `${name}#${claimId}`;
|
||||||
const height = freePublicClaimList[0].height;
|
const height = freePublicClaimList[0].height;
|
||||||
|
const address = freePublicClaimList[0].address;
|
||||||
// 2. check to see if the file is available locally
|
// 2. check to see if the file is available locally
|
||||||
db.File
|
db.File
|
||||||
.findOne({ where: { name, claimId } })
|
.findOne({ where: { name, claimId } })
|
||||||
|
@ -133,7 +137,7 @@ module.exports = {
|
||||||
// 3. otherwise use daemon to retrieve it
|
// 3. otherwise use daemon to retrieve it
|
||||||
} else {
|
} else {
|
||||||
// get the claim and serve it
|
// get the claim and serve it
|
||||||
getClaimAndHandleResponse(uri, height, resolve, reject);
|
getClaimAndHandleResponse(uri, address, height, resolve, reject);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
@ -175,7 +179,7 @@ module.exports = {
|
||||||
// 4. check to see if the claim is free & public
|
// 4. check to see if the claim is free & public
|
||||||
if (isFreePublicClaim(result.claim)) {
|
if (isFreePublicClaim(result.claim)) {
|
||||||
// 5. get claim and serve
|
// 5. get claim and serve
|
||||||
getClaimAndHandleResponse(uri, result.claim.height, resolve, reject);
|
getClaimAndHandleResponse(uri, result.claim.address, result.claim.height, resolve, reject);
|
||||||
} else {
|
} else {
|
||||||
reject(null);
|
reject(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ const db = require('../models');
|
||||||
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
|
const googleApiKey = config.get('AnalyticsConfig.GoogleId');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
postToStats: (action, url, ipAddress, result) => {
|
postToStats (action, url, ipAddress, result) {
|
||||||
logger.silly(`creating ${action} record for statistics db`);
|
logger.silly(`creating ${action} record for statistics db`);
|
||||||
// make sure the result is a string
|
// make sure the result is a string
|
||||||
if (result && (typeof result !== 'string')) {
|
if (result && (typeof result !== 'string')) {
|
||||||
|
@ -27,7 +27,7 @@ module.exports = {
|
||||||
logger.error('sequelize error', error);
|
logger.error('sequelize error', error);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
sendGoogleAnalytics: (action, ip, originalUrl) => {
|
sendGoogleAnalytics (action, ip, originalUrl) {
|
||||||
const visitorId = ip.replace(/\./g, '-');
|
const visitorId = ip.replace(/\./g, '-');
|
||||||
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
const visitor = ua(googleApiKey, visitorId, { strictCidFormat: false, https: true });
|
||||||
switch (action) {
|
switch (action) {
|
||||||
|
@ -55,7 +55,7 @@ module.exports = {
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getStatsSummary: () => {
|
getStatsSummary () {
|
||||||
logger.debug('retrieving site statistics');
|
logger.debug('retrieving site statistics');
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
// get the raw statistics data
|
// get the raw statistics data
|
||||||
|
|
|
@ -28,7 +28,7 @@ function orderClaims (claimsListArray) {
|
||||||
return claimsListArray;
|
return claimsListArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = claimName => {
|
module.exports = (claimName) => {
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
// make a call to the daemon to get the claims list
|
// make a call to the daemon to get the claims list
|
||||||
lbryApi
|
lbryApi
|
||||||
|
|
|
@ -2,6 +2,23 @@ const axios = require('axios');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getWalletList () {
|
||||||
|
logger.debug('getting wallet list');
|
||||||
|
const deferred = new Promise((resolve, reject) => {
|
||||||
|
axios
|
||||||
|
.post('http://localhost:5279/lbryapi', {
|
||||||
|
method: 'wallet_list',
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
const result = response.data.result;
|
||||||
|
resolve(result);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return deferred;
|
||||||
|
},
|
||||||
publishClaim (publishParams) {
|
publishClaim (publishParams) {
|
||||||
logger.debug(`Publishing claim to "${publishParams.name}"`);
|
logger.debug(`Publishing claim to "${publishParams.name}"`);
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
|
|
|
@ -3,7 +3,7 @@ const config = require('config');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createPublishParams: (name, filePath, license, nsfw) => {
|
createPublishParams (name, filePath, license, nsfw) {
|
||||||
logger.debug(`Creating Publish Parameters for "${name}"`);
|
logger.debug(`Creating Publish Parameters for "${name}"`);
|
||||||
// const payAddress = config.get('WalletConfig.LbryPayAddress');
|
// const payAddress = config.get('WalletConfig.LbryPayAddress');
|
||||||
const claimAddress = config.get('WalletConfig.LbryClaimAddress');
|
const claimAddress = config.get('WalletConfig.LbryClaimAddress');
|
||||||
|
@ -40,7 +40,7 @@ module.exports = {
|
||||||
logger.debug('publishParams:', publishParams);
|
logger.debug('publishParams:', publishParams);
|
||||||
return publishParams;
|
return publishParams;
|
||||||
},
|
},
|
||||||
deleteTemporaryFile: (filePath) => {
|
deleteTemporaryFile (filePath) {
|
||||||
fs.unlink(filePath, err => {
|
fs.unlink(filePath, err => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
logger.debug(`successfully deleted ${filePath}`);
|
logger.debug(`successfully deleted ${filePath}`);
|
||||||
|
|
|
@ -10,6 +10,10 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER }) => {
|
||||||
type : STRING,
|
type : STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
},
|
},
|
||||||
|
address: {
|
||||||
|
type : STRING,
|
||||||
|
allowNull: false,
|
||||||
|
},
|
||||||
outpoint: {
|
outpoint: {
|
||||||
type : STRING,
|
type : STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
|
|
@ -118,8 +118,13 @@ document.getElementById('publish-submit').addEventListener('click', function(eve
|
||||||
xhttp.onreadystatechange = function() {
|
xhttp.onreadystatechange = function() {
|
||||||
if (this.readyState == 4 ) {
|
if (this.readyState == 4 ) {
|
||||||
if ( this.status == 200) {
|
if ( this.status == 200) {
|
||||||
console.log(this.response);
|
if (this.response == true) {
|
||||||
|
console.log(true);
|
||||||
//uploader.submitFiles(stagedFiles);
|
//uploader.submitFiles(stagedFiles);
|
||||||
|
} else {
|
||||||
|
alert("That name has already been claimed by spee.ch. Please choose a different name.");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
console.log("request to check claim name failed with status:", this.status);
|
console.log("request to check claim name failed with status:", this.status);
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,10 +33,11 @@ module.exports = app => {
|
||||||
publishController
|
publishController
|
||||||
.checkNameAvailability(params.name)
|
.checkNameAvailability(params.name)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (result.length >= 1) {
|
if (result === true) {
|
||||||
res.status(200).json(false);
|
|
||||||
} else {
|
|
||||||
res.status(200).json(true);
|
res.status(200).json(true);
|
||||||
|
} else {
|
||||||
|
logger.debug(`Rejecting publish request because ${params.name} has already been published via spee.ch`);
|
||||||
|
res.status(200).json(false);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|
Loading…
Reference in a new issue