Merge branch 'master' into v3

This commit is contained in:
advplyr committed 2026-11-30 16:45:15 -06:00
commit 1ed62858dd
3 files changed
+126 -12

No files matched your search

+4
View File
@@ -95,6 +95,10 @@ class SeriesController {
async middleware(req, res, next) {
const series = await Database.seriesModel.findByPk(req.params.id)
if (!series) return res.sendStatus(404)
if (!req.user.checkCanAccessLibrary(series.libraryId)) {
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to access series "${series.id}" without access to the library`)
return res.sendStatus(404)
}
/**
* Filter out any library items not accessible to user
+44 -12
View File
@@ -2,6 +2,10 @@ const { LRUCache } = require('lru-cache')
const Logger = require('../Logger')
const Database = require('../Database')
const PERSONALIZED_URL_PATTERN = /^\/libraries\/[^/]+\/personalized/
const RECENT_EPISODES_URL_PATTERN = /^\/libraries\/[^/]+\/recent-episodes/
const ME_URL_PATTERN = /^\/me(\/|\?|$)/
class ApiCacheManager {
defaultCacheOptions = { max: 1000, maxSize: 10 * 1000 * 1000, sizeCalculation: (item) => item.body.length + JSON.stringify(item.headers).length }
defaultTtlOptions = { ttl: 30 * 60 * 1000 }
@@ -12,6 +16,9 @@ class ApiCacheManager {
constructor(cache = new LRUCache(this.defaultCacheOptions), ttlOptions = this.defaultTtlOptions) {
this.cache = cache
this.ttlOptions = ttlOptions
this.globalVersion = 0
this.personalizedVersion = 0
this.meVersion = 0
}
init(database = Database) {
@@ -20,12 +27,25 @@ class ApiCacheManager {
}
getModelName(model) {
if (typeof model?.name === 'string') return model.name
if (typeof model?.model?.name === 'string') return model.model.name
if (typeof model?.constructor?.name === 'string' && model.constructor.name !== 'Object') return model.constructor.name
const target = Array.isArray(model) ? model[0] : model
if (typeof target?.constructor?.name === 'string' && target.constructor.name !== 'Object' && target.constructor.name !== 'Function') {
return target.constructor.name
}
if (typeof target?.model?.name === 'string') return target.model.name
if (typeof target?.name === 'string') return target.name
return 'unknown'
}
getCacheVersion(url) {
if (PERSONALIZED_URL_PATTERN.test(url) || RECENT_EPISODES_URL_PATTERN.test(url)) {
return this.globalVersion + this.personalizedVersion
}
if (ME_URL_PATTERN.test(url)) {
return this.globalVersion + this.meVersion
}
return this.globalVersion
}
clearByUrlPattern(urlPattern) {
let removed = 0
for (const key of this.cache.keys()) {
@@ -45,10 +65,15 @@ class ApiCacheManager {
let removedPersonalized = 0
let removedRecentEpisodes = 0
if (this.modelsInvalidatingPersonalized.has(modelName)) {
removedPersonalized = this.clearByUrlPattern(/^\/libraries\/[^/]+\/personalized/)
removedRecentEpisodes = this.clearByUrlPattern(/^\/libraries\/[^/]+\/recent-episodes/)
this.personalizedVersion++
removedPersonalized = this.clearByUrlPattern(PERSONALIZED_URL_PATTERN)
removedRecentEpisodes = this.clearByUrlPattern(RECENT_EPISODES_URL_PATTERN)
}
let removedMe = 0
if (this.modelsInvalidatingMe.has(modelName)) {
this.meVersion++
removedMe = this.clearByUrlPattern(ME_URL_PATTERN)
}
const removedMe = this.modelsInvalidatingMe.has(modelName) ? this.clearByUrlPattern(/^\/me(\/|\?|$)/) : 0
Logger.debug(`[ApiCacheManager] ${modelName}.${hook}: cleared user-progress cache slices (personalized=${removedPersonalized}, recentEpisodes=${removedRecentEpisodes}, me=${removedMe})`)
}
@@ -60,6 +85,7 @@ class ApiCacheManager {
}
Logger.debug(`[ApiCacheManager] ${modelName}.${hook}: Clearing cache`)
this.globalVersion++
this.cache.clear()
}
@@ -70,6 +96,7 @@ class ApiCacheManager {
Logger.info(`[ApiCacheManager] Resetting cache`)
this.init()
this.globalVersion++
this.cache.clear()
}
@@ -96,15 +123,20 @@ class ApiCacheManager {
res.send(cached.body)
return
}
const requestCacheVersion = this.getCacheVersion(key.url)
res.originalSend = res.send
res.send = (body) => {
Logger.debug(`[ApiCacheManager] Cache miss: ${stringifiedKey}`)
const cached = { body, headers: res.getHeaders(), statusCode: res.statusCode }
if (key.url.search(/^\/libraries\/.*?\/personalized/) !== -1) {
Logger.debug(`[ApiCacheManager] Caching with ${this.ttlOptions.ttl} ms TTL`)
this.cache.set(stringifiedKey, cached, this.ttlOptions)
if (this.getCacheVersion(key.url) !== requestCacheVersion) {
Logger.debug(`[ApiCacheManager] Skipping cache set for invalidated in-flight request: ${stringifiedKey}`)
} else {
this.cache.set(stringifiedKey, cached)
Logger.debug(`[ApiCacheManager] Cache miss: ${stringifiedKey}`)
const cached = { body, headers: res.getHeaders(), statusCode: res.statusCode }
if (PERSONALIZED_URL_PATTERN.test(key.url)) {
Logger.debug(`[ApiCacheManager] Caching with ${this.ttlOptions.ttl} ms TTL`)
this.cache.set(stringifiedKey, cached, this.ttlOptions)
} else {
this.cache.set(stringifiedKey, cached)
}
}
res.originalSend(body)
}