2021-09-07 05:15:45 +00:00
|
|
|
using System;
|
|
|
|
using System.Xml.Linq;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2021-09-07 05:15:45 +00:00
|
|
|
using NodaTime;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using static MfGames.Nitride.Feeds.Structure.XmlConstants;
|
2021-09-07 05:15:45 +00:00
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Feeds.Structure;
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The type-safe structure of the top-level feed.
|
|
|
|
/// </summary>
|
|
|
|
public record AtomFeed
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
/// Gets or sets the MIME type for the alternate URL.
|
|
|
|
/// </summary>
|
|
|
|
public string AlternateMimeType { get; set; } = "text/html";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the alternate URL for this feed.
|
|
|
|
/// </summary>
|
|
|
|
public Uri? AlternateUrl { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the author for the feed.
|
|
|
|
/// </summary>
|
|
|
|
public AtomAuthor? Author { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the ID of the feed.
|
|
|
|
/// </summary>
|
|
|
|
public string? Id { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the rights (license) of the feed.
|
|
|
|
/// </summary>
|
|
|
|
public string? Rights { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the title of the Feed.
|
2021-09-07 05:15:45 +00:00
|
|
|
/// </summary>
|
2022-06-05 18:44:51 +00:00
|
|
|
public string? Title { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the timestamp that the feed was updated.
|
|
|
|
/// </summary>
|
|
|
|
public Instant? Updated { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the URL associated with this feed.
|
|
|
|
/// </summary>
|
|
|
|
public Uri? Url { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates an XML element out of the feed along with all items inside
|
|
|
|
/// the feed.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public XElement ToXElement()
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
var elem = new XElement(AtomNamespace + "feed");
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(this.Title))
|
2021-09-07 05:15:45 +00:00
|
|
|
{
|
2022-06-05 18:44:51 +00:00
|
|
|
elem.Add(
|
|
|
|
new XElement(
|
|
|
|
AtomNamespace + "title",
|
|
|
|
new XAttribute("type", "text"),
|
|
|
|
new XAttribute(XNamespace.Xml + "lang", "en"),
|
|
|
|
new XText(this.Title)));
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
2022-06-05 18:44:51 +00:00
|
|
|
|
|
|
|
if (this.Url != null)
|
|
|
|
{
|
|
|
|
elem.Add(
|
|
|
|
new XElement(
|
|
|
|
AtomNamespace + "link",
|
|
|
|
new XAttribute("type", "application/atom+xml"),
|
|
|
|
new XAttribute("href", this.Url.ToString()),
|
|
|
|
new XAttribute("rel", "self")));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.AlternateUrl != null)
|
|
|
|
{
|
|
|
|
elem.Add(
|
|
|
|
new XElement(
|
|
|
|
AtomNamespace + "link",
|
|
|
|
new XAttribute("type", this.AlternateMimeType),
|
|
|
|
new XAttribute("href", this.AlternateUrl.ToString()),
|
|
|
|
new XAttribute("rel", "alternate")));
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomHelper.AddIfSet(elem, "updated", this.Updated?.ToString("g", null));
|
|
|
|
AtomHelper.AddIfSet(elem, "id", this.Id);
|
|
|
|
AtomHelper.AddIfSet(elem, this.Author?.ToXElement());
|
|
|
|
AtomHelper.AddIfSet(elem, "rights", this.Rights);
|
|
|
|
|
|
|
|
return elem;
|
2021-09-07 05:15:45 +00:00
|
|
|
}
|
|
|
|
}
|