diff --git a/src/NzbDrone.Core.Test/NotificationTests/GotifyTests/GotifyServiceFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/GotifyTests/GotifyServiceFixture.cs new file mode 100644 index 0000000000..d1de668fd7 --- /dev/null +++ b/src/NzbDrone.Core.Test/NotificationTests/GotifyTests/GotifyServiceFixture.cs @@ -0,0 +1,68 @@ +using System; +using System.Linq; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Notifications; +using NzbDrone.Core.Notifications.Gotify; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.NotificationTests +{ + [TestFixture] + public class GotifyServiceFixture : CoreTest + { + [SetUp] + public void Setup() + { + Subject.Definition = new NotificationDefinition + { + Settings = new GotifySettings + { + Server = "https://example.invalid", + AppToken = "token", + Priority = 5, + IncludeMoviePoster = false, + IncludeInstanceNameInTitle = false, + MetadataLinks = Enumerable.Empty(), + PreferredMetadataLink = (int)MetadataLinkType.Tmdb + } + }; + } + + [TestCase(false, "MyRadarr", false)] + [TestCase(true, "MyRadarr", true)] + [TestCase(true, "", false)] + [TestCase(true, " ", false)] + public void OnDownload_should_append_instance_name_to_title_only_when_enabled_and_non_empty(bool includeInstanceNameInTitle, string instanceName, bool shouldAppendInstanceName) + { + ((GotifySettings)Subject.Definition.Settings).IncludeInstanceNameInTitle = includeInstanceNameInTitle; + + Mocker.GetMock() + .SetupGet(c => c.InstanceName) + .Returns(instanceName); + + var message = new DownloadMessage + { + Movie = new Movie { Title = "Movie" }, + Message = "downloaded" + }; + + Subject.OnDownload(message); + + var suffix = $" - {instanceName}"; + + Predicate titleHasCorrectSuffix = m => + shouldAppendInstanceName + ? m.Title.EndsWith(suffix) + : !m.Title.EndsWith(" - ") && !m.Title.EndsWith(suffix); + + Mocker.GetMock() + .Verify(p => p.SendNotification( + It.Is(m => titleHasCorrectSuffix(m)), + It.IsAny()), + Times.Once()); + } + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 23a3b71ab8..d910e42f68 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -1282,6 +1282,8 @@ "NotificationsGotifySettingsPriorityHelpText": "Priority of the notification", "NotificationsGotifySettingsServer": "Gotify Server", "NotificationsGotifySettingsServerHelpText": "Gotify server URL, including http(s):// and port if needed", + "NotificationsGotifySettingsIncludeAppName": "Include {appName} in Title", + "NotificationsGotifySettingsIncludeAppNameHelpText": "Optionally suffix message title with {appName} to differentiate notifications from different applications", "NotificationsJoinSettingsApiKeyHelpText": "The API Key from your Join account settings (click Join API button).", "NotificationsJoinSettingsDeviceIds": "Device IDs", "NotificationsJoinSettingsDeviceIdsHelpText": "Deprecated, use Device Names instead. Comma separated list of Device IDs you'd like to send notifications to. If unset, all devices will receive notifications.", @@ -1383,7 +1385,7 @@ "NotificationsTelegramSettingsChatId": "Chat ID", "NotificationsTelegramSettingsChatIdHelpText": "You must start a conversation with the bot or add it to your group to receive messages", "NotificationsTelegramSettingsIncludeAppName": "Include {appName} in Title", - "NotificationsTelegramSettingsIncludeAppNameHelpText": "Optionally prefix message title with {appName} to differentiate notifications from different applications", + "NotificationsTelegramSettingsIncludeAppNameHelpText": "Optionally suffix message title with {appName} to differentiate notifications from different applications", "NotificationsTelegramSettingsIncludeInstanceName": "Include Instance Name in Title", "NotificationsTelegramSettingsIncludeInstanceNameHelpText": "Optionally include Instance name in notification", "NotificationsTelegramSettingsMetadataLinks": "Metadata Links", diff --git a/src/NzbDrone.Core/Notifications/Gotify/Gotify.cs b/src/NzbDrone.Core/Notifications/Gotify/Gotify.cs index ca889e1c7b..9973cf2802 100644 --- a/src/NzbDrone.Core/Notifications/Gotify/Gotify.cs +++ b/src/NzbDrone.Core/Notifications/Gotify/Gotify.cs @@ -5,6 +5,7 @@ using FluentValidation.Results; using NLog; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Localization; using NzbDrone.Core.MediaCover; using NzbDrone.Core.Movies; @@ -17,12 +18,14 @@ public class Gotify : NotificationBase private readonly IGotifyProxy _proxy; private readonly ILocalizationService _localizationService; + private readonly IConfigFileProvider _configFileProvider; private readonly Logger _logger; - public Gotify(IGotifyProxy proxy, ILocalizationService localizationService, Logger logger) + public Gotify(IGotifyProxy proxy, ILocalizationService localizationService, IConfigFileProvider configFileProvider, Logger logger) { _proxy = proxy; _localizationService = localizationService; + _configFileProvider = configFileProvider; _logger = logger; } @@ -81,11 +84,16 @@ public override ValidationResult Test() try { var isMarkdown = false; - const string title = "Test Notification"; + var title = "Test Notification"; var sb = new StringBuilder(); sb.AppendLine("This is a test message from Radarr"); + if (Settings.IncludeInstanceNameInTitle && _configFileProvider.InstanceName.IsNotNullOrWhiteSpace()) + { + title += $" - {_configFileProvider.InstanceName}"; + } + var payload = new GotifyMessage { Title = title, @@ -130,6 +138,11 @@ private void SendNotification(string title, string message, Movie movie) sb.AppendLine(message); + if (Settings.IncludeInstanceNameInTitle && _configFileProvider.InstanceName.IsNotNullOrWhiteSpace()) + { + title += $" - {_configFileProvider.InstanceName}"; + } + var payload = new GotifyMessage { Title = title, diff --git a/src/NzbDrone.Core/Notifications/Gotify/GotifySettings.cs b/src/NzbDrone.Core/Notifications/Gotify/GotifySettings.cs index 9920fe9fdf..ecffd44745 100644 --- a/src/NzbDrone.Core/Notifications/Gotify/GotifySettings.cs +++ b/src/NzbDrone.Core/Notifications/Gotify/GotifySettings.cs @@ -64,10 +64,13 @@ public GotifySettings() [FieldDefinition(3, Label = "NotificationsGotifySettingIncludeMoviePoster", Type = FieldType.Checkbox, HelpText = "NotificationsGotifySettingIncludeMoviePosterHelpText")] public bool IncludeMoviePoster { get; set; } - [FieldDefinition(4, Label = "NotificationsGotifySettingsMetadataLinks", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsGotifySettingsMetadataLinksMovieHelpText")] + [FieldDefinition(4, Label = "NotificationsTelegramSettingsIncludeAppName", Type = FieldType.Checkbox, HelpText = "NotificationsTelegramSettingsIncludeAppNameHelpText")] + public bool IncludeInstanceNameInTitle { get; set; } + + [FieldDefinition(5, Label = "NotificationsGotifySettingsMetadataLinks", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsGotifySettingsMetadataLinksMovieHelpText")] public IEnumerable MetadataLinks { get; set; } - [FieldDefinition(5, Label = "NotificationsGotifySettingsPreferredMetadataLink", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsGotifySettingsPreferredMetadataLinkHelpText")] + [FieldDefinition(6, Label = "NotificationsGotifySettingsPreferredMetadataLink", Type = FieldType.Select, SelectOptions = typeof(MetadataLinkType), HelpText = "NotificationsGotifySettingsPreferredMetadataLinkHelpText")] public int PreferredMetadataLink { get; set; } public override NzbDroneValidationResult Validate()