Compare commits
3 Commits
67d89c1831
...
dc83309db7
Author | SHA1 | Date | |
---|---|---|---|
dc83309db7 | |||
bbc76bc305 | |||
369436ccce |
@ -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.Rss;
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Middleware")]
|
||||
public async Task Invoke(HttpContext context)
|
||||
[HttpGet]
|
||||
[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>();
|
||||
|
||||
foreach (IBlogPost post in _blogPostService.GetAllBlogPosts())
|
||||
{
|
||||
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 item = new BlogItem
|
||||
@ -36,7 +43,7 @@ internal sealed class RssMiddleware
|
||||
Comments = $"{url}#disqus_thread",
|
||||
Creator = post.Author.DisplayName,
|
||||
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
|
||||
};
|
||||
blogItems.Add(item);
|
||||
@ -68,7 +75,9 @@ internal sealed class RssMiddleware
|
||||
xmlNamespaces.Add("sy", "http://purl.org/rss/1.0/modules/syndication/");
|
||||
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);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
@ -25,12 +25,6 @@ internal sealed class BlogContext : DbContext
|
||||
/// <value>The collection of blog posts.</value>
|
||||
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>
|
||||
/// Gets the collection of users in the database.
|
||||
/// </summary>
|
||||
@ -49,7 +43,6 @@ internal sealed class BlogContext : DbContext
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new UserConfiguration());
|
||||
}
|
||||
}
|
||||
|
@ -37,6 +37,9 @@ internal sealed class BlogPost : IBlogPost
|
||||
/// <inheritdoc />
|
||||
public DateTimeOffset? Updated { get; internal set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int? WordPressId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ID of the author of this blog post.
|
||||
/// </summary>
|
||||
@ -61,12 +64,6 @@ internal sealed class BlogPost : IBlogPost
|
||||
/// <value>The Disqus URL path.</value>
|
||||
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>
|
||||
/// Gets the Disqus domain for the blog post.
|
||||
/// </summary>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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>
|
||||
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>
|
||||
/// Gets the Disqus identifier for the post.
|
||||
/// </summary>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -23,6 +23,6 @@ internal sealed class TemplateRenderer : HtmlObjectRenderer<TemplateInline>
|
||||
/// <inheritdoc />
|
||||
protected override void Write(HtmlRenderer renderer, TemplateInline template)
|
||||
{
|
||||
renderer.Write(_templateService.RenderTemplate(template));
|
||||
renderer.Write(_templateService.RenderGlobalTemplate(template));
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OliverBooth.Data.Blog;
|
||||
@ -10,6 +11,7 @@ namespace OliverBooth.Services;
|
||||
internal sealed class BlogUserService : IBlogUserService
|
||||
{
|
||||
private readonly IDbContextFactory<BlogContext> _dbContextFactory;
|
||||
private readonly ConcurrentDictionary<Guid, IUser> _userCache = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BlogUserService" /> class.
|
||||
@ -25,8 +27,12 @@ internal sealed class BlogUserService : IBlogUserService
|
||||
/// <inheritdoc />
|
||||
public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user)
|
||||
{
|
||||
if (_userCache.TryGetValue(id, out user)) return true;
|
||||
|
||||
using BlogContext context = _dbContextFactory.CreateDbContext();
|
||||
user = context.Users.Find(id);
|
||||
|
||||
if (user is not null) _userCache.TryAdd(id, user);
|
||||
return user is not null;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
using OliverBooth.Data;
|
||||
using OliverBooth.Markdown.Template;
|
||||
|
||||
namespace OliverBooth.Services;
|
||||
@ -8,12 +9,23 @@ namespace OliverBooth.Services;
|
||||
public interface ITemplateService
|
||||
{
|
||||
/// <summary>
|
||||
/// Renders the specified template with the specified arguments.
|
||||
/// Renders the specified global template with the specified arguments.
|
||||
/// </summary>
|
||||
/// <param name="templateInline">The template to render.</param>
|
||||
/// <returns>The rendered template.</returns>
|
||||
/// <param name="templateInline">The global template to render.</param>
|
||||
/// <returns>The rendered global template.</returns>
|
||||
/// <exception cref="ArgumentNullException">
|
||||
/// <paramref name="templateInline" /> is <see langword="null" />.
|
||||
/// </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);
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using System.Buffers.Binary;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OliverBooth.Data.Blog;
|
||||
using OliverBooth.Data;
|
||||
using OliverBooth.Data.Web;
|
||||
using OliverBooth.Formatting;
|
||||
using OliverBooth.Markdown.Template;
|
||||
using SmartFormat;
|
||||
@ -14,15 +15,16 @@ namespace OliverBooth.Services;
|
||||
internal sealed class TemplateService : ITemplateService
|
||||
{
|
||||
private static readonly Random Random = new();
|
||||
private readonly IDbContextFactory<BlogContext> _webContextFactory;
|
||||
private readonly IDbContextFactory<WebContext> _webContextFactory;
|
||||
private readonly SmartFormatter _formatter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TemplateService" /> class.
|
||||
/// </summary>
|
||||
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
|
||||
/// <param name="webContextFactory">The <see cref="BlogContext" /> factory.</param>
|
||||
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<BlogContext> webContextFactory)
|
||||
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
|
||||
public TemplateService(IServiceProvider serviceProvider,
|
||||
IDbContextFactory<WebContext> webContextFactory)
|
||||
{
|
||||
_formatter = Smart.CreateDefaultSmartFormat();
|
||||
_formatter.AddExtensions(new DefaultSource());
|
||||
@ -34,15 +36,21 @@ internal sealed class TemplateService : ITemplateService
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string RenderTemplate(TemplateInline templateInline)
|
||||
public string RenderGlobalTemplate(TemplateInline templateInline)
|
||||
{
|
||||
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
|
||||
|
||||
using BlogContext webContext = _webContextFactory.CreateDbContext();
|
||||
Template? template = webContext.Templates.Find(templateInline.Name);
|
||||
using WebContext context = _webContextFactory.CreateDbContext();
|
||||
Template? template = context.Templates.Find(templateInline.Name);
|
||||
return RenderTemplate(templateInline, template);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string RenderTemplate(TemplateInline inline, ITemplate? template)
|
||||
{
|
||||
if (template is null)
|
||||
{
|
||||
return $"{{{{{templateInline.Name}}}}}";
|
||||
return $"{{{{{inline.Name}}}}}";
|
||||
}
|
||||
|
||||
Span<byte> randomBytes = stackalloc byte[20];
|
||||
@ -50,9 +58,9 @@ internal sealed class TemplateService : ITemplateService
|
||||
|
||||
var formatted = new
|
||||
{
|
||||
templateInline.ArgumentList,
|
||||
templateInline.ArgumentString,
|
||||
templateInline.Params,
|
||||
inline.ArgumentList,
|
||||
inline.ArgumentString,
|
||||
inline.Params,
|
||||
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
|
||||
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
|
||||
};
|
||||
@ -63,7 +71,7 @@ internal sealed class TemplateService : ITemplateService
|
||||
}
|
||||
catch
|
||||
{
|
||||
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
|
||||
return $"{{{{{inline.Name}|{inline.ArgumentString}}}}}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user