using System; using System.Collections.Generic; using System.Linq; using Gallium; using Nitride.Contents; using Nitride.Feeds.Structure; using NodaTime; using Serilog; using Zio; namespace Nitride.Feeds { /// /// Creates various feeds from the given input. /// [WithProperties] public partial class CreateAtomFeeds : NitrideOperationBase { private readonly ILogger logger; public CreateAtomFeeds(ILogger logger) { this.logger = logger; this.GetAlternateMimeType = _ => "text/html"; } /// /// Gets or sets the base URL for all the links. /// public string? BaseUrl { get; set; } /// /// Gets or sets the alternate MIME type. /// public Func GetAlternateMimeType { get; set; } /// /// Gets or sets the alternate URL associated with the feed. /// public Func? GetAlternateUrl { get; set; } /// /// Gets or sets the callback to get the author for the feed. /// public Func? GetAuthor { get; set; } /// /// Gets or sets the callback to get the entries associated with the /// feed. /// public Func>? GetEntries { get; set; } /// /// Gets or sets the identifier (typically a URL) of the feed. /// public Func? GetId { get; set; } /// /// Gets or sets the callback to get the path of the generated feed. /// public Func? GetPath { get; set; } /// /// Gets or sets the rights (license) of the feed. /// public Func? GetRights { get; set; } /// /// A callback that gets the title of the feed from the given entity. /// public Func? GetTitle { get; set; } /// /// Gets or sets the updated timestamp for the feed. /// public Func? GetUpdated { get; set; } /// /// Gets or sets the URL associated with the feed. /// public Func? GetUrl { get; set; } /// public override IEnumerable Run(IEnumerable input) { this.CheckNotNull(x => this.GetTitle); this.CheckNotNull(x => this.GetPath); this.CheckNotNull(x => this.GetEntries); return input.SelectMany(this.CreateEntityFeed); } private IEnumerable CreateEntityFeed(Entity entity) { // Create the top-level feed. All the nullable callbacks were // verified in the function that calls this. var feed = new AtomFeed() .WithTitle(this.GetTitle?.Invoke(entity)) .WithId(this.GetId?.Invoke(entity)) .WithRights(this.GetRights?.Invoke(entity)) .WithUpdated(this.GetUpdated?.Invoke(entity)) .WithUrl(this.GetUrl?.Invoke(entity)) .WithAlternateUrl(this.GetAlternateUrl?.Invoke(entity)) .WithAlternateMimeType(this.GetAlternateMimeType.Invoke(entity)) .WithAuthor(this.GetAuthor?.Invoke(entity)) .ToXElement(); // Go through all the items inside the feed and add them. foreach (var entry in this.GetEntries!(entity)) { feed.Add(entry.ToXElement()); } // Create the feed entity and return both objects. Entity feedEntity = new Entity() .Set(IsFeed.Instance) .Set(this.GetPath!(entity)) .SetTextContent(feed + "\n"); return new[] { entity, feedEntity }; } } }