Skip to content

Commit 1662e03

Browse files
committed
feat: Filter "Item Added" notification by library
1 parent 3c4b5f2 commit 1662e03

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

Jellyfin.Plugin.Streamyfin/Configuration/Notifications/Notifications.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ public class NotificationConfiguration
1919
public double? RecentEventThreshold { get; set; }
2020
}
2121

22+
public class ItemAddedNotificationConfiguration: NotificationConfiguration
23+
{
24+
[Display(Name = "Enabled libraries", Description = "Enter all library Ids you want to receive notifications from")]
25+
[JsonPropertyName(name: "enabledLibraries")]
26+
public string[] EnabledLibraries { get; set; }
27+
}
28+
2229
public class UserNotificationConfig : NotificationConfiguration
2330
{
2431
[Display(Name = "Jellyfin User Ids", Description = "List of jellyfin user ids that this notification is for.")]
@@ -54,5 +61,5 @@ public class Notifications
5461
[NotNull]
5562
[Display(Name = "Item added", Description = "Get notified when jellyfin adds new Movies or Episodes")]
5663
[JsonPropertyName(name: "itemAdded")]
57-
public NotificationConfiguration? ItemAdded { get; set; }
64+
public ItemAddedNotificationConfiguration? ItemAdded { get; set; }
5865
}

Jellyfin.Plugin.Streamyfin/Pages/Notifications/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ <h2 class="sectionTitle">Notifications</h2>
4141
This notification consolidates the amount of episodes added for a season into one notification if possible to prevent spam.
4242
</div>
4343
</div>
44+
<p>Enabled Libraries</p>
45+
<div class="fieldDescription">
46+
Restrict notifications to specific libraries or leave unchecked to provide no restriction.
47+
</div>
48+
<input id="hidden-library-input" type="hidden" data-key-name="itemAdded" data-prop-name="enabledLibraries" data-is-array="true"/>
49+
<div id="item-library-container" class="checkboxContainer checkboxContainer-withDescription" style="margin-top: 16px">
50+
</div>
4451
<div class="inputContainer">
4552
<label class="inputLabel inputLabelUnfocused" for="item-added-threshold">Recent Event Threshold</label>
4653
<input id="item-added-threshold" data-key-name="itemAdded" data-prop-name="recentEventThreshold" min="0" type="number" is="emby-input">

Jellyfin.Plugin.Streamyfin/Pages/Notifications/index.js

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const saveBtn = document.getElementById('save-notification-btn');
2+
const libraryContainer = document.getElementById('item-library-container');
3+
const hiddenLibraryInput = document.getElementById('hidden-library-input');
24

