New: Multiple Recipients on Email Notifications (Also CC, BCC)

pull/2/head
Qstick 5 years ago
parent 57961df1df
commit 30def1f53a

@ -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<email_multiple_addresses>
{
[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<ProviderDefinition166>("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<int> 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<string> To { get; set; }
}
}

@ -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<EmailSettingsValidator>
{
private EmailSettings _emailSettings;
private TestValidator<EmailSettings> _validator;
[SetUp]
public void Setup()
{
_validator = new TestValidator<EmailSettings>
{
v => v.RuleFor(s => s).SetValidator(Subject)
};
_emailSettings = Builder<EmailSettings>.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();
}
}
}

@ -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<ProviderDefinition166>($"SELECT Id, Settings FROM Notifications WHERE Implementation = 'Email'");
var corrected = new List<ProviderDefinition166>();
foreach (var row in rows)
{
var settings = JsonSerializer.Deserialize<EmailSettings173>(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; }
}
}
}

@ -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;

@ -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<string> To { get; set; }
[FieldDefinition(7, Label = "CC Address(es)", HelpText = "Comma seperated list of email cc recipients", Advanced = true)]
public IEnumerable<string> CC { get; set; }
[FieldDefinition(8, Label = "BCC Address(es)", HelpText = "Comma seperated list of email bcc recipients", Advanced = true)]
public IEnumerable<string> Bcc { get; set; }
public NzbDroneValidationResult Validate()
{

@ -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());
}
};
}

Loading…
Cancel
Save