L'ordine dei tipi (storie, evidenza, post, reel, taggati) ora vale anche per il singolo account: nella scheda account → Impostazioni si sceglie «Come le impostazioni generali» o un ordine suo, con lo stesso elenco da trascinare (o Su/Giù da tastiera) di Limiti e comportamento, ora un modulo solo (download-order.js). Il motore pesa le visite con l'ordine dell'account, la coda dei file mette ogni file al posto del suo tipo nell'ordine del suo account, stabile dentro il tipo (scadenza, scoperta, elemento, posizione). Perché nessun tipo resti fermo per sempre quando il tetto dei dati finisce prima della coda, un posto su otto di ogni giro della coda va al file che aspetta da più tempo, di qualunque tipo. I reel non si inventano (regola 9): un post è reel se viene dalla scheda Reel o se la risposta ha product_type «clips», valore visto nei campioni ripuliti. Il segno si scrive da solo e non si toglie mai, così una copia vecchia non sposta di nuovo i file in coda. Migrazione 0002_ordine_scaricamento (account.download_order, entry.is_reel): prende il primo numero dopo quelle unite, E1 userà il successivo. Prove su motore (seme fisso), coda, lettori, ricette e API; dimostrazione con un account che ha l'ordine suo; guida e decisioni aggiornate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
208 lines
9.3 KiB
C#
208 lines
9.3 KiB
C#
using Febe.Core.Domain;
|
|
using Febe.Core.Engine;
|
|
using Febe.Core.Library;
|
|
|
|
namespace Febe.Web.Api;
|
|
|
|
/// <summary>The settings of an account (spec §4.4, mockup 05) between the domain and the form of the page.</summary>
|
|
public static class AccountSettingsMapper
|
|
{
|
|
public const double MinCustomPostHours = 1;
|
|
public const double MaxCustomPostHours = 48;
|
|
public const double MinCustomStoryHours = 1;
|
|
public const double MaxCustomStoryHours = 24;
|
|
|
|
/// <summary>Largest «Storico, massimo al giorno» the form accepts: twice the «Rapido» value (spec §7.5).</summary>
|
|
public static int MaxBackfillPerDay => CautionProfile.Fast.BackfillPostsPerAccountPerDay * 2;
|
|
|
|
/// <summary>The form of an account.</summary>
|
|
/// <param name="account">The stored account.</param>
|
|
/// <param name="zone">
|
|
/// Alberto's zone: «Da una data» is stored as the first instant of a local day (<see cref="LocalCalendar.StartOfDay"/>),
|
|
/// so it is shown as that local day, which <see cref="Apply"/> turns back into the same instant.
|
|
/// </param>
|
|
public static AccountSettingsDto ToDto(Account account, TimeZoneInfo zone)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
ArgumentNullException.ThrowIfNull(zone);
|
|
var reference = account with { IsFollowed = false, Frequency = account.Frequency == Frequency.Custom ? Frequency.Custom : account.Frequency };
|
|
return new AccountSettingsDto(
|
|
account.KeepUpdated,
|
|
ApiMapping.Frequency(account.Frequency),
|
|
Math.Round((account.CustomPostInterval ?? CheckPolicy.ProfileCheckInterval(reference)).TotalHours, 2),
|
|
Math.Round((account.CustomStoryInterval ?? CheckPolicy.StoryMaxInterval(reference)).TotalHours, 2),
|
|
ApiMapping.Types(account.ContentTypes),
|
|
ApiMapping.Depth(account.Limits.Depth),
|
|
account.Limits.LastPosts,
|
|
account.Limits.FromDate is { } from ? ApiMapping.Day(LocalCalendar.Today(from, zone)) : null,
|
|
account.Limits.BackfillPostsPerDay,
|
|
account.Limits.MaxVideoBytes is { } bytes ? Math.Round(bytes / 1024d / 1024d, 1) : null,
|
|
account.Limits.StoryVideos,
|
|
account.Folder,
|
|
account.DownloadOrder is { } own ? DownloadOrder.Parse(own).ToNames() : null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Applies the form to an account, with Italian errors for values out of range. «Tieni aggiornato» moves the state:
|
|
/// off on an up-to-date account pauses it (the single download is done), on reactivates a paused one.
|
|
/// </summary>
|
|
/// <param name="account">The stored account.</param>
|
|
/// <param name="dto">The form.</param>
|
|
/// <param name="zone">Alberto's zone (dates of «Da una data»).</param>
|
|
/// <param name="now">Now (state change time).</param>
|
|
/// <param name="folder">The folder value already resolved by the caller (null: default).</param>
|
|
public static Account Apply(Account account, AccountSettingsDto dto, TimeZoneInfo zone, DateTimeOffset now, string? folder)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
ArgumentNullException.ThrowIfNull(dto);
|
|
ArgumentNullException.ThrowIfNull(zone);
|
|
var frequency = ApiMapping.ParseFrequency(dto.Frequency) ?? throw new BadRequestException("Frequenza sconosciuta.");
|
|
TimeSpan? customPost = account.CustomPostInterval;
|
|
TimeSpan? customStory = account.CustomStoryInterval;
|
|
if (frequency == Frequency.Custom)
|
|
{
|
|
if (dto.CustomPostHours is not { } postHours || double.IsNaN(postHours) || postHours < MinCustomPostHours || postHours > MaxCustomPostHours)
|
|
{
|
|
throw new BadRequestException($"Su misura: i post si controllano fra ogni {Number(MinCustomPostHours)} e ogni {Number(MaxCustomPostHours)} ore.");
|
|
}
|
|
|
|
if (dto.CustomStoryHours is not { } storyHours || double.IsNaN(storyHours) || storyHours < MinCustomStoryHours || storyHours > MaxCustomStoryHours)
|
|
{
|
|
throw new BadRequestException($"Su misura: fra due controlli delle storie passano da {Number(MinCustomStoryHours)} a {Number(MaxCustomStoryHours)} ore.");
|
|
}
|
|
|
|
customPost = TimeSpan.FromMinutes(Math.Round(postHours * 60));
|
|
customStory = TimeSpan.FromMinutes(Math.Round(storyHours * 60));
|
|
}
|
|
|
|
var types = ApiMapping.Types(dto.Types ?? throw new BadRequestException("Manca «Cosa scaricare»."));
|
|
var depth = ApiMapping.ParseDepth(dto.Depth) ?? throw new BadRequestException("«Quanto indietro nel tempo» non valido.");
|
|
int? lastPosts = null;
|
|
DateTimeOffset? fromDate = null;
|
|
if (depth == HistoryDepth.LastPosts)
|
|
{
|
|
if (dto.LastPosts is not { } n || n < 1 || n > 1_000_000)
|
|
{
|
|
throw new BadRequestException("Indica quanti post tenere: almeno 1.");
|
|
}
|
|
|
|
lastPosts = n;
|
|
}
|
|
else if (depth == HistoryDepth.FromDate)
|
|
{
|
|
var day = ApiMapping.ParseDay(dto.FromDate) ?? throw new BadRequestException("Indica la data da cui partire.");
|
|
if (day > LocalCalendar.Today(now, zone))
|
|
{
|
|
throw new BadRequestException("La data da cui partire non può essere nel futuro.");
|
|
}
|
|
|
|
fromDate = LocalCalendar.StartOfDay(day, zone);
|
|
}
|
|
|
|
if (dto.BackfillPerDay < 1 || dto.BackfillPerDay > MaxBackfillPerDay)
|
|
{
|
|
throw new BadRequestException($"Storico, massimo al giorno: fra 1 e {ItalianText.Number(MaxBackfillPerDay)} post.");
|
|
}
|
|
|
|
long? maxVideo = null;
|
|
if (dto.MaxVideoMb is { } mb)
|
|
{
|
|
if (double.IsNaN(mb) || mb < 1 || mb > 100_000)
|
|
{
|
|
throw new BadRequestException("Salta i video oltre: un numero di MB da 1 in su, oppure vuoto per nessun limite.");
|
|
}
|
|
|
|
maxVideo = (long)Math.Round(mb * 1024 * 1024);
|
|
}
|
|
|
|
var state = account.State;
|
|
var changedAt = account.StateChangedAt;
|
|
if (account.KeepUpdated && !dto.KeepUpdated && state == AccountState.UpToDate)
|
|
{
|
|
state = AccountState.Paused;
|
|
changedAt = now;
|
|
}
|
|
else if (!account.KeepUpdated && dto.KeepUpdated && state == AccountState.Paused)
|
|
{
|
|
state = ReactivatedState(account, depth);
|
|
changedAt = now;
|
|
}
|
|
|
|
return account with
|
|
{
|
|
KeepUpdated = dto.KeepUpdated,
|
|
Frequency = frequency,
|
|
CustomPostInterval = customPost,
|
|
CustomStoryInterval = customStory,
|
|
ContentTypes = types,
|
|
Limits = account.Limits with
|
|
{
|
|
Depth = depth,
|
|
LastPosts = lastPosts,
|
|
FromDate = fromDate,
|
|
BackfillPostsPerDay = dto.BackfillPerDay,
|
|
MaxVideoBytes = maxVideo,
|
|
StoryVideos = dto.StoryVideos,
|
|
},
|
|
Folder = folder,
|
|
DownloadOrder = dto.DownloadOrder is { Count: > 0 } names ? DownloadOrder.From(names).ToString() : null,
|
|
State = state,
|
|
StateChangedAt = changedAt,
|
|
};
|
|
}
|
|
|
|
/// <summary>State of a paused account that comes back: backfilling while its history is not complete.</summary>
|
|
public static AccountState ReactivatedState(Account account, HistoryDepth? depth = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
var d = depth ?? account.Limits.Depth;
|
|
return account.BackfillCompletedAt is null && d != HistoryDepth.FromDate && CheckPolicy.WantsProfileChecks(account)
|
|
? AccountState.Backfilling
|
|
: AccountState.UpToDate;
|
|
}
|
|
|
|
/// <summary>«Nuovi post ogni 3 ore circa, storie almeno ogni 6 ore» beside the frequency buttons.</summary>
|
|
public static string FrequencyHint(Account account)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
var parts = new List<string>();
|
|
if (CheckPolicy.WantsProfileChecks(account))
|
|
{
|
|
parts.Add(account.IsFollowed
|
|
? "nuovi post dal giro dei profili che segui, profilo riaperto " + ItalianText.Every(CheckPolicy.ProfileCheckInterval(account))
|
|
: "nuovi post " + ItalianText.Every(CheckPolicy.ProfileCheckInterval(account)) + " circa");
|
|
}
|
|
|
|
if (CheckPolicy.WantsStories(account))
|
|
{
|
|
parts.Add("storie almeno " + ItalianText.Every(CheckPolicy.StoryMaxInterval(account)));
|
|
}
|
|
|
|
return parts.Count == 0 ? "Niente da controllare con queste scelte." : ItalianText.Capitalize(string.Join(", ", parts));
|
|
}
|
|
|
|
/// <summary>The note under the panel: what being followed or not means for this account.</summary>
|
|
public static string? Note(Account account)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(account);
|
|
if (account.State == AccountState.ArchiveOnly || account.IsMine)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!account.IsFollowed && !account.IsPrivate)
|
|
{
|
|
return "È un profilo pubblico che non segui: per vedere le novità Febe deve aprire la sua pagina. Se diventa privato, Febe si ferma e ti avvisa.";
|
|
}
|
|
|
|
if (account.IsFollowed)
|
|
{
|
|
return "Lo segui: le novità arrivano dal giro dei profili che segui, senza aprire ogni volta la sua pagina.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string Number(double value) => ItalianText.Decimal(value, 1);
|
|
}
|