Update search query to use user permissions

This commit is contained in:
advplyr 2023-08-19 14:11:34 -05:00
parent c77cead9ae
commit f21d69339f
4 changed files with 71 additions and 55 deletions

View File

@ -804,7 +804,7 @@ class LibraryController {
const limit = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12 const limit = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 12
const query = req.query.q.trim().toLowerCase() 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) res.json(matches)
} }

View File

@ -168,16 +168,17 @@ module.exports = {
/** /**
* Search library items * Search library items
* @param {import('../../objects/user/User')} oldUser
* @param {import('../../objects/Library')} oldLibrary * @param {import('../../objects/Library')} oldLibrary
* @param {string} query * @param {string} query
* @param {number} limit * @param {number} limit
* @returns {{book:object[], narrators:object[], authors:object[], tags:object[], series:object[], podcast:object[]}} * @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) { if (oldLibrary.isBook) {
return libraryItemsBookFilters.search(oldLibrary, query, limit, 0) return libraryItemsBookFilters.search(oldUser, oldLibrary, query, limit, 0)
} else { } else {
return libraryItemsPodcastFilters.search(oldLibrary, query, limit, 0) return libraryItemsPodcastFilters.search(oldUser, oldLibrary, query, limit, 0)
} }
} }
} }

View File

@ -5,7 +5,7 @@ const Logger = require('../../Logger')
module.exports = { module.exports = {
/** /**
* User permissions to restrict books for explicit content & tags * User permissions to restrict books for explicit content & tags
* @param {oldUser} user * @param {import('../../objects/user/User')} user
* @returns {{ bookWhere:Sequelize.WhereOptions, replacements:object }} * @returns {{ bookWhere:Sequelize.WhereOptions, replacements:object }}
*/ */
getUserPermissionBookWhereQuery(user) { getUserPermissionBookWhereQuery(user) {
@ -929,39 +929,46 @@ module.exports = {
/** /**
* Search books, authors, series * Search books, authors, series
* @param {import('../../objects/user/User')} oldUser
* @param {import('../../objects/Library')} oldLibrary * @param {import('../../objects/Library')} oldLibrary
* @param {string} query * @param {string} query
* @param {number} limit * @param {number} limit
* @param {number} offset * @param {number} offset
* @returns {{book:object[], narrators:object[], authors:object[], tags:object[], series:object[]}} * @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 // Search title, subtitle, asin, isbn
const books = await Database.bookModel.findAll({ const books = await Database.bookModel.findAll({
where: { where: [
[Sequelize.Op.or]: [ {
{ [Sequelize.Op.or]: [
title: { {
[Sequelize.Op.substring]: query title: {
[Sequelize.Op.substring]: query
}
},
{
subtitle: {
[Sequelize.Op.substring]: query
}
},
{
asin: {
[Sequelize.Op.substring]: query
}
},
{
isbn: {
[Sequelize.Op.substring]: query
}
} }
}, ]
{ },
subtitle: { ...userPermissionBookWhere.bookWhere
[Sequelize.Op.substring]: query ],
} replacements: userPermissionBookWhere.replacements,
},
{
asin: {
[Sequelize.Op.substring]: query
}
},
{
isbn: {
[Sequelize.Op.substring]: query
}
}
]
},
include: [ include: [
{ {
model: Database.libraryItemModel, model: Database.libraryItemModel,
@ -1060,11 +1067,13 @@ module.exports = {
}, },
libraryId: oldLibrary.id libraryId: oldLibrary.id
}, },
replacements: userPermissionBookWhere.replacements,
include: { include: {
separate: true, separate: true,
model: Database.models.bookSeries, model: Database.models.bookSeries,
include: { include: {
model: Database.bookModel, model: Database.bookModel,
where: userPermissionBookWhere.bookWhere,
include: { include: {
model: Database.libraryItemModel model: Database.libraryItemModel
} }

View File

@ -6,8 +6,8 @@ const Logger = require('../../Logger')
module.exports = { module.exports = {
/** /**
* User permissions to restrict podcasts for explicit content & tags * User permissions to restrict podcasts for explicit content & tags
* @param {oldUser} user * @param {import('../../objects/user/User')} user
* @returns {object} { podcastWhere:Sequelize.WhereOptions, replacements:string[] } * @returns {{ podcastWhere:Sequelize.WhereOptions, replacements:object }}
*/ */
getUserPermissionPodcastWhereQuery(user) { getUserPermissionPodcastWhereQuery(user) {
const podcastWhere = [] const podcastWhere = []
@ -295,39 +295,45 @@ module.exports = {
/** /**
* Search podcasts * Search podcasts
* @param {import('../../objects/user/User')} oldUser
* @param {import('../../objects/Library')} oldLibrary * @param {import('../../objects/Library')} oldLibrary
* @param {string} query * @param {string} query
* @param {number} limit * @param {number} limit
* @param {number} offset * @param {number} offset
* @returns {{podcast:object[], tags:object[]}} * @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 // Search title, author, itunesId, itunesArtistId
const podcasts = await Database.podcastModel.findAll({ const podcasts = await Database.podcastModel.findAll({
where: { where: [
[Sequelize.Op.or]: [ {
{ [Sequelize.Op.or]: [
title: { {
[Sequelize.Op.substring]: query title: {
[Sequelize.Op.substring]: query
}
},
{
author: {
[Sequelize.Op.substring]: query
}
},
{
itunesId: {
[Sequelize.Op.substring]: query
}
},
{
itunesArtistId: {
[Sequelize.Op.substring]: query
}
} }
}, ]
{ },
author: { ...userPermissionPodcastWhere.podcastWhere
[Sequelize.Op.substring]: query ],
} replacements: userPermissionPodcastWhere.replacements,
},
{
itunesId: {
[Sequelize.Op.substring]: query
}
},
{
itunesArtistId: {
[Sequelize.Op.substring]: query
}
}
]
},
include: [ include: [
{ {
model: Database.libraryItemModel, model: Database.libraryItemModel,