mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-04-16 23:14:11 +02:00
Added an On Library Item notifcation event
This commit is contained in:
@@ -3,6 +3,7 @@ const Logger = require('../Logger')
|
||||
const SocketAuthority = require('../SocketAuthority')
|
||||
const Database = require('../Database')
|
||||
const { notificationData } = require('../utils/notifications')
|
||||
const Json = require('../libs/archiver/lib/plugins/json')
|
||||
|
||||
class NotificationManager {
|
||||
constructor() {
|
||||
@@ -90,6 +91,30 @@ class NotificationManager {
|
||||
this.triggerNotification('onBackupFailed', eventData)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('../models/LibraryItem')} libraryItem
|
||||
*/
|
||||
async onLibraryItemAdded(libraryItem) {
|
||||
|
||||
try {
|
||||
if (!Database.notificationSettings.isUseable) return
|
||||
|
||||
if (!Database.notificationSettings.getHasActiveNotificationsForEvent('onLibraryItemAdded')) {
|
||||
Logger.debug(`[NotificationManager] onLibraryItemAdded: No active notifications`)
|
||||
return
|
||||
}
|
||||
|
||||
const eventData = {
|
||||
itemTitle: libraryItem.title,
|
||||
itemAuthor: libraryItem.authorNamesFirstLast,
|
||||
mediaType: libraryItem.mediaType
|
||||
}
|
||||
this.triggerNotification('onLibraryItemAdded', eventData)
|
||||
} catch (error) {
|
||||
Logger.error('[NotificationManager] Error in onLibraryItemAdded:', error)
|
||||
}
|
||||
}
|
||||
|
||||
onTest() {
|
||||
this.triggerNotification('onTest')
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ const LibraryItemScanner = require('./LibraryItemScanner')
|
||||
const LibraryScan = require('./LibraryScan')
|
||||
const LibraryItemScanData = require('./LibraryItemScanData')
|
||||
const Task = require('../objects/Task')
|
||||
const NotificationManager = require('../managers/NotificationManager')
|
||||
|
||||
class LibraryScanner {
|
||||
constructor() {
|
||||
@@ -141,6 +142,7 @@ class LibraryScanner {
|
||||
* @returns {Promise<boolean>} true if scan canceled
|
||||
*/
|
||||
async scanLibrary(libraryScan, forceRescan) {
|
||||
|
||||
// Make sure library filter data is set
|
||||
// this is used to check for existing authors & series
|
||||
await libraryFilters.getFilterData(libraryScan.libraryMediaType, libraryScan.libraryId)
|
||||
@@ -628,6 +630,7 @@ class LibraryScanner {
|
||||
const newLibraryItem = await LibraryItemScanner.scanPotentialNewLibraryItem(fullPath, library, folder, isSingleMediaItem)
|
||||
if (newLibraryItem) {
|
||||
SocketAuthority.libraryItemEmitter('item_added', newLibraryItem)
|
||||
NotificationManager.onLibraryItemAdded(newLibraryItem)
|
||||
}
|
||||
itemGroupingResults[itemDir] = newLibraryItem ? ScanResult.ADDED : ScanResult.NOTHING
|
||||
}
|
||||
|
||||
@@ -60,6 +60,22 @@ module.exports.notificationData = {
|
||||
errorMsg: 'Example error message'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'onLibraryItemAdded',
|
||||
requiresLibrary: true,
|
||||
description: 'Triggered when a new item is added to a library',
|
||||
descriptionKey: 'NotificationOnLibraryItemAddedDescription',
|
||||
variables: ['itemTitle', 'itemAuthor', 'mediaType'],
|
||||
defaults: {
|
||||
title: 'New {{mediaType}} Added!',
|
||||
body: '{{itemTitle}} by {{itemAuthor}} has been added to one of your libraries.'
|
||||
},
|
||||
testData: {
|
||||
itemTitle: 'Test Item',
|
||||
itemAuthor: 'Test Author',
|
||||
mediaType: 'Book'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'onTest',
|
||||
requiresLibrary: false,
|
||||
@@ -67,11 +83,19 @@ module.exports.notificationData = {
|
||||
descriptionKey: 'NotificationOnTestDescription',
|
||||
variables: ['version'],
|
||||
defaults: {
|
||||
title: 'Test Notification on Abs {{version}}',
|
||||
body: 'Test notificataion body for abs {{version}}.'
|
||||
title: 'New Book Added!',
|
||||
body: '{{bookTitle}} by {{bookAuthor}} has been added to {{libraryName}} library.'
|
||||
},
|
||||
testData: {
|
||||
version: 'v' + version
|
||||
libraryItemId: 'li_notification_test',
|
||||
libraryId: 'lib_test',
|
||||
libraryName: 'Audiobooks',
|
||||
mediaTags: 'TestTag1, TestTag2',
|
||||
bookTitle: 'Test Book',
|
||||
bookAuthor: 'Test Author',
|
||||
bookDescription: 'Description of the test book.',
|
||||
bookGenres: 'TestGenre1, TestGenre2',
|
||||
bookNarrator: 'Test Narrator'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user