This commit is contained in:
Paul DeVito 2026-05-06 13:51:21 +02:00 committed by GitHub
commit 1122be3399
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 744 additions and 5 deletions

View file

@ -162,6 +162,11 @@ class Database {
return this.models.device
}
/** @type {typeof import('./models/UserSeriesFollow')} */
get userSeriesFollowModel() {
return this.models.userSeriesFollow
}
/**
* Check if db file exists
* @returns {boolean}
@ -345,6 +350,7 @@ class Database {
require('./models/Setting').init(this.sequelize)
require('./models/CustomMetadataProvider').init(this.sequelize)
require('./models/MediaItemShare').init(this.sequelize)
require('./models/UserSeriesFollow').init(this.sequelize)
return this.sequelize.sync({ force, alter: false })
}

View file

@ -513,5 +513,87 @@ class MeController {
const data = await userStats.getStatsForYear(req.user.id, year)
res.json(data)
}
/**
* POST: /api/me/follows/series/:id
* Follow a series
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async followSeries(req, res) {
const series = await Database.seriesModel.findByPk(req.params.id)
if (!series) {
Logger.error(`[MeController] followSeries: Series ${req.params.id} not found`)
return res.sendStatus(404)
}
const existing = await Database.userSeriesFollowModel.findOne({
where: { userId: req.user.id, seriesId: series.id }
})
if (!existing) {
await Database.userSeriesFollowModel.create({
userId: req.user.id,
seriesId: series.id
})
Database.userModel.seriesFollowChanged(req.user.id)
}
const seriesFollowing = await Database.userSeriesFollowModel.getFollowedSeriesIdsForUser(req.user.id)
SocketAuthority.clientEmitter(req.user.id, 'user_series_follows_updated', { seriesFollowing })
res.sendStatus(200)
}
/**
* DELETE: /api/me/follows/series/:id
* Unfollow a series
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async unfollowSeries(req, res) {
const deleted = await Database.userSeriesFollowModel.destroy({
where: { userId: req.user.id, seriesId: req.params.id }
})
if (!deleted) {
return res.sendStatus(404)
}
Database.userModel.seriesFollowChanged(req.user.id)
const seriesFollowing = await Database.userSeriesFollowModel.getFollowedSeriesIdsForUser(req.user.id)
SocketAuthority.clientEmitter(req.user.id, 'user_series_follows_updated', { seriesFollowing })
res.sendStatus(200)
}
/**
* GET: /api/me/follows
* Get all follows for the current user
* Optional query param: ?type=series
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async getFollows(req, res) {
const type = req.query.type
const result = {}
if (!type || type === 'series') {
const seriesFollows = await Database.userSeriesFollowModel.findAll({
where: { userId: req.user.id },
include: { model: Database.seriesModel, attributes: ['id', 'name', 'libraryId'] },
order: [['createdAt', 'DESC']]
})
result.series = seriesFollows.map((sf) => ({
seriesId: sf.seriesId,
seriesName: sf.series?.name || null,
libraryId: sf.series?.libraryId || null,
createdAt: sf.createdAt.valueOf()
}))
}
res.json(result)
}
}
module.exports = new MeController()

View file

@ -0,0 +1,110 @@
/**
* @typedef MigrationContext
* @property {import('sequelize').QueryInterface} queryInterface - a suquelize QueryInterface object.
* @property {import('../Logger')} logger - a Logger object.
*
* @typedef MigrationOptions
* @property {MigrationContext} context - an object containing the migration context.
*/
const migrationVersion = '2.34.0'
const migrationName = `${migrationVersion}-create-user-series-follows`
const loggerPrefix = `[${migrationVersion} migration]`
/**
* This upward migration creates the userSeriesFollows table for tracking
* which users follow which series.
*
* @param {MigrationOptions} options - an object containing the migration context.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function up({ context: { queryInterface, logger } }) {
logger.info(`${loggerPrefix} UPGRADE BEGIN: ${migrationName}`)
if (await queryInterface.tableExists('userSeriesFollows')) {
logger.info(`${loggerPrefix} table "userSeriesFollows" already exists`)
} else {
logger.info(`${loggerPrefix} creating table "userSeriesFollows"`)
const DataTypes = queryInterface.sequelize.Sequelize.DataTypes
await queryInterface.createTable('userSeriesFollows', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
userId: {
type: DataTypes.UUID,
references: {
model: {
tableName: 'users'
},
key: 'id'
},
allowNull: false,
onDelete: 'CASCADE'
},
seriesId: {
type: DataTypes.UUID,
references: {
model: {
tableName: 'series'
},
key: 'id'
},
allowNull: false,
onDelete: 'CASCADE'
}
})
logger.info(`${loggerPrefix} created table "userSeriesFollows"`)
// Index for "get all series a user follows" (most frequent query)
await queryInterface.addIndex('userSeriesFollows', {
name: 'user_series_follows_userId',
fields: ['userId']
})
logger.info(`${loggerPrefix} added index on userId`)
// Unique constraint to prevent duplicate follows
await queryInterface.addIndex('userSeriesFollows', {
name: 'user_series_follows_unique',
fields: ['userId', 'seriesId'],
unique: true
})
logger.info(`${loggerPrefix} added unique index on (userId, seriesId)`)
// Index for "find all followers of series X" (for notifications)
await queryInterface.addIndex('userSeriesFollows', {
name: 'user_series_follows_seriesId',
fields: ['seriesId']
})
logger.info(`${loggerPrefix} added index on seriesId`)
}
logger.info(`${loggerPrefix} UPGRADE END: ${migrationName}`)
}
/**
* This downward migration script removes the userSeriesFollows table.
*
* @param {MigrationOptions} options - an object containing the migration context.
* @returns {Promise<void>} - A promise that resolves when the migration is complete.
*/
async function down({ context: { queryInterface, logger } }) {
logger.info(`${loggerPrefix} DOWNGRADE BEGIN: ${migrationName}`)
if (await queryInterface.tableExists('userSeriesFollows')) {
logger.info(`${loggerPrefix} dropping table "userSeriesFollows"`)
await queryInterface.dropTable('userSeriesFollows')
logger.info(`${loggerPrefix} dropped table "userSeriesFollows"`)
} else {
logger.info(`${loggerPrefix} table "userSeriesFollows" does not exist`)
}
logger.info(`${loggerPrefix} DOWNGRADE END: ${migrationName}`)
}
module.exports = { up, down }

