2023-08-13 18:02:58 +01:00
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-05-05 02:18:20 +01:00
|
|
|
|
using OliverBooth.Common.Data.Blog;
|
|
|
|
|
using OliverBooth.Common.Services;
|
2023-08-13 17:33:54 +01:00
|
|
|
|
using OliverBooth.Data.Blog.Rss;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
|
2023-08-13 18:02:58 +01:00
|
|
|
|
namespace OliverBooth.Controllers.Blog;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
|
2023-08-13 18:02:58 +01:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("blog/feed")]
|
|
|
|
|
public class RssController : Controller
|
2023-08-09 21:08:57 +01:00
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
|
private readonly IBlogPostService _blogPostService;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
|
2023-08-13 18:02:58 +01: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 21:08:57 +01:00
|
|
|
|
{
|
2023-08-12 20:13:47 +01:00
|
|
|
|
_blogPostService = blogPostService;
|
2023-08-09 21:08:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 18:02:58 +01:00
|
|
|
|
[HttpGet]
|
2024-11-06 14:37:58 +00:00
|
|
|
|
[Produces("application/xml")]
|
2023-08-13 18:02:58 +01:00
|
|
|
|
public IActionResult OnGet()
|
2023-08-09 21:08:57 +01:00
|
|
|
|
{
|
2024-11-06 14:37:58 +00:00
|
|
|
|
Response.ContentType = "application/xml";
|
2023-08-09 21:08:57 +01:00
|
|
|
|
|
2023-08-13 18:02:58 +01:00
|
|
|
|
var baseUrl = $"https://{Request.Host}/blog";
|
2023-08-09 21:08:57 +01:00
|
|
|
|
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}";
|
2023-08-13 18:02:58 +01:00
|
|
|
|
string excerpt = _blogPostService.RenderPost(post);
|
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"),
|
2023-08-13 18:02:58 +01:00
|
|
|
|
Guid = post.WordPressId.HasValue ? $"{baseUrl}?p={post.WordPressId.Value}" : $"{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/");
|
|
|
|
|
|
2023-08-13 18:02:58 +01:00
|
|
|
|
using var writer = new StreamWriter(Response.BodyWriter.AsStream());
|
2023-08-09 21:08:57 +01:00
|
|
|
|
serializer.Serialize(writer, rss, xmlNamespaces);
|
2023-08-13 18:02:58 +01:00
|
|
|
|
|
|
|
|
|
return Ok();
|
2023-08-09 21:08:57 +01:00
|
|
|
|
}
|
|
|
|
|
}
|