2018-04-29 19:31:26 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
|
2018-04-29 21:17:23 +02:00
|
|
|
const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
|
|
|
|
|
2018-04-28 00:17:09 +02:00
|
|
|
module.exports = (sequelize, { STRING }) => {
|
2018-04-29 19:31:26 +02:00
|
|
|
const Blocked = sequelize.define(
|
2018-04-28 00:17:09 +02:00
|
|
|
'Blocked',
|
|
|
|
{
|
|
|
|
outpoint: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
2018-04-29 19:31:26 +02:00
|
|
|
|
2018-06-30 05:34:48 +02:00
|
|
|
Blocked.isNotBlocked = function (outpoint) {
|
|
|
|
logger.debug(`checking to see if ${outpoint} is not blocked`);
|
2018-04-29 19:31:26 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findOne({
|
|
|
|
where: {
|
2018-06-30 05:34:48 +02:00
|
|
|
outpoint,
|
2018-04-29 19:31:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(result => {
|
2018-04-29 21:17:23 +02:00
|
|
|
if (result) {
|
|
|
|
return reject(BLOCKED_CLAIM);
|
2018-04-29 19:31:26 +02:00
|
|
|
}
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 21:17:23 +02:00
|
|
|
logger.error(error);
|
|
|
|
reject(BLOCKED_CLAIM);
|
2018-04-29 19:31:26 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-06-30 05:34:48 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-29 19:31:26 +02:00
|
|
|
return Blocked;
|
2018-04-28 00:17:09 +02:00
|
|
|
};
|