diff --git a/index.js b/index.js index d4eb8274..f4a540bd 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,7 @@ const { getWalletBalance } = require('./server/lbrynet'); const configureLogging = require('./server/utils/configureLogging.js'); const configureSlack = require('./server/utils/configureSlack.js'); const speechPassport = require('./server/speechPassport'); +const refreshBlockedList = require('./server/models/utils/refreshBlockedList.js'); const { details: { port: PORT }, @@ -135,7 +136,7 @@ function Server () { } logger.info(`Peforming updates...`); return Promise.all([ - [], + refreshBlockedList(), db.Tor.refreshTable(), ]) .then(([updatedBlockedList, updatedTorList]) => { diff --git a/server/controllers/api/blocked/index.js b/server/controllers/api/blocked/index.js new file mode 100644 index 00000000..d81503c4 --- /dev/null +++ b/server/controllers/api/blocked/index.js @@ -0,0 +1,22 @@ +const logger = require('winston'); +const refreshBlockedList = require('../../../models/utils/refreshBlockedList.js'); + +const updateBlockedList = (req, res) => { + refreshBlockedList() + .then(data => { + logger.info('finished updating blocked content list'); + res.status(200).json({ + success: true, + data, + }); + }) + .catch(error => { + logger.error(error); + res.status(500).json({ + success: false, + error, + }); + }); +}; + +module.exports = updateBlockedList; diff --git a/server/controllers/api/claim/blockedList/index.js b/server/models/utils/refreshBlockedList.js similarity index 67% rename from server/controllers/api/claim/blockedList/index.js rename to server/models/utils/refreshBlockedList.js index abc80d8e..ec70ac0d 100644 --- a/server/controllers/api/claim/blockedList/index.js +++ b/server/models/utils/refreshBlockedList.js @@ -1,8 +1,8 @@ const logger = require('winston'); -const db = require('../../../../models'); +const db = require('../index.js'); -const updateBlockedList = (req, res) => { - return fetch('https://api.lbry.io/file/list_blocked') +const refreshBlockedList = () => { + return fetch('https://api.lbry.io/file/list_blocked') .then(response => { return response.json(); }) @@ -16,7 +16,6 @@ const updateBlockedList = (req, res) => { return jsonResponse.data.outpoints; }) .then(outpoints => { - logger.info('number of blocked outpoints:', outpoints.length); let updatePromises = []; outpoints.forEach(outpoint => { // logger.debug('outpoint:', outpoint); @@ -29,13 +28,12 @@ const updateBlockedList = (req, res) => { .then(Claim => { if (Claim) { const { claimId, name } = Claim; - logger.debug(`creating record in Blocked for ${name}#${claimId}`); const blocked = { claimId, name, outpoint, }; - return db.upsert(db.Blocked, blocked, blocked, 'Blocked') + return db.upsert(db.Blocked, blocked, blocked, 'Blocked'); } }) .catch(error => { @@ -45,12 +43,11 @@ const updateBlockedList = (req, res) => { return Promise.all(updatePromises); }) .then(() => { - logger.info('finished updating blocked content list'); - res.status(200).json({success: true, message: 'finished updating blocked content list'}); + return db.Blocked.findAll({raw: true}); }) - .catch((error) => { - logger.error(error); + .catch(error => { + throw error; }); }; -module.exports = updateBlockedList; +module.exports = refreshBlockedList; diff --git a/server/routes/api/index.js b/server/routes/api/index.js index b0d10a1a..bb24e5b2 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -1,9 +1,12 @@ +// middleware +const multipartMiddleware = require('../../middleware/multipartMiddleware'); +const torCheckMiddleware = require('../../middleware/torCheckMiddleware'); +// route handlers const channelAvailability = require('../../controllers/api/channel/availability'); const channelClaims = require('../../controllers/api/channel/claims'); const channelData = require('../../controllers/api/channel/data'); const channelShortId = require('../../controllers/api/channel/shortId'); const claimAvailability = require('../../controllers/api/claim/availability'); -const claimBlockedList = require('../../controllers/api/claim/blockedList'); const claimData = require('../../controllers/api/claim/data/'); const claimGet = require('../../controllers/api/claim/get'); const claimList = require('../../controllers/api/claim/list'); @@ -15,9 +18,8 @@ const fileAvailability = require('../../controllers/api/file/availability'); const userPassword = require('../../controllers/api/user/password'); const publishingConfig = require('../../controllers/api/config/site/publishing'); const getTorList = require('../../controllers/api/tor'); +const getBlockedList = require('../../controllers/api/blocked'); -const multipartMiddleware = require('../../middleware/multipartMiddleware'); -const torCheckMiddleware = require('../../middleware/torCheckMiddleware'); module.exports = (app) => { // channel routes @@ -27,7 +29,6 @@ module.exports = (app) => { app.get('/api/channel/claims/:channelName/:channelClaimId/:page', torCheckMiddleware, channelClaims); // claim routes app.get('/api/claim/availability/:name', torCheckMiddleware, claimAvailability); - app.get('/api/claim/blocked-list/', torCheckMiddleware, claimBlockedList); app.get('/api/claim/data/:claimName/:claimId', torCheckMiddleware, claimData); app.get('/api/claim/get/:name/:claimId', torCheckMiddleware, claimGet); app.get('/api/claim/list/:name', torCheckMiddleware, claimList); @@ -43,4 +44,6 @@ module.exports = (app) => { app.get('/api/config/site/publishing', torCheckMiddleware, publishingConfig); // tor app.get('/api/tor', torCheckMiddleware, getTorList); + // blocked + app.get('/api/blocked', torCheckMiddleware, getBlockedList); };