Compare commits

..

3 Commits

12 changed files with 75 additions and 148 deletions

View File

@ -1,32 +1,39 @@
using System.Diagnostics.CodeAnalysis; using System.Xml.Serialization;
using System.Xml.Serialization; using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
using OliverBooth.Data.Blog.Rss; using OliverBooth.Data.Blog.Rss;
using OliverBooth.Services; using OliverBooth.Services;
namespace OliverBooth.Middleware; namespace OliverBooth.Controllers.Blog;
internal sealed class RssMiddleware [ApiController]
[Route("blog/feed")]
public class RssController : Controller
{ {
private readonly IBlogPostService _blogPostService; private readonly IBlogPostService _blogPostService;
public RssMiddleware(RequestDelegate _, IBlogPostService blogPostService) /// <summary>
/// Initializes a new instance of the <see cref="RssController" /> class.
/// </summary>
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
public RssController(IBlogPostService blogPostService)
{ {
_blogPostService = blogPostService; _blogPostService = blogPostService;
} }
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Middleware")] [HttpGet]
public async Task Invoke(HttpContext context) [Produces("application/rss+xml")]
public IActionResult OnGet()
{ {
context.Response.ContentType = "application/rss+xml"; Response.ContentType = "application/rss+xml";
var baseUrl = $"https://{context.Request.Host}/blog"; var baseUrl = $"https://{Request.Host}/blog";
var blogItems = new List<BlogItem>(); var blogItems = new List<BlogItem>();
foreach (IBlogPost post in _blogPostService.GetAllBlogPosts()) foreach (IBlogPost post in _blogPostService.GetAllBlogPosts())
{ {
var url = $"{baseUrl}/{post.Published:yyyy/MM/dd}/{post.Slug}"; var url = $"{baseUrl}/{post.Published:yyyy/MM/dd}/{post.Slug}";
string excerpt = _blogPostService.RenderExcerpt(post, out _); string excerpt = _blogPostService.RenderPost(post);
var description = $"{excerpt}<p><a href=\"{url}\">Read more...</a></p>"; var description = $"{excerpt}<p><a href=\"{url}\">Read more...</a></p>";
var item = new BlogItem var item = new BlogItem
@ -36,7 +43,7 @@ internal sealed class RssMiddleware
Comments = $"{url}#disqus_thread", Comments = $"{url}#disqus_thread",
Creator = post.Author.DisplayName, Creator = post.Author.DisplayName,
PubDate = post.Published.ToString("R"), PubDate = post.Published.ToString("R"),
Guid = $"{baseUrl}?pid={post.Id}", Guid = post.WordPressId.HasValue ? $"{baseUrl}?p={post.WordPressId.Value}" : $"{baseUrl}?pid={post.Id}",
Description = description Description = description
}; };
blogItems.Add(item); blogItems.Add(item);
@ -68,7 +75,9 @@ internal sealed class RssMiddleware
xmlNamespaces.Add("sy", "http://purl.org/rss/1.0/modules/syndication/"); xmlNamespaces.Add("sy", "http://purl.org/rss/1.0/modules/syndication/");
xmlNamespaces.Add("slash", "http://purl.org/rss/1.0/modules/slash/"); xmlNamespaces.Add("slash", "http://purl.org/rss/1.0/modules/slash/");
await using var writer = new StreamWriter(context.Response.BodyWriter.AsStream()); using var writer = new StreamWriter(Response.BodyWriter.AsStream());
serializer.Serialize(writer, rss, xmlNamespaces); serializer.Serialize(writer, rss, xmlNamespaces);
return Ok();
} }
} }

View File

@ -25,12 +25,6 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of blog posts.</value> /// <value>The collection of blog posts.</value>
public DbSet<BlogPost> BlogPosts { get; private set; } = null!; public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
/// <summary>
/// Gets the collection of templates in the database.
/// </summary>
/// <value>The collection of templates.</value>
public DbSet<Template> Templates { get; private set; } = null!;
/// <summary> /// <summary>
/// Gets the collection of users in the database. /// Gets the collection of users in the database.
/// </summary> /// </summary>
@ -49,7 +43,6 @@ internal sealed class BlogContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new BlogPostConfiguration()); modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration()); modelBuilder.ApplyConfiguration(new UserConfiguration());
} }
} }

View File

