2019-09-07 01:20:27 +02:00
|
|
|
const fetch = require('node-fetch');
|
2020-02-07 01:28:30 +01:00
|
|
|
const { Op, fn, col } = require('sequelize');
|
2019-09-07 01:20:27 +02:00
|
|
|
|
2020-02-06 21:55:17 +01:00
|
|
|
const BooksController = require('../bookData');
|
|
|
|
const { quickSearchInventaire } = require('./Inventaire');
|
|
|
|
|
|
|
|
const defaultSearchOptions = {
|
|
|
|
searchBy: 'name', // A column name in the BookReference model, mainly 'name' or 'description'
|
|
|
|
source: 'inventaire',
|
|
|
|
language: 'en',
|
|
|
|
}
|
2019-09-12 19:35:43 +02:00
|
|
|
|
2019-09-07 01:20:27 +02:00
|
|
|
class SearchController {
|
2020-02-07 23:28:59 +01:00
|
|
|
constructor(sequelizeModels) {
|
2020-02-06 21:55:17 +01:00
|
|
|
this.models = sequelizeModels;
|
2019-09-07 01:20:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 01:28:30 +01:00
|
|
|
get bookReferenceSearchAttributes() {
|
|
|
|
return {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
as: 'Interactions',
|
|
|
|
model: this.models.Review,
|
|
|
|
attributes: ['id'],
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
as: 'Reviews',
|
|
|
|
model: this.models.Review,
|
|
|
|
attributes: ['id'],
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
as: 'Ratings',
|
|
|
|
model: this.models.Review,
|
|
|
|
attributes: ['rating'],
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
], // These are all subsets of Review model specified in BookReference associations
|
|
|
|
attributes: [
|
|
|
|
[col('BookReference.id'), 'id'],
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'sources',
|
|
|
|
'covers',
|
|
|
|
[fn('COUNT', col('Interactions.id')), 'totalInteractions'],
|
|
|
|
[fn('COUNT', col('Reviews.id')), 'numReviews'],
|
|
|
|
[fn('AVG', col('Ratings.rating')), 'averageRating'],
|
|
|
|
],
|
|
|
|
order: [[col('totalInteractions'), 'DESC']],
|
|
|
|
};
|
2020-02-06 21:55:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
async search(searchTerm, options = defaultSearchOptions) {
|
|
|
|
const searchBy = options.searchBy.replace('title', 'name').replace('author', 'description');
|
|
|
|
const { source, language } = options;
|
|
|
|
|
|
|
|
const bookReferences = await this.searchReferences(searchTerm, options);
|
2020-02-06 21:55:17 +01:00
|
|
|
let searchResults;
|
2020-02-07 23:28:59 +01:00
|
|
|
switch (source) {
|
2020-02-06 21:55:17 +01:00
|
|
|
case 'openlibrary': {
|
2020-02-07 23:28:59 +01:00
|
|
|
searchResults = await this.searchOpenLibrary(searchBy);
|
2020-02-06 21:55:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'inventaire':
|
|
|
|
default: {
|
2020-02-07 23:28:59 +01:00
|
|
|
searchResults = await quickSearchInventaire(searchTerm, language);
|
2020-02-06 21:55:17 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any search results that match refs with the same URI and delete from results array
|
2020-02-07 01:28:30 +01:00
|
|
|
const urisToCheck = searchResults.filter(
|
2020-02-07 23:28:59 +01:00
|
|
|
result => !bookReferences.some(ref => result.uri === ref.sources[source])
|
2020-02-07 01:28:30 +01:00
|
|
|
).map(result => result.uri);
|
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
let extraReferences = [];
|
2020-02-07 01:28:30 +01:00
|
|
|
if (urisToCheck.length > 0) {
|
2020-02-07 23:28:59 +01:00
|
|
|
extraReferences = await this.searchReferencesBySourceCodes(source, urisToCheck);
|
2020-02-07 01:28:30 +01:00
|
|
|
}
|
2020-02-06 21:55:17 +01:00
|
|
|
return [
|
|
|
|
...bookReferences,
|
2020-02-07 23:28:59 +01:00
|
|
|
...extraReferences,
|
|
|
|
...searchResults.filter( // Only show the rest of the search results
|
|
|
|
result => !extraReferences.some(
|
|
|
|
ref => result.uri === ref.sources[source]
|
|
|
|
)
|
|
|
|
),
|
2020-02-06 21:55:17 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
async searchReferences(searchTerm, options = defaultSearchOptions) {
|
|
|
|
const searchBy = options.searchBy.replace('title', 'name').replace('author', 'description');
|
|
|
|
const { language } = options;
|
|
|
|
|
2020-02-07 01:28:30 +01:00
|
|
|
const { BookReference } = this.models;
|
2020-02-06 21:55:17 +01:00
|
|
|
|
|
|
|
const exact = await BookReference.findAll({
|
|
|
|
where: {
|
|
|
|
[Op.and]: [ // All of the contained cases are true
|
|
|
|
{
|
2020-02-07 23:28:59 +01:00
|
|
|
[searchBy]: searchTerm, // searchBy is exactly searchTerm
|
2020-02-06 21:55:17 +01:00
|
|
|
},
|
|
|
|
{
|
2020-02-07 23:28:59 +01:00
|
|
|
locale: language,
|
2020-02-06 21:55:17 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
2020-02-07 01:28:30 +01:00
|
|
|
...this.bookReferenceSearchAttributes,
|
|
|
|
}).then( // Empty results give 1 empty model in an array, so filter those out
|
|
|
|
references => references.filter(ref => typeof ref.id !== 'undefined' && ref.id !== null)
|
|
|
|
);
|
2020-02-06 21:55:17 +01:00
|
|
|
|
|
|
|
if (exact.length > 0) {
|
|
|
|
return exact;
|
2019-09-09 22:19:22 +02:00
|
|
|
}
|
2020-02-07 01:28:30 +01:00
|
|
|
|
2020-02-06 21:55:17 +01:00
|
|
|
// If no exact matches are found, return any approximate ones.
|
|
|
|
return await BookReference.findAll({
|
|
|
|
where: {
|
|
|
|
[Op.and]: [ // All of the contained cases are true
|
|
|
|
{
|
2020-02-07 23:28:59 +01:00
|
|
|
[searchBy]: { // `name` or `description`
|
|
|
|
[Op.substring]: searchTerm, // LIKE '%searchTerm%'
|
2020-02-06 21:55:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-07 23:28:59 +01:00
|
|
|
locale: language,
|
2020-02-06 21:55:17 +01:00
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
2020-02-07 01:28:30 +01:00
|
|
|
...this.bookReferenceSearchAttributes,
|
|
|
|
}).then( // Empty results give 1 empty model in an array, so filter those out
|
|
|
|
references => references.filter(ref => typeof ref.id !== 'undefined' && ref.id !== null)
|
|
|
|
);
|
2020-02-06 21:55:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async searchReferencesBySourceCode(source, sourceId) {
|
|
|
|
const sourceJSONKey = `"${source}"`; // Enable searching withing JSON column.
|
|
|
|
return await this.models.BookReference.findOne({
|
|
|
|
where: {
|
|
|
|
source: {
|
|
|
|
[sourceJSONKey]: { // Where the object key is the source
|
|
|
|
[Op.eq]: sourceId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-07 01:28:30 +01:00
|
|
|
...this.bookReferenceSearchAttributes,
|
|
|
|
}).then( // Empty results give 1 empty model in an array, so filter those out
|
|
|
|
references => references.filter(ref => typeof ref.id !== 'undefined' && ref.id !== null)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async searchReferencesBySourceCodes(source, sourceIds) {
|
|
|
|
const sourceJSONKey = `"${source}"`; // Enable searching withing JSON column.
|
2020-02-07 23:28:59 +01:00
|
|
|
return await this.models.BookReference.findAll({
|
2020-02-07 01:28:30 +01:00
|
|
|
where: {
|
|
|
|
[Op.or]: sourceIds.map(sourceId => ({
|
|
|
|
source: {
|
|
|
|
[sourceJSONKey]: sourceId,
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
...this.bookReferenceSearchAttributes,
|
|
|
|
}).then( // Empty results give 1 empty model in an array, so filter those out
|
|
|
|
references => references.filter(ref => typeof ref.id !== 'undefined' && ref.id !== null)
|
|
|
|
);
|
2019-09-09 22:19:22 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 01:20:27 +02:00
|
|
|
/**
|
|
|
|
* Query a MediaWiki api.php instance with the given options
|
|
|
|
*/
|
|
|
|
mediaWikiQuery(endpoint, options) {
|
|
|
|
/**
|
|
|
|
* Create a uniquely-named callback that will process the JSONP results
|
|
|
|
*/
|
|
|
|
var createCallback = function (k) {
|
|
|
|
var i = 1;
|
|
|
|
var callbackName;
|
|
|
|
do {
|
|
|
|
callbackName = 'searchCallback' + i;
|
|
|
|
i = i + 1;
|
|
|
|
} while (window[callbackName])
|
|
|
|
window[callbackName] = k;
|
|
|
|
return callbackName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flatten an object into a URL query string.
|
|
|
|
* For example: { foo: 'bar', baz: 42 } becomes 'foo=bar&baz=42'
|
|
|
|
*/
|
|
|
|
var queryStr = function (options) {
|
|
|
|
var query = [];
|
|
|
|
for (var i in options) {
|
|
|
|
if (options.hasOwnProperty(i)) {
|
|
|
|
query.push(encodeURIComponent(i) + '=' + encodeURIComponent(options[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return query.join('&');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a function that can be applied to a callback. The callback processes
|
|
|
|
* the JSON results of the API call.
|
|
|
|
*/
|
|
|
|
return function (k) {
|
|
|
|
options.format = 'json';
|
|
|
|
options.callback = createCallback(k);
|
|
|
|
var script = document.createElement('script');
|
|
|
|
script.id = 'searchResults';
|
|
|
|
script.src = endpoint + '?' + queryStr(options);
|
|
|
|
var head = document.getElementsByTagName('head')[0];
|
|
|
|
head.appendChild(script);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async searchWikiBooks(term) {
|
2020-02-07 23:28:59 +01:00
|
|
|
if (!term) {
|
2019-09-07 01:20:27 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = this.mediaWikiQuery('https://en.wikibooks.org/w/api.php', {
|
|
|
|
action: 'query',
|
|
|
|
list: 'search',
|
2020-02-07 23:28:59 +01:00
|
|
|
srsearch: term,
|
2019-09-07 01:20:27 +02:00
|
|
|
srprop: '',
|
|
|
|
});
|
|
|
|
query(response => {
|
|
|
|
console.log(response);
|
|
|
|
const searchScript = document.getElementById('searchResults');
|
|
|
|
searchScript.parentNode.removeChild(searchScript);
|
|
|
|
for (let property in window) {
|
|
|
|
if (property.includes('searchCallback')) {
|
|
|
|
delete window[property];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bookResults = [];
|
|
|
|
const pageids = response.query.search.map(item => item.pageid);
|
|
|
|
const propsQuery = this.mediaWikiQuery('https://en.wikibooks.org/w/api.php', {
|
|
|
|
action: 'query',
|
|
|
|
pageids: pageids.join('|'),
|
|
|
|
prop: 'categories|pageprops',
|
|
|
|
});
|
|
|
|
propsQuery(propsResponse => {
|
|
|
|
console.log(propsResponse);
|
|
|
|
for (let pageid in propsResponse.query.pages) {
|
|
|
|
if (propsResponse.query.pages[pageid].hasOwnProperty('categories')) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return bookResults;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
async searchOpenLibrary(searchTerm, searchBy = 'title') {
|
|
|
|
if (!searchTerm) {
|
2019-09-07 01:20:27 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
return fetch(`https://openlibrary.org/search.json?${searchBy}=${encodeURIComponent(searchTerm)}`)
|
2019-09-07 01:20:27 +02:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(response => {
|
|
|
|
if (!response.hasOwnProperty('docs')) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-09-26 20:22:58 +02:00
|
|
|
|
2020-02-07 23:28:59 +01:00
|
|
|
const booksController = new BooksController('openLibrary');
|
2019-09-26 20:22:58 +02:00
|
|
|
|
2019-09-07 01:20:27 +02:00
|
|
|
// Format the response into usable objects
|
|
|
|
const docs = response.docs.map(doc => {
|
2019-09-26 20:22:58 +02:00
|
|
|
return booksController.handleOpenLibraryEntity(doc);
|
2019-09-07 01:20:27 +02:00
|
|
|
});
|
|
|
|
|
2019-09-26 21:59:40 +02:00
|
|
|
let results = [];
|
2019-09-07 01:20:27 +02:00
|
|
|
// Filter out duplicate items with the same title and author
|
2019-09-26 21:59:40 +02:00
|
|
|
docs.forEach(doc => {
|
|
|
|
const existingDoc = results.find((filterResult) => {
|
|
|
|
return filterResult.title === doc.title && filterResult.description === doc.description;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existingDoc) {
|
|
|
|
results.push(doc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
results = results.map(result => {
|
2019-09-07 01:20:27 +02:00
|
|
|
// Find any duplicates in case they have different cover data
|
|
|
|
const duplicates = docs.filter(doc => {
|
2019-09-26 20:22:58 +02:00
|
|
|
return doc.name.toLowerCase() === result.name.toLowerCase() && doc.description === result.description;
|
2019-09-07 01:20:27 +02:00
|
|
|
});
|
|
|
|
result.covers = [];
|
|
|
|
duplicates.forEach(duplicate => {
|
2019-09-26 20:22:58 +02:00
|
|
|
if (duplicate.coverId !== false) {
|
|
|
|
result.covers.push({
|
|
|
|
uri: duplicate.coverId,
|
|
|
|
url: `//covers.openlibrary.org/b/id/${duplicate.coverId}-M.jpg`,
|
|
|
|
});
|
2019-09-07 01:20:27 +02:00
|
|
|
}
|
|
|
|
});
|
2019-09-26 20:22:58 +02:00
|
|
|
delete result.coverId;
|
2019-09-07 01:20:27 +02:00
|
|
|
return result;
|
2019-09-26 20:22:58 +02:00
|
|
|
}).map(bookData => {
|
|
|
|
// Use bookController to get community data
|
|
|
|
booksController.uri = bookData.uri; // Update booksController.uri for each book when fetching community data.
|
|
|
|
const communityData = booksController.getCommunityData(5);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...bookData,
|
|
|
|
...communityData,
|
|
|
|
};
|
2019-09-07 01:20:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}).catch(error => {
|
|
|
|
console.log(error);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SearchController;
|