spee.ch/server/models/blocked.js

76 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-04-29 19:31:26 +02:00
const logger = require('winston');
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
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: {
outpoint,
2018-04-29 19:31:26 +02:00
},
})
.then(result => {
if (result) {
return reject(BLOCKED_CLAIM);
2018-04-29 19:31:26 +02:00
}
resolve(true);
})
.catch(error => {
logger.error(error);
reject(BLOCKED_CLAIM);
2018-04-29 19:31:26 +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
};