@ -37,6 +37,9 @@ internal sealed class BlogPost : IBlogPost
/// <inheritdoc /> /// <inheritdoc />
public DateTimeOffset? Updated { get; internal set; } public DateTimeOffset? Updated { get; internal set; }
/// <inheritdoc />
public int? WordPressId { get; set; }
/// <summary> /// <summary>
/// Gets or sets the ID of the author of this blog post. /// Gets or sets the ID of the author of this blog post.
/// </summary> /// </summary>
@ -61,12 +64,6 @@ internal sealed class BlogPost : IBlogPost
/// <value>The Disqus URL path.</value> /// <value>The Disqus URL path.</value>
internal string? DisqusPath { get; set; } internal string? DisqusPath { get; set; }
/// <summary>
/// Gets or sets the WordPress ID of this blog post.
/// </summary>
/// <value>The WordPress ID of this blog post.</value>
internal int? WordPressId { get; set; }
/// <summary> /// <summary>
/// Gets the Disqus domain for the blog post. /// Gets the Disqus domain for the blog post.
/// </summary> /// </summary>

View File

@ -1,19 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace OliverBooth.Data.Blog.Configuration;
/// <summary>
/// Represents the configuration for the <see cref="Template" /> entity.
/// </summary>
internal sealed class TemplateConfiguration : IEntityTypeConfiguration<Template>
{
public void Configure(EntityTypeBuilder<Template> builder)
{
builder.ToTable("Template");
builder.HasKey(e => e.Name);
builder.Property(e => e.Name).IsRequired();
builder.Property(e => e.FormatString).IsRequired();
}
}

View File

@ -69,6 +69,14 @@ public interface IBlogPost
/// <value>The update date and time, or <see langword="null" /> if the post has not been updated.</value> /// <value>The update date and time, or <see langword="null" /> if the post has not been updated.</value>
DateTimeOffset? Updated { get; } DateTimeOffset? Updated { get; }
/// <summary>
/// Gets the WordPress ID of the post.
/// </summary>
/// <value>
/// The WordPress ID of the post, or <see langword="null" /> if the post was not imported from WordPress.
/// </value>
int? WordPressId { get; }
/// <summary> /// <summary>
/// Gets the Disqus identifier for the post. /// Gets the Disqus identifier for the post.
/// </summary> /// </summary>

View File

@ -1,74 +0,0 @@
namespace OliverBooth.Data.Blog;
/// <summary>
/// Represents a MediaWiki-style template.
/// </summary>
public sealed class Template : ITemplate, IEquatable<Template>
{
/// <inheritdoc />
public string FormatString { get; internal set; } = string.Empty;
/// <inheritdoc />
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator ==(Template? left, Template? right) => Equals(left, right);
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are not equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator !=(Template? left, Template? right) => !(left == right);
/// <summary>
/// Returns a value indicating whether this instance of <see cref="Template" /> is equal to another
/// instance.
/// </summary>
/// <param name="other">An instance to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="other" /> is equal to this instance; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(Template? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="obj" /> is an instance of <see cref="Template" /> and
/// equals the value of this instance; otherwise, <see langword="false" />.
/// </returns>
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is Template other && Equals(other);
}
/// <summary>
/// Gets the hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
return Name.GetHashCode();
}
}

View File

@ -23,6 +23,6 @@ internal sealed class TemplateRenderer : HtmlObjectRenderer<TemplateInline>
/// <inheritdoc /> /// <inheritdoc />
protected override void Write(HtmlRenderer renderer, TemplateInline template) protected override void Write(HtmlRenderer renderer, TemplateInline template)
{ {
renderer.Write(_templateService.RenderTemplate(template)); renderer.Write(_templateService.RenderGlobalTemplate(template));
} }
} }

View File

@ -1,13 +0,0 @@
namespace OliverBooth.Middleware;
internal static class RssEndpointExtensions
{
public static IEndpointConventionBuilder MapRssFeed(this IEndpointRouteBuilder endpoints, string pattern)
{
RequestDelegate pipeline = endpoints.CreateApplicationBuilder()
.UseMiddleware<RssMiddleware>()
.Build();
return endpoints.Map(pattern, pipeline).WithDisplayName("RSS Feed");
}
}

View File

@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog; using OliverBooth.Data.Blog;
@ -10,6 +11,7 @@ namespace OliverBooth.Services;
internal sealed class BlogUserService : IBlogUserService internal sealed class BlogUserService : IBlogUserService
{ {
private readonly IDbContextFactory<BlogContext> _dbContextFactory; private readonly IDbContextFactory<BlogContext> _dbContextFactory;
private readonly ConcurrentDictionary<Guid, IUser> _userCache = new();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="BlogUserService" /> class. /// Initializes a new instance of the <see cref="BlogUserService" /> class.
@ -25,8 +27,12 @@ internal sealed class BlogUserService : IBlogUserService
/// <inheritdoc /> /// <inheritdoc />
public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user) public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user)
{ {
if (_userCache.TryGetValue(id, out user)) return true;
using BlogContext context = _dbContextFactory.CreateDbContext(); using BlogContext context = _dbContextFactory.CreateDbContext();
user = context.Users.Find(id); user = context.Users.Find(id);
if (user is not null) _userCache.TryAdd(id, user);
return user is not null; return user is not null;
} }
} }

