|
8 | 8 | using Microsoft.EntityFrameworkCore;
|
9 | 9 | using Microsoft.Extensions.Options;
|
10 | 10 | using Microsoft.Extensions.Primitives;
|
| 11 | +using MimeKit; |
11 | 12 | using TravelBlog.Configuration;
|
12 | 13 | using TravelBlog.Database;
|
13 | 14 | using TravelBlog.Database.Entities;
|
14 | 15 | using TravelBlog.Extensions;
|
15 | 16 | using TravelBlog.Models;
|
16 | 17 | using TravelBlog.Services;
|
17 |
| -using TravelBlog.Services.LightJobManager; |
18 | 18 | using UAParser;
|
19 | 19 |
|
20 | 20 | namespace TravelBlog.Controllers;
|
21 | 21 |
|
22 | 22 | [Route("~/post/{id?}/{action=Index}")]
|
23 | 23 | public class BlogPostController : Controller
|
24 | 24 | {
|
25 |
| - private readonly IOptions<SiteOptions> options; |
| 25 | + private readonly IOptions<SiteOptions> siteOptions; |
| 26 | + private readonly IOptions<MailingOptions> mailingOptions; |
26 | 27 | private readonly DatabaseContext database;
|
27 |
| - private readonly JobSchedulerService<MailJob, MailJobContext> scheduler; |
| 28 | + private readonly EmailDeliveryService deliveryService; |
28 | 29 | private readonly AuthenticationService authentication;
|
29 | 30 | private readonly MarkdownService markdown;
|
30 | 31 |
|
31 |
| - public BlogPostController(IOptions<SiteOptions> options, DatabaseContext database, |
32 |
| - JobSchedulerService<MailJob, MailJobContext> scheduler, AuthenticationService authentication, MarkdownService markdown) |
| 32 | + public BlogPostController(IOptions<SiteOptions> siteOptions, IOptions<MailingOptions> mailingOptions, DatabaseContext database, |
| 33 | + EmailDeliveryService deliveryService, AuthenticationService authentication, MarkdownService markdown) |
33 | 34 | {
|
34 |
| - this.options = options; |
| 35 | + this.siteOptions = siteOptions; |
| 36 | + this.mailingOptions = mailingOptions; |
35 | 37 | this.database = database;
|
36 |
| - this.scheduler = scheduler; |
| 38 | + this.deliveryService = deliveryService; |
37 | 39 | this.authentication = authentication;
|
38 | 40 | this.markdown = markdown;
|
39 | 41 | }
|
@@ -202,17 +204,25 @@ public async Task<IActionResult> Publish(int id, string title, string? content,
|
202 | 204 | private async Task NotifySubscribers(BlogPost post)
|
203 | 205 | {
|
204 | 206 | List<Subscriber> subscribers = await database.Subscribers
|
205 |
| - .Where(s => s.ConfirmationTime != default && s.DeletionTime == default).ToListAsync(); |
| 207 | + .Where(s => s.MailAddress != null && s.ConfirmationTime != default && s.DeletionTime == default).ToListAsync(); |
206 | 208 |
|
207 |
| - await scheduler.Enqueue(subscribers.Select(s => |
| 209 | + await deliveryService.Enqueue(subscribers.Select(subscriber => |
208 | 210 | {
|
209 |
| - string postUrl = Url.ContentLink($"~/post/{post.Id}/auth?token={s.Token}"); |
210 |
| - string unsubscribeUrl = Url.ContentLink("~/unsubscribe?token=" + s.Token); |
211 |
| - string message = $"Hey {s.GivenName},\r\n" + |
212 |
| - $"es wurde etwas neues auf {options.Value.BlogName} gepostet:\r\n" + |
| 211 | + string postUrl = Url.ContentLink($"~/post/{post.Id}/auth?token={subscriber.Token}"); |
| 212 | + string unsubscribeUrl = Url.ContentLink("~/unsubscribe?token=" + subscriber.Token); |
| 213 | + string message = $"Hey {subscriber.GivenName},\r\n" + |
| 214 | + $"es wurde etwas neues auf {siteOptions.Value.BlogName} gepostet:\r\n" + |
213 | 215 | $"{postUrl}\r\n\r\n" +
|
214 | 216 | $"Du kannst dich von diesem Blog jederzeit hier abmelden: {unsubscribeUrl}";
|
215 |
| - return new MailJob(id: default, s.Id, subject: "Neuer Post", message); |
216 |
| - })); |
| 217 | + |
| 218 | + MimeMessage mimeMessage = new(); |
| 219 | + mimeMessage.From.Add(new MailboxAddress(mailingOptions.Value.SenderName, mailingOptions.Value.SenderAddress)); |
| 220 | + mimeMessage.To.Add(new MailboxAddress(subscriber.GetName(), subscriber.MailAddress)); |
| 221 | + mimeMessage.ReplyTo.Add(new MailboxAddress(mailingOptions.Value.AuthorName, mailingOptions.Value.AuthorAddress)); |
| 222 | + mimeMessage.Subject = "Neuer Post"; |
| 223 | + mimeMessage.Body = new TextPart("plain") { Text = message }; |
| 224 | + |
| 225 | + return (subscriber.MailAddress!, mimeMessage); |
| 226 | + }), post.Id); |
217 | 227 | }
|
218 | 228 | }
|
0 commit comments