Skip to content

Commit

Permalink
Remove old library, folder and librarysettings model
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Aug 28, 2024
1 parent fd827b2 commit c45c823
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 405 deletions.
2 changes: 1 addition & 1 deletion server/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FolderWatcher extends EventEmitter {
constructor() {
super()

/** @type {{id:string, name:string, folders:import('./objects/Folder')[], paths:string[], watcher:Watcher[]}[]} */
/** @type {{id:string, name:string, libraryFolders:import('./models/Folder')[], paths:string[], watcher:Watcher[]}[]} */
this.libraryWatchers = []
/** @type {PendingFileUpdate[]} */
this.pendingFileUpdates = []
Expand Down
20 changes: 8 additions & 12 deletions server/controllers/LibraryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ class LibraryController {
* @param {Response} res
*/
async findAll(req, res) {
const libraries = await Database.libraryModel.getAllOldLibraries()
const libraries = await Database.libraryModel.getAllWithFolders()

const librariesAccessible = req.user.permissions?.librariesAccessible || []
if (librariesAccessible.length) {
return res.json({
libraries: libraries.filter((lib) => librariesAccessible.includes(lib.id)).map((lib) => lib.toJSON())
libraries: libraries.filter((lib) => librariesAccessible.includes(lib.id)).map((lib) => lib.toOldJSON())
})
}

res.json({
libraries: libraries.map((lib) => lib.toJSON())
libraries: libraries.map((lib) => lib.toOldJSON())
})
}

Expand All @@ -198,16 +198,15 @@ class LibraryController {
const filterdata = await libraryFilters.getFilterData(req.library.mediaType, req.library.id)
const customMetadataProviders = await Database.customMetadataProviderModel.getForClientByMediaType(req.library.mediaType)

const oldLibrary = Database.libraryModel.getOldLibrary(req.library)
return res.json({
filterdata,
issues: filterdata.numIssues,
numUserPlaylists: await Database.playlistModel.getNumPlaylistsForUserAndLibrary(req.user.id, req.library.id),
customMetadataProviders,
library: oldLibrary
library: req.library.toOldJSON()
})
}
res.json(oldLibrary)
res.json(req.library.toOldJSON())
}

/**
Expand Down Expand Up @@ -1051,9 +1050,7 @@ class LibraryController {
Logger.error(`[LibraryController] Non-root user "${req.user.username}" attempted to match library items`)
return res.sendStatus(403)
}
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)
Scanner.matchLibraryItems(oldLibrary)
Scanner.matchLibraryItems(req.library)
res.sendStatus(200)
}

Expand All @@ -1071,10 +1068,9 @@ class LibraryController {
return res.sendStatus(403)
}
res.sendStatus(200)
// TODO: Update to new library model
const oldLibrary = Database.libraryModel.getOldLibrary(req.library)

const forceRescan = req.query.force === '1'
await LibraryScanner.scan(oldLibrary, forceRescan)
await LibraryScanner.scan(req.library, forceRescan)

await Database.resetLibraryIssuesFilterData(req.library.id)
Logger.info('[LibraryController] Scan complete')
Expand Down
8 changes: 4 additions & 4 deletions server/managers/CronManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class CronManager {
startCronForLibrary(_library) {
Logger.debug(`[CronManager] Init library scan cron for ${_library.name} on schedule ${_library.settings.autoScanCronExpression}`)
const libScanCron = cron.schedule(_library.settings.autoScanCronExpression, async () => {
const oldLibrary = await Database.libraryModel.getOldById(_library.id)
if (!oldLibrary) {
const library = await Database.libraryModel.findByIdWithFolders(_library.id)
if (!library) {
Logger.error(`[CronManager] Library not found for scan cron ${_library.id}`)
} else {
Logger.debug(`[CronManager] Library scan cron executing for ${oldLibrary.name}`)
LibraryScanner.scan(oldLibrary)
Logger.debug(`[CronManager] Library scan cron executing for ${library.name}`)
LibraryScanner.scan(library)
}
})
this.libraryScanCrons.push({
Expand Down
129 changes: 6 additions & 123 deletions server/models/Library.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { DataTypes, Model } = require('sequelize')
const Logger = require('../Logger')
const oldLibrary = require('../objects/Library')

/**
* @typedef LibrarySettingsObject
Expand Down Expand Up @@ -98,114 +97,6 @@ class Library extends Model {
})
}

/**
* Get all old libraries
* @returns {Promise<oldLibrary[]>}
*/
static async getAllOldLibraries() {
const libraries = await this.findAll({
include: this.sequelize.models.libraryFolder,
order: [['displayOrder', 'ASC']]
})
return libraries.map((lib) => this.getOldLibrary(lib))
}

/**
* Convert expanded Library to oldLibrary
* @param {Library} libraryExpanded
* @returns {oldLibrary}
*/
static getOldLibrary(libraryExpanded) {
const folders = libraryExpanded.libraryFolders.map((folder) => {
return {
id: folder.id,
fullPath: folder.path,
libraryId: folder.libraryId,
addedAt: folder.createdAt.valueOf()
}
})
return new oldLibrary({
id: libraryExpanded.id,
oldLibraryId: libraryExpanded.extraData?.oldLibraryId || null,
name: libraryExpanded.name,
folders,
displayOrder: libraryExpanded.displayOrder,
icon: libraryExpanded.icon,
mediaType: libraryExpanded.mediaType,
provider: libraryExpanded.provider,
settings: libraryExpanded.settings,
lastScan: libraryExpanded.lastScan?.valueOf() || null,
lastScanVersion: libraryExpanded.lastScanVersion || null,
lastScanMetadataPrecedence: libraryExpanded.extraData?.lastScanMetadataPrecedence || null,
createdAt: libraryExpanded.createdAt.valueOf(),
lastUpdate: libraryExpanded.updatedAt.valueOf()
})
}

/**
* Update library and library folders
* @param {object} oldLibrary
* @returns {Promise<Library|null>}
*/
static async updateFromOld(oldLibrary) {
const existingLibrary = await this.findByPk(oldLibrary.id, {
include: this.sequelize.models.libraryFolder
})
if (!existingLibrary) {
Logger.error(`[Library] Failed to update library ${oldLibrary.id} - not found`)
return null
}

const library = this.getFromOld(oldLibrary)

const libraryFolders = oldLibrary.folders.map((folder) => {
return {
id: folder.id,
path: folder.fullPath,
libraryId: library.id
}
})
for (const libraryFolder of libraryFolders) {
const existingLibraryFolder = existingLibrary.libraryFolders.find((lf) => lf.id === libraryFolder.id)
if (!existingLibraryFolder) {
await this.sequelize.models.libraryFolder.create(libraryFolder)
} else if (existingLibraryFolder.path !== libraryFolder.path) {
await existingLibraryFolder.update({ path: libraryFolder.path })
}
}

const libraryFoldersRemoved = existingLibrary.libraryFolders.filter((lf) => !libraryFolders.some((_lf) => _lf.id === lf.id))
for (const existingLibraryFolder of libraryFoldersRemoved) {
await existingLibraryFolder.destroy()
}

return existingLibrary.update(library)
}

static getFromOld(oldLibrary) {
const extraData = {}
if (oldLibrary.oldLibraryId) {
extraData.oldLibraryId = oldLibrary.oldLibraryId
}
if (oldLibrary.lastScanMetadataPrecedence) {
extraData.lastScanMetadataPrecedence = oldLibrary.lastScanMetadataPrecedence
}
return {
id: oldLibrary.id,
name: oldLibrary.name,
displayOrder: oldLibrary.displayOrder,
icon: oldLibrary.icon || null,
mediaType: oldLibrary.mediaType || null,
provider: oldLibrary.provider,
settings: oldLibrary.settings?.toJSON() || {},
lastScan: oldLibrary.lastScan || null,
lastScanVersion: oldLibrary.lastScanVersion || null,
createdAt: oldLibrary.createdAt,
updatedAt: oldLibrary.lastUpdate,
extraData
}
}

/**
* Destroy library by id
* @param {string} libraryId
Expand All @@ -231,20 +122,6 @@ class Library extends Model {
return libraries.map((l) => l.id)
}

/**
* Find Library by primary key & return oldLibrary
* @param {string} libraryId
* @returns {Promise<oldLibrary|null>} Returns null if not found
*/
static async getOldById(libraryId) {
if (!libraryId) return null
const library = await this.findByPk(libraryId, {
include: this.sequelize.models.libraryFolder
})
if (!library) return null
return this.getOldLibrary(library)
}

/**
* Get the largest value in the displayOrder column
* Used for setting a new libraries display order
Expand Down Expand Up @@ -308,6 +185,12 @@ class Library extends Model {
get isBook() {
return this.mediaType === 'book'
}
/**
* @returns {string[]}
*/
get lastScanMetadataPrecedence() {
return this.extraData?.lastScanMetadataPrecedence || []
}

/**
* TODO: Update to use new model
Expand Down
38 changes: 0 additions & 38 deletions server/objects/Folder.js

This file was deleted.

95 changes: 0 additions & 95 deletions server/objects/Library.js

This file was deleted.

Loading

0 comments on commit c45c823

Please sign in to comment.