jellyfin/MediaBrowser.Model/Notifications/NotificationOptions.cs

132 lines
4.3 KiB
C#
Raw Normal View History

#nullable disable
2020-02-04 01:49:27 +01:00
#pragma warning disable CS1591
using System;
2020-05-20 19:07:53 +02:00
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
2018-12-28 00:27:57 +01:00
namespace MediaBrowser.Model.Notifications
{
public class NotificationOptions
{
public NotificationOptions()
{
Options = new[]
{
new NotificationOption(NotificationType.TaskFailed.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.ServerRestartRequired.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.ApplicationUpdateAvailable.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.ApplicationUpdateInstalled.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.PluginUpdateInstalled.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.PluginUninstalled.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.InstallationFailed.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.PluginInstalled.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.PluginError.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
},
new NotificationOption(NotificationType.UserLockedOut.ToString())
2018-12-28 00:27:57 +01:00
{
Enabled = true,
SendToUserMode = SendToUserType.Admins
}
};
}
public NotificationOption[] Options { get; set; }
2018-12-28 00:27:57 +01:00
public NotificationOption GetOptions(string type)
{
foreach (NotificationOption i in Options)
2018-12-28 00:27:57 +01:00
{
2020-05-01 16:48:33 +02:00
if (string.Equals(type, i.Type, StringComparison.OrdinalIgnoreCase))
{
return i;
}
2018-12-28 00:27:57 +01:00
}
2020-05-01 16:48:33 +02:00
2018-12-28 00:27:57 +01:00
return null;
}
public bool IsEnabled(string type)
{
NotificationOption opt = GetOptions(type);
2018-12-28 00:27:57 +01:00
2022-12-05 15:01:13 +01:00
return opt is not null && opt.Enabled;
2018-12-28 00:27:57 +01:00
}
public bool IsServiceEnabled(string service, string notificationType)
{
NotificationOption opt = GetOptions(notificationType);
2018-12-28 00:27:57 +01:00
2022-12-05 15:00:20 +01:00
return opt is null
2021-12-20 13:31:07 +01:00
|| !opt.DisabledServices.Contains(service, StringComparison.OrdinalIgnoreCase);
2018-12-28 00:27:57 +01:00
}
public bool IsEnabledToMonitorUser(string type, Guid userId)
{
NotificationOption opt = GetOptions(type);
2018-12-28 00:27:57 +01:00
2022-12-05 15:01:13 +01:00
return opt is not null
2021-04-19 11:38:27 +02:00
&& opt.Enabled
2021-12-20 13:31:07 +01:00
&& !opt.DisabledMonitorUsers.Contains(userId.ToString("N"), StringComparison.OrdinalIgnoreCase);
2018-12-28 00:27:57 +01:00
}
2020-05-20 19:07:53 +02:00
public bool IsEnabledToSendToUser(string type, string userId, User user)
2018-12-28 00:27:57 +01:00
{
NotificationOption opt = GetOptions(type);
2018-12-28 00:27:57 +01:00
2022-12-05 15:01:13 +01:00
if (opt is not null && opt.Enabled)
2018-12-28 00:27:57 +01:00
{
if (opt.SendToUserMode == SendToUserType.All)
{
return true;
}
2020-05-13 04:10:35 +02:00
if (opt.SendToUserMode == SendToUserType.Admins && user.HasPermission(PermissionKind.IsAdministrator))
2018-12-28 00:27:57 +01:00
{
return true;
}
2021-12-20 13:31:07 +01:00
return opt.SendToUsers.Contains(userId, StringComparison.OrdinalIgnoreCase);
2018-12-28 00:27:57 +01:00
}
return false;
}
}
}