audiobookshelf/server/models/MediaProgress.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-03-12 20:51:45 +01:00
const { DataTypes, Model } = require('sequelize')
/*
* Polymorphic association: https://sequelize.org/docs/v6/advanced-association-concepts/polymorphic-associations/
* Book has many MediaProgress. PodcastEpisode has many MediaProgress.
*/
module.exports = (sequelize) => {
class MediaProgress extends Model {
getMediaItem(options) {
if (!this.mediaItemType) return Promise.resolve(null)
2023-03-19 21:19:22 +01:00
const mixinMethodName = `get${sequelize.uppercaseFirst(this.mediaItemType)}`
2023-03-12 20:51:45 +01:00
return this[mixinMethodName](options)
}
}
MediaProgress.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
2023-03-19 21:19:22 +01:00
mediaItemId: DataTypes.UUIDV4,
2023-03-12 20:51:45 +01:00
mediaItemType: DataTypes.STRING,
2023-03-18 22:56:57 +01:00
duration: DataTypes.FLOAT,
currentTime: DataTypes.FLOAT,
2023-03-12 20:51:45 +01:00
isFinished: DataTypes.BOOLEAN,
hideFromContinueListening: DataTypes.BOOLEAN,
finishedAt: DataTypes.DATE
}, {
sequelize,
2023-03-19 21:19:22 +01:00
modelName: 'mediaProgress'
2023-03-12 20:51:45 +01:00
})
2023-03-19 21:19:22 +01:00
const { book, podcastEpisode, user } = sequelize.models
book.hasMany(MediaProgress, {
foreignKey: 'mediaItemId',
2023-03-12 20:51:45 +01:00
constraints: false,
scope: {
2023-03-19 21:19:22 +01:00
mediaItemType: 'book'
2023-03-12 20:51:45 +01:00
}
})
2023-03-19 21:19:22 +01:00
MediaProgress.belongsTo(book, { foreignKey: 'mediaItemId', constraints: false })
2023-03-12 20:51:45 +01:00
2023-03-19 21:19:22 +01:00
podcastEpisode.hasMany(MediaProgress, {
foreignKey: 'mediaItemId',
2023-03-12 20:51:45 +01:00
constraints: false,
scope: {
2023-03-19 21:19:22 +01:00
mediaItemType: 'podcastEpisode'
2023-03-12 20:51:45 +01:00
}
})
2023-03-19 21:19:22 +01:00
MediaProgress.belongsTo(podcastEpisode, { foreignKey: 'mediaItemId', constraints: false })
2023-03-12 20:51:45 +01:00
MediaProgress.addHook('afterFind', findResult => {
2023-03-19 21:19:22 +01:00
if (!findResult) return
2023-03-12 20:51:45 +01:00
if (!Array.isArray(findResult)) findResult = [findResult]
2023-03-19 21:19:22 +01:00
2023-03-12 20:51:45 +01:00
for (const instance of findResult) {
2023-03-19 21:19:22 +01:00
if (instance.mediaItemType === 'book' && instance.book !== undefined) {
instance.mediaItem = instance.book
instance.dataValues.mediaItem = instance.dataValues.book
} else if (instance.mediaItemType === 'podcastEpisode' && instance.podcastEpisode !== undefined) {
instance.mediaItem = instance.podcastEpisode
instance.dataValues.mediaItem = instance.dataValues.podcastEpisode
2023-03-12 20:51:45 +01:00
}
// To prevent mistakes:
2023-03-19 21:19:22 +01:00
delete instance.book
delete instance.dataValues.book
delete instance.podcastEpisode
delete instance.dataValues.podcastEpisode
2023-03-12 20:51:45 +01:00
}
})
2023-03-19 21:19:22 +01:00
user.hasMany(MediaProgress)
MediaProgress.belongsTo(user)
2023-03-12 20:51:45 +01:00
return MediaProgress
}