35
const getValues = () => ({
46
notifications: Array.from(document.querySelectorAll('[data-key-name][data-prop-name]')).reduce((acc, el) => {
@@ -39,7 +41,7 @@ export default function (view, params) {
3941

4042
// init code here
4143
view.addEventListener('viewshow', (e) => {
42-
import(window.ApiClient.getUrl("web/configurationpage?name=shared.js")).then((shared) => {
44+
import(window.ApiClient.getUrl("web/configurationpage?name=shared.js")).then(async (shared) => {
4345
shared.setPage("Notifications");
4446

4547
document.getElementById("notification-endpoint").innerText = shared.NOTIFICATION_URL
@@ -52,6 +54,60 @@ export default function (view, params) {
5254
shared.setDomValues(document, notifications);
5355
})
5456

57+
const folders = await window.ApiClient.get("/Library/VirtualFolders")
58+
.then((response) => response.json())
59+
60+
if (folders.length === 0) {
61+
libraryContainer.append("No libraries available")
62+
}
63+
64+
folders.forEach(folder => {
65+
if (!document.getElementById(folder.ItemId)) {
66+
const checkboxContainer = document.createElement("label")
67+
const checkboxInput = document.createElement("input")
68+
const checkboxLabel = document.createElement("span")
69+
70+
checkboxContainer.className = "emby-checkbox-label"
71+
72+
checkboxInput.setAttribute("id", folder.ItemId)
73+
checkboxInput.setAttribute("type", "checkbox")
74+
checkboxInput.setAttribute("is", "emby-checkbox")
75+
checkboxInput.checked = (shared
76+
.getConfig().notifications.itemAdded.enabledLibraries || [])
77+
.includes(folder.ItemId) == true
78+
79+
80+
shared.keyedEventListener(checkboxInput, 'change', function () {
81+
const isEnabled = checkboxInput.checked
82+
let currentList = hiddenLibraryInput.value.split(",").filter(Boolean)
83+
84+
if (isEnabled)
85+
currentList = [...new Set(currentList.concat(folder.ItemId))]
86+
else
87+
currentList = currentList.filter(id => id !== folder.ItemId)
88+
89+
hiddenLibraryInput.value = currentList.join(",")
90+
91+
shared.setConfig(updateNotificationConfig(
92+
"itemAdded",
93+
shared.getConfig(),
94+
"enabledLibraries",
95+
shared.getElValue(hiddenLibraryInput)
96+
));
97+
})
98+
99+
checkboxLabel.className = "checkboxLabel"
100+
checkboxLabel.innerText = folder.Name
101+
102+
checkboxContainer.append(
103+
checkboxInput,
104+
checkboxLabel
105+
)
106+
107+
libraryContainer.append(checkboxContainer)
108+
}
109+
})
110+
55111
document.querySelectorAll('[data-key-name][data-prop-name]').forEach(el => {
56112
shared.keyedEventListener(el, 'change', function () {
57113
shared.setConfig(updateNotificationConfig(
@@ -65,12 +121,6 @@ export default function (view, params) {
65121

66122
shared.keyedEventListener(saveBtn, 'click', function (e) {
67123
e.preventDefault();
68-
const config = shared.getConfig();
69-
70-
shared.setConfig({
71-
...config,
72-
...getValues()
73-
});
74124
shared.saveConfig()
75125
})
76126
})

Jellyfin.Plugin.Streamyfin/Pages/shared.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export const StreamyfinTabs = () => [
162162
// region on Shared init
163163
if (!window.Streamyfin?.shared) {
164164
// import json-yaml library
165-
import(window.ApiClient.getUrl("web/configurationpage?name=js-yaml.js")).then((jsYaml) => {
165+
await import(window.ApiClient.getUrl("web/configurationpage?name=js-yaml.js")).then(async (jsYaml) => {
166166
tools.jsYaml = jsYaml;
167167

168168
//fetch default configuration
@@ -175,13 +175,14 @@ if (!window.Streamyfin?.shared) {
175175

176176
// fetch schema
177177
// We want to define any pages first before setting any values
178-
fetch(SCHEMA_URL)
178+
await fetch(SCHEMA_URL)
179179
.then(async (response) => setSchema(await response.json()))
180-
.then(() => {
180+
.then(async () => {
181181

182182
// fetch configuration
183-
window.ApiClient.ajax({type: 'GET', url: YAML_URL, contentType: 'application/json'})
183+
await window.ApiClient.ajax({type: 'GET', url: YAML_URL, contentType: 'application/json'})
184184
.then(async function (response) {
185+
console.log("Getting actual config")
185186
const {Value} = await response.json();
186187
setYamlConfig(Value)
187188
})

Jellyfin.Plugin.Streamyfin/PushNotifications/Events/ItemAdded/ItemAddedService.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Jellyfin.Plugin.Streamyfin.PushNotifications.Events.ItemAdded;
99
using Jellyfin.Plugin.Streamyfin.PushNotifications.models;
1010
using MediaBrowser.Controller;
11-
using MediaBrowser.Controller.Entities;
1211
using MediaBrowser.Controller.Entities.Movies;
1312
using MediaBrowser.Controller.Entities.TV;
1413
using MediaBrowser.Controller.Library;
@@ -43,6 +42,23 @@ private void ItemAddedHandler(object? sender, ItemChangeEventArgs itemChangeEven
4342
) return;
4443

4544
var item = itemChangeEventArgs.Item;
45+
var enabledLibraries = Config.notifications.ItemAdded.EnabledLibraries;
46+
var virtualFolder = _libraryManager.GetVirtualFolders()
47+
.Find(folder => folder.Locations.Any(location => item?.Path?.Contains(location) == true));
48+
49+
if (
50+
virtualFolder != null &&
51+
enabledLibraries.Length > 0 &&
52+
!enabledLibraries.Contains(virtualFolder.ItemId)
53+
)
54+
{
55+
_logger.LogInformation(
56+
"Failed to notify about item {0} - {1}. Library {2} currently not enabled for notifications: ",
57+
item.GetType().Name, item.Name.Escape(), virtualFolder.Name
58+
);
59+
return;
60+
}
61+
4662
_logger.LogInformation("Item added is {0} - {1}", item.GetType().Name, item.Name.Escape());
4763

4864
switch (item)

0 commit comments

Comments
 (0)