22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
44using System ;
5- using System . Collections . ObjectModel ;
65using System . Collections . Generic ;
76using System . Globalization ;
87using System . Linq ;
98using System . Net . Mail ;
109using System . Text ;
11- using System . Threading . Tasks ;
1210using AnglicanGeek . MarkdownMailer ;
1311using NuGet . Services . Validation ;
1412using NuGet . Services . Validation . Issues ;
@@ -17,11 +15,9 @@ namespace NuGetGallery.Services
1715{
1816 public class CoreMessageService : ICoreMessageService
1917 {
20- private static readonly ReadOnlyCollection < TimeSpan > RetryDelays = Array . AsReadOnly ( new [ ] {
21- TimeSpan . FromSeconds ( 0.1 ) ,
22- TimeSpan . FromSeconds ( 1 ) ,
23- TimeSpan . FromSeconds ( 10 )
24- } ) ;
18+ protected CoreMessageService ( )
19+ {
20+ }
2521
2622 public CoreMessageService ( IMailSender mailSender , ICoreMessageServiceConfiguration coreConfiguration )
2723 {
@@ -32,7 +28,7 @@ public CoreMessageService(IMailSender mailSender, ICoreMessageServiceConfigurati
3228 public IMailSender MailSender { get ; protected set ; }
3329 public ICoreMessageServiceConfiguration CoreConfiguration { get ; protected set ; }
3430
35- public async Task SendPackageAddedNoticeAsync ( Package package , string packageUrl , string packageSupportUrl , string emailSettingsUrl )
31+ public void SendPackageAddedNotice ( Package package , string packageUrl , string packageSupportUrl , string emailSettingsUrl )
3632 {
3733 string subject = $ "[{ CoreConfiguration . GalleryOwner . DisplayName } ] Package published - { package . PackageRegistration . Id } { package . Version } ";
3834 string body = $@ "The package [{ package . PackageRegistration . Id } { package . Version } ]({ packageUrl } ) was recently published on { CoreConfiguration . GalleryOwner . DisplayName } by { package . User . Username } . If this was not intended, please [contact support]({ packageSupportUrl } ).
@@ -53,12 +49,12 @@ [change your email notification settings]({emailSettingsUrl}).
5349
5450 if ( mailMessage . To . Any ( ) )
5551 {
56- await SendMessageAsync ( mailMessage ) ;
52+ SendMessage ( mailMessage ) ;
5753 }
5854 }
5955 }
6056
61- public async Task SendPackageValidationFailedNoticeAsync ( Package package , PackageValidationSet validationSet , string packageUrl , string packageSupportUrl , string announcementsUrl , string twitterUrl )
57+ public void SendPackageValidationFailedNotice ( Package package , PackageValidationSet validationSet , string packageUrl , string packageSupportUrl , string announcementsUrl , string twitterUrl )
6258 {
6359 var validationIssues = validationSet . GetValidationIssues ( ) ;
6460
@@ -99,7 +95,7 @@ public async Task SendPackageValidationFailedNoticeAsync(Package package, Packag
9995
10096 if ( mailMessage . To . Any ( ) )
10197 {
102- await SendMessageAsync ( mailMessage ) ;
98+ SendMessage ( mailMessage , copySender : false ) ;
10399 }
104100 }
105101 }
@@ -135,7 +131,7 @@ private static string ParseValidationIssue(ValidationIssue validationIssue, stri
135131 }
136132 }
137133
138- public async Task SendValidationTakingTooLongNoticeAsync ( Package package , string packageUrl )
134+ public void SendValidationTakingTooLongNotice ( Package package , string packageUrl )
139135 {
140136 string subject = "[{0}] Package validation taking longer than expected - {1} {2}" ;
141137 string body = "It is taking longer than expected for your package [{1} {2}]({3}) to get published.\n \n " +
@@ -167,7 +163,7 @@ public async Task SendValidationTakingTooLongNoticeAsync(Package package, string
167163
168164 if ( mailMessage . To . Any ( ) )
169165 {
170- await SendMessageAsync ( mailMessage ) ;
166+ SendMessage ( mailMessage , copySender : false ) ;
171167 }
172168 }
173169 }
@@ -197,54 +193,30 @@ protected static void AddOwnersSubscribedToPackagePushedNotification(PackageRegi
197193 }
198194 }
199195
200- protected virtual async Task SendMessageAsync ( MailMessage mailMessage )
196+ protected void SendMessage ( MailMessage mailMessage )
201197 {
202- int attempt = 0 ;
203- bool success = false ;
204- while ( ! success )
205- {
206- try
207- {
208- await AttemptSendMessageAsync ( mailMessage , attempt + 1 ) ;
209- success = true ;
210- }
211- catch ( SmtpException )
212- {
213- if ( attempt < RetryDelays . Count )
214- {
215- await Task . Delay ( RetryDelays [ attempt ] ) ;
216- attempt ++ ;
217- }
218- else
219- {
220- throw ;
221- }
222- }
223- }
198+ SendMessage ( mailMessage , copySender : false ) ;
224199 }
225200
226- protected virtual Task AttemptSendMessageAsync ( MailMessage mailMessage , int attemptNumber )
201+ virtual protected void SendMessage ( MailMessage mailMessage , bool copySender )
227202 {
228- // AnglicanGeek.MarkdownMailer doesn't have an async overload
229203 MailSender . Send ( mailMessage ) ;
230- return Task . CompletedTask ;
231- }
232-
233- protected async Task SendMessageToSenderAsync ( MailMessage mailMessage )
234- {
235- using ( var senderCopy = new MailMessage (
236- CoreConfiguration . GalleryOwner ,
237- mailMessage . ReplyToList . First ( ) ) )
204+ if ( copySender )
238205 {
239- senderCopy . Subject = mailMessage . Subject + " [Sender Copy]" ;
240- senderCopy . Body = string . Format (
241- CultureInfo . CurrentCulture ,
242- "You sent the following message via {0}: {1}{1}{2}" ,
243- CoreConfiguration . GalleryOwner . DisplayName ,
244- Environment . NewLine ,
245- mailMessage . Body ) ;
206+ var senderCopy = new MailMessage (
207+ CoreConfiguration . GalleryOwner ,
208+ mailMessage . ReplyToList . First ( ) )
209+ {
210+ Subject = mailMessage . Subject + " [Sender Copy]" ,
211+ Body = string . Format (
212+ CultureInfo . CurrentCulture ,
213+ "You sent the following message via {0}: {1}{1}{2}" ,
214+ CoreConfiguration . GalleryOwner . DisplayName ,
215+ Environment . NewLine ,
216+ mailMessage . Body ) ,
217+ } ;
246218 senderCopy . ReplyToList . Add ( mailMessage . ReplyToList . First ( ) ) ;
247- await SendMessageAsync ( senderCopy ) ;
219+ MailSender . Send ( senderCopy ) ;
248220 }
249221 }
250222 }
0 commit comments