mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-03 00:06:46 +01:00
PlaybackSession, Playlist, PlaylistMediaItem, Device data models
This commit is contained in:
parent
a1a923df94
commit
bed3758268
@ -62,6 +62,10 @@ class Database {
|
|||||||
require('./models/PodcastTag')(this.sequelize)
|
require('./models/PodcastTag')(this.sequelize)
|
||||||
require('./models/Collection')(this.sequelize)
|
require('./models/Collection')(this.sequelize)
|
||||||
require('./models/CollectionBook')(this.sequelize)
|
require('./models/CollectionBook')(this.sequelize)
|
||||||
|
require('./models/Playlist')(this.sequelize)
|
||||||
|
require('./models/PlaylistMediaItem')(this.sequelize)
|
||||||
|
require('./models/Device')(this.sequelize)
|
||||||
|
require('./models/PlaybackSession')(this.sequelize)
|
||||||
|
|
||||||
return this.sequelize.sync()
|
return this.sequelize.sync()
|
||||||
}
|
}
|
||||||
|
@ -52,20 +52,19 @@ module.exports = (sequelize) => {
|
|||||||
AudioBookmark.addHook('afterFind', findResult => {
|
AudioBookmark.addHook('afterFind', findResult => {
|
||||||
if (!Array.isArray(findResult)) findResult = [findResult]
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
||||||
for (const instance of findResult) {
|
for (const instance of findResult) {
|
||||||
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
|
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
|
||||||
instance.mediaItem = instance.book
|
instance.MediaItem = instance.Book
|
||||||
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
|
||||||
instance.mediaItem = instance.podcastEpisode
|
instance.MediaItem = instance.PodcastEpisode
|
||||||
}
|
}
|
||||||
// To prevent mistakes:
|
// To prevent mistakes:
|
||||||
delete instance.book
|
delete instance.Book
|
||||||
delete instance.dataValues.book
|
delete instance.dataValues.Book
|
||||||
delete instance.podcastEpisode
|
delete instance.PodcastEpisode
|
||||||
delete instance.dataValues.podcastEpisode
|
delete instance.dataValues.PodcastEpisode
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
User.hasMany(AudioBookmark)
|
User.hasMany(AudioBookmark)
|
||||||
AudioBookmark.belongsTo(User)
|
AudioBookmark.belongsTo(User)
|
||||||
|
|
||||||
|
@ -51,16 +51,16 @@ module.exports = (sequelize) => {
|
|||||||
AudioTrack.addHook('afterFind', findResult => {
|
AudioTrack.addHook('afterFind', findResult => {
|
||||||
if (!Array.isArray(findResult)) findResult = [findResult]
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
||||||
for (const instance of findResult) {
|
for (const instance of findResult) {
|
||||||
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
|
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
|
||||||
instance.mediaItem = instance.book
|
instance.MediaItem = instance.Book
|
||||||
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
|
||||||
instance.mediaItem = instance.podcastEpisode
|
instance.MediaItem = instance.PodcastEpisode
|
||||||
}
|
}
|
||||||
// To prevent mistakes:
|
// To prevent mistakes:
|
||||||
delete instance.book
|
delete instance.Book
|
||||||
delete instance.dataValues.book
|
delete instance.dataValues.Book
|
||||||
delete instance.podcastEpisode
|
delete instance.PodcastEpisode
|
||||||
delete instance.dataValues.podcastEpisode
|
delete instance.dataValues.PodcastEpisode
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
29
server/models/Device.js
Normal file
29
server/models/Device.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const { DataTypes, Model } = require('sequelize')
|
||||||
|
|
||||||
|
module.exports = (sequelize) => {
|
||||||
|
class Device extends Model { }
|
||||||
|
|
||||||
|
Device.init({
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
identifier: DataTypes.STRING,
|
||||||
|
clientName: DataTypes.STRING, // e.g. Abs Web, Abs Android
|
||||||
|
clientVersion: DataTypes.STRING,
|
||||||
|
ipAddress: DataTypes.STRING,
|
||||||
|
deviceName: DataTypes.STRING, // e.g. Windows 10 Chrome, Google Pixel 6, Apple iPhone 10,3
|
||||||
|
deviceVersion: DataTypes.STRING // e.g. Browser version or Android SDK
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'Device'
|
||||||
|
})
|
||||||
|
|
||||||
|
const { User } = sequelize.models
|
||||||
|
|
||||||
|
User.hasMany(Device)
|
||||||
|
Device.belongsTo(User)
|
||||||
|
|
||||||
|
return Device
|
||||||
|
}
|
@ -55,16 +55,16 @@ module.exports = (sequelize) => {
|
|||||||
MediaProgress.addHook('afterFind', findResult => {
|
MediaProgress.addHook('afterFind', findResult => {
|
||||||
if (!Array.isArray(findResult)) findResult = [findResult]
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
||||||
for (const instance of findResult) {
|
for (const instance of findResult) {
|
||||||
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
|
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
|
||||||
instance.mediaItem = instance.book
|
instance.MediaItem = instance.Book
|
||||||
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
|
||||||
instance.mediaItem = instance.podcastEpisode
|
instance.MediaItem = instance.PodcastEpisode
|
||||||
}
|
}
|
||||||
// To prevent mistakes:
|
// To prevent mistakes:
|
||||||
delete instance.book
|
delete instance.Book
|
||||||
delete instance.dataValues.book
|
delete instance.dataValues.Book
|
||||||
delete instance.podcastEpisode
|
delete instance.PodcastEpisode
|
||||||
delete instance.dataValues.podcastEpisode
|
delete instance.dataValues.PodcastEpisode
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
79
server/models/PlaybackSession.js
Normal file
79
server/models/PlaybackSession.js
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
const { DataTypes, Model } = require('sequelize')
|
||||||
|
|
||||||
|
const uppercaseFirst = str => `${str[0].toUpperCase()}${str.substr(1)}`
|
||||||
|
|
||||||
|
module.exports = (sequelize) => {
|
||||||
|
class PlaybackSession extends Model {
|
||||||
|
getMediaItem(options) {
|
||||||
|
if (!this.mediaItemType) return Promise.resolve(null)
|
||||||
|
const mixinMethodName = `get${uppercaseFirst(this.mediaItemType)}`
|
||||||
|
return this[mixinMethodName](options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PlaybackSession.init({
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
mediaItemId: DataTypes.UUIDV4,
|
||||||
|
mediaItemType: DataTypes.STRING,
|
||||||
|
displayTitle: DataTypes.STRING,
|
||||||
|
displayAuthor: DataTypes.STRING,
|
||||||
|
duration: DataTypes.INTEGER,
|
||||||
|
playMethod: DataTypes.STRING,
|
||||||
|
mediaPlayer: DataTypes.STRING,
|
||||||
|
startTime: DataTypes.INTEGER,
|
||||||
|
currentTime: DataTypes.INTEGER,
|
||||||
|
timeListening: DataTypes.INTEGER,
|
||||||
|
serverVersion: DataTypes.STRING
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'PlaybackSession'
|
||||||
|
})
|
||||||
|
|
||||||
|
const { Book, PodcastEpisode, User, Device } = sequelize.models
|
||||||
|
|
||||||
|
Book.hasMany(PlaybackSession, {
|
||||||
|
foreignKey: 'mediaItemId',
|
||||||
|
constraints: false,
|
||||||
|
scope: {
|
||||||
|
mediaItemType: 'book'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
PlaybackSession.belongsTo(Book, { foreignKey: 'mediaItemId', constraints: false })
|
||||||
|
|
||||||
|
PodcastEpisode.hasOne(PlaybackSession, {
|
||||||
|
foreignKey: 'mediaItemId',
|
||||||
|
constraints: false,
|
||||||
|
scope: {
|
||||||
|
mediaItemType: 'podcastEpisode'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
PlaybackSession.belongsTo(PodcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
|
||||||
|
|
||||||
|
PlaybackSession.addHook('afterFind', findResult => {
|
||||||
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
||||||
|
for (const instance of findResult) {
|
||||||
|
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
|
||||||
|
instance.MediaItem = instance.Book
|
||||||
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
|
||||||
|
instance.MediaItem = instance.PodcastEpisode
|
||||||
|
}
|
||||||
|
// To prevent mistakes:
|
||||||
|
delete instance.Book
|
||||||
|
delete instance.dataValues.Book
|
||||||
|
delete instance.PodcastEpisode
|
||||||
|
delete instance.dataValues.PodcastEpisode
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
User.hasMany(PlaybackSession)
|
||||||
|
PlaybackSession.belongsTo(User)
|
||||||
|
|
||||||
|
Device.hasMany(PlaybackSession)
|
||||||
|
PlaybackSession.belongsTo(Device)
|
||||||
|
|
||||||
|
return PlaybackSession
|
||||||
|
}
|
27
server/models/Playlist.js
Normal file
27
server/models/Playlist.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const { DataTypes, Model } = require('sequelize')
|
||||||
|
|
||||||
|
module.exports = (sequelize) => {
|
||||||
|
class Playlist extends Model { }
|
||||||
|
|
||||||
|
Playlist.init({
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
name: DataTypes.STRING,
|
||||||
|
description: DataTypes.TEXT
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'Playlist'
|
||||||
|
})
|
||||||
|
|
||||||
|
const { Library, User } = sequelize.models
|
||||||
|
Library.hasMany(Playlist)
|
||||||
|
Playlist.belongsTo(Library)
|
||||||
|
|
||||||
|
User.hasMany(Playlist)
|
||||||
|
Playlist.belongsTo(User)
|
||||||
|
|
||||||
|
return Playlist
|
||||||
|
}
|
67
server/models/PlaylistMediaItem.js
Normal file
67
server/models/PlaylistMediaItem.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
const { DataTypes, Model } = require('sequelize')
|
||||||
|
|
||||||
|
const uppercaseFirst = str => `${str[0].toUpperCase()}${str.substr(1)}`
|
||||||
|
|
||||||
|
module.exports = (sequelize) => {
|
||||||
|
class PlaylistMediaItem extends Model {
|
||||||
|
getMediaItem(options) {
|
||||||
|
if (!this.mediaItemType) return Promise.resolve(null)
|
||||||
|
const mixinMethodName = `get${uppercaseFirst(this.mediaItemType)}`
|
||||||
|
return this[mixinMethodName](options)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PlaylistMediaItem.init({
|
||||||
|
id: {
|
||||||
|
type: DataTypes.UUID,
|
||||||
|
defaultValue: DataTypes.UUIDV4,
|
||||||
|
primaryKey: true
|
||||||
|
},
|
||||||
|
mediaItemId: DataTypes.UUIDV4,
|
||||||
|
mediaItemType: DataTypes.STRING
|
||||||
|
}, {
|
||||||
|
sequelize,
|
||||||
|
modelName: 'PlaylistMediaItem'
|
||||||
|
})
|
||||||
|
|
||||||
|
const { Book, PodcastEpisode, Playlist } = sequelize.models
|
||||||
|
|
||||||
|
Book.hasMany(PlaylistMediaItem, {
|
||||||
|
foreignKey: 'mediaItemId',
|
||||||
|
constraints: false,
|
||||||
|
scope: {
|
||||||
|
mediaItemType: 'book'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
PlaylistMediaItem.belongsTo(Book, { foreignKey: 'mediaItemId', constraints: false })
|
||||||
|
|
||||||
|
PodcastEpisode.hasOne(PlaylistMediaItem, {
|
||||||
|
foreignKey: 'mediaItemId',
|
||||||
|
constraints: false,
|
||||||
|
scope: {
|
||||||
|
mediaItemType: 'podcastEpisode'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
PlaylistMediaItem.belongsTo(PodcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
|
||||||
|
|
||||||
|
PlaylistMediaItem.addHook('afterFind', findResult => {
|
||||||
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
||||||
|
for (const instance of findResult) {
|
||||||
|
if (instance.mediaItemType === 'book' && instance.Book !== undefined) {
|
||||||
|
instance.MediaItem = instance.Book
|
||||||
|
} else if (instance.mediaItemType === 'podcastEpisode' && instance.PodcastEpisode !== undefined) {
|
||||||
|
instance.MediaItem = instance.PodcastEpisode
|
||||||
|
}
|
||||||
|
// To prevent mistakes:
|
||||||
|
delete instance.Book
|
||||||
|
delete instance.dataValues.Book
|
||||||
|
delete instance.PodcastEpisode
|
||||||
|
delete instance.dataValues.PodcastEpisode
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Playlist.hasMany(PlaylistMediaItem)
|
||||||
|
PlaylistMediaItem.belongsTo(Playlist)
|
||||||
|
|
||||||
|
return PlaylistMediaItem
|
||||||
|
}
|
@ -10,6 +10,7 @@ module.exports = (sequelize) => {
|
|||||||
primaryKey: true
|
primaryKey: true
|
||||||
},
|
},
|
||||||
username: DataTypes.STRING,
|
username: DataTypes.STRING,
|
||||||
|
email: DataTypes.STRING,
|
||||||
pash: DataTypes.STRING,
|
pash: DataTypes.STRING,
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
token: DataTypes.STRING,
|
token: DataTypes.STRING,
|
||||||
|
Loading…
Reference in New Issue
Block a user