Interfaccia, parte 2: Attività, Impostazioni, collegamento, Importa, doppioni, primo avvio ed errori di configurazione
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
90 files changed
+7825
-49
No files matched your search
@@ -0,0 +1,106 @@
|
||||
using System.Globalization;
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Engine;
|
||||
using Febe.Core.Storage;
|
||||
using Febe.Web.Engine;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Febe.Web.Api;
|
||||
|
||||
/// <summary>
|
||||
/// What the Attività page needs beside the engine snapshot (spec §7.6, §7.9, mockups 06 and 11): the history of the
|
||||
/// stops, the still frame of the browser, the probable duplicates waiting for Alberto.
|
||||
/// </summary>
|
||||
public static class ActivityApi
|
||||
{
|
||||
/// <summary>Days of stops shown by the page.</summary>
|
||||
public const int HistoryDays = 14;
|
||||
|
||||
public static void Map(IEndpointRouteBuilder endpoints)
|
||||
{
|
||||
endpoints.MapGet("/api/engine/incidents", async (IIncidentHistory history, IClock clock, CancellationToken ct) =>
|
||||
ApiHttp.Json(await IncidentsAsync(history, clock, ct).ConfigureAwait(false), FebeJsonContext.Default.IncidentListDto));
|
||||
|
||||
endpoints.MapGet("/api/browser/preview", async (IBrowserPreview preview, CancellationToken ct) =>
|
||||
{
|
||||
var frame = await preview.GetLatestAsync(ct).ConfigureAwait(false);
|
||||
var dto = frame is null
|
||||
? new BrowserPreviewDto(false, null, null, null)
|
||||
: new BrowserPreviewDto(true, WithoutQuery(frame.Url), frame.At.ToUnixTimeMilliseconds(), "/api/browser/preview/frame?v=" + frame.At.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture));
|
||||
return ApiHttp.Json(dto, FebeJsonContext.Default.BrowserPreviewDto);
|
||||
});
|
||||
|
||||
endpoints.MapGet("/api/browser/preview/frame", async (HttpContext context, IBrowserPreview preview, CancellationToken ct) =>
|
||||
{
|
||||
var frame = await preview.GetLatestAsync(ct).ConfigureAwait(false);
|
||||
if (frame is null)
|
||||
{
|
||||
throw new NotFoundException("Nessuna immagine del browser in questo momento.");
|
||||
}
|
||||
|
||||
context.Response.Headers.CacheControl = "no-store";
|
||||
return Results.Bytes(frame.Jpeg, "image/jpeg");
|
||||
});
|
||||
|
||||
endpoints.MapGet("/api/duplicates/count", async (IFebeStorage storage, CancellationToken ct) =>
|
||||
{
|
||||
var download = await storage.Duplicates.CountPendingAsync(DuplicateOrigin.Download, ct).ConfigureAwait(false);
|
||||
var import = await storage.Duplicates.CountPendingAsync(DuplicateOrigin.Import, ct).ConfigureAwait(false);
|
||||
return ApiHttp.Json(new DuplicateCountsDto(download, import), FebeJsonContext.Default.DuplicateCountsDto);
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<IncidentListDto> IncidentsAsync(IIncidentHistory history, IClock clock, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(history);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
var now = clock.Now;
|
||||
var zone = clock.LocalZone;
|
||||
var items = await history.ListAsync(now - TimeSpan.FromDays(HistoryDays), now + TimeSpan.FromMinutes(1), ct).ConfigureAwait(false);
|
||||
var dtos = items.Select(i => new IncidentDto(
|
||||
i.Id,
|
||||
ApiMapping.Local(i.At, now, zone),
|
||||
Level(i.Level),
|
||||
Title(i.Level),
|
||||
i.Signal,
|
||||
WithoutQuery(i.Url),
|
||||
i.StoppedUntil is { } until ? ApiMapping.Local(until, now, zone) : null,
|
||||
i.ResolvedAt is { } resolved ? ApiMapping.Local(resolved, now, zone) : null,
|
||||
i.IsOpen,
|
||||
i.Note)).ToList();
|
||||
var throttles = items.Count(i => i.Level == SignalLevel.Throttle && i.At >= now - TimeSpan.FromDays(7));
|
||||
var advice = throttles >= 3
|
||||
? $"{ItalianText.Count(throttles, "rallentamento", "rallentamenti")} negli ultimi sette giorni: conviene passare a un profilo più prudente o seguire meno account (Impostazioni, Limiti e comportamento)."
|
||||
: null;
|
||||
return new IncidentListDto(dtos, throttles, advice);
|
||||
}
|
||||
|
||||
public static string Level(SignalLevel level) => level switch
|
||||
{
|
||||
SignalLevel.Throttle => "throttle",
|
||||
SignalLevel.Challenge => "challenge",
|
||||
_ => "disconnect",
|
||||
};
|
||||
|
||||
public static string Title(SignalLevel level) => level switch
|
||||
{
|
||||
SignalLevel.Throttle => "Rallentamento",
|
||||
SignalLevel.Challenge => "Verifica richiesta",
|
||||
_ => "Disconnessione",
|
||||
};
|
||||
|
||||
/// <summary>An address without its query string and fragment (signed addresses never reach the page).</summary>
|
||||
public static string? WithoutQuery(string? url)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var cut = url.IndexOfAny(['?', '#']);
|
||||
return cut < 0 ? url : url[..cut];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user