diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index 97f9c981..ea5876b3 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -804,7 +804,7 @@ class LibraryController { const limit = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12 const query = req.query.q.trim().toLowerCase() - const matches = await libraryItemFilters.search(req.library, query, limit) + const matches = await libraryItemFilters.search(req.user, req.library, query, limit) res.json(matches) } diff --git a/server/utils/queries/libraryItemFilters.js b/server/utils/queries/libraryItemFilters.js index 51bb4131..e7ffe701 100644 --- a/server/utils/queries/libraryItemFilters.js +++ b/server/utils/queries/libraryItemFilters.js @@ -168,16 +168,17 @@ module.exports = { /** * Search library items + * @param {import('../../objects/user/User')} oldUser * @param {import('../../objects/Library')} oldLibrary * @param {string} query * @param {number} limit * @returns {{book:object[], narrators:object[], authors:object[], tags:object[], series:object[], podcast:object[]}} */ - search(oldLibrary, query, limit) { + search(oldUser, oldLibrary, query, limit) { if (oldLibrary.isBook) { - return libraryItemsBookFilters.search(oldLibrary, query, limit, 0) + return libraryItemsBookFilters.search(oldUser, oldLibrary, query, limit, 0) } else { - return libraryItemsPodcastFilters.search(oldLibrary, query, limit, 0) + return libraryItemsPodcastFilters.search(oldUser, oldLibrary, query, limit, 0) } } } \ No newline at end of file diff --git a/server/utils/queries/libraryItemsBookFilters.js b/server/utils/queries/libraryItemsBookFilters.js index 21ef5efa..1a7fcbdc 100644 --- a/server/utils/queries/libraryItemsBookFilters.js +++ b/server/utils/queries/libraryItemsBookFilters.js @@ -5,7 +5,7 @@ const Logger = require('../../Logger') module.exports = { /** * User permissions to restrict books for explicit content & tags - * @param {oldUser} user + * @param {import('../../objects/user/User')} user * @returns {{ bookWhere:Sequelize.WhereOptions, replacements:object }} */ getUserPermissionBookWhereQuery(user) { @@ -929,39 +929,46 @@ module.exports = { /** * Search books, authors, series + * @param {import('../../objects/user/User')} oldUser * @param {import('../../objects/Library')} oldLibrary * @param {string} query * @param {number} limit * @param {number} offset * @returns {{book:object[], narrators:object[], authors:object[], tags:object[], series:object[]}} */ - async search(oldLibrary, query, limit, offset) { + async search(oldUser, oldLibrary, query, limit, offset) { + const userPermissionBookWhere = this.getUserPermissionBookWhereQuery(oldUser) + // Search title, subtitle, asin, isbn const books = await Database.bookModel.findAll({ - where: { - [Sequelize.Op.or]: [ - { - title: { - [Sequelize.Op.substring]: query + where: [ + { + [Sequelize.Op.or]: [ + { + title: { + [Sequelize.Op.substring]: query + } + }, + { + subtitle: { + [Sequelize.Op.substring]: query + } + }, + { + asin: { + [Sequelize.Op.substring]: query + } + }, + { + isbn: { + [Sequelize.Op.substring]: query + } } - }, - { - subtitle: { - [Sequelize.Op.substring]: query - } - }, - { - asin: { - [Sequelize.Op.substring]: query - } - }, - { - isbn: { - [Sequelize.Op.substring]: query - } - } - ] - }, + ] + }, + ...userPermissionBookWhere.bookWhere + ], + replacements: userPermissionBookWhere.replacements, include: [ { model: Database.libraryItemModel, @@ -1060,11 +1067,13 @@ module.exports = { }, libraryId: oldLibrary.id }, + replacements: userPermissionBookWhere.replacements, include: { separate: true, model: Database.models.bookSeries, include: { model: Database.bookModel, + where: userPermissionBookWhere.bookWhere, include: { model: Database.libraryItemModel } diff --git a/server/utils/queries/libraryItemsPodcastFilters.js b/server/utils/queries/libraryItemsPodcastFilters.js index 7dc87a60..b13125ba 100644 --- a/server/utils/queries/libraryItemsPodcastFilters.js +++ b/server/utils/queries/libraryItemsPodcastFilters.js @@ -6,8 +6,8 @@ const Logger = require('../../Logger') module.exports = { /** * User permissions to restrict podcasts for explicit content & tags - * @param {oldUser} user - * @returns {object} { podcastWhere:Sequelize.WhereOptions, replacements:string[] } + * @param {import('../../objects/user/User')} user + * @returns {{ podcastWhere:Sequelize.WhereOptions, replacements:object }} */ getUserPermissionPodcastWhereQuery(user) { const podcastWhere = [] @@ -295,39 +295,45 @@ module.exports = { /** * Search podcasts + * @param {import('../../objects/user/User')} oldUser * @param {import('../../objects/Library')} oldLibrary * @param {string} query * @param {number} limit * @param {number} offset * @returns {{podcast:object[], tags:object[]}} */ - async search(oldLibrary, query, limit, offset) { + async search(oldUser, oldLibrary, query, limit, offset) { + const userPermissionPodcastWhere = this.getUserPermissionPodcastWhereQuery(user) // Search title, author, itunesId, itunesArtistId const podcasts = await Database.podcastModel.findAll({ - where: { - [Sequelize.Op.or]: [ - { - title: { - [Sequelize.Op.substring]: query + where: [ + { + [Sequelize.Op.or]: [ + { + title: { + [Sequelize.Op.substring]: query + } + }, + { + author: { + [Sequelize.Op.substring]: query + } + }, + { + itunesId: { + [Sequelize.Op.substring]: query + } + }, + { + itunesArtistId: { + [Sequelize.Op.substring]: query + } } - }, - { - author: { - [Sequelize.Op.substring]: query - } - }, - { - itunesId: { - [Sequelize.Op.substring]: query - } - }, - { - itunesArtistId: { - [Sequelize.Op.substring]: query - } - } - ] - }, + ] + }, + ...userPermissionPodcastWhere.podcastWhere + ], + replacements: userPermissionPodcastWhere.replacements, include: [ { model: Database.libraryItemModel,