Skip to content

Commit

Permalink
Don't attempt to send emails if they have no recipients (#6637)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Bommarito committed Nov 9, 2018
1 parent 236df59 commit 1a4da8d
Show file tree
Hide file tree
Showing 31 changed files with 86 additions and 151 deletions.
13 changes: 1 addition & 12 deletions src/NuGetGallery.Core/NuGetGallery.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NuGet.Services.Messaging.Email">
<Version>2.36.0-sb-noemailrecipients-2192794</Version>
<PackageReference Include="NuGet.Services.Entities">
<Version>2.32.0-agr-license-2170862</Version>
</PackageReference>
<PackageReference Include="NuGet.Services.Messaging">
<Version>2.31.0</Version>
<Version>2.36.0</Version>
</PackageReference>
<PackageReference Include="NuGet.StrongName.AnglicanGeek.MarkdownMailer">
<Version>1.2.0</Version>
Expand Down Expand Up @@ -243,12 +238,6 @@
<PackageReference Include="NuGet.Packaging">
<Version>5.0.0-preview1.5665</Version>
</PackageReference>
<PackageReference Include="NuGet.Services.Validation">
<Version>2.36.0</Version>
</PackageReference>
<PackageReference Include="NuGet.Services.Validation.Issues">
<Version>2.36.0</Version>
</PackageReference>
<PackageReference Include="WindowsAzure.Storage">
<Version>7.1.2</Version>
</PackageReference>
Expand Down
16 changes: 9 additions & 7 deletions src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public static class BindingKeys

protected override void Load(ContainerBuilder builder)
{
var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false);
var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration);
builder.RegisterInstance(loggerFactory)
.AsSelf()
.As<ILoggerFactory>();
builder.RegisterGeneric(typeof(Logger<>))
.As(typeof(ILogger<>))
.SingleInstance();

var telemetryClient = TelemetryClientWrapper.Instance;
builder.RegisterInstance(telemetryClient)
.AsSelf()
Expand Down Expand Up @@ -466,18 +475,11 @@ private static void RegisterAsynchronousEmailMessagingService(ContainerBuilder b
.Keyed<ITopicClient>(BindingKeys.EmailPublisherTopic)
.OnRelease(x => x.Close());

// Create an ILoggerFactory
var loggerConfiguration = LoggingSetup.CreateDefaultLoggerConfiguration(withConsoleLogger: false);
var loggerFactory = LoggingSetup.CreateLoggerFactory(loggerConfiguration);

builder
.RegisterType<EmailMessageEnqueuer>()
.WithParameter(new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(ITopicClient),
(pi, ctx) => ctx.ResolveKeyed<ITopicClient>(BindingKeys.EmailPublisherTopic)))
.WithParameter(new ResolvedParameter(
(pi, ctx) => pi.ParameterType == typeof(ILogger<EmailMessageEnqueuer>),
(pi, ctx) => loggerFactory.CreateLogger<EmailMessageEnqueuer>()))
.As<IEmailMessageEnqueuer>();

builder.RegisterType<AsynchronousEmailMessageService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@
using System.Linq;
using System.Net.Mail;
using NuGet.Services.Entities;
using NuGet.Services.Messaging.Email;

