using System.Text; namespace Febe.Core.Domain; /// /// 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 rows. /// /// 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). /// /// public sealed record Entry { /// Local key (0 until stored). public long Id { get; init; } public required long AccountId { get; init; } public required EntryKind Kind { get; init; } /// Instagram identifier, as text. Null only for imported entries that were not recognized. public string? InstagramId { get; init; } /// Post code (short code), when the kind has one. public string? Code { get; init; } /// Publication time, UTC. public required DateTimeOffset PublishedAt { get; init; } /// Caption. Never written to logs (spec §13.2). public string? Caption { get; init; } /// Author's user name, for tagged posts (the author is someone else). 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; } /// When Alberto deleted it from the library (state ). public DateTimeOffset? DeletedAt { get; init; } /// For stories: when the story stops being available on the site (downloads give them precedence). public DateTimeOffset? ExpiresAt { get; init; } /// The value of the {codice} placeholder (spec §4.3) for this entry. /// SHA-256 of the file, needed for profile pictures (first eight hex digits). 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; } } /// An entry with its files, in position order. public sealed record EntryWithMedia(Entry Entry, IReadOnlyList Media); /// Derives the state of an entry from the states of its files. public static class EntryStateRules { /// /// 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. /// public static EntryState FromMedia(EntryState current, Provenance provenance, IReadOnlyCollection 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; } /// States whose entries appear in the library mosaic (they have at least one file). public static IReadOnlyList Visible { get; } = [EntryState.Downloaded, EntryState.Partial, EntryState.Imported]; /// True when the file state means a file is on disk. public static bool IsPresent(MediaState state) => state is MediaState.Downloaded or MediaState.Imported; }