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