View file

@ -357,7 +357,7 @@ class User extends Model {
[sequelize.Op.like]: username
}
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -382,7 +382,7 @@ class User extends Model {
[sequelize.Op.like]: email
}
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -402,7 +402,7 @@ class User extends Model {
if (cachedUser) return cachedUser
const user = await this.findByPk(userId, {
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -426,7 +426,7 @@ class User extends Model {
where: {
[sequelize.Op.or]: [{ id: userId }, { 'extraData.oldUserId': userId }]
},
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -447,7 +447,7 @@ class User extends Model {
const user = await this.findOne({
where: sequelize.where(sequelize.literal(`extraData->>"authOpenIDSub"`), sub),
include: this.sequelize.models.mediaProgress
include: [this.sequelize.models.mediaProgress, this.sequelize.models.userSeriesFollow]
})
if (user) userCache.set(user)
@ -506,6 +506,17 @@ class User extends Model {
}
}
/**
* Invalidate cached user when series follows change
* @param {string} userId
*/
static seriesFollowChanged(userId) {
const cachedUser = userCache.getById(userId)
if (cachedUser) {
userCache.delete(userId)
}
}
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize
@ -621,6 +632,7 @@ class User extends Model {
isOldToken: this.isOldToken,
mediaProgress: this.mediaProgresses?.map((mp) => mp.getOldMediaProgress()) || [],
seriesHideFromContinueListening: [...seriesHideFromContinueListening],
seriesFollowing: this.userSeriesFollows?.map((f) => f.seriesId) || [],
bookmarks: this.bookmarks?.map((b) => ({ ...b })) || [],
isActive: this.isActive,
isLocked: this.isLocked,

View file

@ -0,0 +1,84 @@
const { DataTypes, Model } = require('sequelize')
class UserSeriesFollow extends Model {
constructor(values, options) {
super(values, options)
/** @type {UUIDV4} */
this.id
/** @type {UUIDV4} */
this.userId
/** @type {UUIDV4} */
this.seriesId
/** @type {Date} */
this.createdAt
}
/**
* Get array of series IDs that a user is following
* @param {string} userId
* @returns {Promise<string[]>}
*/
static async getFollowedSeriesIdsForUser(userId) {
const follows = await this.findAll({
where: { userId },
attributes: ['seriesId']
})
return follows.map((f) => f.seriesId)
}
/**
* Initialize model
* @param {import('../Database').sequelize} sequelize
*/
static init(sequelize) {
super.init(
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
}
},
{
sequelize,
modelName: 'userSeriesFollow',
timestamps: true,
updatedAt: false,
indexes: [
{
name: 'user_series_follows_userId',
fields: ['userId']
},
{
name: 'user_series_follows_unique',
fields: ['userId', 'seriesId'],
unique: true
},
{
name: 'user_series_follows_seriesId',
fields: ['seriesId']
}
]
}
)
// Super Many-to-Many
// ref: https://sequelize.org/docs/v6/advanced-association-concepts/advanced-many-to-many/#the-best-of-both-worlds-the-super-many-to-many-relationship
const { user, series } = sequelize.models
user.belongsToMany(series, { through: UserSeriesFollow, as: 'followedSeries' })
series.belongsToMany(user, { through: UserSeriesFollow, as: 'followers' })
user.hasMany(UserSeriesFollow, {
onDelete: 'CASCADE'
})
UserSeriesFollow.belongsTo(user)
series.hasMany(UserSeriesFollow, {
onDelete: 'CASCADE'
})
UserSeriesFollow.belongsTo(series)
}
}
module.exports = UserSeriesFollow

View file

@ -188,6 +188,9 @@ class ApiRouter {
this.router.get('/me/series/:id/readd-to-continue-listening', MeController.readdSeriesFromContinueListening.bind(this))
this.router.get('/me/stats/year/:year', MeController.getStatsForYear.bind(this))
this.router.post('/me/ereader-devices', MeController.updateUserEReaderDevices.bind(this))
this.router.post('/me/follows/series/:id', MeController.followSeries.bind(this))
this.router.delete('/me/follows/series/:id', MeController.unfollowSeries.bind(this))
this.router.get('/me/follows', MeController.getFollows.bind(this))
//
// Backup Routes

View file

@ -13,6 +13,7 @@ const TaskManager = require('../managers/TaskManager')
const LibraryItemScanner = require('./LibraryItemScanner')
const LibraryScan = require('./LibraryScan')
const LibraryItemScanData = require('./LibraryItemScanData')
const { notifyFollowersOfNewBook } = require('../utils/followNotifications')
const Task = require('../objects/Task')
class LibraryScanner {
@ -263,6 +264,7 @@ class LibraryScanner {
const newLibraryItem = await LibraryItemScanner.scanNewLibraryItem(libraryItemData, libraryScan.library.settings, libraryScan)
if (newLibraryItem) {
newLibraryItems.push(newLibraryItem)
notifyFollowersOfNewBook(newLibraryItem)
libraryScan.resultsAdded++
}
@ -633,6 +635,7 @@ class LibraryScanner {
const newLibraryItem = await LibraryItemScanner.scanPotentialNewLibraryItem(fullPath, library, folder, isSingleMediaItem)
if (newLibraryItem) {
SocketAuthority.libraryItemEmitter('item_added', newLibraryItem)
notifyFollowersOfNewBook(newLibraryItem)
}
itemGroupingResults[itemDir] = newLibraryItem ? ScanResult.ADDED : ScanResult.NOTHING
}

View file

@ -0,0 +1,50 @@
const Database = require('../Database')
const SocketAuthority = require('../SocketAuthority')
const Logger = require('../Logger')
/**
* Notify users following a series that a new book was added.
* Called after a new library item is created by the scanner.
*
* @param {import('../models/LibraryItem')} libraryItem - the newly created expanded library item
*/
async function notifyFollowersOfNewBook(libraryItem) {
if (libraryItem.mediaType !== 'book') return
if (!libraryItem.media?.series?.length) return
const seriesIds = libraryItem.media.series.map((s) => s.id)
if (!seriesIds.length) return
const follows = await Database.userSeriesFollowModel.findAll({
where: { seriesId: seriesIds },
attributes: ['userId', 'seriesId']
})
if (!follows.length) return
// Group by userId to send one event per user
const userSeriesMap = {}
for (const follow of follows) {
if (!userSeriesMap[follow.userId]) {
userSeriesMap[follow.userId] = []
}
userSeriesMap[follow.userId].push(follow.seriesId)
}
const libraryItemJson = libraryItem.toOldJSONExpanded()
for (const [userId, followedSeriesIds] of Object.entries(userSeriesMap)) {
const matchedSeries = libraryItem.media.series
.filter((s) => followedSeriesIds.includes(s.id))
.map((s) => ({ id: s.id, name: s.name }))
SocketAuthority.clientEmitter(userId, 'followed_series_book_added', {
libraryItem: libraryItemJson,
series: matchedSeries
})
Logger.debug(`[FollowNotifications] Notified user ${userId} of new book in followed series: ${matchedSeries.map((s) => s.name).join(', ')}`)
}
}
module.exports = { notifyFollowersOfNewBook }