actually use push target filters when new books are published

pull/39/head
unknown 2019-02-20 13:04:24 -05:00
parent 460cf44c4c
commit a734069b74
4 changed files with 40 additions and 11 deletions

View File

@ -7,6 +7,7 @@
const HttpError = require('../errors/HttpError')
const request = require('request')
const uriRegex = /^(.+:\/\/)?(.+\.)*(.+\.).{1,}(:\d+)?(.+)?/i
module.exports = {
publish: async function (req, res) {
@ -73,16 +74,27 @@ module.exports = {
async function sendUpdatesAsync (id) {
const book = await Book.findOne({ id })
const targets = await TargetUrl.find()
if (!book) return
for (const i in targets) {
sails.log('sending ' + book.id + ' info to ' + targets[i].url)
const item = targets[i]
const { author: fAuthor, publisher: fPublisher, title: fTitle, isbn: fIsbn, url } = item
const { author: bAuthor, publisher: bPublisher, title: bTitle, isbn: bIsbn } = book
sails.log('sending ' + book.id + ' info to ' + url)
if (uriRegex.test(url)) {
if (fAuthor && !((bAuthor || '').includes(fAuthor))) continue
if (fPublisher && !((bPublisher || '').includes(fPublisher))) continue
if (fTitle && !((bTitle || '').includes(fTitle))) continue
if (fIsbn && !((bIsbn || '').includes(fIsbn))) continue
request.post({
url: targets[i].url,
url: url,
headers: { 'User-Agent': 'RoE-aggregator' },
form: book
}, function (err, httpResp, body) {
if (err) {
sails.log(`error: failed to send book ${id} to ${targets[i].url}`)
sails.log(`error: failed to send book ${id} to ${url}`)
}
})
}
}
}

View File

@ -19,6 +19,7 @@ module.exports = {
},
title: { type: 'string', required: true },
author: { type: 'string' },
publisher: { type: 'string' },
isbn: { type: 'string' },
version: { type: 'string' }

View File

@ -0,0 +1,15 @@
exports.up = function (knex, Promise) {
return Promise.all([
knex.schema.table('book', t => {
t.string('publisher')
})
])
}
exports.down = function (knex, Promise) {
return Promise.all([
knex.schema.table('book', t => {
t.dropColumns('publisher')
})
])
}

View File

@ -21,7 +21,8 @@
"debug": "node --inspect app.js",
"custom-tests": "echo 'Nothing yet'",
"db:migrate": "knex migrate:latest",
"db:rollback": "knex migrate:rollback"
"db:rollback": "knex migrate:rollback",
"db:gmig": "knex migrate:make"
},
"dependencies": {
"@sailshq/connect-redis": "^3.2.1",