Skip to content

Commit 08528a9

Browse files
committed
Fix formatting
1 parent e31ff3d commit 08528a9

File tree

9 files changed

+131
-129
lines changed

9 files changed

+131
-129
lines changed

src/MiddleMail.Client.RabbitMQ/MiddleMailClient.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using EasyNetQ;
44
using Microsoft.Extensions.Options;

src/MiddleMail.Delivery.Smtp/MimeMessageBuilder.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.Extensions.Options;
77

88
namespace MiddleMail.Delivery.Smtp {
9-
9+
1010
/// <summary>
1111
/// Builds MIME messages from an <see cref="EMailMessage" />.
1212
/// </summary>
@@ -22,22 +22,22 @@ public MimeMessageBuilder(IOptions<MimeMessageOptions> options) {
2222
/// Note that <paramref name="emailMessage" /> must specify at least a plaintext version.
2323
/// </summary>
2424
public MimeMessage Create(EmailMessage emailMessage) {
25-
if(emailMessage.PlainText == null) {
25+
if (emailMessage.PlainText == null) {
2626
throw new ArgumentException($"EmailMessage should always contains a Plaintext, but it is null.", nameof(emailMessage));
2727
}
28-
28+
2929
var mimeMessage = new MimeMessage();
3030
mimeMessage.From.Add(new MailboxAddress(emailMessage.From.name, emailMessage.From.address));
3131
mimeMessage.To.Add(new MailboxAddress(emailMessage.To.name, emailMessage.To.address));
3232

3333
emailMessage.Cc.ForEach(cc => mimeMessage.Cc.Add(new MailboxAddress(cc.name, cc.address)));
3434

35-
if(emailMessage.ReplyTo.HasValue) {
35+
if (emailMessage.ReplyTo.HasValue) {
3636
mimeMessage.ReplyTo.Add(new MailboxAddress(emailMessage.ReplyTo.Value.name, emailMessage.ReplyTo.Value.address));
3737
}
3838
mimeMessage.Subject = emailMessage.Subject;
3939

40-
if(emailMessage.HtmlText == null) {
40+
if (emailMessage.HtmlText == null) {
4141
createBodyPlainText(emailMessage, mimeMessage);
4242
} else {
4343
createBodyMultipart(emailMessage, mimeMessage);
@@ -58,15 +58,15 @@ private void createBodyPlainText(EmailMessage emailMessage, MimeMessage message)
5858
private void createBodyMultipart(EmailMessage emailMessage, MimeMessage message) {
5959
var bodyBuilder = new BodyBuilder();
6060
bodyBuilder.HtmlBody = emailMessage.HtmlText;
61-
bodyBuilder.TextBody = emailMessage.PlainText;
61+
bodyBuilder.TextBody = emailMessage.PlainText;
6262

6363
message.Body = bodyBuilder.ToMessageBody();
6464
}
6565

6666
private void setHeaders(EmailMessage emailMessage, MimeMessage message) {
6767
foreach (var item in emailMessage.Headers) {
6868
// do not override any headers
69-
var header = message.Headers.FirstOrDefault(h => h.Field == item.Key);
69+
var header = message.Headers.FirstOrDefault(h => h.Field == item.Key);
7070
if (header == null) {
7171
message.Headers.Add(item.Key, item.Value);
7272
}

src/MiddleMail.Delivery.Smtp/SmtpDeliverer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public async Task DeliverAsync(EmailMessage emailMessage) {
2323
MimeMessage mimeMessage;
2424
try {
2525
mimeMessage = builder.Create(emailMessage);
26-
} catch(Exception e) {
26+
} catch (Exception e) {
2727
throw new MimeMessageBuilderException(emailMessage, e);
2828
}
2929
try {
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
using System.Linq;
2-
using System;
3-
using System.Threading.Tasks;
4-
using MiddleMail.Model;
5-
using Nest;
6-
using MiddleMail.Exceptions;
7-
using Microsoft.Extensions.Configuration;
8-
using Microsoft.Extensions.Options;
9-
10-
namespace MiddleMail.Storage.ElasticSearch {
11-
12-
/// <summary>
13-
/// A mail activity storage backed by ElasticSearch.
14-
/// Use <see cref="ElasticSearchStorageOptions" /> to configure the connection and index.
15-
/// </summary>
16-
public class ElasticSearchStorage : IMailStorage {
17-
18-
private readonly ElasticClient client;
19-
private readonly ElasticSearchStorageOptions options;
20-
public ElasticSearchStorage(IOptions<ElasticSearchStorageOptions> options) {
21-
this.options = options.Value;
22-
var node = new Uri(this.options.Uri);
23-
client = new ElasticClient(new ConnectionSettings(node));
24-
client.Indices.Create(index, index => index
25-
.Map<EmailDocument>(m => m
26-
.AutoMap()
27-
));
28-
}
29-
30-
public async Task SetProcessedAsync(EmailMessage emailMessage) {
31-
await updateOrCreateAsync(emailMessage,
32-
update: (EmailDocument emailDocument) => { },
33-
create: () => new EmailDocument(emailMessage));
34-
}
35-
36-
public async Task SetSentAsync(EmailMessage emailMessage) {
37-
await updateOrCreateAsync(emailMessage,
38-
update: (EmailDocument emailDocument) => {
39-
emailDocument.Sent = DateTime.UtcNow;
40-
},
41-
create: () => new EmailDocument(emailMessage, sent: DateTime.UtcNow));
42-
}
43-
44-
public async Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
45-
await updateOrCreateAsync(emailMessage,
46-
update: (EmailDocument emailDocument) => {
47-
emailDocument.Error = errorMessage;
48-
},
49-
create: () => new EmailDocument(emailMessage, error: errorMessage));
50-
}
51-
52-
private async Task updateOrCreateAsync(EmailMessage emailMessage, Action<EmailDocument> update, Func<EmailDocument> create) {
53-
var emailDocument = await searchDocument(emailMessage.Id);
54-
if(emailDocument != null) {
55-
if (emailDocument.Sent != null) {
56-
throw new EMailMessageAlreadySentStorageException(emailMessage);
57-
}
58-
update(emailDocument);
59-
emailDocument.RetryCount = emailMessage.RetryCount;
60-
emailDocument.LastProcessed = DateTime.UtcNow;
61-
} else {
62-
emailDocument = create();
63-
}
64-
var response = await this.client.IndexAsync(emailDocument, i => i.Index(index));
65-
if (!response.IsValid) {
66-
throw new Exception(response.ServerError.ToString());
67-
}
68-
}
69-
70-
public async Task<bool?> GetSentAsync(EmailMessage emailMessage) {
71-
var existingDocument = await searchDocument(emailMessage.Id);
72-
return existingDocument?.Sent != null;
73-
}
74-
75-
public async Task<string> GetErrorAsync(EmailMessage emailMessage) {
76-
var existingDocument = await searchDocument(emailMessage.Id);
77-
return existingDocument?.Error;
78-
}
79-
80-
private string index => options.Index;
81-
82-
private async Task<EmailDocument> searchDocument(Guid id) {
83-
var response = await client.SearchAsync<EmailDocument>(s => s
84-
.Index(index)
85-
.Query(q => q
86-
.Term(c => c
87-
.Field(p => p.Id)
88-
.Value(id))));
89-
90-
return response.Documents.SingleOrDefault();
91-
}
92-
}
93-
}
1+
using System.Linq;
2+
using System;
3+
using System.Threading.Tasks;
4+
using MiddleMail.Model;
5+
using Nest;
6+
using MiddleMail.Exceptions;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.Options;
9+
10+
namespace MiddleMail.Storage.ElasticSearch {
11+
12+
/// <summary>
13+
/// A mail activity storage backed by ElasticSearch.
14+
/// Use <see cref="ElasticSearchStorageOptions" /> to configure the connection and index.
15+
/// </summary>
16+
public class ElasticSearchStorage : IMailStorage {
17+
18+
private readonly ElasticClient client;
19+
private readonly ElasticSearchStorageOptions options;
20+
public ElasticSearchStorage(IOptions<ElasticSearchStorageOptions> options) {
21+
this.options = options.Value;
22+
var node = new Uri(this.options.Uri);
23+
client = new ElasticClient(new ConnectionSettings(node));
24+
client.Indices.Create(index, index => index
25+
.Map<EmailDocument>(m => m
26+
.AutoMap()
27+
));
28+
}
29+
30+
public async Task SetProcessedAsync(EmailMessage emailMessage) {
31+
await updateOrCreateAsync(emailMessage,
32+
update: (EmailDocument emailDocument) => { },
33+
create: () => new EmailDocument(emailMessage));
34+
}
35+
36+
public async Task SetSentAsync(EmailMessage emailMessage) {
37+
await updateOrCreateAsync(emailMessage,
38+
update: (EmailDocument emailDocument) => {
39+
emailDocument.Sent = DateTime.UtcNow;
40+
},
41+
create: () => new EmailDocument(emailMessage, sent: DateTime.UtcNow));
42+
}
43+
44+
public async Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
45+
await updateOrCreateAsync(emailMessage,
46+
update: (EmailDocument emailDocument) => {
47+
emailDocument.Error = errorMessage;
48+
},
49+
create: () => new EmailDocument(emailMessage, error: errorMessage));
50+
}
51+
52+
private async Task updateOrCreateAsync(EmailMessage emailMessage, Action<EmailDocument> update, Func<EmailDocument> create) {
53+
var emailDocument = await searchDocument(emailMessage.Id);
54+
if (emailDocument != null) {
55+
if (emailDocument.Sent != null) {
56+
throw new EMailMessageAlreadySentStorageException(emailMessage);
57+
}
58+
update(emailDocument);
59+
emailDocument.RetryCount = emailMessage.RetryCount;
60+
emailDocument.LastProcessed = DateTime.UtcNow;
61+
} else {
62+
emailDocument = create();
63+
}
64+
var response = await this.client.IndexAsync(emailDocument, i => i.Index(index));
65+
if (!response.IsValid) {
66+
throw new Exception(response.ServerError.ToString());
67+
}
68+
}
69+
70+
public async Task<bool?> GetSentAsync(EmailMessage emailMessage) {
71+
var existingDocument = await searchDocument(emailMessage.Id);
72+
return existingDocument?.Sent != null;
73+
}
74+
75+
public async Task<string> GetErrorAsync(EmailMessage emailMessage) {
76+
var existingDocument = await searchDocument(emailMessage.Id);
77+
return existingDocument?.Error;
78+
}
79+
80+
private string index => options.Index;
81+
82+
private async Task<EmailDocument> searchDocument(Guid id) {
83+
var response = await client.SearchAsync<EmailDocument>(s => s
84+
.Index(index)
85+
.Query(q => q
86+
.Term(c => c
87+
.Field(p => p.Id)
88+
.Value(id))));
89+
90+
return response.Documents.SingleOrDefault();
91+
}
92+
}
93+
}

src/MiddleMail.Storage.Memory/MemoryStorage.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ namespace MiddleMail.Storage.Memory {
1111
/// A reference implementation for <see cref="IMailStorage" /> backed by a dictionary in memory.
1212
/// </summary>
1313
public class MemoryStorage : IMailStorage {
14-
14+
1515
private readonly ConcurrentDictionary<EmailMessage, (bool sent, string error)> storage;
1616

1717
public MemoryStorage() {
1818
this.storage = new ConcurrentDictionary<EmailMessage, (bool sent, string error)>();
1919
}
2020

2121
public Task SetProcessedAsync(EmailMessage emailMessage) {
22-
storage.AddOrUpdate(emailMessage,
23-
addValue: (false, null),
22+
storage.AddOrUpdate(emailMessage,
23+
addValue: (false, null),
2424
updateValueFactory: (EmailMessage key, (bool sent, string error) value) => {
2525
if (value.sent) {
2626
throw new EMailMessageAlreadySentStorageException(emailMessage);
@@ -32,8 +32,8 @@ public Task SetProcessedAsync(EmailMessage emailMessage) {
3232
}
3333

3434
public Task SetSentAsync(EmailMessage emailMessage) {
35-
storage.AddOrUpdate(emailMessage,
36-
addValue: (true, null),
35+
storage.AddOrUpdate(emailMessage,
36+
addValue: (true, null),
3737
updateValueFactory: (EmailMessage key, (bool sent, string error) value) => {
3838
if (value.sent) {
3939
throw new EMailMessageAlreadySentStorageException(emailMessage);
@@ -45,7 +45,7 @@ public Task SetSentAsync(EmailMessage emailMessage) {
4545
}
4646

4747
public Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
48-
storage.AddOrUpdate(emailMessage,
48+
storage.AddOrUpdate(emailMessage,
4949
addValue: (false, errorMessage),
5050
updateValueFactory: (EmailMessage key, (bool sent, string error) value) => {
5151
if (value.sent) {
@@ -56,7 +56,7 @@ public Task SetErrorAsync(EmailMessage emailMessage, string errorMessage) {
5656
);
5757
return Task.CompletedTask;
5858
}
59-
59+
6060
public Task<bool?> GetSentAsync(EmailMessage emailMessage) {
6161
var found = storage.TryGetValue(emailMessage, out var value);
6262
if (found) {

src/MiddleMail/IMailStorage.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using MiddleMail.Model;
33

44
namespace MiddleMail {
5-
5+
66
/// <summary>
77
/// A storage to persist email activity. When calling any of the methods that set data, the order must be kept.
88
/// Writing data does not need to be instantly and might not be reflected when reading it directly back.
@@ -14,7 +14,7 @@ public interface IMailStorage {
1414
/// This can happen multiple times.
1515
/// </summary>
1616
Task SetProcessedAsync(EmailMessage emailMessage);
17-
17+
1818
/// <summary>
1919
/// Store that an <see cref="EmailMessage" /> was successfully sent.
2020
/// </summary>

src/MiddleMail/MessageProcessor.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,38 @@ public async Task ProcessAsync(EmailMessage emailMessage) {
3535
string cached = null;
3636
try {
3737
cached = await cache.GetStringAsync(emailMessage.Id.ToString());
38-
} catch(Exception e) {
38+
} catch (Exception e) {
3939
throw new GeneralProcessingException(emailMessage, e);
4040
}
4141

42-
if(cached != null) {
42+
if (cached != null) {
4343
logger.LogInformation($"Caught duplicate email {emailMessage.Id}");
4444
return;
4545
}
4646

47-
if(emailMessage.Store) {
47+
if (emailMessage.Store) {
4848
await tryStoreOrLogAsync(() => storage.SetProcessedAsync(emailMessage));
4949
}
5050
try {
5151
await deliverer.DeliverAsync(emailMessage);
52-
} catch (Exception e){
53-
if(emailMessage.Store) {
52+
} catch (Exception e) {
53+
if (emailMessage.Store) {
5454
await tryStoreOrLogAsync(() => storage.SetErrorAsync(emailMessage, e.ToString()));
5555
}
5656
throw;
5757
}
58-
58+
5959
// if the cache throws an exception we do not rethrow a GeneralProcessingException here because the message has already been delivered
6060
await cache.SetStringAsync(emailMessage.Id.ToString(), "t");
61-
if(emailMessage.Store) {
61+
if (emailMessage.Store) {
6262
await tryStoreOrLogAsync(() => storage.SetSentAsync(emailMessage));
6363
}
6464
}
6565

6666
private async Task tryStoreOrLogAsync(Func<Task> storeFunc) {
6767
try {
6868
await storeFunc();
69-
} catch(Exception e) {
69+
} catch (Exception e) {
7070
logger.LogError(e, "Exception while storing EmailMessage.");
7171
}
7272
}

0 commit comments

Comments
 (0)