namespace NuGetGallery.Infrastructure.Mail
{
public class EmailRecipientsWithPermission
: IEmailRecipients
public static class GalleryEmailRecipientsUtility
{
public EmailRecipientsWithPermission(
User user,
ActionRequiringAccountPermissions action,
IReadOnlyList<MailAddress> cc = null,
IReadOnlyList<MailAddress> bcc = null,
IReadOnlyList<MailAddress> replyTo = null)
public static IReadOnlyList<MailAddress> GetAddressesWithPermission(User user, ActionRequiringAccountPermissions action)
{
if (user == null)
{
Expand All @@ -30,23 +23,6 @@ public EmailRecipientsWithPermission(
throw new ArgumentNullException(nameof(action));
}

To = AddAddressesWithPermission(user, action);

CC = cc ?? new List<MailAddress>();
Bcc = bcc ?? new List<MailAddress>();
ReplyTo = replyTo ?? new List<MailAddress>();
}

public IReadOnlyList<MailAddress> To { get; }

public IReadOnlyList<MailAddress> CC { get; }

public IReadOnlyList<MailAddress> Bcc { get; }

public IReadOnlyList<MailAddress> ReplyTo { get; }

private static IReadOnlyList<MailAddress> AddAddressesWithPermission(User user, ActionRequiringAccountPermissions action)
{
var recipients = new List<MailAddress>();

if (user is Organization organization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public ContactOwnersMessage(

public override IEmailRecipients GetRecipients()
{
var to = EmailRecipients.GetAllOwners(
Package.PackageRegistration,
requireEmailAllowed: true);

return new EmailRecipients(
to,
to: EmailRecipients.GetAllOwners(
Package.PackageRegistration,
requireEmailAllowed: true),
replyTo: new[] { FromAddress });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ public OrganizationMemberRemovedMessage(

public override IEmailRecipients GetRecipients()
{
if (!Organization.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { Organization.ToMailAddress() },
to: Organization.EmailAllowed
? new[] { Organization.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { RemovedUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ public OrganizationMemberUpdatedMessage(

public override IEmailRecipients GetRecipients()
{
if (!Organization.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { Organization.ToMailAddress() },
to: Organization.EmailAllowed
? new[] { Organization.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { Membership.Member.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ public OrganizationMembershipRequestCanceledMessage(

public override IEmailRecipients GetRecipients()
{
if (!PendingUser.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { PendingUser.ToMailAddress() },
to: PendingUser.EmailAllowed
? new[] { PendingUser.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { Organization.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public OrganizationMembershipRequestDeclinedMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
Organization,
ActionsRequiringPermissions.ManageAccount,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
Organization,
ActionsRequiringPermissions.ManageAccount),
replyTo: new[] { PendingUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ public OrganizationMembershipRequestInitiatedMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
Organization,
ActionsRequiringPermissions.ManageAccount,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
Organization,
ActionsRequiringPermissions.ManageAccount),
replyTo: new[] { RequestingUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,10 @@ public OrganizationMembershipRequestMessage(

public override IEmailRecipients GetRecipients()
{
if (!NewUser.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { NewUser.ToMailAddress() },
to: NewUser.EmailAllowed
? new[] { NewUser.ToMailAddress() }
: new MailAddress[0],
replyTo: new[]
{
Organization.ToMailAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ public OrganizationTransformAcceptedMessage(

public override IEmailRecipients GetRecipients()
{
if (!_accountToTransform.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { _accountToTransform.ToMailAddress() },
to: _accountToTransform.EmailAllowed
? new[] { _accountToTransform.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { _adminUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ public OrganizationTransformInitiatedMessage(

public override IEmailRecipients GetRecipients()
{
if (!_accountToTransform.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { _accountToTransform.ToMailAddress() },
to: _accountToTransform.EmailAllowed
? new[] { _accountToTransform.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { _adminUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ public OrganizationTransformRejectedMessage(

public override IEmailRecipients GetRecipients()
{
if (!AccountToSendTo.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { AccountToSendTo.ToMailAddress() },
to: AccountToSendTo.EmailAllowed
? new[] { AccountToSendTo.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { AccountToReplyTo.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ public OrganizationTransformRequestMessage(

public override IEmailRecipients GetRecipients()
{
if (!_adminUser.EmailAllowed)
{
return EmailRecipients.None;
}

return new EmailRecipients(
to: new[] { _adminUser.ToMailAddress() },
to: _adminUser.EmailAllowed
? new[] { _adminUser.ToMailAddress() }
: new MailAddress[0],
replyTo: new[] { _accountToTransform.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public PackageDeletedNoticeMessage(

public override IEmailRecipients GetRecipients()
{
var to = EmailRecipients.GetAllOwners(
Package.PackageRegistration,
requireEmailAllowed: false);
return new EmailRecipients(to);
return new EmailRecipients(
to: EmailRecipients.GetAllOwners(
Package.PackageRegistration,
requireEmailAllowed: false));
}

public override string GetSubject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public PackageOwnerAddedMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest),
replyTo: new[] { _configuration.GalleryNoReplyAddress });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public PackageOwnerRemovedMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest),
replyTo: new[] { FromUser.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public PackageOwnershipRequestCanceledMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
NewOwner,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
NewOwner,
ActionsRequiringPermissions.HandlePackageOwnershipRequest),
replyTo: new[] { RequestingOwner.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ public PackageOwnershipRequestDeclinedMessage(

public override IEmailRecipients GetRecipients()
{
if (!RequestingOwner.EmailAllowed)
{
// This will prevent the email from being sent.
return EmailRecipients.None;
}

return new EmailRecipientsWithPermission(
RequestingOwner,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: RequestingOwner.EmailAllowed
? GalleryEmailRecipientsUtility.GetAddressesWithPermission(
RequestingOwner, ActionsRequiringPermissions.HandlePackageOwnershipRequest)
: new MailAddress[0],
replyTo: new[] { NewOwner.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public PackageOwnershipRequestInitiatedMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
ReceivingOwner,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
ReceivingOwner,
ActionsRequiringPermissions.HandlePackageOwnershipRequest),
replyTo: new[] { NewOwner.ToMailAddress() });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ public PackageOwnershipRequestMessage(

public override IEmailRecipients GetRecipients()
{
return new EmailRecipientsWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest,
return new EmailRecipients(
to: GalleryEmailRecipientsUtility.GetAddressesWithPermission(
ToUser,
ActionsRequiringPermissions.HandlePackageOwnershipRequest),
replyTo: new[] { FromUser.ToMailAddress() });
}

Expand Down
Loading

0 comments on commit 1a4da8d

Please sign in to comment.