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 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)
}

View File

@ -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)
}
}
}

View File

@ -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
}

View File

@ -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,