2023-08-13 17:02:58 +00:00
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-08-13 16:33:54 +00:00
|
|
|
|
using OliverBooth.Data.Blog;
|
|
|
|
|
using OliverBooth.Data.Blog.Rss;
|
|
|
|
|
using OliverBooth.Services;
|
2023-08-09 20:08:57 +00:00
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
namespace OliverBooth.Controllers.Blog;
|
2023-08-09 20:08:57 +00:00
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("blog/feed")]
|
|
|
|
|
public class RssController : Controller
|
2023-08-09 20:08:57 +00:00
|
|
|
|
{
|
2023-08-12 19:13:47 +00:00
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
2023-08-09 20:08:57 +00:00
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RssController" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
|
|
|
|
public RssController(IBlogPostService blogPostService)
|
2023-08-09 20:08:57 +00:00
|
|
|
|
{
|
2023-08-12 19:13:47 +00:00
|
|
|
|
_blogPostService = blogPostService;
|
2023-08-09 20:08:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Produces("application/rss+xml")]
|
|
|
|
|
public IActionResult OnGet()
|
2023-08-09 20:08:57 +00:00
|
|
|
|
{
|
2023-08-13 17:02:58 +00:00
|
|
|
|
Response.ContentType = "application/rss+xml";
|
2023-08-09 20:08:57 +00:00
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
var baseUrl = $"https://{Request.Host}/blog";
|
2023-08-09 20:08:57 +00:00
|
|
|
|
var blogItems = new List<BlogItem>();
|
|
|
|
|
|
2023-08-12 19:13:47 +00:00
|
|
|
|
foreach (IBlogPost post in _blogPostService.GetAllBlogPosts())
|
2023-08-09 20:08:57 +00:00
|
|
|
|
{
|
2023-08-12 19:13:47 +00:00
|
|
|
|
var url = $"{baseUrl}/{post.Published:yyyy/MM/dd}/{post.Slug}";
|
2023-08-13 17:02:58 +00:00
|
|
|
|
string excerpt = _blogPostService.RenderPost(post);
|
2023-08-09 20:08:57 +00:00
|
|
|
|
var description = $"{excerpt}<p><a href=\"{url}\">Read more...</a></p>";
|
|
|
|
|
|
|
|
|
|
var item = new BlogItem
|
|
|
|
|
{
|
2023-08-12 19:13:47 +00:00
|
|
|
|
Title = post.Title,
|
2023-08-09 20:08:57 +00:00
|
|
|
|
Link = url,
|
|
|
|
|
Comments = $"{url}#disqus_thread",
|
2023-08-12 19:13:47 +00:00
|
|
|
|
Creator = post.Author.DisplayName,
|
|
|
|
|
PubDate = post.Published.ToString("R"),
|
2023-08-13 17:02:58 +00:00
|
|
|
|
Guid = post.WordPressId.HasValue ? $"{baseUrl}?p={post.WordPressId.Value}" : $"{baseUrl}?pid={post.Id}",
|
2023-08-09 20:08:57 +00: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 19:13:47 +00:00
|
|
|
|
Title = "Oliver Booth",
|
2023-08-09 20:08:57 +00: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/");
|
|
|
|
|
|
2023-08-13 17:02:58 +00:00
|
|
|
|
using var writer = new StreamWriter(Response.BodyWriter.AsStream());
|
2023-08-09 20:08:57 +00:00
|
|
|
|
serializer.Serialize(writer, rss, xmlNamespaces);
|
2023-08-13 17:02:58 +00:00
|
|
|
|
|
|
|
|
|
return Ok();
|
2023-08-09 20:08:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|