Files
Febe/src/Febe.Core/Domain/Account.cs
T
Claude 3aeaa17da7 M1 Dati e libreria: modello, archivi SQLite con scrittore unico, nomi dei file, scrittura sicura, miniature, impronte, _febe.jsonl, copie, eliminazione e ricostruzione
Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
2026-10-13 16:06:15 +00:00

217 lines
8.6 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>
/// 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,
};
}
}