Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notification support for new items being added to a library #3464

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/objects/Notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ components:
notificationEventName:
type: string
description: The name of the event the notification will fire on.
enum: ['onPodcastEpisodeDownloaded', 'onBackupCompleted', 'onBackupFailed', 'onTest']
enum: ['onItemsAdded', 'onPodcastEpisodeDownloaded', 'onBackupCompleted', 'onBackupFailed', 'onTest']
urls:
type: array
items:
Expand Down
1 change: 1 addition & 0 deletions docs/openapi.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will want to edit the file in docs/objects/Notification.yaml to add the new element to the NotificationEventName. The docs/openapi.json is automatically generated from those files and should not be edited directly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, whoops. I misread the previous edit, disregard.

Original file line number Diff line number Diff line change
Expand Up @@ -3225,6 +3225,7 @@
"type": "string",
"description": "The name of the event the notification will fire on.",
"enum": [
"onItemsAdded",
"onPodcastEpisodeDownloaded",
"onBackupCompleted",
"onBackupFailed",
Expand Down
26 changes: 26 additions & 0 deletions server/managers/NotificationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ class NotificationManager {
return notificationData
}

async onItemsAdded(LibraryItems) {
if (!Database.notificationSettings.isUseable) return

if (!Database.notificationSettings.getHasActiveNotificationsForEvent('onItemsAdded')) {
Logger.debug(`[NotificationManager] onItemsAdded: No active notifications`)
return
}

for (const item of LibraryItems) {
Logger.debug(`[NotificationManager] onItemsAdded: Item "${item.media.metadata.title}"`)
const library = await Database.libraryModel.findByPk(item.libraryId)
const eventData = {
libraryItemId: item.id,
libraryId: item.libraryId,
libraryName: library?.name || 'Unknown',
tags: (item.media.tags || []).join(', ') || 'None',
title: item.media.metadata.title,
authors: (item.media.metadata.authors.map(a => a.name) || []).join(', ') || '',
description: item.media.metadata.description || '',
genres: (item.media.metadata.genres || []).join(', ') || 'None',
publishedYear: item.media.metadata.publishedYear || '',
}
this.triggerNotification('onItemsAdded', eventData)
}
}

async onPodcastEpisodeDownloaded(libraryItem, episode) {
if (!Database.notificationSettings.isUseable) return

Expand Down
8 changes: 6 additions & 2 deletions server/scanner/LibraryScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const LibraryItemScanData = require('./LibraryItemScanData')
const Task = require('../objects/Task')

class LibraryScanner {
constructor() {
constructor(notificationManager) {
this.notificationManager = notificationManager
this.cancelLibraryScan = {}
/** @type {string[]} - library ids */
this.librariesScanning = []
Expand Down Expand Up @@ -284,6 +285,7 @@ class LibraryScanner {
'items_added',
newOldLibraryItems.map((li) => li.toJSONExpanded())
)
this.notificationManager.onItemsAdded(newOldLibraryItems)
newOldLibraryItems = []
}

Expand All @@ -296,6 +298,7 @@ class LibraryScanner {
'items_added',
newOldLibraryItems.map((li) => li.toJSONExpanded())
)
this.notificationManager.onItemsAdded(newOldLibraryItems)
}
}

Expand Down Expand Up @@ -647,14 +650,15 @@ class LibraryScanner {
if (newLibraryItem) {
const oldNewLibraryItem = Database.libraryItemModel.getOldLibraryItem(newLibraryItem)
SocketAuthority.emitter('item_added', oldNewLibraryItem.toJSONExpanded())
this.notificationManager.onItemsAdded([oldNewLibraryItem])
}
itemGroupingResults[itemDir] = newLibraryItem ? ScanResult.ADDED : ScanResult.NOTHING
}

return itemGroupingResults
}
}
module.exports = new LibraryScanner()
module.exports = new LibraryScanner(new NotificationManager())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server doesn't run because NotificationManager is not defined.
We wouldn't want to instantiate a new instance of NotificationManager though because it is already instantiated in Server.js.
The NotificationManager could be made a singleton like the LibraryScanner so it can be imported anywhere

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to a singleton so you can merge master to fix
7cd8d7f

Copy link
Author

@silvercolt silvercolt Sep 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I ran the server just fine with the new instantiation of NotificationManager here. Maybe I missed something when migrating my changes from a branch off master to my fork. Agreed, a singleton would have been better, but I didn't feel comfortable making a sweeping change like that, so I appreciate the enhancement. I will update accordingly.


function ItemToFileInoMatch(libraryItem1, libraryItem2) {
return libraryItem1.isFile && libraryItem2.libraryFiles.some((lf) => lf.ino === libraryItem1.ino)
Expand Down
22 changes: 22 additions & 0 deletions server/utils/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@ const { version } = require('../../package.json')

module.exports.notificationData = {
events: [
{
name: 'onItemsAdded',
requiresLibrary: true,
libraryMediaType: 'item',
description: 'Triggered when an item is added to the library',
variables: ['libraryItemId', 'libraryId', 'libraryName', 'tags', 'title', 'authors', 'description', 'genres', 'publishedYear'],
defaults: {
title: 'New Book!',
body: '{{title}} has been added to {{libraryName}} library.'
},
testData: {
libraryItemId: 'li_notification_test',
libraryId: 'lib_test',
libraryName: 'My Library',
tags: 'TestTag1, TestTag2',
title: 'ABS Test Book',
authors: 'Author1, Author2',
description: 'Description of the Abs Test Book belongs here.',
genres: 'TestGenre1, TestGenre2',
publishedYear: '2020'
}
},
{
name: 'onPodcastEpisodeDownloaded',
requiresLibrary: true,
Expand Down