Skip to content

Commit 9855ce7

Browse files
author
Scott Bommarito
authored
Don't attempt to send emails if they have no recipients (#6637)
1 parent 5091aa4 commit 9855ce7

31 files changed

Lines changed: 87 additions & 150 deletions

src/NuGetGallery.Core/NuGetGallery.Core.csproj

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,8 @@
202202
<EmbeddedResource Include="Infrastructure\MigrateUserToOrganization.sql" />
203203
</ItemGroup>
204204
<ItemGroup>
205-
<PackageReference Include="NuGet.Services.Entities">
206-
<Version>2.32.0-agr-license-2170862</Version>
207-
</PackageReference>
208-
<PackageReference Include="NuGet.Services.Messaging">
209-
<Version>2.31.0</Version>
205+
<PackageReference Include="NuGet.Services.Messaging.Email">
206+
<Version>2.36.0</Version>
210207
</PackageReference>
211208
<PackageReference Include="NuGet.StrongName.AnglicanGeek.MarkdownMailer">
212209
<Version>1.2.0</Version>
@@ -241,12 +238,6 @@
241238
<PackageReference Include="NuGet.Packaging">
242239
<Version>5.0.0-preview1.5665</Version>
243240
</PackageReference>
244-
<PackageReference Include="NuGet.Services.Validation">
245-
<Version>2.36.0</Version>
246-
</PackageReference>
247-
<PackageReference Include="NuGet.Services.Validation.Issues">
248-
<Version>2.36.0</Version>
249-
</PackageReference>
250241
<PackageReference Include="WindowsAzure.Storage">
251242
<Version>7.1.2</Version>
252243
</PackageReference>

src/NuGetGallery/App_Start/DefaultDependenciesModule.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public static class BindingKeys
5858

5959
protected override void Load(ContainerBuilder builder)
6060
{
61+
var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false);
62+
var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration);
63+
builder.RegisterInstance(loggerFactory)
64+
.AsSelf()
65+
.As<ILoggerFactory>();
66+
builder.RegisterGeneric(typeof(Logger<>))
67+
.As(typeof(ILogger<>))
68+
.SingleInstance();
69+
6170
var telemetryClient = TelemetryClientWrapper.Instance;
6271
builder.RegisterInstance(telemetryClient)
6372
.AsSelf()
@@ -466,18 +475,11 @@ private static void RegisterAsynchronousEmailMessagingService(ContainerBuilder b
466475
.Keyed<ITopicClient>(BindingKeys.EmailPublisherTopic)
467476
.OnRelease(x => x.Close());
468477

469-
// Create an ILoggerFactory
470-
var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false);
471-
var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration);
472-
473478
builder
474479
.RegisterType<EmailMessageEnqueuer>()
475480
.WithParameter(new ResolvedParameter(
476481
(pi, ctx) => pi.ParameterType == typeof(ITopicClient),
477482
(pi, ctx) => ctx.ResolveKeyed<ITopicClient>(BindingKeys.EmailPublisherTopic)))
478-
.WithParameter(new ResolvedParameter(
479-
(pi, ctx) => pi.ParameterType == typeof(ILogger<EmailMessageEnqueuer>),
480-
(pi, ctx) => loggerFactory.CreateLogger<EmailMessageEnqueuer>()))
481483
.As<IEmailMessageEnqueuer>();
482484

483485
builder.RegisterType<AsynchronousEmailMessageService>()

src/NuGetGallery/Infrastructure/Mail/EmailRecipientsWithPermission.cs renamed to src/NuGetGallery/Infrastructure/Mail/GalleryEmailRecipientsUtility.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66
using System.Linq;
77
using System.Net.Mail;
88
using NuGet.Services.Entities;
9-
using NuGet.Services.Messaging.Email;
109

