2023-08-11 15:51:20 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-08-09 21:08:57 +01:00
|
|
|
using System.Xml.Serialization;
|
2023-08-13 17:33:54 +01:00
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
using OliverBooth.Data.Blog.Rss;
|
|
|
|
using OliverBooth.Services;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
2023-08-13 17:33:54 +01:00
|
|
|
namespace OliverBooth.Middleware;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
|
|
|
internal sealed class RssMiddleware
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
private readonly IBlogPostService _blogPostService;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
public RssMiddleware(RequestDelegate _, IBlogPostService blogPostService)
|
2023-08-09 21:08:57 +01:00
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
_blogPostService = blogPostService;
|
2023-08-09 21:08:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Middleware")]
|
|
|
|
public async Task Invoke(HttpContext context)
|
|
|
|
{
|
|
|
|
context.Response.ContentType = "application/rss+xml";
|
|
|
|
|
|
|
|
var baseUrl = $"https://{context.Request.Host}/blog";
|
|
|
|
var blogItems = new List<BlogItem>();
|
|
|
|
|
2023-08-12 20:13:47 +01:00
|
|
|
foreach (IBlogPost post in _blogPostService.GetAllBlogPosts())
|
2023-08-09 21:08:57 +01:00
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
var url = $"{baseUrl}/{post.Published:yyyy/MM/dd}/{post.Slug}";
|
|
|
|
string excerpt = _blogPostService.RenderExcerpt(post, out _);
|
2023-08-09 21:08:57 +01:00
|
|
|
var description = $"{excerpt}<p><a href=\"{url}\">Read more...</a></p>";
|
|
|
|
|
|
|
|
var item = new BlogItem
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
Title = post.Title,
|
2023-08-09 21:08:57 +01:00
|
|
|
Link = url,
|
|
|
|
Comments = $"{url}#disqus_thread",
|
2023-08-12 20:13:47 +01:00
|
|
|
Creator = post.Author.DisplayName,
|
|
|
|
PubDate = post.Published.ToString("R"),
|
|
|
|
Guid = $"{baseUrl}?pid={post.Id}",
|
2023-08-09 21:08:57 +01:00
|
|
|
Description = description
|
|
|
|
};
|
|
|
|
blogItems.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
var rss = new BlogRoot
|
|
|
|
{
|
|
|
|
Channel = new BlogChannel
|
|
|
|
{
|
|
|
|
AtomLink = new AtomLink
|
|
|
|
{
|
|
|
|
Href = $"{baseUrl}/feed/",
|
|
|
|
},
|
|
|
|
Description = $"{baseUrl}/",
|
|
|
|
LastBuildDate = DateTimeOffset.UtcNow.ToString("R"),
|
|
|
|
Link = $"{baseUrl}/",
|
2023-08-12 20:13:47 +01:00
|
|
|
Title = "Oliver Booth",
|
2023-08-09 21:08:57 +01:00
|
|
|
Generator = $"{baseUrl}/",
|
|
|
|
Items = blogItems
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var serializer = new XmlSerializer(typeof(BlogRoot));
|
|
|
|
var xmlNamespaces = new XmlSerializerNamespaces();
|
|
|
|
xmlNamespaces.Add("content", "http://purl.org/rss/1.0/modules/content/");
|
|
|
|
xmlNamespaces.Add("wfw", "http://wellformedweb.org/CommentAPI/");
|
|
|
|
xmlNamespaces.Add("dc", "http://purl.org/dc/elements/1.1/");
|
|
|
|
xmlNamespaces.Add("atom", "http://www.w3.org/2005/Atom");
|
|
|
|
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());
|
|
|
|
serializer.Serialize(writer, rss, xmlNamespaces);
|
|
|
|
}
|
|
|
|
}
|