View File

@ -1,3 +1,4 @@
using OliverBooth.Data;
using OliverBooth.Markdown.Template; using OliverBooth.Markdown.Template;
namespace OliverBooth.Services; namespace OliverBooth.Services;
@ -8,12 +9,23 @@ namespace OliverBooth.Services;
public interface ITemplateService public interface ITemplateService
{ {
/// <summary> /// <summary>
/// Renders the specified template with the specified arguments. /// Renders the specified global template with the specified arguments.
/// </summary> /// </summary>
/// <param name="templateInline">The template to render.</param> /// <param name="templateInline">The global template to render.</param>
/// <returns>The rendered template.</returns> /// <returns>The rendered global template.</returns>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="templateInline" /> is <see langword="null" />. /// <paramref name="templateInline" /> is <see langword="null" />.
/// </exception> /// </exception>
string RenderTemplate(TemplateInline templateInline); string RenderGlobalTemplate(TemplateInline templateInline);
/// <summary>
/// Renders the specified global template with the specified arguments.
/// </summary>
/// <param name="templateInline">The global template to render.</param>
/// <param name="template">The database template object.</param>
/// <returns>The rendered global template.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="templateInline" /> is <see langword="null" />.
/// </exception>
string RenderTemplate(TemplateInline templateInline, ITemplate? template);
} }

View File

@ -1,6 +1,7 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog; using OliverBooth.Data;
using OliverBooth.Data.Web;
using OliverBooth.Formatting; using OliverBooth.Formatting;
using OliverBooth.Markdown.Template; using OliverBooth.Markdown.Template;
using SmartFormat; using SmartFormat;
@ -14,15 +15,16 @@ namespace OliverBooth.Services;
internal sealed class TemplateService : ITemplateService internal sealed class TemplateService : ITemplateService
{ {
private static readonly Random Random = new(); private static readonly Random Random = new();
private readonly IDbContextFactory<BlogContext> _webContextFactory; private readonly IDbContextFactory<WebContext> _webContextFactory;
private readonly SmartFormatter _formatter; private readonly SmartFormatter _formatter;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TemplateService" /> class. /// Initializes a new instance of the <see cref="TemplateService" /> class.
/// </summary> /// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param> /// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
/// <param name="webContextFactory">The <see cref="BlogContext" /> factory.</param> /// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<BlogContext> webContextFactory) public TemplateService(IServiceProvider serviceProvider,
IDbContextFactory<WebContext> webContextFactory)
{ {
_formatter = Smart.CreateDefaultSmartFormat(); _formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource()); _formatter.AddExtensions(new DefaultSource());
@ -34,15 +36,21 @@ internal sealed class TemplateService : ITemplateService
} }
/// <inheritdoc /> /// <inheritdoc />
public string RenderTemplate(TemplateInline templateInline) public string RenderGlobalTemplate(TemplateInline templateInline)
{ {
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline)); if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
using BlogContext webContext = _webContextFactory.CreateDbContext(); using WebContext context = _webContextFactory.CreateDbContext();
Template? template = webContext.Templates.Find(templateInline.Name); Template? template = context.Templates.Find(templateInline.Name);
return RenderTemplate(templateInline, template);
}
/// <inheritdoc />
public string RenderTemplate(TemplateInline inline, ITemplate? template)
{
if (template is null) if (template is null)
{ {
return $"{{{{{templateInline.Name}}}}}"; return $"{{{{{inline.Name}}}}}";
} }
Span<byte> randomBytes = stackalloc byte[20]; Span<byte> randomBytes = stackalloc byte[20];
@ -50,9 +58,9 @@ internal sealed class TemplateService : ITemplateService
var formatted = new var formatted = new
{ {
templateInline.ArgumentList, inline.ArgumentList,
templateInline.ArgumentString, inline.ArgumentString,
templateInline.Params, inline.Params,
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]), RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"), RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
}; };
@ -63,7 +71,7 @@ internal sealed class TemplateService : ITemplateService
} }
catch catch
{ {
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}"; return $"{{{{{inline.Name}|{inline.ArgumentString}}}}}";
} }
} }
} }