1110
namespace NuGetGallery.Infrastructure.Mail
1211
{
13-
public class EmailRecipientsWithPermission
14-
: IEmailRecipients
12+
public static class GalleryEmailRecipientsUtility
1513
{
16-
public EmailRecipientsWithPermission(
17-
User user,
18-
ActionRequiringAccountPermissions action,
19-
IReadOnlyList<MailAddress> cc = null,
20-
IReadOnlyList<MailAddress> bcc = null,
21-
IReadOnlyList<MailAddress> replyTo = null)
14+
public static IReadOnlyList<MailAddress> GetAddressesWithPermission(User user, ActionRequiringAccountPermissions action)
2215
{
2316
if (user == null)
2417
{
@@ -30,23 +23,6 @@ public EmailRecipientsWithPermission(
3023
throw new ArgumentNullException(nameof(action));
3124
}
3225

33-
To = AddAddressesWithPermission(user, action);
34-
35-
CC = cc ?? new List<MailAddress>();
36-
Bcc = bcc ?? new List<MailAddress>();
37-
ReplyTo = replyTo ?? new List<MailAddress>();
38-
}
39-
40-
public IReadOnlyList<MailAddress> To { get; }
41-
42-
public IReadOnlyList<MailAddress> CC { get; }
43-
44-
public IReadOnlyList<MailAddress> Bcc { get; }
45-
46-
public IReadOnlyList<MailAddress> ReplyTo { get; }
47-
48-
private static IReadOnlyList<MailAddress> AddAddressesWithPermission(User user, ActionRequiringAccountPermissions action)
49-
{
5026
var recipients = new List<MailAddress>();
5127

5228
if (user is Organization organization)

src/NuGetGallery/Infrastructure/Mail/Messages/ContactOwnersMessage.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ public ContactOwnersMessage(
3939

4040
public override IEmailRecipients GetRecipients()
4141
{
42-
var to = EmailRecipients.GetAllOwners(
43-
Package.PackageRegistration,
44-
requireEmailAllowed: true);
45-
4642
return new EmailRecipients(
47-
to,
43+
to: EmailRecipients.GetAllOwners(
44+
Package.PackageRegistration,
45+
requireEmailAllowed: true),
4846
replyTo: new[] { FromAddress });
4947
}
5048

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMemberRemovedMessage.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ public OrganizationMemberRemovedMessage(
3131

3232
public override IEmailRecipients GetRecipients()
3333
{
34-
if (!Organization.EmailAllowed)
35-
{
36-
return EmailRecipients.None;
37-
}
38-
3934
return new EmailRecipients(
40-
to: new[] { Organization.ToMailAddress() },
35+
to: Organization.EmailAllowed
36+
? new[] { Organization.ToMailAddress() }
37+
: new MailAddress[0],
4138
replyTo: new[] { RemovedUser.ToMailAddress() });
4239
}
4340

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMemberUpdatedMessage.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ public OrganizationMemberUpdatedMessage(
3131

3232
public override IEmailRecipients GetRecipients()
3333
{
34-
if (!Organization.EmailAllowed)
35-
{
36-
return EmailRecipients.None;
37-
}
38-
3934
return new EmailRecipients(
40-
to: new[] { Organization.ToMailAddress() },
35+
to: Organization.EmailAllowed
36+
? new[] { Organization.ToMailAddress() }
37+
: new MailAddress[0],
4138
replyTo: new[] { Membership.Member.ToMailAddress() });
4239
}
4340

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMembershipRequestCanceledMessage.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ public OrganizationMembershipRequestCanceledMessage(
3131

3232
public override IEmailRecipients GetRecipients()
3333
{
34-
if (!PendingUser.EmailAllowed)
35-
{
36-
return EmailRecipients.None;
37-
}
38-
3934
return new EmailRecipients(
40-
to: new[] { PendingUser.ToMailAddress() },
35+
to: PendingUser.EmailAllowed
36+
? new[] { PendingUser.ToMailAddress() }
37+
: new MailAddress[0],
4138
replyTo: new[] { Organization.ToMailAddress() });
4239
}
4340

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMembershipRequestDeclinedMessage.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public OrganizationMembershipRequestDeclinedMessage(
3131

3232
public override IEmailRecipients GetRecipients()
3333
{
34-
return new EmailRecipientsWithPermission(
35-
Organization,
36-
ActionsRequiringPermissions.ManageAccount,
34+
return new EmailRecipients(
35+
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
36+
Organization,
37+
ActionsRequiringPermissions.ManageAccount),
3738
replyTo: new[] { PendingUser.ToMailAddress() });
3839
}
3940

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMembershipRequestInitiatedMessage.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public OrganizationMembershipRequestInitiatedMessage(
4343

4444
public override IEmailRecipients GetRecipients()
4545
{
46-
return new EmailRecipientsWithPermission(
47-
Organization,
48-
ActionsRequiringPermissions.ManageAccount,
46+
return new EmailRecipients(
47+
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
48+
Organization,
49+
ActionsRequiringPermissions.ManageAccount),
4950
replyTo: new[] { RequestingUser.ToMailAddress() });
5051
}
5152

src/NuGetGallery/Infrastructure/Mail/Messages/OrganizationMembershipRequestMessage.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ public OrganizationMembershipRequestMessage(
5050

5151
public override IEmailRecipients GetRecipients()
5252
{
53-
if (!NewUser.EmailAllowed)
54-
{
55-
return EmailRecipients.None;
56-
}
57-
5853
return new EmailRecipients(
59-
to: new[] { NewUser.ToMailAddress() },
54+
to: NewUser.EmailAllowed
55+
? new[] { NewUser.ToMailAddress() }
56+
: new MailAddress[0],
6057
replyTo: new[]
6158
{
6259
Organization.ToMailAddress(),

0 commit comments

Comments
 (0)