From a734069b74b76f6be2102a2003abe10fbdf0018c Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 20 Feb 2019 13:04:24 -0500 Subject: [PATCH] actually use push target filters when new books are published --- api/controllers/BooksController.js | 32 +++++++++++++------ api/models/Book.js | 1 + .../20190220123908_AddPublisherToBooks.js | 15 +++++++++ package.json | 3 +- 4 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 migrations/20190220123908_AddPublisherToBooks.js diff --git a/api/controllers/BooksController.js b/api/controllers/BooksController.js index 51779ad..1b2a456 100644 --- a/api/controllers/BooksController.js +++ b/api/controllers/BooksController.js @@ -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) - request.post({ - url: targets[i].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}`) - } - }) + 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: url, + headers: { 'User-Agent': 'RoE-aggregator' }, + form: book + }, function (err, httpResp, body) { + if (err) { + sails.log(`error: failed to send book ${id} to ${url}`) + } + }) + } } } diff --git a/api/models/Book.js b/api/models/Book.js index 11962de..2c62d4e 100644 --- a/api/models/Book.js +++ b/api/models/Book.js @@ -19,6 +19,7 @@ module.exports = { }, title: { type: 'string', required: true }, author: { type: 'string' }, + publisher: { type: 'string' }, isbn: { type: 'string' }, version: { type: 'string' } diff --git a/migrations/20190220123908_AddPublisherToBooks.js b/migrations/20190220123908_AddPublisherToBooks.js new file mode 100644 index 0000000..108aeaa --- /dev/null +++ b/migrations/20190220123908_AddPublisherToBooks.js @@ -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') + }) + ]) +} diff --git a/package.json b/package.json index 146e463..37c182a 100644 --- a/package.json +++ b/package.json @@ -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",