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-18 22:56:57 +01:00
|
|
|
const mixinMethodName = `get${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-18 22:56:57 +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,
|
|
|
|
modelName: 'Feed'
|
|
|
|
})
|
|
|
|
|
|
|
|
const { User, LibraryItem, Collection, Series, Playlist } = sequelize.models
|
|
|
|
|
|
|
|
User.hasMany(Feed)
|
|
|
|
Feed.belongsTo(User)
|
|
|
|
|
|
|
|
LibraryItem.hasMany(Feed, {
|
2023-03-18 22:56:57 +01:00
|
|
|
foreignKey: 'EntityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-18 22:56:57 +01:00
|
|
|
entityType: 'LibraryItem'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-18 22:56:57 +01:00
|
|
|
Feed.belongsTo(LibraryItem, { foreignKey: 'EntityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
|
|
|
Collection.hasMany(Feed, {
|
2023-03-18 22:56:57 +01:00
|
|
|
foreignKey: 'EntityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-18 22:56:57 +01:00
|
|
|
entityType: 'Collection'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-18 22:56:57 +01:00
|
|
|
Feed.belongsTo(Collection, { foreignKey: 'EntityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
|
|
|
Series.hasMany(Feed, {
|
2023-03-18 22:56:57 +01:00
|
|
|
foreignKey: 'EntityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-18 22:56:57 +01:00
|
|
|
entityType: 'Series'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-18 22:56:57 +01:00
|
|
|
Feed.belongsTo(Series, { foreignKey: 'EntityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
|
|
|
Playlist.hasMany(Feed, {
|
2023-03-18 22:56:57 +01:00
|
|
|
foreignKey: 'EntityId',
|
2023-03-13 23:13:31 +01:00
|
|
|
constraints: false,
|
|
|
|
scope: {
|
2023-03-18 22:56:57 +01:00
|
|
|
entityType: 'Playlist'
|
2023-03-13 23:13:31 +01:00
|
|
|
}
|
|
|
|
})
|
2023-03-18 22:56:57 +01:00
|
|
|
Feed.belongsTo(Playlist, { foreignKey: 'EntityId', constraints: false })
|
2023-03-13 23:13:31 +01:00
|
|
|
|
|
|
|
Feed.addHook('afterFind', findResult => {
|
|
|
|
if (!Array.isArray(findResult)) findResult = [findResult]
|
|
|
|
for (const instance of findResult) {
|
2023-03-18 22:56:57 +01:00
|
|
|
if (instance.entityType === 'LibraryItem' && instance.LibraryItem !== undefined) {
|
2023-03-13 23:13:31 +01:00
|
|
|
instance.Entity = instance.LibraryItem
|
2023-03-18 22:56:57 +01:00
|
|
|
} else if (instance.mediaItemType === 'Collection' && instance.Collection !== undefined) {
|
2023-03-13 23:13:31 +01:00
|
|
|
instance.Entity = instance.Collection
|
2023-03-18 22:56:57 +01:00
|
|
|
} else if (instance.mediaItemType === 'Series' && instance.Series !== undefined) {
|
2023-03-13 23:13:31 +01:00
|
|
|
instance.Entity = instance.Series
|
2023-03-18 22:56:57 +01:00
|
|
|
} else if (instance.mediaItemType === 'Playlist' && instance.Playlist !== undefined) {
|
2023-03-13 23:13:31 +01:00
|
|
|
instance.Entity = instance.Playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent mistakes:
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return Feed
|
|
|
|
}
|