2023-03-13 23:13:31 +01:00
|
|
|
const { DataTypes, Model } = require('sequelize')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Polymorphic association: https://sequelize.org/docs/v6/advanced-association-concepts/polymorphic-associations/
|
|
|
|
* Feeds can be created from LibraryItem, Collection, Playlist or Series
|
|
|
|
*/
|
|
|
|
module.exports = (sequelize) => {
|
|
|
|
class Feed extends Model {
|
|
|
|
getEntity(options) {
|
|
|
|
if (!this.entityType) return Promise.resolve(null)
|
2023-03-19 21:19:22 +01:00
|
|
|
const mixinMethodName = `get${sequelize.uppercaseFirst(this.entityType)}`
|
2023-03-13 23:13:31 +01:00
|
|
|
return this[mixinMethodName](options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Feed.init({
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
slug: DataTypes.STRING,
|
|
|
|
entityType: DataTypes.STRING,
|
2023-03-19 21:19:22 +01:00
|
|
|
entityId: DataTypes.UUIDV4,
|
2023-03-13 23:13:31 +01:00
|
|
|
entityUpdatedAt: DataTypes.DATE,
|
|
|
|
serverAddress: DataTypes.STRING,
|
|
|
|
feedURL: DataTypes.STRING,
|
|
|
|
imageURL: DataTypes.STRING,
|
|
|
|
siteURL: DataTypes.STRING,
|
|
|
|
title: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT,
|
|
|
|
author: DataTypes.STRING,
|
|
|
|
podcastType: DataTypes.STRING,
|
|
|
|
language: DataTypes.STRING,
|
|
|
|
ownerName: DataTypes.STRING,
|
|
|
|
ownerEmail: DataTypes.STRING,
|
|
|
|
explicit: DataTypes.BOOLEAN,
|
|
|
|
preventIndexing: DataTypes.BOOLEAN
|
|
|
|
}, {
|
|
|
|
sequelize,
|
2023-03-19 21:19:22 +01:00
|
|
|
modelName: 'feed'
|
2023-03-13 23:13:31 +01:00
|
|
|
})
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
const { user, libraryItem, collection, series, playlist } = sequelize.models
|
2023-03-13 23:13:31 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
user.hasMany(Feed)
|
|
|
|
Feed.belongsTo(user)
|
2023-03-13 23:13:31 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
libraryItem.hasMany(Feed, {
|
|
|
|
foreignKey: 'entityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
entityType: 'libraryItem'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
Feed.belongsTo(libraryItem, { foreignKey: 'entityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
collection.hasMany(Feed, {
|
|
|
|
foreignKey: 'entityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
entityType: 'collection'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
Feed.belongsTo(collection, { foreignKey: 'entityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
series.hasMany(Feed, {
|
|
|
|
foreignKey: 'entityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
entityType: 'series'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
Feed.belongsTo(series, { foreignKey: 'entityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
playlist.hasMany(Feed, {
|
|
|
|
foreignKey: 'entityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-19 21:19:22 +01:00
|
|
|
entityType: 'playlist'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-19 21:19:22 +01:00
|
|
|
Feed.belongsTo(playlist, { foreignKey: 'entityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
|
|
|
Feed.addHook('afterFind', findResult => {
|
2023-03-19 21:19:22 +01:00
|
|
|
if (!findResult) return
|
|
|
|
|
2023-03-13 23:13:31 +01:00
|
|
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
|
|
|
for (const instance of findResult) {
|
2023-03-19 21:19:22 +01:00
|
|
|
if (instance.entityType === 'libraryItem' && instance.libraryItem !== undefined) {
|
|
|
|
instance.entity = instance.libraryItem
|
|
|
|
instance.dataValues.entity = instance.dataValues.libraryItem
|
|
|
|
} else if (instance.entityType === 'collection' && instance.collection !== undefined) {
|
|
|
|
instance.entity = instance.collection
|
|
|
|
instance.dataValues.entity = instance.dataValues.collection
|
|
|
|
} else if (instance.entityType === 'series' && instance.series !== undefined) {
|
|
|
|
instance.entity = instance.series
|
|
|
|
instance.dataValues.entity = instance.dataValues.series
|
|
|
|
} else if (instance.entityType === 'playlist' && instance.playlist !== undefined) {
|
|
|
|
instance.entity = instance.playlist
|
|
|
|
instance.dataValues.entity = instance.dataValues.playlist
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent mistakes:
|
2023-03-19 21:19:22 +01:00
|
|
|
delete instance.libraryItem
|
|
|
|
delete instance.dataValues.libraryItem
|
|
|
|
delete instance.collection
|
|
|
|
delete instance.dataValues.collection
|
|
|
|
delete instance.series
|
|
|
|
delete instance.dataValues.series
|
|
|
|
delete instance.playlist
|
|
|
|
delete instance.dataValues.playlist
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return Feed
|
|
|
|
}
|