using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using MfGames.Gallium;
using MfGames.Nitride.Contents;
using MfGames.Nitride.Feeds.Structure;
using NodaTime;
using Serilog;
using Zio;
namespace MfGames.Nitride.Feeds;
///
/// Creates various feeds from the given input.
///
[WithProperties]
public partial class CreateAtomFeed : OperationBase
{
private readonly ILogger logger;
private readonly IValidator validator;
public CreateAtomFeed(
ILogger logger,
IValidator validator)
{
this.logger = logger;
this.validator = validator;
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.validator.ValidateAndThrow(this);
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
{
Title = this.GetTitle?.Invoke(entity),
Id = this.GetId?.Invoke(entity),
Rights = this.GetRights?.Invoke(entity),
Updated = this.GetUpdated?.Invoke(entity),
Url = this.GetUrl?.Invoke(entity),
AlternateUrl = this.GetAlternateUrl?.Invoke(entity),
AlternateMimeType = this.GetAlternateMimeType.Invoke(entity),
Author = this.GetAuthor?.Invoke(entity),
}.ToXElement();
// Go through all the items inside the feed and add them.
foreach (AtomEntry? 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 };
}
}