Clean out old unused functions, Device updates for replacing DeviceInfo

This commit is contained in:
advplyr 2024-09-12 16:36:39 -05:00
parent 3d9af89e24
commit 01fbea02f1
7 changed files with 142 additions and 158 deletions

View File

@ -144,6 +144,11 @@ class Database {
return this.models.mediaItemShare return this.models.mediaItemShare
} }
/** @type {typeof import('./models/Device')} */
get deviceModel() {
return this.models.device
}
/** /**
* Check if db file exists * Check if db file exists
* @returns {boolean} * @returns {boolean}
@ -489,21 +494,6 @@ class Database {
return this.models.playbackSession.removeById(sessionId) 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) { replaceTagInFilterData(oldTag, newTag) {
for (const libraryId in this.libraryFilterData) { for (const libraryId in this.libraryFilterData) {
const indexOf = this.libraryFilterData[libraryId].tags.findIndex((n) => n === oldTag) const indexOf = this.libraryFilterData[libraryId].tags.findIndex((n) => n === oldTag)

View File

@ -51,16 +51,16 @@ class PlaybackSessionManager {
deviceInfo.setData(ip, ua, clientDeviceInfo, serverVersion, req.user?.id) deviceInfo.setData(ip, ua, clientDeviceInfo, serverVersion, req.user?.id)
if (clientDeviceInfo?.deviceId) { if (clientDeviceInfo?.deviceId) {
const existingDevice = await Database.getDeviceByDeviceId(clientDeviceInfo.deviceId) const existingDevice = await Database.deviceModel.getOldDeviceByDeviceId(clientDeviceInfo.deviceId)
if (existingDevice) { if (existingDevice) {
if (existingDevice.update(deviceInfo)) { if (existingDevice.update(deviceInfo)) {
await Database.updateDevice(existingDevice) await Database.deviceModel.updateFromOld(existingDevice)
} }
return existingDevice return existingDevice
} }
} }
await Database.createDevice(deviceInfo) await Database.deviceModel.createFromOld(deviceInfo)
return deviceInfo return deviceInfo
} }

View File

@ -30,23 +30,6 @@ class CustomMetadataProvider extends Model {
this.updatedAt this.updatedAt
} }
getSlug() {
return `custom-${this.id}`
}
/**
* Safe for clients
* @returns {ClientCustomMetadataProvider}
*/
toClientJson() {
return {
id: this.id,
name: this.name,
mediaType: this.mediaType,
slug: this.getSlug()
}
}
/** /**
* Get providers for client by media type * Get providers for client by media type
* Currently only available for "book" media type * Currently only available for "book" media type
@ -61,7 +44,7 @@ class CustomMetadataProvider extends Model {
mediaType mediaType
} }
}) })
return customMetadataProviders.map(cmp => cmp.toClientJson()) return customMetadataProviders.map((cmp) => cmp.toClientJson())
} }
/** /**
@ -82,7 +65,8 @@ class CustomMetadataProvider extends Model {
* @param {import('../Database').sequelize} sequelize * @param {import('../Database').sequelize} sequelize
*/ */
static init(sequelize) { static init(sequelize) {
super.init({ super.init(
{
id: { id: {
type: DataTypes.UUID, type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4, defaultValue: DataTypes.UUIDV4,
@ -93,10 +77,29 @@ class CustomMetadataProvider extends Model {
url: DataTypes.STRING, url: DataTypes.STRING,
authHeaderValue: DataTypes.STRING, authHeaderValue: DataTypes.STRING,
extraData: DataTypes.JSON extraData: DataTypes.JSON
}, { },
{
sequelize, sequelize,
modelName: 'customMetadataProvider' modelName: 'customMetadataProvider'
}) }
)
}
getSlug() {
return `custom-${this.id}`
}
/**
* Safe for clients
* @returns {ClientCustomMetadataProvider}
*/
toClientJson() {
return {
id: this.id,
name: this.name,
mediaType: this.mediaType,
slug: this.getSlug()
}
} }
} }

View File

@ -29,33 +29,6 @@ class Device extends Model {
this.updatedAt 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) { static async getOldDeviceByDeviceId(deviceId) {
const device = await this.findOne({ const device = await this.findOne({
where: { where: {
@ -145,6 +118,60 @@ class Device extends Model {
}) })
Device.belongsTo(user) 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 module.exports = Device

View File

@ -82,8 +82,8 @@ class PlaybackSession {
/** /**
* Session data to send to clients * Session data to send to clients
* @param {[oldLibraryItem]} libraryItem optional * @param {Object} [libraryItem] - old library item
* @returns {object} * @returns
*/ */
toJSONForClient(libraryItem) { toJSONForClient(libraryItem) {
return { return {
@ -255,11 +255,5 @@ class PlaybackSession {
this.timeListening += Number.parseFloat(timeListened) this.timeListening += Number.parseFloat(timeListened)
this.updatedAt = Date.now() 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 module.exports = PlaybackSession

View File

@ -233,15 +233,6 @@ class Podcast {
this.episodes.push(podcastEpisode) 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) { removeEpisode(episodeId) {
const episode = this.episodes.find((ep) => ep.id === episodeId) const episode = this.episodes.find((ep) => ep.id === episodeId)
if (episode) { if (episode) {

View File

@ -27,9 +27,9 @@ class BookMetadata {
construct(metadata) { construct(metadata) {
this.title = metadata.title this.title = metadata.title
this.subtitle = metadata.subtitle this.subtitle = metadata.subtitle
this.authors = (metadata.authors?.map) ? metadata.authors.map(a => ({ ...a })) : [] this.authors = metadata.authors?.map ? metadata.authors.map((a) => ({ ...a })) : []
this.narrators = metadata.narrators ? [...metadata.narrators].filter(n => n) : [] this.narrators = metadata.narrators ? [...metadata.narrators].filter((n) => n) : []
this.series = (metadata.series?.map) ? metadata.series.map(s => ({ ...s })) : [] this.series = metadata.series?.map ? metadata.series.map((s) => ({ ...s })) : []
this.genres = metadata.genres ? [...metadata.genres] : [] this.genres = metadata.genres ? [...metadata.genres] : []
this.publishedYear = metadata.publishedYear || null this.publishedYear = metadata.publishedYear || null
this.publishedDate = metadata.publishedDate || null this.publishedDate = metadata.publishedDate || null
@ -46,9 +46,9 @@ class BookMetadata {
return { return {
title: this.title, title: this.title,
subtitle: this.subtitle, 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], 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], genres: [...this.genres],
publishedYear: this.publishedYear, publishedYear: this.publishedYear,
publishedDate: this.publishedDate, publishedDate: this.publishedDate,
@ -89,9 +89,9 @@ class BookMetadata {
title: this.title, title: this.title,
titleIgnorePrefix: this.titlePrefixAtEnd, titleIgnorePrefix: this.titlePrefixAtEnd,
subtitle: this.subtitle, 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], narrators: [...this.narrators],
series: this.series.map(s => ({ ...s })), series: this.series.map((s) => ({ ...s })),
genres: [...this.genres], genres: [...this.genres],
publishedYear: this.publishedYear, publishedYear: this.publishedYear,
publishedDate: this.publishedDate, publishedDate: this.publishedDate,
@ -111,8 +111,8 @@ class BookMetadata {
toJSONForMetadataFile() { toJSONForMetadataFile() {
const json = this.toJSON() const json = this.toJSON()
json.authors = json.authors.map(au => au.name) json.authors = json.authors.map((au) => au.name)
json.series = json.series.map(se => { json.series = json.series.map((se) => {
if (!se.sequence) return se.name if (!se.sequence) return se.name
return `${se.name} #${se.sequence}` return `${se.name} #${se.sequence}`
}) })
@ -131,36 +131,31 @@ class BookMetadata {
} }
get authorName() { get authorName() {
if (!this.authors.length) return '' 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 '' 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() { get seriesName() {
if (!this.series.length) return '' if (!this.series.length) return ''
return this.series.map(se => { return this.series
.map((se) => {
if (!se.sequence) return se.name if (!se.sequence) return se.name
return `${se.name} #${se.sequence}` return `${se.name} #${se.sequence}`
}).join(', ') })
} .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
} }
get narratorName() { get narratorName() {
return this.narrators.join(', ') return this.narrators.join(', ')
} }
getSeries(seriesId) { getSeries(seriesId) {
return this.series.find(se => se.id == seriesId) return this.series.find((se) => se.id == seriesId)
} }
getSeriesSequence(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 if (!series) return null
return series.sequence || '' return series.sequence || ''
} }
@ -180,21 +175,5 @@ class BookMetadata {
} }
return hasUpdates 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 module.exports = BookMetadata