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
138 lines
5.4 KiB
C#
138 lines
5.4 KiB
C#
using System.Text;
|
|
|
|
namespace Febe.Core.Domain;
|
|
|
|
/// <summary>
|
|
/// An entry («elemento», spec §4.3): one publication — a post (even with several photos and videos), a story, an item
|
|
/// of a highlight collection, a tagged post or a profile picture. Its files are <see cref="Media"/> rows.
|
|
/// <para>
|
|
/// Unique per (account, kind, Instagram identifier): the same post may appear under two accounts, as a post of one and
|
|
/// as a tagged post of the other; they are two entries with two copies of the files (spec §8.3).
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed record Entry
|
|
{
|
|
/// <summary>Local key (0 until stored).</summary>
|
|
public long Id { get; init; }
|
|
|
|
public required long AccountId { get; init; }
|
|
|
|
public required EntryKind Kind { get; init; }
|
|
|
|
/// <summary>Instagram identifier, as text. Null only for imported entries that were not recognized.</summary>
|
|
public string? InstagramId { get; init; }
|
|
|
|
/// <summary>Post code (short code), when the kind has one.</summary>
|
|
public string? Code { get; init; }
|
|
|
|
/// <summary>Publication time, UTC.</summary>
|
|
public required DateTimeOffset PublishedAt { get; init; }
|
|
|
|
/// <summary>Caption. Never written to logs (spec §13.2).</summary>
|
|
public string? Caption { get; init; }
|
|
|
|
/// <summary>Author's user name, for tagged posts (the author is someone else).</summary>
|
|
public string? AuthorUsername { get; init; }
|
|
|
|
public string? AuthorInstagramId { get; init; }
|
|
|
|
public EntryState State { get; init; } = EntryState.ToDownload;
|
|
|
|
public Provenance Provenance { get; init; } = Provenance.Downloaded;
|
|
|
|
public required DateTimeOffset DiscoveredAt { get; init; }
|
|
|
|
public DateTimeOffset? DownloadedAt { get; init; }
|
|
|
|
/// <summary>When Alberto deleted it from the library (state <see cref="EntryState.Deleted"/>).</summary>
|
|
public DateTimeOffset? DeletedAt { get; init; }
|
|
|
|
/// <summary>For stories: when the story stops being available on the site (downloads give them precedence).</summary>
|
|
public DateTimeOffset? ExpiresAt { get; init; }
|
|
|
|
/// <summary>The value of the <c>{codice}</c> placeholder (spec §4.3) for this entry.</summary>
|
|
/// <param name="sha256">SHA-256 of the file, needed for profile pictures (first eight hex digits).</param>
|
|
public string NameCode(string? sha256 = null) => Kind switch
|
|
{
|
|
EntryKind.Post or EntryKind.Tagged => Code ?? InstagramId ?? "senza-codice",
|
|
EntryKind.Story or EntryKind.Highlight => InstagramId ?? Code ?? "senza-codice",
|
|
EntryKind.ProfilePicture => sha256 is { Length: >= 8 } ? sha256[..8] : InstagramId ?? "profilo",
|
|
_ => Code ?? InstagramId ?? "senza-codice",
|
|
};
|
|
|
|
// Keeps the caption out of ToString(): records are easy to log by mistake.
|
|
private bool PrintMembers(StringBuilder builder)
|
|
{
|
|
builder.Append($"Id = {Id}, AccountId = {AccountId}, Kind = {Kind}, InstagramId = {InstagramId}, Code = {Code}, ");
|
|
builder.Append($"PublishedAt = {PublishedAt:O}, State = {State}, Provenance = {Provenance}");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>An entry with its files, in position order.</summary>
|
|
public sealed record EntryWithMedia(Entry Entry, IReadOnlyList<Media> Media);
|
|
|
|
/// <summary>Derives the state of an entry from the states of its files.</summary>
|
|
public static class EntryStateRules
|
|
{
|
|
/// <summary>
|
|
/// State of an entry given its files. A deleted entry stays deleted; files present (downloaded or imported) and
|
|
/// files still to download give «parziale»; skipped files (a limit) are settled, not pending.
|
|
/// </summary>
|
|
public static EntryState FromMedia(EntryState current, Provenance provenance, IReadOnlyCollection<MediaState> media)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(media);
|
|
if (current == EntryState.Deleted || media.Count == 0)
|
|
{
|
|
return current;
|
|
}
|
|
|
|
int present = 0, pending = 0, failed = 0, imported = 0;
|
|
foreach (var state in media)
|
|
{
|
|
switch (state)
|
|
{
|
|
case MediaState.Downloaded:
|
|
present++;
|
|
break;
|
|
case MediaState.Imported:
|
|
present++;
|
|
imported++;
|
|
break;
|
|
case MediaState.ToDownload:
|
|
case MediaState.Expired:
|
|
pending++;
|
|
break;
|
|
case MediaState.Failed:
|
|
failed++;
|
|
break;
|
|
default:
|
|
break; // Skipped, Deleted: settled without a file
|
|
}
|
|
}
|
|
|
|
if (present == media.Count)
|
|
{
|
|
return imported == media.Count || provenance == Provenance.Imported ? EntryState.Imported : EntryState.Downloaded;
|
|
}
|
|
|
|
if (pending > 0)
|
|
{
|
|
return present > 0 ? EntryState.Partial : EntryState.ToDownload;
|
|
}
|
|
|
|
if (failed > 0)
|
|
{
|
|
return present > 0 ? EntryState.Partial : EntryState.Failed;
|
|
}
|
|
|
|
return present > 0 ? EntryState.Partial : EntryState.Skipped;
|
|
}
|
|
|
|
/// <summary>States whose entries appear in the library mosaic (they have at least one file).</summary>
|
|
public static IReadOnlyList<EntryState> Visible { get; } = [EntryState.Downloaded, EntryState.Partial, EntryState.Imported];
|
|
|
|
/// <summary>True when the file state means a file is on disk.</summary>
|
|
public static bool IsPresent(MediaState state) => state is MediaState.Downloaded or MediaState.Imported;
|
|
}
|