From 01fbea02f19bb19b5d2e8629fdbe4662d278e872 Mon Sep 17 00:00:00 2001 From: advplyr Date: Thu, 12 Sep 2024 16:36:39 -0500 Subject: [PATCH] Clean out old unused functions, Device updates for replacing DeviceInfo --- server/Database.js | 20 +--- server/managers/PlaybackSessionManager.js | 6 +- server/models/CustomMetadataProvider.js | 109 +++++++++++----------- server/models/Device.js | 81 ++++++++++------ server/objects/PlaybackSession.js | 10 +- server/objects/mediaTypes/Podcast.js | 9 -- server/objects/metadata/BookMetadata.js | 65 +++++-------- 7 files changed, 142 insertions(+), 158 deletions(-) diff --git a/server/Database.js b/server/Database.js index 289bef09..9eedfe1c 100644 --- a/server/Database.js +++ b/server/Database.js @@ -144,6 +144,11 @@ class Database { return this.models.mediaItemShare } + /** @type {typeof import('./models/Device')} */ + get deviceModel() { + return this.models.device + } + /** * Check if db file exists * @returns {boolean} @@ -489,21 +494,6 @@ class Database { return this.models.playbackSession.removeById(sessionId) } - getDeviceByDeviceId(deviceId) { - if (!this.sequelize) return false - return this.models.device.getOldDeviceByDeviceId(deviceId) - } - - updateDevice(oldDevice) { - if (!this.sequelize) return false - return this.models.device.updateFromOld(oldDevice) - } - - createDevice(oldDevice) { - if (!this.sequelize) return false - return this.models.device.createFromOld(oldDevice) - } - replaceTagInFilterData(oldTag, newTag) { for (const libraryId in this.libraryFilterData) { const indexOf = this.libraryFilterData[libraryId].tags.findIndex((n) => n === oldTag) diff --git a/server/managers/PlaybackSessionManager.js b/server/managers/PlaybackSessionManager.js index 14a60d18..4318841e 100644 --- a/server/managers/PlaybackSessionManager.js +++ b/server/managers/PlaybackSessionManager.js @@ -51,16 +51,16 @@ class PlaybackSessionManager { deviceInfo.setData(ip, ua, clientDeviceInfo, serverVersion, req.user?.id) if (clientDeviceInfo?.deviceId) { - const existingDevice = await Database.getDeviceByDeviceId(clientDeviceInfo.deviceId) + const existingDevice = await Database.deviceModel.getOldDeviceByDeviceId(clientDeviceInfo.deviceId) if (existingDevice) { if (existingDevice.update(deviceInfo)) { - await Database.updateDevice(existingDevice) + await Database.deviceModel.updateFromOld(existingDevice) } return existingDevice } } - await Database.createDevice(deviceInfo) + await Database.deviceModel.createFromOld(deviceInfo) return deviceInfo } diff --git a/server/models/CustomMetadataProvider.js b/server/models/CustomMetadataProvider.js index 8218e419..ca2e20a7 100644 --- a/server/models/CustomMetadataProvider.js +++ b/server/models/CustomMetadataProvider.js @@ -30,6 +30,61 @@ class CustomMetadataProvider extends Model { this.updatedAt } + /** + * Get providers for client by media type + * Currently only available for "book" media type + * + * @param {string} mediaType + * @returns {Promise} + */ + static async getForClientByMediaType(mediaType) { + if (mediaType !== 'book') return [] + const customMetadataProviders = await this.findAll({ + where: { + mediaType + } + }) + return customMetadataProviders.map((cmp) => cmp.toClientJson()) + } + + /** + * Check if provider exists by slug + * + * @param {string} providerSlug + * @returns {Promise} + */ + static async checkExistsBySlug(providerSlug) { + const providerId = providerSlug?.split?.('custom-')[1] + if (!providerId) return false + + return (await this.count({ where: { id: providerId } })) > 0 + } + + /** + * Initialize model + * @param {import('../Database').sequelize} sequelize + */ + static init(sequelize) { + super.init( + { + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + primaryKey: true + }, + name: DataTypes.STRING, + mediaType: DataTypes.STRING, + url: DataTypes.STRING, + authHeaderValue: DataTypes.STRING, + extraData: DataTypes.JSON + }, + { + sequelize, + modelName: 'customMetadataProvider' + } + ) + } + getSlug() { return `custom-${this.id}` } @@ -46,58 +101,6 @@ class CustomMetadataProvider extends Model { slug: this.getSlug() } } - - /** - * Get providers for client by media type - * Currently only available for "book" media type - * - * @param {string} mediaType - * @returns {Promise} - */ - static async getForClientByMediaType(mediaType) { - if (mediaType !== 'book') return [] - const customMetadataProviders = await this.findAll({ - where: { - mediaType - } - }) - return customMetadataProviders.map(cmp => cmp.toClientJson()) - } - - /** - * Check if provider exists by slug - * - * @param {string} providerSlug - * @returns {Promise} - */ - static async checkExistsBySlug(providerSlug) { - const providerId = providerSlug?.split?.('custom-')[1] - if (!providerId) return false - - return (await this.count({ where: { id: providerId } })) > 0 - } - - /** - * Initialize model - * @param {import('../Database').sequelize} sequelize - */ - static init(sequelize) { - super.init({ - id: { - type: DataTypes.UUID, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - name: DataTypes.STRING, - mediaType: DataTypes.STRING, - url: DataTypes.STRING, - authHeaderValue: DataTypes.STRING, - extraData: DataTypes.JSON - }, { - sequelize, - modelName: 'customMetadataProvider' - }) - } } -module.exports = CustomMetadataProvider \ No newline at end of file +module.exports = CustomMetadataProvider diff --git a/server/models/Device.js b/server/models/Device.js index 896967e4..8866d357 100644 --- a/server/models/Device.js +++ b/server/models/Device.js @@ -29,33 +29,6 @@ class Device extends Model { this.updatedAt } - getOldDevice() { - let browserVersion = null - let sdkVersion = null - if (this.clientName === 'Abs Android') { - sdkVersion = this.deviceVersion || null - } else { - browserVersion = this.deviceVersion || null - } - - return new oldDevice({ - id: this.id, - deviceId: this.deviceId, - userId: this.userId, - ipAddress: this.ipAddress, - browserName: this.extraData.browserName || null, - browserVersion, - osName: this.extraData.osName || null, - osVersion: this.extraData.osVersion || null, - clientVersion: this.clientVersion || null, - manufacturer: this.extraData.manufacturer || null, - model: this.extraData.model || null, - sdkVersion, - deviceName: this.deviceName, - clientName: this.clientName - }) - } - static async getOldDeviceByDeviceId(deviceId) { const device = await this.findOne({ where: { @@ -145,6 +118,60 @@ class Device extends Model { }) Device.belongsTo(user) } + + toOldJSON() { + let browserVersion = null + let sdkVersion = null + if (this.clientName === 'Abs Android') { + sdkVersion = this.deviceVersion || null + } else { + browserVersion = this.deviceVersion || null + } + + return { + id: this.id, + deviceId: this.deviceId, + userId: this.userId, + ipAddress: this.ipAddress, + browserName: this.extraData.browserName || null, + browserVersion, + osName: this.extraData.osName || null, + osVersion: this.extraData.osVersion || null, + clientVersion: this.clientVersion || null, + manufacturer: this.extraData.manufacturer || null, + model: this.extraData.model || null, + sdkVersion, + deviceName: this.deviceName, + clientName: this.clientName + } + } + + getOldDevice() { + let browserVersion = null + let sdkVersion = null + if (this.clientName === 'Abs Android') { + sdkVersion = this.deviceVersion || null + } else { + browserVersion = this.deviceVersion || null + } + + return new oldDevice({ + id: this.id, + deviceId: this.deviceId, + userId: this.userId, + ipAddress: this.ipAddress, + browserName: this.extraData.browserName || null, + browserVersion, + osName: this.extraData.osName || null, + osVersion: this.extraData.osVersion || null, + clientVersion: this.clientVersion || null, + manufacturer: this.extraData.manufacturer || null, + model: this.extraData.model || null, + sdkVersion, + deviceName: this.deviceName, + clientName: this.clientName + }) + } } module.exports = Device diff --git a/server/objects/PlaybackSession.js b/server/objects/PlaybackSession.js index a294b72c..6950a544 100644 --- a/server/objects/PlaybackSession.js +++ b/server/objects/PlaybackSession.js @@ -82,8 +82,8 @@ class PlaybackSession { /** * Session data to send to clients - * @param {[oldLibraryItem]} libraryItem optional - * @returns {object} + * @param {Object} [libraryItem] - old library item + * @returns */ toJSONForClient(libraryItem) { return { @@ -255,11 +255,5 @@ class PlaybackSession { this.timeListening += Number.parseFloat(timeListened) this.updatedAt = Date.now() } - - // New date since start of listening session - checkDateRollover() { - if (!this.date) return false - return date.format(new Date(), 'YYYY-MM-DD') !== this.date - } } module.exports = PlaybackSession diff --git a/server/objects/mediaTypes/Podcast.js b/server/objects/mediaTypes/Podcast.js index bca741a2..c7d91d0d 100644 --- a/server/objects/mediaTypes/Podcast.js +++ b/server/objects/mediaTypes/Podcast.js @@ -233,15 +233,6 @@ class Podcast { this.episodes.push(podcastEpisode) } - addNewEpisodeFromAudioFile(audioFile, index) { - const pe = new PodcastEpisode() - pe.libraryItemId = this.libraryItemId - pe.podcastId = this.id - audioFile.index = 1 // Only 1 audio file per episode - pe.setDataFromAudioFile(audioFile, index) - this.episodes.push(pe) - } - removeEpisode(episodeId) { const episode = this.episodes.find((ep) => ep.id === episodeId) if (episode) { diff --git a/server/objects/metadata/BookMetadata.js b/server/objects/metadata/BookMetadata.js index 490b9949..6d3dae43 100644 --- a/server/objects/metadata/BookMetadata.js +++ b/server/objects/metadata/BookMetadata.js @@ -6,7 +6,7 @@ class BookMetadata { this.title = null this.subtitle = null this.authors = [] - this.narrators = [] // Array of strings + this.narrators = [] // Array of strings this.series = [] this.genres = [] // Array of strings this.publishedYear = null @@ -27,9 +27,9 @@ class BookMetadata { construct(metadata) { this.title = metadata.title this.subtitle = metadata.subtitle - this.authors = (metadata.authors?.map) ? metadata.authors.map(a => ({ ...a })) : [] - this.narrators = metadata.narrators ? [...metadata.narrators].filter(n => n) : [] - this.series = (metadata.series?.map) ? metadata.series.map(s => ({ ...s })) : [] + this.authors = metadata.authors?.map ? metadata.authors.map((a) => ({ ...a })) : [] + this.narrators = metadata.narrators ? [...metadata.narrators].filter((n) => n) : [] + this.series = metadata.series?.map ? metadata.series.map((s) => ({ ...s })) : [] this.genres = metadata.genres ? [...metadata.genres] : [] this.publishedYear = metadata.publishedYear || null this.publishedDate = metadata.publishedDate || null @@ -46,9 +46,9 @@ class BookMetadata { return { title: this.title, subtitle: this.subtitle, - authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id + authors: this.authors.map((a) => ({ ...a })), // Author JSONMinimal with name and id narrators: [...this.narrators], - series: this.series.map(s => ({ ...s })), // Series JSONMinimal with name, id and sequence + series: this.series.map((s) => ({ ...s })), // Series JSONMinimal with name, id and sequence genres: [...this.genres], publishedYear: this.publishedYear, publishedDate: this.publishedDate, @@ -89,9 +89,9 @@ class BookMetadata { title: this.title, titleIgnorePrefix: this.titlePrefixAtEnd, subtitle: this.subtitle, - authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id + authors: this.authors.map((a) => ({ ...a })), // Author JSONMinimal with name and id narrators: [...this.narrators], - series: this.series.map(s => ({ ...s })), + series: this.series.map((s) => ({ ...s })), genres: [...this.genres], publishedYear: this.publishedYear, publishedDate: this.publishedDate, @@ -111,8 +111,8 @@ class BookMetadata { toJSONForMetadataFile() { const json = this.toJSON() - json.authors = json.authors.map(au => au.name) - json.series = json.series.map(se => { + json.authors = json.authors.map((au) => au.name) + json.series = json.series.map((se) => { if (!se.sequence) return se.name return `${se.name} #${se.sequence}` }) @@ -131,36 +131,31 @@ class BookMetadata { } get authorName() { if (!this.authors.length) return '' - return this.authors.map(au => au.name).join(', ') + return this.authors.map((au) => au.name).join(', ') } - get authorNameLF() { // Last, First + get authorNameLF() { + // Last, First if (!this.authors.length) return '' - return this.authors.map(au => parseNameString.nameToLastFirst(au.name)).join(', ') + return this.authors.map((au) => parseNameString.nameToLastFirst(au.name)).join(', ') } get seriesName() { if (!this.series.length) return '' - return this.series.map(se => { - if (!se.sequence) return se.name - return `${se.name} #${se.sequence}` - }).join(', ') - } - get firstSeriesName() { - if (!this.series.length) return '' - return this.series[0].name - } - get firstSeriesSequence() { - if (!this.series.length) return '' - return this.series[0].sequence + return this.series + .map((se) => { + if (!se.sequence) return se.name + return `${se.name} #${se.sequence}` + }) + .join(', ') } get narratorName() { return this.narrators.join(', ') } getSeries(seriesId) { - return this.series.find(se => se.id == seriesId) + return this.series.find((se) => se.id == seriesId) } getSeriesSequence(seriesId) { - const series = this.series.find(se => se.id == seriesId) + const series = this.series.find((se) => se.id == seriesId) if (!series) return null return series.sequence || '' } @@ -180,21 +175,5 @@ class BookMetadata { } return hasUpdates } - - // Updates author name - updateAuthor(updatedAuthor) { - const author = this.authors.find(au => au.id === updatedAuthor.id) - if (!author || author.name == updatedAuthor.name) return false - author.name = updatedAuthor.name - return true - } - - replaceAuthor(oldAuthor, newAuthor) { - this.authors = this.authors.filter(au => au.id !== oldAuthor.id) // Remove old author - this.authors.push({ - id: newAuthor.id, - name: newAuthor.name - }) - } } module.exports = BookMetadata