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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
4 changes: 4 additions & 0 deletions server/scanner/LibraryScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -284,6 +285,7 @@ class LibraryScanner {
'items_added',
newOldLibraryItems.map((li) => li.toJSONExpanded())
)
NotificationManager.onItemsAdded(newOldLibraryItems)
newOldLibraryItems = []
}

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

Expand Down Expand Up @@ -647,6 +650,7 @@ class LibraryScanner {
if (newLibraryItem) {
const oldNewLibraryItem = Database.libraryItemModel.getOldLibraryItem(newLibraryItem)
SocketAuthority.emitter('item_added', oldNewLibraryItem.toJSONExpanded())
NotificationManager.onItemsAdded([oldNewLibraryItem])
}
itemGroupingResults[itemDir] = newLibraryItem ? ScanResult.ADDED : ScanResult.NOTHING
}
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