Raccolta: 429 del CDN come rallentamento, attese crescenti e recupero dei falliti, storico e seguiti senza pagina di ripresa, lacuna sotto la prima pagina, eliminazioni durante lo scaricamento (regole-3, cdn-429, dl-1, post-loop, walk-1, check-1, scroll-1, reader-1, h9-1, ring-1, library-2, dedup-1, tmp-1, budget-1, regole-4, kind-1, vocab-1)

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

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 00:23:03 +00:00
1 parent 78c92eb2a4
commit b16e3a837d
28 files changed
+2619 -200

No files matched your search

@@ -8,24 +8,128 @@ namespace Febe.Core.Collection.Recipes;
public sealed partial class RecipeRunner
{
/// <summary>A walk through the pages of a profile tab (Storico on the grid, Taggati, Reel).</summary>
/// <summary>A walk through the pages of a profile tab (Storico on the grid, Taggati, Reel, the gap of the check).</summary>
private sealed record ProfileWalk(EntryKind Kind, string? TabTarget, string ResumePage, string SettingPrefix)
{
public static readonly ProfileWalk Posts = new(EntryKind.Post, null, SitePages.ProfileFrom, "raccolta.post");
public static readonly ProfileWalk Tagged = new(EntryKind.Tagged, SiteTargets.ProfileTabTagged, SitePages.TaggedFrom, "raccolta.taggati");
public static readonly ProfileWalk Reels = new(EntryKind.Post, SiteTargets.ProfileTabReels, SitePages.ReelsFrom, "raccolta.reel");
/// <summary>The posts missing below the first page of the grid, filled by the profile check (same pages as the grid).</summary>
public static readonly ProfileWalk Gap = new(EntryKind.Post, null, SitePages.ProfileFrom, "raccolta.lacuna");
public bool IsGrid => TabTarget is null;
public string CursorKey(long accountId) => $"{SettingPrefix}.{accountId.ToString(CultureInfo.InvariantCulture)}.cursore";
/// <summary>The grid and the reels come newest first (pinned posts aside); the order of tagged posts is not known.</summary>
public bool IsChronological => Kind != EntryKind.Tagged;
public string CompletedKey(long accountId) => $"{SettingPrefix}.{accountId.ToString(CultureInfo.InvariantCulture)}.completo";
public string CursorKey(long accountId) => Key(accountId, "cursore");
public string CompletedKey(long accountId) => Key(accountId, "completo");
/// <summary>The last item read before the stored cursor («time|id»): with it the point is found again by scrolling.</summary>
public string MarkKey(long accountId) => Key(accountId, "segno");
/// <summary>Gap only: known posts newer than this time do not end the walk (an older gap is still open below them).</summary>
public string KnownBeforeKey(long accountId) => Key(accountId, "fino");
private string Key(long accountId, string part) => $"{SettingPrefix}.{accountId.ToString(CultureInfo.InvariantCulture)}.{part}";
}
/// <summary>
/// Where a walk stopped: the cursor the site gave after the last page read, and the last item of that page. Without
/// a resume page in the map the walk gets back there by scrolling (decision 76): new posts on top move the pages
/// and their cursors, the item stays where it is.
/// </summary>
private sealed record ResumePoint(string Cursor, string? LastId, DateTimeOffset? LastAt)
{
public static ResumePoint Parse(string cursor, string? mark)
{
var separator = mark?.IndexOf('|', StringComparison.Ordinal) ?? -1;
if (mark is null || separator <= 0)
{
return new ResumePoint(cursor, null, null);
}
var at = DateTimeOffset.TryParse(mark[..separator], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var time) ? time : (DateTimeOffset?)null;
var id = mark[(separator + 1)..];
return new ResumePoint(cursor, id.Length > 0 ? id : null, at);
}
public static string? MarkOf(ContentItem? item) =>
item is null ? null : $"{item.TakenAt.UtcDateTime.ToString("O", CultureInfo.InvariantCulture)}|{item.Id}";
/// <summary>
/// True when the page of <paramref name="items"/> holds the stored point or comes after it: the same cursor,
/// the same last item, or (newest first) a regular post older than it.
/// </summary>
public bool IsReachedBy(PageInfo? page, IReadOnlyList<ContentItem> items, Func<ContentItem, bool> olderCounts) =>
(page?.Cursor is { } cursor && string.Equals(cursor, Cursor, StringComparison.Ordinal))
|| (LastId is { } id && items.Any(i => string.Equals(i.Id, id, StringComparison.Ordinal)))
|| (LastAt is { } at && items.Any(i => olderCounts(i) && i.TakenAt < at));
}
/// <summary>What stops a walk at known content: nothing, any known post, or only known posts older than a time.</summary>
private sealed record KnownStop(bool Enabled, DateTimeOffset? Before = null)
{
public static readonly KnownStop Never = new(false);
public static readonly KnownStop Any = new(true);
public bool Stops(ContentItem item) => Enabled && (Before is not { } before || item.TakenAt < before);
}
/// <summary>How a walk over the pages of a profile ended.</summary>
private enum WalkEnd
{
/// <summary>No more pages: the beginning of the profile.</summary>
EndOfList,
/// <summary>A history limit of the account (from a date, last N posts).</summary>
Limit,
/// <summary>Known content, where the walk was to stop at it.</summary>
Known,
/// <summary>The pages allowed to this run were read.</summary>
Pages,
/// <summary>No page came after the bursts (counted as an empty page).</summary>
NoData,
}
/// <summary>The rules of one walk over the pages of a profile, starting from the page on screen.</summary>
private sealed record WalkRules
{
/// <summary>A stored point to get back to by scrolling first: the pages before it are read and saved, not counted.</summary>
public ResumePoint? CatchUpTo { get; init; }
/// <summary>Pages of this run counted before the walk (a resume page, a tab).</summary>
public int PagesRead { get; init; }
public required int MaxPages { get; init; }
/// <summary>Known content that ends the walk, once past the stored point.</summary>
public required KnownStop Stop { get; init; }
/// <summary>«Lo storico non corre più veloce dei download» before each page.</summary>
public required bool CheckDownloads { get; init; }
/// <summary>Whether the backfill suspension is announced in the timeline («Storico sospeso»).</summary>
public bool AnnounceSuspension { get; init; } = true;
/// <summary>Stores the point after each page read past the stored one: its cursor and its last item.</summary>
public required Func<string, ContentItem?, Task> Store { get; init; }
/// <summary>Items already saved by the caller (they are not saved again).</summary>
public HashSet<string> Processed { get; init; } = new(StringComparer.Ordinal);
}
/// <summary>
/// Controllo del profilo (spec §6.3): opens the profile (1 counted action), reads its state, the first page of
/// posts and the list of highlight collections, keeps a copy of the profile picture when it changes, and stops at
/// known content. Pinned posts are recognized and skipped for the stop rule.
/// known content. Pinned posts are recognized and skipped for the stop rule. When the first page ends with posts
/// Febe did not have, more may be missing below it: the check goes on down the grid until known content
/// (<see cref="FillGapAsync"/>).
/// </summary>
private async Task<RecipeStatus> ProfileCheckAsync(RecipeRun run)
{
@@ -48,8 +152,13 @@ public sealed partial class RecipeRunner
run.NewEntries++;
}
var reachedKnown = false;
foreach (var item in Contents(reads, c => IsOwnedBy(c, account)))
// The whole first page is read (it is on screen already): known posts at its top do not hide new ones below
// them, e.g. when the giro dei seguiti saved only the newest. Pinned posts are known but stop nothing.
var walk = ProfileWalk.Posts;
var items = PageItems(reads, account, walk);
var sawKnown = false;
SaveOutcome? tail = null;
foreach (var item in items)
{
var saved = await Writer.SaveAsync(account, item, EntryKind.Post, run.Session.SessionId, run.Ct).ConfigureAwait(false);
if (saved.Outcome == SaveOutcome.Created)
@@ -57,35 +166,135 @@ public sealed partial class RecipeRunner
run.NewEntries++;
run.AccountsWithNewPosts.Add(account.Id);
}
else if (saved.Outcome == SaveOutcome.Known && !item.Pinned)
if (!IsPinned(item, items, walk))
{
reachedKnown = true; // pinned posts are known but do not stop the check
break;
tail = saved.Outcome;
sawKnown |= saved.Outcome == SaveOutcome.Known;
}
}
var page = LastPage(reads);
if (!reachedKnown && account.BackfillCompletedAt is null && account.BackfillCursor is null)
if (!sawKnown && account.BackfillCompletedAt is null && account.BackfillCursor is null)
{
if (page?.HasMore == true && page.Cursor is { } cursor)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, cursor, null, run.Ct).ConfigureAwait(false);
await Env.Storage.Settings.SetAsync(walk.MarkKey(account.Id), ResumePoint.MarkOf(items.LastOrDefault()), run.Ct).ConfigureAwait(false);
}
else if (page?.HasMore == false)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
}
}
else if (CollectionWriter.Wants(account, EntryKind.Post) && (account.BackfillCursor is not null || account.BackfillCompletedAt is not null))
{
await FillGapAsync(run, account, reads, items, tailIsNew: tail == SaveOutcome.Created).ConfigureAwait(false);
}
await Env.Driver.MaybeDistractionAsync(run.Ct).ConfigureAwait(false);
return RecipeStatus.Completed;
}
/// <summary>
/// The second half of the profile check, once the backfill has started or ended: when the first page ends with
/// posts Febe did not have (Febe off for days, the account paused or unreachable), the posts between it and the
/// known ones would be fetched by nothing else. The check goes on down the grid, one counted page each and at most
/// <see cref="RecipeOptions.GapPagesPerCheck"/>, until it meets known posts; the rest waits for the next check from
/// the stored point. A refused permit, the end of the session or too many files waiting for download leave the
/// gap for later: the check itself is done.
/// </summary>
private async Task FillGapAsync(RecipeRun run, Account account, IReadOnlyList<CapturedRead> reads, IReadOnlyList<ContentItem> firstPage, bool tailIsNew)
{
var walk = ProfileWalk.Gap;
var settings = Env.Storage.Settings;
var keys = new[] { walk.CursorKey(account.Id), walk.MarkKey(account.Id), walk.KnownBeforeKey(account.Id) };
var storedCursor = await settings.GetAsync(keys[0], run.Ct).ConfigureAwait(false);
var stored = storedCursor is null ? null : ResumePoint.Parse(storedCursor, await settings.GetAsync(keys[1], run.Ct).ConfigureAwait(false));
var before = ParseTime(await settings.GetAsync(keys[2], run.Ct).ConfigureAwait(false));
async Task ClearAsync() => await settings.SetManyAsync(keys.ToDictionary(k => k, _ => (string?)null), run.Ct).ConfigureAwait(false);
if (LastPage(reads)?.HasMore == false)
{
if (stored is not null || before is not null)
{
await ClearAsync().ConfigureAwait(false); // the whole grid is on the first page, read just now
}
return;
}
// Known posts newer than the end of an older gap still open further down do not end the walk: one walk fills
// both. A stored point also stops only at posts older than its last item (those before it were saved by it).
var floor = Earliest(before, stored?.LastAt);
IReadOnlyList<CapturedRead> start = reads;
ResumePoint? catchUpTo = null;
var pages = 0;
if (!tailIsNew)
{
if (stored is null)
{
return;
}
if (Env.SiteMap.HasPage(walk.ResumePage))
{
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.OpenAsync(walk.ResumePage, Values(("username", account.Username), ("cursor", stored.Cursor))).ConfigureAwait(false);
start = await run.WaitForDataAsync(mark, r => r.Contents.Count > 0 || r.Page is not null).ConfigureAwait(false);
if (!run.Judge(start, r => r.Contents.Count > 0 || r.Page is not null, null))
{
return; // an empty page, counted: the gap stays for the next check
}
pages = 1;
}
else
{
catchUpTo = stored; // scroll down from the first page, on screen, back to the stored point
}
}
try
{
var (end, _, _) = await WalkPagesAsync(run, account, walk, start, new WalkRules
{
CatchUpTo = catchUpTo,
PagesRead = pages,
MaxPages = Math.Max(1, Env.Options.GapPagesPerCheck),
Stop = floor is { } f ? new KnownStop(true, f) : KnownStop.Any,
CheckDownloads = true,
AnnounceSuspension = false,
Processed = new HashSet<string>(firstPage.Select(i => i.Id), StringComparer.Ordinal),
Store = (cursor, last) => settings.SetManyAsync(
new Dictionary<string, string?>
{
[keys[0]] = cursor,
[keys[1]] = ResumePoint.MarkOf(last),
[keys[2]] = floor?.ToString("O", CultureInfo.InvariantCulture),
},
run.Ct),
}).ConfigureAwait(false);
if (end is WalkEnd.EndOfList or WalkEnd.Limit or WalkEnd.Known)
{
await ClearAsync().ConfigureAwait(false);
}
}
catch (RecipeStopException stop) when (stop.Status == RecipeStatus.Denied)
{
// No permit, the session is over, or too many files wait for download: the gap waits for the next check.
}
}
/// <summary>
/// Storico, Taggati and Reel (spec §6.3): one page per permit, resuming from the stored point and stopping at the
/// beginning of the profile. The backfill stops while the account has more than 60 files waiting for download or
/// the data budget is used up (the engine checks too, the recipe must stop as well). Once a tab has been walked to
/// the end, later runs only check its first page and stop at known content.
/// beginning of the profile. Without a resume page in the map (profileFrom, taggedFrom, reelsFrom are optional)
/// the walk scrolls from the top back to the stored point, each page counted by the Governor and saved on the
/// way, and only the pages after it count against the pages of the run (decision 76): never a silent restart. The
/// backfill stops while the account has more than 60 files waiting for download or the data budget is used up
/// (the engine checks too, the recipe must stop as well). Once a tab has been walked to the end, later runs only
/// check its first page and stop at known content.
/// </summary>
private async Task<RecipeStatus> WalkAsync(RecipeRun run, ProfileWalk walk)
{
@@ -121,6 +330,7 @@ public sealed partial class RecipeRunner
}
}
var target = cursor is null ? null : ResumePoint.Parse(cursor, await settings.GetAsync(walk.MarkKey(account.Id), run.Ct).ConfigureAwait(false));
if (!checkOnly)
{
await EnsureBackfillAllowedAsync(run, account).ConfigureAwait(false);
@@ -130,12 +340,13 @@ public sealed partial class RecipeRunner
var parameters = Values(("username", account.Username));
IReadOnlyList<CapturedRead> reads;
var pages = 0;
if (cursor is not null && Env.SiteMap.HasPage(walk.ResumePage))
ResumePoint? catchUpTo = null;
if (target is not null && Env.SiteMap.HasPage(walk.ResumePage))
{
// Resume where the last run stopped: the page of the site that starts after the stored point.
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.OpenAsync(walk.ResumePage, Values(("username", account.Username), ("cursor", cursor))).ConfigureAwait(false);
await run.OpenAsync(walk.ResumePage, Values(("username", account.Username), ("cursor", target.Cursor))).ConfigureAwait(false);
reads = await run.WaitForDataAsync(mark, r => r.Contents.Count > 0 || r.Page is not null).ConfigureAwait(false);
if (!run.Judge(reads, r => r.Contents.Count > 0 || r.Page is not null, Env.SiteMap.BuildPath(SitePages.Profile, parameters)))
{
@@ -159,54 +370,32 @@ public sealed partial class RecipeRunner
throw RecipeRun.Stop(RecipeStatus.Completed, EmptyPageMessage);
}
pages++;
if (target is null)
{
pages++;
}
}
catchUpTo = target;
}
var finished = false;
var processed = new HashSet<string>(StringComparer.Ordinal);
while (true)
var (end, caughtUp, catchUpPages) = await WalkPagesAsync(run, account, walk, reads, new WalkRules
{
var (reachedKnown, reachedLimit) = await SaveWalkPageAsync(run, account, walk, reads, processed, stopAtKnown: checkOnly).ConfigureAwait(false);
var page = LastPage(reads);
if (reachedLimit || page?.HasMore == false)
{
finished = !checkOnly || page?.HasMore == false;
break;
}
CatchUpTo = catchUpTo,
PagesRead = pages,
MaxPages = maxPages,
Stop = checkOnly ? KnownStop.Any : KnownStop.Never,
CheckDownloads = !checkOnly,
Store = (next, last) => StoreWalkPointAsync(run, account, walk, next, last),
}).ConfigureAwait(false);
if (checkOnly && reachedKnown)
{
break;
}
cursor = page?.Cursor ?? cursor;
await StoreWalkCursorAsync(run, account, walk, cursor).ConfigureAwait(false);
if (pages >= maxPages)
{
break;
}
if (!checkOnly)
{
await EnsureBackfillAllowedAsync(run, account).ConfigureAwait(false);
}
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
reads = await ScrollForPageAsync(run, r => r.Contents.Count > 0 || r.Page is not null).ConfigureAwait(false);
pages++;
if (reads.Count == 0)
{
break; // nothing came in after the bursts (counted as an empty page)
}
}
if (finished)
if (end == WalkEnd.EndOfList || (end == WalkEnd.Limit && !checkOnly))
{
run.Finished = true;
if (walk.IsGrid)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
await settings.SetAsync(walk.MarkKey(account.Id), null, run.Ct).ConfigureAwait(false);
await Writer.EventAsync("storico-completato", $"Storico di {account.Username} completato.", EventSeverity.Success, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
}
else
@@ -215,27 +404,107 @@ public sealed partial class RecipeRunner
new Dictionary<string, string?>
{
[walk.CursorKey(account.Id)] = null,
[walk.MarkKey(account.Id)] = null,
[walk.CompletedKey(account.Id)] = Env.Clock.Now.ToString("O", CultureInfo.InvariantCulture),
},
run.Ct).ConfigureAwait(false);
}
}
else if (!caughtUp)
{
await Writer.EventAsync(
"ripresa-scorrendo",
$"«{Name(run.Request.Kind)}» di {account.Username}: {ItalianText.Number(catchUpPages + 1)} pagine lette scorrendo senza ritrovare ancora il punto in cui si era fermato. Il punto resta salvato: si riprende da lì alla prossima sessione.",
EventSeverity.Info,
account.Id,
run.Session.SessionId,
run.Ct).ConfigureAwait(false);
}
return RecipeStatus.Completed;
}
/// <summary>
/// Saves the new items of a walk page. Returns whether known content was met (only meaningful when
/// <paramref name="stopAtKnown"/>) and whether a history limit of the account was reached (from a date, last N posts).
/// Saves the pages of a walk, from the page on screen: first back to the stored point when there is one (pages
/// read and saved, not counted against <see cref="WalkRules.MaxPages"/>, no point stored before it is reached),
/// then one page per permit until the end of the profile, a history limit, known content (when the rules say so)
/// or the pages of the run. Returns how it ended, whether the stored point was reached, and the pages scrolled to
/// get back there.
/// </summary>
private async Task<(bool ReachedKnown, bool ReachedLimit)> SaveWalkPageAsync(RecipeRun run, Account account, ProfileWalk walk, IReadOnlyList<CapturedRead> reads, HashSet<string> processed, bool stopAtKnown)
private async Task<(WalkEnd End, bool CaughtUp, int CatchUpPages)> WalkPagesAsync(RecipeRun run, Account account, ProfileWalk walk, IReadOnlyList<CapturedRead> reads, WalkRules rules)
{
var items = Contents(reads, c => walk.Kind == EntryKind.Tagged ? !IsOwnedBy(c, account) : IsOwnedBy(c, account))
.Where(c => processed.Add(c.Id))
.ToList();
foreach (var item in items)
var pending = rules.CatchUpTo;
var pages = rules.PagesRead;
var catchUp = 0;
while (true)
{
if (walk.Kind != EntryKind.Tagged && !item.Pinned)
var items = PageItems(reads, account, walk);
var page = LastPage(reads);
if (pending is not null && pending.IsReachedBy(page, items, i => walk.IsChronological && !IsPinned(i, items, walk)))
{
pending = null; // this page holds the stored point: from here on the walk goes on as usual
}
var (reachedKnown, reachedLimit) = await SaveWalkPageAsync(run, account, walk, items, rules.Processed, pending is null ? rules.Stop : KnownStop.Never).ConfigureAwait(false);
if (reachedLimit)
{
return (WalkEnd.Limit, pending is null, catchUp);
}
if (page?.HasMore == false)
{
return (WalkEnd.EndOfList, pending is null, catchUp);
}
if (reachedKnown)
{
return (WalkEnd.Known, true, catchUp);
}
if (pending is null && page?.Cursor is { } next)
{
await rules.Store(next, items.LastOrDefault()).ConfigureAwait(false);
}
if (pending is null && pages >= rules.MaxPages)
{
return (WalkEnd.Pages, true, catchUp);
}
if (rules.CheckDownloads)
{
await EnsureBackfillAllowedAsync(run, account, rules.AnnounceSuspension).ConfigureAwait(false);
}
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
var counted = pending is null;
reads = await ScrollForPageAsync(run, r => r.Contents.Count > 0 || r.Page is not null).ConfigureAwait(false);
if (counted)
{
pages++;
}
else
{
catchUp++;
}
if (reads.Count == 0)
{
return (WalkEnd.NoData, pending is null, catchUp); // nothing came in after the bursts (counted as an empty page)
}
}
}
/// <summary>
/// Saves the new items of a walk page. Returns whether known content that ends the walk was met and whether a
/// history limit of the account was reached (from a date, last N posts).
/// </summary>
private async Task<(bool ReachedKnown, bool ReachedLimit)> SaveWalkPageAsync(RecipeRun run, Account account, ProfileWalk walk, IReadOnlyList<ContentItem> page, HashSet<string> processed, KnownStop stop)
{
foreach (var item in page.Where(c => processed.Add(c.Id)).ToList())
{
var pinned = IsPinned(item, page, walk);
if (walk.Kind != EntryKind.Tagged && !pinned)
{
if (account.Limits.Depth == HistoryDepth.FromDate && account.Limits.FromDate is { } from && item.TakenAt < from)
{
@@ -256,7 +525,7 @@ public sealed partial class RecipeRunner
run.NewEntries++;
run.AccountsWithNewPosts.Add(account.Id);
}
else if (stopAtKnown && saved.Outcome == SaveOutcome.Known && !item.Pinned)
else if (saved.Outcome == SaveOutcome.Known && !pinned && stop.Stops(item))
{
return (true, false);
}
@@ -265,33 +534,48 @@ public sealed partial class RecipeRunner
return (false, false);
}
private async Task StoreWalkCursorAsync(RecipeRun run, Account account, ProfileWalk walk, string? cursor)
/// <summary>The stored point of a walk: its cursor, and its last item to find it again by scrolling.</summary>
private async Task StoreWalkPointAsync(RecipeRun run, Account account, ProfileWalk walk, string cursor, ContentItem? last)
{
if (cursor is null)
{
return;
}
if (walk.IsGrid)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, cursor, null, run.Ct).ConfigureAwait(false);
await Env.Storage.Settings.SetAsync(walk.MarkKey(account.Id), ResumePoint.MarkOf(last), run.Ct).ConfigureAwait(false);
}
else
{
await Env.Storage.Settings.SetAsync(walk.CursorKey(account.Id), cursor, run.Ct).ConfigureAwait(false);
await Env.Storage.Settings.SetManyAsync(
new Dictionary<string, string?>
{
[walk.CursorKey(account.Id)] = cursor,
[walk.MarkKey(account.Id)] = ResumePoint.MarkOf(last),
},
run.Ct).ConfigureAwait(false);
}
}
/// <summary>The posts of a walk page: of the account (tagged: of others), never a story or a collection item.</summary>
private static IReadOnlyList<ContentItem> PageItems(IReadOnlyList<CapturedRead> reads, Account account, ProfileWalk walk) =>
Contents(reads, c => IsPostLike(c) && (walk.Kind == EntryKind.Tagged ? !IsOwnedBy(c, account) : IsOwnedBy(c, account)));
/// <summary>Pinned on top of the profile: the vocabulary says so, or (newest first) the order of the page does.</summary>
private bool IsPinned(ContentItem item, IReadOnlyList<ContentItem> page, ProfileWalk walk) =>
walk.IsChronological ? LooksPinned(item, page) : item.Pinned;
/// <summary>
/// «Lo storico non corre più veloce dei download» (spec §6.3): stops while the account has more than 60 files waiting
/// or the data budget is used up.
/// </summary>
private async Task EnsureBackfillAllowedAsync(RecipeRun run, Account account)
private async Task EnsureBackfillAllowedAsync(RecipeRun run, Account account, bool announce = true)
{
var pending = await Env.Storage.Media.CountPendingDownloadsAsync(account.Id, run.Ct).ConfigureAwait(false);
if (pending > Env.Options.MaxPendingDownloadsForBackfill)
{
await Writer.EventAsync("storico-sospeso", $"Storico di {account.Username} sospeso: {ItalianText.Number(pending)} file aspettano di essere scaricati.", EventSeverity.Info, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
if (announce)
{
await Writer.EventAsync("storico-sospeso", $"Storico di {account.Username} sospeso: {ItalianText.Number(pending)} file aspettano di essere scaricati.", EventSeverity.Info, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
}
throw RecipeRun.Stop(RecipeStatus.Denied, $"Storico sospeso: {ItalianText.Number(pending)} file di {account.Username} aspettano di essere scaricati (più di {Env.Options.MaxPendingDownloadsForBackfill}).");
}
@@ -350,30 +634,35 @@ public sealed partial class RecipeRunner
/// <summary>
/// Scrolls with bursts until a new page of data that satisfies <paramref name="expected"/> arrives, at most
/// <see cref="RecipeOptions.BurstsPerPage"/> bursts. Returns the reads of that page (empty when none came).
/// <see cref="RecipeOptions.BurstsPerPage"/> bursts. Only the expected data ends the bursts: a response the page
/// fetches for something else (a background poll, a bare status) says nothing about the next page. When the
/// bursts are used up without it, the responses received are judged: an unknown shape is «formato non
/// riconosciuto», anything else an empty page. Returns the reads of that page (empty when none came).
/// </summary>
private async Task<IReadOnlyList<CapturedRead>> ScrollForPageAsync(RecipeRun run, Func<ReadResult, bool> expected)
{
var mark = Env.Capture.Mark();
var seen = mark;
for (var burst = 0; burst < Env.Options.BurstsPerPage; burst++)
{
await Env.Driver.ScrollBurstAsync(run.Ct).ConfigureAwait(false);
run.ThrowIfSignal();
if (Env.Capture.Pending > 0 || Env.Capture.Since(mark).Count > 0)
if (Env.Capture.Pending > 0 || Env.Capture.Mark() != seen)
{
await run.SettleAsync().ConfigureAwait(false);
seen = Env.Capture.Mark();
}
var reads = Env.Capture.Since(mark);
if (reads.Any(r => r.Result.Shape != ReadShape.Recognized || expected(r.Result)))
var page = reads.Where(r => r.Result.Shape == ReadShape.Recognized && expected(r.Result)).ToList();
if (page.Count > 0)
{
return run.Judge(reads, expected, null)
? reads.Where(r => r.Result.Shape == ReadShape.Recognized && expected(r.Result)).ToList()
: [];
run.Judge(reads, expected, null);
return page;
}
}
run.Judge([], expected, null);
run.Judge(Env.Capture.Since(mark), expected, null);
return [];
}
@@ -382,4 +671,10 @@ public sealed partial class RecipeRunner
private static PageInfo? LastPage(IEnumerable<CapturedRead> reads) =>
reads.Where(r => r.Result.Page is not null).Select(r => r.Result.Page).LastOrDefault();
private static DateTimeOffset? ParseTime(string? value) =>
DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var time) ? time : null;
private static DateTimeOffset? Earliest(DateTimeOffset? a, DateTimeOffset? b) =>
a is null ? b : b is null ? a : a < b ? a : b;
}