diff --git a/server/controllers/api/blocked/index.js b/server/controllers/api/blocked/index.js index d81503c4..aad2dfc2 100644 --- a/server/controllers/api/blocked/index.js +++ b/server/controllers/api/blocked/index.js @@ -1,8 +1,8 @@ const logger = require('winston'); -const refreshBlockedList = require('../../../models/utils/refreshBlockedList.js'); +const db = require('../../../models'); const updateBlockedList = (req, res) => { - refreshBlockedList() + db.Blocked.refreshTable() .then(data => { logger.info('finished updating blocked content list'); res.status(200).json({ diff --git a/server/controllers/api/claim/longId/index.js b/server/controllers/api/claim/longId/index.js index 0eb3668c..27f66b1c 100644 --- a/server/controllers/api/claim/longId/index.js +++ b/server/controllers/api/claim/longId/index.js @@ -22,7 +22,10 @@ const claimLongId = ({ ip, originalUrl, body, params }, res) => { getClaimId(channelName, channelClaimId, claimName, claimId) .then(fullClaimId => { claimId = fullClaimId; - return db.Blocked.isNotBlocked(fullClaimId, claimName); + return db.Claim.getOutpoint(claimName, fullClaimId); + }) + .then(outpoint => { + return db.Blocked.isNotBlocked(outpoint); }) .then(() => { res.status(200).json({success: true, data: claimId}); diff --git a/server/controllers/assets/utils/getClaimIdAndServeAsset.js b/server/controllers/assets/utils/getClaimIdAndServeAsset.js index 83c4c770..6603d046 100644 --- a/server/controllers/assets/utils/getClaimIdAndServeAsset.js +++ b/server/controllers/assets/utils/getClaimIdAndServeAsset.js @@ -17,8 +17,12 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId getClaimId(channelName, channelClaimId, claimName, claimId) .then(fullClaimId => { claimId = fullClaimId; - logger.debug('FULL CLAIM ID:', fullClaimId); - return db.Blocked.isNotBlocked(fullClaimId, claimName); + logger.debug('Full claim id:', fullClaimId); + return db.Claim.getOutpoint(claimName, fullClaimId); + }) + .then(outpoint => { + logger.debug('Outpoint:', outpoint); + return db.Blocked.isNotBlocked(outpoint); }) .then(() => { return getLocalFileRecord(claimId, claimName); diff --git a/server/models/blocked.js b/server/models/blocked.js index 12a04ca9..5e413889 100644 --- a/server/models/blocked.js +++ b/server/models/blocked.js @@ -6,14 +6,6 @@ module.exports = (sequelize, { STRING }) => { const Blocked = sequelize.define( 'Blocked', { - claimId: { - type : STRING, - allowNull: false, - }, - name: { - type : STRING, - allowNull: false, - }, outpoint: { type : STRING, allowNull: false, @@ -24,13 +16,12 @@ module.exports = (sequelize, { STRING }) => { } ); - Blocked.isNotBlocked = function (claimId, name) { - logger.debug(`checking to see if ${name}#${claimId} is not blocked`); + Blocked.isNotBlocked = function (outpoint) { + logger.debug(`checking to see if ${outpoint} is not blocked`); return new Promise((resolve, reject) => { this.findOne({ where: { - claimId, - name, + outpoint, }, }) .then(result => { @@ -46,5 +37,39 @@ module.exports = (sequelize, { STRING }) => { }); }; + Blocked.refreshTable = function () { + let blockedList = []; + return fetch('https://api.lbry.io/file/list_blocked') + .then(response => { + return response.json(); + }) + .then(jsonResponse => { + if (!jsonResponse.data) { + throw new Error('no data in list_blocked response'); + } + if (!jsonResponse.data.outpoints) { + throw new Error('no outpoints in list_blocked response'); + } + return jsonResponse.data.outpoints; + }) + .then(outpoints => { + logger.debug('total outpoints:', outpoints.length); + // prep the records + for (let i = 0; i < outpoints.length; i++) { + blockedList.push({ + outpoint: outpoints[i], + }); + } + // clear the table + return this.destroy({ + truncate: true, + }); + }) + .then(() => { + // fill the table + return this.bulkCreate(blockedList); + }); + }; + return Blocked; }; diff --git a/server/models/claim.js b/server/models/claim.js index 128d76ee..7e2a5f61 100644 --- a/server/models/claim.js +++ b/server/models/claim.js @@ -378,5 +378,27 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { }); }; + Claim.getOutpoint = function (name, claimId) { + return this + .findAll({ + where : { name, claimId }, + attributes: ['outpoint'], + }) + .then(result => { + logger.debug('outpoint result'); + switch (result.length) { + case 0: + throw new Error(`no record found for ${name}#${claimId}`); + case 1: + return result[0].dataValues.outpoint; + default: + throw new Error(`more than one record found for ${name}#${claimId}`); + } + }) + .catch(error => { + throw error; + }); + }; + return Claim; }; diff --git a/server/models/tor.js b/server/models/tor.js index 51541545..a314e92d 100644 --- a/server/models/tor.js +++ b/server/models/tor.js @@ -1,3 +1,4 @@ +const logger = require('winston'); const { details: { ipAddress } } = require('@config/siteConfig'); module.exports = (sequelize, { STRING }) => { @@ -25,6 +26,8 @@ module.exports = (sequelize, { STRING }) => { return response.json(); }) .then(jsonResponse => { + logger.debug('total tor nodes:', jsonResponse.length); + // prep the records for (let i = 0; i < jsonResponse.length; i++) { torList.push({ address : jsonResponse[i].Address, diff --git a/server/models/utils/refreshBlockedList.js b/server/models/utils/refreshBlockedList.js index ec70ac0d..3364bd84 100644 --- a/server/models/utils/refreshBlockedList.js +++ b/server/models/utils/refreshBlockedList.js @@ -16,9 +16,15 @@ const refreshBlockedList = () => { return jsonResponse.data.outpoints; }) .then(outpoints => { + db.Claim.findAll({ + where: { + outpoint: outpoints, + }, + }); + let updatePromises = []; outpoints.forEach(outpoint => { - // logger.debug('outpoint:', outpoint); + logger.debug('blocked outpoint:', outpoint); updatePromises.push(db.Claim .findOne({ where: {