using System; using System.Xml.Linq; using NodaTime; using static MfGames.Nitride.Feeds.Structure.XmlConstants; namespace MfGames.Nitride.Feeds.Structure; /// /// The type-safe structure of the top-level feed. /// public record AtomFeed { /// /// Gets or sets the MIME type for the alternate URL. /// public string AlternateMimeType { get; set; } = "text/html"; /// /// Gets or sets the alternate URL for this feed. /// public Uri? AlternateUrl { get; set; } /// /// Gets or sets the author for the feed. /// public AtomAuthor? Author { get; set; } /// /// Gets or sets the ID of the feed. /// public string? Id { get; set; } /// /// Gets or sets the rights (license) of the feed. /// public string? Rights { get; set; } /// /// Gets or sets the title of the Feed. /// public string? Title { get; set; } /// /// Gets or sets the timestamp that the feed was updated. /// public Instant? Updated { get; set; } /// /// Gets or sets the URL associated with this feed. /// public Uri? Url { get; set; } /// /// Creates an XML element out of the feed along with all items inside /// the feed. /// /// public XElement ToXElement() { var elem = new XElement(AtomNamespace + "feed"); if (!string.IsNullOrWhiteSpace(this.Title)) { elem.Add( new XElement( AtomNamespace + "title", new XAttribute("type", "text"), new XAttribute(XNamespace.Xml + "lang", "en"), new XText(this.Title))); } 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; } }