Files
Febe/src/Febe.Core/Domain/Account.cs
T
Alby96andClaude Opus 5.5 0547c747c5 D1, seconda parte: «Cosa scaricare prima» anche per account, reel distinti e quota per chi aspetta
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>
2026-11-25 15:25:34 +01:00

224 lines
9.0 KiB
C#

namespace Febe.Core.Domain;
/// <summary>«Quanto indietro nel tempo» (spec §4.4).</summary>
public enum HistoryDepth
{
/// <summary>Tutto: the whole history.</summary>
All,
/// <summary>Ultimi N post: only the most recent <see cref="AccountLimits.LastPosts"/> posts.</summary>
LastPosts,
/// <summary>Da una data: only content published from <see cref="AccountLimits.FromDate"/> onward.</summary>
FromDate,
}
/// <summary>Why an account is «Non raggiungibile» (spec §4.2).</summary>
public enum UnreachableReason
{
/// <summary>Private profile that Alberto does not follow: never touched (rule 6).</summary>
PrivateNotFollowed,
/// <summary>The profile does not exist (any more).</summary>
NotFound,
/// <summary>The profile blocked Alberto or is restricted.</summary>
Blocked,
/// <summary>A profile that is not followed changed name: Alberto must indicate the new one.</summary>
Renamed,
}
/// <summary>
/// Notices that put an account in the «Da controllare» filter together with the Unreachable state (spec §4.2).
/// They stay until Alberto acknowledges them.
/// </summary>
[Flags]
public enum AccountNotices
{
None = 0,
/// <summary>The profile changed name: Febe updated it and kept the folder.</summary>
Renamed = 1,
/// <summary>A reader met a response format it did not recognize (spec §6.4).</summary>
UnrecognizedFormat = 2,
}
/// <summary>Per-account limits (spec §4.4 «Limiti»). Account and general limits apply together: the stricter wins.</summary>
public sealed record AccountLimits
{
public HistoryDepth Depth { get; init; } = HistoryDepth.All;
/// <summary>Number of most recent posts to keep when <see cref="Depth"/> is <see cref="HistoryDepth.LastPosts"/>.</summary>
public int? LastPosts { get; init; }
/// <summary>First instant (UTC) of the period to download when <see cref="Depth"/> is <see cref="HistoryDepth.FromDate"/>.</summary>
public DateTimeOffset? FromDate { get; init; }
/// <summary>«Storico, massimo al giorno»: backfill posts per day for this account.</summary>
public int BackfillPostsPerDay { get; init; } = CautionProfile.Prudent.BackfillPostsPerAccountPerDay;
/// <summary>Videos larger than this are skipped. Null means no limit (the default).</summary>
public long? MaxVideoBytes { get; init; }
/// <summary>«Scarica anche i video delle storie».</summary>
public bool StoryVideos { get; init; } = true;
}
/// <summary>
/// An Instagram account that Febe archives (spec §4.2, §4.4, §8.3).
/// <para>
/// The identity of an account is its numeric Instagram identifier (<see cref="InstagramId"/>), never the user name:
/// a renamed profile keeps its row, its entries and its folder. <see cref="Id"/> is the local row key used by the
/// database and by <c>IGovernor</c>; it exists because accounts created by the import («Solo archivio») may not know
/// their Instagram identifier yet.
/// </para>
/// </summary>
public sealed record Account
{
/// <summary>Local key (0 until stored).</summary>
public long Id { get; init; }
/// <summary>Numeric Instagram identifier, as text. Unique when present; null only for archive-only accounts.</summary>
public string? InstagramId { get; init; }
public required string Username { get; init; }
public string? DisplayName { get; init; }
public bool IsPrivate { get; init; }
/// <summary>Alberto follows this profile.</summary>
public bool IsFollowed { get; init; }
/// <summary>Alberto's own profile («Tu»). At most one account has it.</summary>
public bool IsMine { get; init; }
public AccountState State { get; init; } = AccountState.UpToDate;
/// <summary>When <see cref="State"/> last changed (e.g. «Messo in pausa da te il 2 ottobre»).</summary>
public DateTimeOffset? StateChangedAt { get; init; }
public UnreachableReason? UnreachableReason { get; init; }
/// <summary>«Tieni aggiornato». With false a single download is made, then the account stays paused.</summary>
public bool KeepUpdated { get; init; } = true;
public Frequency Frequency { get; init; } = Frequency.Normal;
/// <summary>«Su misura»: how often to check posts.</summary>
public TimeSpan? CustomPostInterval { get; init; }
/// <summary>«Su misura»: maximum interval between two story checks.</summary>
public TimeSpan? CustomStoryInterval { get; init; }
/// <summary>«Cosa scaricare».</summary>
public ContentTypes ContentTypes { get; init; } = ContentTypes.PostPhotos | ContentTypes.PostVideos | ContentTypes.Stories
| ContentTypes.Highlights | ContentTypes.ProfilePicture;
public AccountLimits Limits { get; init; } = new();
/// <summary>
/// «Cosa scaricare prima» of this account (spec §4.4, §7.2, tappa D1): the stored text of its own order (the Italian
/// names of the five kinds, read by <c>Febe.Core.Engine.DownloadOrder.Parse</c>). Null: «Come le impostazioni
/// generali».
/// </summary>
public string? DownloadOrder { get; init; }
/// <summary>
/// Folder of the account. Null means the default <c>&lt;Libreria&gt;/&lt;username&gt;</c>; a relative path is relative
/// to the library (a renamed account keeps its old folder this way); an absolute path may be outside the library.
/// Resolved by <c>Febe.Core.Library.LibraryLayout.AccountFolder</c>.
/// </summary>
public string? Folder { get; init; }
/// <summary>Opaque resume point of the backfill (Storico), written by the collection pipeline.</summary>
public string? BackfillCursor { get; init; }
/// <summary>When the backfill reached the limit or the oldest post.</summary>
public DateTimeOffset? BackfillCompletedAt { get; init; }
/// <summary>Number of posts declared by the profile, when known (used to estimate the backfill).</summary>
public int? PostCount { get; init; }
public DateTimeOffset? LastProfileCheckAt { get; init; }
public DateTimeOffset? NextProfileCheckAt { get; init; }
public DateTimeOffset? LastStoriesCheckAt { get; init; }
public DateTimeOffset? NextStoriesCheckAt { get; init; }
public AccountNotices Notices { get; init; }
/// <summary>Last user name before a rename.</summary>
public string? PreviousUsername { get; init; }
public DateTimeOffset? RenamedAt { get; init; }
/// <summary>Last time a reader met an unknown format for this account.</summary>
public DateTimeOffset? FormatUnrecognizedAt { get; init; }
public required DateTimeOffset AddedAt { get; init; }
/// <summary>True for the «Da controllare» filter: unreachable, renamed or with an unrecognized format.</summary>
public bool NeedsAttention => State == AccountState.Unreachable || Notices != AccountNotices.None;
/// <summary>
/// A new account with the values of a starting point (spec §4.2). Common values: frequency Normal,
/// tagged posts only for Alberto's own profile, backfill per day from the caution profile, no video size limit,
/// story videos included, default folder.
/// </summary>
/// <param name="username">User name.</param>
/// <param name="startingPoint">One of the three starting points.</param>
/// <param name="now">Current time (from IClock).</param>
/// <param name="zone">Alberto's time zone: «da oggi» starts at local midnight.</param>
/// <param name="profile">Caution profile in use (defaults to Prudente).</param>
/// <param name="isMine">Alberto's own profile: «Post in cui è taggato» is on.</param>
public static Account Create(
string username,
StartingPoint startingPoint,
DateTimeOffset now,
TimeZoneInfo zone,
CautionProfile? profile = null,
bool isMine = false)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username);
ArgumentNullException.ThrowIfNull(zone);
profile ??= CautionProfile.Prudent;
var everything = ContentTypes.PostPhotos | ContentTypes.PostVideos | ContentTypes.Stories
| ContentTypes.Highlights | ContentTypes.ProfilePicture;
var types = startingPoint == StartingPoint.OnlyStories
? ContentTypes.Stories | ContentTypes.Highlights
: everything;
if (isMine && startingPoint != StartingPoint.OnlyStories)
{
types |= ContentTypes.Tagged;
}
var limits = new AccountLimits { BackfillPostsPerDay = profile.BackfillPostsPerAccountPerDay };
if (startingPoint == StartingPoint.OnlyNewFromToday)
{
limits = limits with
{
Depth = HistoryDepth.FromDate,
FromDate = LocalCalendar.StartOfDay(LocalCalendar.Today(now, zone), zone),
};
}
return new Account
{
Username = username.Trim().TrimStart('@'),
IsMine = isMine,
KeepUpdated = true,
Frequency = Frequency.Normal,
ContentTypes = types,
Limits = limits,
AddedAt = now,
StateChangedAt = now,
};
}
}