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

@ -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 }