From 5c78e5a391ebd2a0be3c311ad857732512c4b695 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Thu, 8 Feb 2024 14:36:38 +0100 Subject: [PATCH] Addead Features configuration for RSS and Email Subscriptions --- Wave/Controllers/RssController.cs | 19 +++++++++---------- Wave/Data/Features.cs | 6 ++++++ Wave/Program.cs | 1 + Wave/Services/EmailBackgroundWorker.cs | 10 ++++++---- 4 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 Wave/Data/Features.cs diff --git a/Wave/Controllers/RssController.cs b/Wave/Controllers/RssController.cs index ef4f9e9..2342f8c 100644 --- a/Wave/Controllers/RssController.cs +++ b/Wave/Controllers/RssController.cs @@ -1,35 +1,34 @@ -using System.Collections.ObjectModel; -using System.Linq; -using System.ServiceModel.Syndication; -using System.Xml; +using System.ServiceModel.Syndication; using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; -using Microsoft.Net.Http.Headers; using Wave.Data; -using Wave.Data.Migrations.postgres; namespace Wave.Controllers; [ApiController] [Route("/[controller]")] -public class RssController(IOptions customizations, ApplicationDbContext context) : ControllerBase { +public class RssController(IOptions customizations, ApplicationDbContext context, IOptions features) : ControllerBase { private ApplicationDbContext Context { get; } = context; private IOptions Customizations { get; } = customizations; + private IOptions Features { get; } = features; [HttpGet("rss.xml", Name = "RssFeed")] - [Produces("application/rss+xml")] + [Produces("application/rss+xml", "application/json")] [ResponseCache(Duration = 60*15, Location = ResponseCacheLocation.Any)] public async Task GetRssFeedAsync(string? category = null) { + if (!Features.Value.Rss) return Unauthorized("RSS is disabled"); + var feed = await CreateFeedAll("RssFeed", category); if (feed is null) return NotFound(); return Ok(feed); } [HttpGet("atom.xml", Name = "AtomFeed")] - [Produces("application/atom+xml")] + [Produces("application/atom+xml", "application/json")] [ResponseCache(Duration = 60*15, Location = ResponseCacheLocation.Any)] public async Task GetAtomFeedAsync(string? category = null) { + if (!Features.Value.Rss) return Unauthorized("RSS is disabled"); + var feed = await CreateFeedAll("AtomFeed", category); if (feed is null) return NotFound(); return Ok(feed); diff --git a/Wave/Data/Features.cs b/Wave/Data/Features.cs new file mode 100644 index 0000000..0af52b3 --- /dev/null +++ b/Wave/Data/Features.cs @@ -0,0 +1,6 @@ +namespace Wave.Data; + +public class Features { + public bool Rss { get; set; } = true; + public bool EmailSubscriptions { get; set; } = false; +} \ No newline at end of file diff --git a/Wave/Program.cs b/Wave/Program.cs index 84edbcf..e320cda 100644 --- a/Wave/Program.cs +++ b/Wave/Program.cs @@ -112,6 +112,7 @@ builder.Services.AddScoped(); builder.Services.AddHttpClient(); +builder.Services.Configure(builder.Configuration.GetSection(nameof(Features))); builder.Services.Configure(builder.Configuration.GetSection(nameof(Customization))); builder.Services.AddCascadingValue("TitlePrefix", sf => (sf.GetService>()?.Value.AppName ?? "Wave") + " - "); diff --git a/Wave/Services/EmailBackgroundWorker.cs b/Wave/Services/EmailBackgroundWorker.cs index a23cd4a..ab0d350 100644 --- a/Wave/Services/EmailBackgroundWorker.cs +++ b/Wave/Services/EmailBackgroundWorker.cs @@ -1,7 +1,5 @@ using MailKit.Net.Smtp; using MailKit.Security; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using MimeKit; @@ -11,15 +9,18 @@ namespace Wave.Services; -public class EmailBackgroundWorker(ILogger logger, IDbContextFactory contextFactory, IOptions config, IOptions customizations) : IHostedService, IDisposable { +public class EmailBackgroundWorker(ILogger logger, IDbContextFactory contextFactory, IOptions config, IOptions customizations, IOptions features) : IHostedService, IDisposable { private ILogger Logger { get; } = logger; private IDbContextFactory ContextFactory { get; } = contextFactory; private SmtpConfiguration Configuration { get; } = config.Value; private Customization Customizations { get; } = customizations.Value; + private Features Features { get; } = features.Value; private Timer? Timer { get; set; } public Task StartAsync(CancellationToken cancellationToken) { + if (!Features.EmailSubscriptions) return Task.CompletedTask; + Logger.LogInformation("Background email worker starting."); // we want this timer to execute every 15 minutes, at fixed times (:00, :15, :30, :45) @@ -34,6 +35,8 @@ public class EmailBackgroundWorker(ILogger logger, IDbCon } public Task StopAsync(CancellationToken cancellationToken) { + if (!Features.EmailSubscriptions) return Task.CompletedTask; + Logger.LogInformation("Background email worker stopping."); Timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; @@ -47,7 +50,6 @@ public class EmailBackgroundWorker(ILogger logger, IDbCon private void DoWork(object? _) { try { Logger.LogInformation("Checking Articles..."); - if (string.IsNullOrWhiteSpace(Configuration.SenderName)) return; using var context = ContextFactory.CreateDbContext(); var now = DateTimeOffset.UtcNow;