Skip to content

Commit

Permalink
Cleanup Collection model to remove oldCollection references
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Sep 4, 2024
1 parent 0344a63 commit 5ec8406
Showing 1 changed file with 103 additions and 102 deletions.
205 changes: 103 additions & 102 deletions server/models/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,78 +115,6 @@ class Collection extends Model {
.filter((c) => c)
}

/**
* Get old collection toJSONExpanded, items filtered for user permissions
*
* @param {import('./User')|null} user
* @param {string[]} [include]
* @returns {Promise<oldCollection>} oldCollection.toJSONExpanded
*/
async getOldJsonExpanded(user, include) {
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']
}
}
],
order: [Sequelize.literal('`collectionBook.order` ASC')]
})) || []

const oldCollection = this.sequelize.models.collection.getOldCollection(this)

// Filter books using user permissions
// TODO: Handle user permission restrictions on initial query
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
}
}
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
}

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])
}
}

return collectionExpanded
}

/**
* Get old collection from Collection
* @param {Collection} collectionExpanded
Expand Down Expand Up @@ -250,36 +178,6 @@ class Collection extends Model {
return this.getOldCollection(collection)
}

/**
* Get old collection from current
* @returns {Promise<oldCollection>}
*/
async getOld() {
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']
}
}
],
order: [Sequelize.literal('`collectionBook.order` ASC')]
})) || []

return this.sequelize.models.collection.getOldCollection(this)
}

/**
* Remove all collections belonging to library
* @param {string} libraryId
Expand Down Expand Up @@ -320,6 +218,109 @@ class Collection extends Model {
library.hasMany(Collection)
Collection.belongsTo(library)
}

/**
* Get old collection toJSONExpanded, items filtered for user permissions
*
* @param {import('./User')|null} user
* @param {string[]} [include]
* @returns {Promise<oldCollection>} oldCollection.toJSONExpanded
*/
async getOldJsonExpanded(user, include) {
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']
}
}
],
order: [Sequelize.literal('`collectionBook.order` ASC')]
})) || []

// Filter books using user permissions
// TODO: Handle user permission restrictions on initial query
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
}
}
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 && this.books.length) {
return null
}

const collectionExpanded = this.toOldJSONExpanded(libraryItems)

if (include?.includes('rssfeed')) {
const feeds = await this.getFeeds()
if (feeds?.length) {
collectionExpanded.rssFeed = this.sequelize.models.feed.getOldFeed(feeds[0])
}
}

return collectionExpanded
}

/**
*
* @param {string[]} libraryItemIds
* @returns
*/
toOldJSON(libraryItemIds) {
return {
id: this.id,
libraryId: this.libraryId,
name: this.name,
description: this.description,
books: [...libraryItemIds],
lastUpdate: this.updatedAt.valueOf(),
createdAt: this.createdAt.valueOf()
}
}

/**
*
* @param {import('../objects/LibraryItem')} oldLibraryItems
* @returns
*/
toOldJSONExpanded(oldLibraryItems) {
const json = this.toOldJSON(oldLibraryItems.map((li) => li.id))
json.books = json.books
.map((libraryItemId) => {
const book = oldLibraryItems.find((li) => li.id === libraryItemId)
return book ? book.toJSONExpanded() : null
})
.filter((b) => !!b)
return json
}
}

module.exports = Collection

0 comments on commit 5ec8406

Please sign in to comment.