From 30def1f53a310a44a048fbd99a8023bba4b9c602 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 13 Apr 2020 02:52:06 -0400 Subject: [PATCH] New: Multiple Recipients on Email Notifications (Also CC, BCC) --- .../174_email_multiple_addressesFixture.cs | 87 ++++++++++++++ .../EmailSettingsValidatorFixture.cs | 113 ++++++++++++++++++ .../Migration/174_email_multiple_addresses.cs | 96 +++++++++++++++ .../Notifications/Email/EmailService.cs | 7 +- .../Notifications/Email/EmailSettings.cs | 29 ++++- src/Radarr.Http/ClientSchema/SchemaBuilder.cs | 2 +- 6 files changed, 326 insertions(+), 8 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/174_email_multiple_addressesFixture.cs create mode 100644 src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/174_email_multiple_addresses.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/174_email_multiple_addressesFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/174_email_multiple_addressesFixture.cs new file mode 100644 index 000000000..f409af468 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/174_email_multiple_addressesFixture.cs @@ -0,0 +1,87 @@ +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using Newtonsoft.Json.Linq; +using NUnit.Framework; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class email_multiple_addressesFixture : MigrationTest + { + [Test] + public void should_convert_to_list_on_email_lists() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Notifications").Row(new + { + OnGrab = true, + OnDownload = true, + OnUpgrade = true, + OnHealthIssue = true, + IncludeHealthWarnings = true, + OnRename = true, + Name = "Gmail Radarr", + Implementation = "Email", + Tags = "[]", + Settings = new EmailSettings173 + { + Server = "smtp.gmail.com", + Port = 563, + To = "radarr@radarr.video" + }.ToJson(), + ConfigContract = "EmailSettings" + }); + }); + + var items = db.Query("SELECT * FROM Notifications"); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Email"); + items.First().ConfigContract.Should().Be("EmailSettings"); + items.First().Settings.To.Count().Should().Be(1); + } + } + + public class ProviderDefinition166 + { + public int Id { get; set; } + public string Implementation { get; set; } + public string ConfigContract { get; set; } + public EmailSettings174 Settings { get; set; } + public string Name { get; set; } + public bool OnGrab { get; set; } + public bool OnDownload { get; set; } + public bool OnUpgrade { get; set; } + public bool OnRename { get; set; } + public bool OnHealthIssue { get; set; } + public bool IncludeHealthWarnings { get; set; } + public List Tags { get; set; } + } + + public class EmailSettings173 + { + public string Server { get; set; } + public int Port { get; set; } + public bool Ssl { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public string To { get; set; } + } + + public class EmailSettings174 + { + public string Server { get; set; } + public int Port { get; set; } + public bool Ssl { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public IEnumerable To { get; set; } + } +} diff --git a/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs new file mode 100644 index 000000000..db729edb2 --- /dev/null +++ b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs @@ -0,0 +1,113 @@ +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Notifications.Email; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; + +namespace NzbDrone.Core.Test.NotificationTests.EmailTests +{ + [TestFixture] + public class EmailSettingsValidatorFixture : CoreTest + { + private EmailSettings _emailSettings; + private TestValidator _validator; + + [SetUp] + public void Setup() + { + _validator = new TestValidator + { + v => v.RuleFor(s => s).SetValidator(Subject) + }; + + _emailSettings = Builder.CreateNew() + .With(s => s.Server = "someserver") + .With(s => s.Port = 567) + .With(s => s.Ssl = true) + .With(s => s.From = "radarr@radarr.video") + .With(s => s.To = new string[] { "radarr@radarr.video" }) + .Build(); + } + + [Test] + public void should_be_valid_if_all_settings_valid() + { + _validator.Validate(_emailSettings).IsValid.Should().BeTrue(); + } + + [Test] + public void should_not_be_valid_if_port_is_out_of_range() + { + _emailSettings.Port = 900000; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [Test] + public void should_not_be_valid_if_server_is_empty() + { + _emailSettings.Server = ""; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [Test] + public void should_not_be_valid_if_from_is_empty() + { + _emailSettings.From = ""; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [TestCase("radarr")] + [TestCase("radarr@radarr")] + [TestCase("radarr.video")] + public void should_not_be_valid_if_from_is_invalid(string email) + { + _emailSettings.From = email; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [TestCase("radarr")] + [TestCase("radarr@radarr")] + [TestCase("radarr.video")] + public void should_not_be_valid_if_to_is_invalid(string email) + { + _emailSettings.To = new string[] { email }; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [TestCase("radarr")] + [TestCase("radarr@radarr")] + [TestCase("radarr.video")] + public void should_not_be_valid_if_cc_is_invalid(string email) + { + _emailSettings.CC = new string[] { email }; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [TestCase("radarr")] + [TestCase("radarr@radarr")] + [TestCase("radarr.video")] + public void should_not_be_valid_if_bcc_is_invalid(string email) + { + _emailSettings.Bcc = new string[] { email }; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + + [Test] + public void should_not_be_valid_if_to_bcc_cc_are_all_empty() + { + _emailSettings.To = new string[] { }; + _emailSettings.CC = new string[] { }; + _emailSettings.Bcc = new string[] { }; + + _validator.Validate(_emailSettings).IsValid.Should().BeFalse(); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/174_email_multiple_addresses.cs b/src/NzbDrone.Core/Datastore/Migration/174_email_multiple_addresses.cs new file mode 100644 index 000000000..d01375b26 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/174_email_multiple_addresses.cs @@ -0,0 +1,96 @@ +using System.Collections.Generic; +using System.Data; +using System.Text.Json; +using Dapper; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(174)] + public class email_multiple_addresses : NzbDroneMigrationBase + { + private readonly JsonSerializerOptions _serializerSettings; + + public email_multiple_addresses() + { + _serializerSettings = new JsonSerializerOptions + { + AllowTrailingCommas = true, + IgnoreNullValues = false, + PropertyNameCaseInsensitive = true, + DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }; + } + + protected override void MainDbUpgrade() + { + Execute.WithConnection(ChangeEmailAddressType); + } + + private void ChangeEmailAddressType(IDbConnection conn, IDbTransaction tran) + { + var rows = conn.Query($"SELECT Id, Settings FROM Notifications WHERE Implementation = 'Email'"); + + var corrected = new List(); + + foreach (var row in rows) + { + var settings = JsonSerializer.Deserialize(row.Settings, _serializerSettings); + + var newSettings = new EmailSettings174 + { + Server = settings.Server, + Port = settings.Port, + Ssl = settings.Ssl, + Username = settings.Username, + Password = settings.Password, + From = settings.From, + To = new string[] { settings.To }, + CC = new string[] { }, + Bcc = new string[] { } + }; + + corrected.Add(new ProviderDefinition166 + { + Id = row.Id, + Settings = JsonSerializer.Serialize(newSettings, _serializerSettings) + }); + } + + var updateSql = "UPDATE Notifications SET Settings = @Settings WHERE Id = @Id"; + conn.Execute(updateSql, corrected, transaction: tran); + } + + private class ProviderDefinition166 : ModelBase + { + public string Settings { get; set; } + } + + private class EmailSettings173 + { + public string Server { get; set; } + public int Port { get; set; } + public bool Ssl { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public string To { get; set; } + } + + private class EmailSettings174 + { + public string Server { get; set; } + public int Port { get; set; } + public bool Ssl { get; set; } + public string Username { get; set; } + public string Password { get; set; } + public string From { get; set; } + public string[] To { get; set; } + public string[] CC { get; set; } + public string[] Bcc { get; set; } + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Email/EmailService.cs b/src/NzbDrone.Core/Notifications/Email/EmailService.cs index 8f8f88a62..8a82cb79d 100644 --- a/src/NzbDrone.Core/Notifications/Email/EmailService.cs +++ b/src/NzbDrone.Core/Notifications/Email/EmailService.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using System.Net; using System.Net.Mail; using FluentValidation.Results; @@ -26,7 +27,9 @@ namespace NzbDrone.Core.Notifications.Email var email = new MailMessage(); email.From = new MailAddress(settings.From); - email.To.Add(settings.To); + settings.To.ToList().ForEach(x => email.To.Add(x)); + settings.CC.ToList().ForEach(x => email.CC.Add(x)); + settings.Bcc.ToList().ForEach(x => email.Bcc.Add(x)); email.Subject = subject; email.Body = body; diff --git a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs index 793b7b163..750c9fca2 100644 --- a/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs +++ b/src/NzbDrone.Core/Notifications/Email/EmailSettings.cs @@ -1,4 +1,6 @@ -using FluentValidation; +using System.Collections.Generic; +using System.Linq; +using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Validation; @@ -11,8 +13,15 @@ namespace NzbDrone.Core.Notifications.Email { RuleFor(c => c.Server).NotEmpty(); RuleFor(c => c.Port).InclusiveBetween(1, 65535); - RuleFor(c => c.From).NotEmpty(); - RuleFor(c => c.To).NotEmpty(); + RuleFor(c => c.From).NotEmpty().EmailAddress(); + RuleForEach(c => c.To).EmailAddress(); + RuleForEach(c => c.CC).EmailAddress(); + RuleForEach(c => c.Bcc).EmailAddress(); + + // Only require one of three send fields to be set + RuleFor(c => c.To).NotEmpty().Unless(c => c.Bcc.Any() || c.CC.Any()); + RuleFor(c => c.CC).NotEmpty().Unless(c => c.To.Any() || c.Bcc.Any()); + RuleFor(c => c.Bcc).NotEmpty().Unless(c => c.To.Any() || c.CC.Any()); } } @@ -25,6 +34,10 @@ namespace NzbDrone.Core.Notifications.Email Server = "smtp.gmail.com"; Port = 587; Ssl = true; + + To = new string[] { }; + CC = new string[] { }; + Bcc = new string[] { }; } [FieldDefinition(0, Label = "Server", HelpText = "Hostname or IP of Email server")] @@ -45,8 +58,14 @@ namespace NzbDrone.Core.Notifications.Email [FieldDefinition(5, Label = "From Address")] public string From { get; set; } - [FieldDefinition(6, Label = "Recipient Address")] - public string To { get; set; } + [FieldDefinition(6, Label = "Recipient Address(es)", HelpText = "Comma seperated list of email recipients")] + public IEnumerable To { get; set; } + + [FieldDefinition(7, Label = "CC Address(es)", HelpText = "Comma seperated list of email cc recipients", Advanced = true)] + public IEnumerable CC { get; set; } + + [FieldDefinition(8, Label = "BCC Address(es)", HelpText = "Comma seperated list of email bcc recipients", Advanced = true)] + public IEnumerable Bcc { get; set; } public NzbDroneValidationResult Validate() { diff --git a/src/Radarr.Http/ClientSchema/SchemaBuilder.cs b/src/Radarr.Http/ClientSchema/SchemaBuilder.cs index a2897b736..eb8efc7da 100644 --- a/src/Radarr.Http/ClientSchema/SchemaBuilder.cs +++ b/src/Radarr.Http/ClientSchema/SchemaBuilder.cs @@ -210,7 +210,7 @@ namespace Radarr.Http.ClientSchema } else { - return fieldValue.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + return fieldValue.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()); } }; }