2023-08-12 22:01:27 +02:00
|
|
|
const { DataTypes, Model, Sequelize } = require('sequelize')
|
2023-07-05 01:14:44 +02:00
|
|
|
|
|
|
|
const oldCollection = require('../objects/Collection')
|
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
class Collection extends Model {
|
|
|
|
constructor(values, options) {
|
|
|
|
super(values, options)
|
|
|
|
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.id
|
|
|
|
/** @type {string} */
|
|
|
|
this.name
|
|
|
|
/** @type {string} */
|
|
|
|
this.description
|
|
|
|
/** @type {UUIDV4} */
|
|
|
|
this.libraryId
|
|
|
|
/** @type {Date} */
|
|
|
|
this.updatedAt
|
|
|
|
/** @type {Date} */
|
|
|
|
this.createdAt
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all old collections toJSONExpanded, items filtered for user permissions
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {oldUser} [user]
|
|
|
|
* @param {string} [libraryId]
|
|
|
|
* @param {string[]} [include]
|
|
|
|
* @returns {Promise<oldCollection[]>} oldCollection.toJSONExpanded
|
2023-08-16 01:03:43 +02:00
|
|
|
*/
|
|
|
|
static async getOldCollectionsJsonExpanded(user, libraryId, include) {
|
|
|
|
let collectionWhere = null
|
|
|
|
if (libraryId) {
|
|
|
|
collectionWhere = {
|
|
|
|
libraryId
|
2023-08-12 00:49:06 +02:00
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2023-08-12 00:49:06 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
// Optionally include rssfeed for collection
|
|
|
|
const collectionIncludes = []
|
|
|
|
if (include.includes('rssfeed')) {
|
|
|
|
collectionIncludes.push({
|
|
|
|
model: this.sequelize.models.feed
|
2023-08-12 00:49:06 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
const collections = await this.findAll({
|
|
|
|
where: collectionWhere,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.book,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.libraryItem
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.author,
|
|
|
|
through: {
|
|
|
|
attributes: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.series,
|
|
|
|
through: {
|
|
|
|
attributes: ['sequence']
|
|
|
|
}
|
2024-05-29 00:24:02 +02:00
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
]
|
|
|
|
},
|
|
|
|
...collectionIncludes
|
|
|
|
],
|
|
|
|
order: [[this.sequelize.models.book, this.sequelize.models.collectionBook, 'order', 'ASC']]
|
|
|
|
})
|
|
|
|
// TODO: Handle user permission restrictions on initial query
|
2024-05-29 00:24:02 +02:00
|
|
|
return collections
|
|
|
|
.map((c) => {
|
|
|
|
const oldCollection = this.getOldCollection(c)
|
|
|
|
|
|
|
|
// Filter books using user permissions
|
|
|
|
const books =
|
|
|
|
c.books?.filter((b) => {
|
|
|
|
if (user) {
|
|
|
|
if (b.tags?.length && !user.checkCanAccessLibraryItemWithTags(b.tags)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (b.explicit === true && !user.canAccessExplicitContent) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}) || []
|
|
|
|
|
|
|
|
// Map to library items
|
|
|
|
const libraryItems = books.map((b) => {
|
|
|
|
const libraryItem = b.libraryItem
|
|
|
|
delete b.libraryItem
|
|
|
|
libraryItem.media = b
|
|
|
|
return this.sequelize.models.libraryItem.getOldLibraryItem(libraryItem)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Users with restricted permissions will not see this collection
|
|
|
|
if (!books.length && oldCollection.books.length) {
|
|
|
|
return null
|
2023-08-12 22:01:27 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
const collectionExpanded = oldCollection.toJSONExpanded(libraryItems)
|
2023-08-12 22:01:27 +02:00
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
// Map feed if found
|
|
|
|
if (c.feeds?.length) {
|
|
|
|
collectionExpanded.rssFeed = this.sequelize.models.feed.getOldFeed(c.feeds[0])
|
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
return collectionExpanded
|
|
|
|
})
|
|
|
|
.filter((c) => c)
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
/**
|
|
|
|
* Get old collection toJSONExpanded, items filtered for user permissions
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {oldUser} [user]
|
|
|
|
* @param {string[]} [include]
|
|
|
|
* @returns {Promise<oldCollection>} oldCollection.toJSONExpanded
|
2023-08-16 01:03:43 +02:00
|
|
|
*/
|
|
|
|
async getOldJsonExpanded(user, include) {
|
2024-05-29 00:24:02 +02:00
|
|
|
this.books =
|
|
|
|
(await this.getBooks({
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.libraryItem
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.author,
|
|
|
|
through: {
|
|
|
|
attributes: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.series,
|
|
|
|
through: {
|
|
|
|
attributes: ['sequence']
|
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2024-05-29 00:24:02 +02:00
|
|
|
],
|
|
|
|
order: [Sequelize.literal('`collectionBook.order` ASC')]
|
|
|
|
})) || []
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
const oldCollection = this.sequelize.models.collection.getOldCollection(this)
|
|
|
|
|
|
|
|
// Filter books using user permissions
|
|
|
|
// TODO: Handle user permission restrictions on initial query
|
2024-05-29 00:24:02 +02:00
|
|
|
const books =
|
|
|
|
this.books?.filter((b) => {
|
|
|
|
if (user) {
|
|
|
|
if (b.tags?.length && !user.checkCanAccessLibraryItemWithTags(b.tags)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (b.explicit === true && !user.canAccessExplicitContent) {
|
|
|
|
return false
|
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2024-05-29 00:24:02 +02:00
|
|
|
return true
|
|
|
|
}) || []
|
2023-08-16 01:03:43 +02:00
|
|
|
|
|
|
|
// Map to library items
|
2024-05-29 00:24:02 +02:00
|
|
|
const libraryItems = books.map((b) => {
|
2023-08-16 01:03:43 +02:00
|
|
|
const libraryItem = b.libraryItem
|
|
|
|
delete b.libraryItem
|
|
|
|
libraryItem.media = b
|
|
|
|
return this.sequelize.models.libraryItem.getOldLibraryItem(libraryItem)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Users with restricted permissions will not see this collection
|
|
|
|
if (!books.length && oldCollection.books.length) {
|
|
|
|
return null
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
const collectionExpanded = oldCollection.toJSONExpanded(libraryItems)
|
|
|
|
|
|
|
|
if (include?.includes('rssfeed')) {
|
|
|
|
const feeds = await this.getFeeds()
|
|
|
|
if (feeds?.length) {
|
|
|
|
collectionExpanded.rssFeed = this.sequelize.models.feed.getOldFeed(feeds[0])
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
return collectionExpanded
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get old collection from Collection
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {Collection} collectionExpanded
|
2023-08-16 01:03:43 +02:00
|
|
|
* @returns {oldCollection}
|
|
|
|
*/
|
|
|
|
static getOldCollection(collectionExpanded) {
|
2024-05-29 00:24:02 +02:00
|
|
|
const libraryItemIds = collectionExpanded.books?.map((b) => b.libraryItem?.id || null).filter((lid) => lid) || []
|
2023-08-16 01:03:43 +02:00
|
|
|
return new oldCollection({
|
|
|
|
id: collectionExpanded.id,
|
|
|
|
libraryId: collectionExpanded.libraryId,
|
|
|
|
name: collectionExpanded.name,
|
|
|
|
description: collectionExpanded.description,
|
|
|
|
books: libraryItemIds,
|
|
|
|
lastUpdate: collectionExpanded.updatedAt.valueOf(),
|
|
|
|
createdAt: collectionExpanded.createdAt.valueOf()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {oldCollection} oldCollection
|
|
|
|
* @returns {Promise<Collection>}
|
|
|
|
*/
|
2023-08-16 01:03:43 +02:00
|
|
|
static createFromOld(oldCollection) {
|
|
|
|
const collection = this.getFromOld(oldCollection)
|
|
|
|
return this.create(collection)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getFromOld(oldCollection) {
|
|
|
|
return {
|
|
|
|
id: oldCollection.id,
|
|
|
|
name: oldCollection.name,
|
|
|
|
description: oldCollection.description,
|
|
|
|
libraryId: oldCollection.libraryId
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2023-07-22 23:18:55 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
static removeById(collectionId) {
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
id: collectionId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get old collection by id
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {string} collectionId
|
2023-08-16 01:03:43 +02:00
|
|
|
* @returns {Promise<oldCollection|null>} returns null if not found
|
|
|
|
*/
|
|
|
|
static async getOldById(collectionId) {
|
|
|
|
if (!collectionId) return null
|
|
|
|
const collection = await this.findByPk(collectionId, {
|
|
|
|
include: {
|
|
|
|
model: this.sequelize.models.book,
|
|
|
|
include: this.sequelize.models.libraryItem
|
|
|
|
},
|
|
|
|
order: [[this.sequelize.models.book, this.sequelize.models.collectionBook, 'order', 'ASC']]
|
|
|
|
})
|
|
|
|
if (!collection) return null
|
|
|
|
return this.getOldCollection(collection)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get old collection from current
|
|
|
|
* @returns {Promise<oldCollection>}
|
|
|
|
*/
|
|
|
|
async getOld() {
|
2024-05-29 00:24:02 +02:00
|
|
|
this.books =
|
|
|
|
(await this.getBooks({
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.libraryItem
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.author,
|
|
|
|
through: {
|
|
|
|
attributes: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: this.sequelize.models.series,
|
|
|
|
through: {
|
|
|
|
attributes: ['sequence']
|
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2024-05-29 00:24:02 +02:00
|
|
|
],
|
|
|
|
order: [Sequelize.literal('`collectionBook.order` ASC')]
|
|
|
|
})) || []
|
2023-08-12 22:01:27 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
return this.sequelize.models.collection.getOldCollection(this)
|
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
/**
|
|
|
|
* Remove all collections belonging to library
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {string} libraryId
|
2023-08-16 01:03:43 +02:00
|
|
|
* @returns {Promise<number>} number of collections destroyed
|
|
|
|
*/
|
|
|
|
static async removeAllForLibrary(libraryId) {
|
|
|
|
if (!libraryId) return 0
|
|
|
|
return this.destroy({
|
|
|
|
where: {
|
|
|
|
libraryId
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-22 23:18:55 +02:00
|
|
|
|
2023-08-16 01:03:43 +02:00
|
|
|
/**
|
|
|
|
* Initialize model
|
2024-05-29 00:24:02 +02:00
|
|
|
* @param {import('../Database').sequelize} sequelize
|
2023-08-16 01:03:43 +02:00
|
|
|
*/
|
|
|
|
static init(sequelize) {
|
2024-05-29 00:24:02 +02:00
|
|
|
super.init(
|
|
|
|
{
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
defaultValue: DataTypes.UUIDV4,
|
|
|
|
primaryKey: true
|
|
|
|
},
|
|
|
|
name: DataTypes.STRING,
|
|
|
|
description: DataTypes.TEXT
|
2023-08-16 01:03:43 +02:00
|
|
|
},
|
2024-05-29 00:24:02 +02:00
|
|
|
{
|
|
|
|
sequelize,
|
|
|
|
modelName: 'collection'
|
|
|
|
}
|
|
|
|
)
|
2023-08-16 01:03:43 +02:00
|
|
|
|
|
|
|
const { library } = sequelize.models
|
|
|
|
|
|
|
|
library.hasMany(Collection)
|
|
|
|
Collection.belongsTo(library)
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2023-08-16 01:03:43 +02:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2024-05-29 00:24:02 +02:00
|
|
|
module.exports = Collection
|