Skip to content

Commit

Permalink
Updates to migrate off of old library model
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Aug 24, 2024
1 parent 5d13fae commit 159ccd8
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 133 deletions.
20 changes: 6 additions & 14 deletions server/controllers/LibraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,13 @@ class LibraryController {
payload.offset = payload.page * payload.limit

// TODO: Temporary way of handling collapse sub-series. Either remove feature or handle through sql queries
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)
const filterByGroup = payload.filterBy?.split('.').shift()
const filterByValue = filterByGroup ? libraryFilters.decode(payload.filterBy.replace(`${filterByGroup}.`, '')) : null
if (filterByGroup === 'series' && filterByValue !== 'no-series' && payload.collapseseries) {
const seriesId = libraryFilters.decode(payload.filterBy.split('.')[1])
payload.results = await libraryHelpers.handleCollapseSubseries(payload, seriesId, req.user, oldLibrary)
payload.results = await libraryHelpers.handleCollapseSubseries(payload, seriesId, req.user, req.library)
} else {
const { libraryItems, count } = await Database.libraryItemModel.getByFilterAndSort(oldLibrary, req.user, payload)
const { libraryItems, count } = await Database.libraryItemModel.getByFilterAndSort(req.library, req.user, payload)
payload.results = libraryItems
payload.total = count
}
Expand Down Expand Up @@ -589,9 +587,6 @@ class LibraryController {
* @param {Response} res
*/
async getAllSeriesForLibrary(req, res) {
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)

const include = (req.query.include || '')
.split(',')
.map((v) => v.trim().toLowerCase())
Expand All @@ -610,7 +605,7 @@ class LibraryController {
}

const offset = payload.page * payload.limit
const { series, count } = await seriesFilters.getFilteredSeries(oldLibrary, req.user, payload.filterBy, payload.sortBy, payload.sortDesc, include, payload.limit, offset)
const { series, count } = await seriesFilters.getFilteredSeries(req.library, req.user, payload.filterBy, payload.sortBy, payload.sortDesc, include, payload.limit, offset)

payload.total = count
payload.results = series
Expand Down Expand Up @@ -741,14 +736,12 @@ class LibraryController {
* @param {Response} res
*/
async getUserPersonalizedShelves(req, res) {
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)
const limitPerShelf = req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) || 10 : 10
const include = (req.query.include || '')
.split(',')
.map((v) => v.trim().toLowerCase())
.filter((v) => !!v)
const shelves = await Database.libraryItemModel.getPersonalizedShelves(oldLibrary, req.user, include, limitPerShelf)
const shelves = await Database.libraryItemModel.getPersonalizedShelves(req.library, req.user, include, limitPerShelf)
res.json(shelves)
}

Expand Down Expand Up @@ -1098,16 +1091,15 @@ class LibraryController {
if (req.library.mediaType !== 'podcast') {
return res.sendStatus(404)
}
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)

const payload = {
episodes: [],
limit: req.query.limit && !isNaN(req.query.limit) ? Number(req.query.limit) : 0,
page: req.query.page && !isNaN(req.query.page) ? Number(req.query.page) : 0
}

const offset = payload.page * payload.limit
payload.episodes = await libraryItemsPodcastFilters.getRecentEpisodes(req.user, oldLibrary, payload.limit, offset)
payload.episodes = await libraryItemsPodcastFilters.getRecentEpisodes(req.user, req.library, payload.limit, offset)
res.json(payload)
}

Expand Down
6 changes: 3 additions & 3 deletions server/controllers/MiscController.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class MiscController {
const files = Object.values(req.files)
const { title, author, series, folder: folderId, library: libraryId } = req.body

const library = await Database.libraryModel.getOldById(libraryId)
const library = await Database.libraryModel.findByPk(libraryId)
if (!library) {
return res.status(404).send(`Library not found with id ${libraryId}`)
}
const folder = library.folders.find((fold) => fold.id === folderId)
const folder = library.libraryFolders.find((fold) => fold.id === folderId)
if (!folder) {
return res.status(404).send(`Folder not found with id ${folderId} in library ${library.name}`)
}
Expand All @@ -63,7 +63,7 @@ class MiscController {
// before sanitizing all the directory parts to remove illegal chars and finally prepending
// the base folder path
const cleanedOutputDirectoryParts = outputDirectoryParts.filter(Boolean).map((part) => sanitizeFilename(part))
const outputDirectory = Path.join(...[folder.fullPath, ...cleanedOutputDirectoryParts])
const outputDirectory = Path.join(...[folder.path, ...cleanedOutputDirectoryParts])

await fs.ensureDir(outputDirectory)

Expand Down
4 changes: 2 additions & 2 deletions server/controllers/PodcastController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class PodcastController {
}
const payload = req.body

const library = await Database.libraryModel.getOldById(payload.libraryId)
const library = await Database.libraryModel.findByPk(payload.libraryId)
if (!library) {
Logger.error(`[PodcastController] Create: Library not found "${payload.libraryId}"`)
return res.status(404).send('Library not found')
}

const folder = library.folders.find((fold) => fold.id === payload.folderId)
const folder = library.libraryFolders.find((fold) => fold.id === payload.folderId)
if (!folder) {
Logger.error(`[PodcastController] Create: Folder not found "${payload.folderId}"`)
return res.status(404).send('Folder not found')
Expand Down
2 changes: 1 addition & 1 deletion server/managers/NotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NotificationManager {
}

Logger.debug(`[NotificationManager] onPodcastEpisodeDownloaded: Episode "${episode.title}" for podcast ${libraryItem.media.metadata.title}`)
const library = await Database.libraryModel.getOldById(libraryItem.libraryId)
const library = await Database.libraryModel.findByPk(libraryItem.libraryId)
const eventData = {
libraryItemId: libraryItem.id,
libraryId: libraryItem.libraryId,
Expand Down
8 changes: 3 additions & 5 deletions server/models/LibraryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const LibraryFile = require('../objects/files/LibraryFile')
const Book = require('./Book')
const Podcast = require('./Podcast')

const ShareManager = require('../managers/ShareManager')

/**
* @typedef LibraryFileObject
* @property {string} ino
Expand Down Expand Up @@ -559,14 +557,14 @@ class LibraryItem extends Model {

/**
* Get library items using filter and sort
* @param {oldLibrary} library
* @param {import('./Library')} library
* @param {import('./User')} user
* @param {object} options
* @returns {{ libraryItems:oldLibraryItem[], count:number }}
*/
static async getByFilterAndSort(library, user, options) {
let start = Date.now()
const { libraryItems, count } = await libraryFilters.getFilteredLibraryItems(library, user, options)
const { libraryItems, count } = await libraryFilters.getFilteredLibraryItems(library.id, user, options)
Logger.debug(`Loaded ${libraryItems.length} of ${count} items for libary page in ${((Date.now() - start) / 1000).toFixed(2)}s`)

return {
Expand Down Expand Up @@ -602,7 +600,7 @@ class LibraryItem extends Model {

/**
* Get home page data personalized shelves
* @param {oldLibrary} library
* @param {import('./Library')} library
* @param {import('./User')} user
* @param {string[]} include
* @param {number} limit
Expand Down
Loading

0 comments on commit 159ccd8

Please sign in to comment.