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,117 @@
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Alerts;
|
||||
|
||||
/// <summary>The events that deserve an alert (spec §13.3). The red strip in the interface is always on.</summary>
|
||||
/// <param name="Verification">«Verifica richiesta».</param>
|
||||
/// <param name="Disconnection">«Disconnessione».</param>
|
||||
/// <param name="Throttle">«Fermo per rallentamento e ripresa».</param>
|
||||
/// <param name="BackfillDone">«Storico di un account completato».</param>
|
||||
/// <param name="DiskLow">«Disco quasi pieno».</param>
|
||||
/// <param name="UnknownFormat">«Formato non riconosciuto».</param>
|
||||
public sealed record AlertEvents(bool Verification, bool Disconnection, bool Throttle, bool BackfillDone, bool DiskLow, bool UnknownFormat)
|
||||
{
|
||||
public static AlertEvents All { get; } = new(true, true, true, true, true, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Where and when Febe sends alerts (Impostazioni, Notifiche; spec §13.3): the events, Windows notifications (desktop),
|
||||
/// Telegram with the bot token and the chat identifier. The token is a secret: it is stored in the settings table of
|
||||
/// the Data folder, never logged, never sent back to the interface.
|
||||
/// </summary>
|
||||
public sealed record AlertSettings(AlertEvents Events, bool WindowsNotifications, string? TelegramChatId, string? TelegramToken)
|
||||
{
|
||||
public static AlertSettings Default { get; } = new(AlertEvents.All, true, null, null);
|
||||
|
||||
/// <summary>Both the bot token and the chat identifier are set.</summary>
|
||||
public bool TelegramConfigured => !string.IsNullOrWhiteSpace(TelegramToken) && !string.IsNullOrWhiteSpace(TelegramChatId);
|
||||
|
||||
/// <summary>Never prints the token.</summary>
|
||||
public override string ToString() =>
|
||||
$"AlertSettings {{ Events = {Events}, WindowsNotifications = {WindowsNotifications}, TelegramChatId = {(TelegramChatId is null ? "-" : "impostato")}, TelegramToken = {(TelegramToken is null ? "-" : "***")} }}";
|
||||
}
|
||||
|
||||
/// <summary>Setting keys of the alerts (spec §8.3 <c>setting</c>).</summary>
|
||||
public static class AlertSettingKeys
|
||||
{
|
||||
public const string Verification = "avvisi.verifica";
|
||||
public const string Disconnection = "avvisi.disconnessione";
|
||||
public const string Throttle = "avvisi.rallentamento";
|
||||
public const string BackfillDone = "avvisi.storico-completato";
|
||||
public const string DiskLow = "avvisi.disco-quasi-pieno";
|
||||
public const string UnknownFormat = "avvisi.formato-non-riconosciuto";
|
||||
public const string Windows = "avvisi.windows";
|
||||
public const string TelegramChat = "avvisi.telegram.chat";
|
||||
|
||||
/// <summary>The bot token: a secret (rule 10), never logged nor shown.</summary>
|
||||
public const string TelegramToken = "avvisi.telegram.gettone";
|
||||
}
|
||||
|
||||
/// <summary>Reads and writes <see cref="AlertSettings"/>.</summary>
|
||||
public static class AlertSettingsStore
|
||||
{
|
||||
public static async Task<AlertSettings> LoadAsync(ISettingsStore store, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
var all = await store.GetAllAsync(ct).ConfigureAwait(false);
|
||||
bool Flag(string key) => !all.TryGetValue(key, out var text) || !bool.TryParse(text, out var value) || value;
|
||||
string? Text(string key) => all.TryGetValue(key, out var text) && !string.IsNullOrWhiteSpace(text) ? text : null;
|
||||
return new AlertSettings(
|
||||
new AlertEvents(
|
||||
Flag(AlertSettingKeys.Verification),
|
||||
Flag(AlertSettingKeys.Disconnection),
|
||||
Flag(AlertSettingKeys.Throttle),
|
||||
Flag(AlertSettingKeys.BackfillDone),
|
||||
Flag(AlertSettingKeys.DiskLow),
|
||||
Flag(AlertSettingKeys.UnknownFormat)),
|
||||
Flag(AlertSettingKeys.Windows),
|
||||
Text(AlertSettingKeys.TelegramChat),
|
||||
Text(AlertSettingKeys.TelegramToken));
|
||||
}
|
||||
|
||||
/// <summary>Stores the settings in one transaction (a null token or chat removes it).</summary>
|
||||
public static Task SaveAsync(ISettingsStore store, AlertSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(store);
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
static string B(bool value) => value ? "true" : "false";
|
||||
return store.SetManyAsync(
|
||||
new Dictionary<string, string?>
|
||||
{
|
||||
[AlertSettingKeys.Verification] = B(settings.Events.Verification),
|
||||
[AlertSettingKeys.Disconnection] = B(settings.Events.Disconnection),
|
||||
[AlertSettingKeys.Throttle] = B(settings.Events.Throttle),
|
||||
[AlertSettingKeys.BackfillDone] = B(settings.Events.BackfillDone),
|
||||
[AlertSettingKeys.DiskLow] = B(settings.Events.DiskLow),
|
||||
[AlertSettingKeys.UnknownFormat] = B(settings.Events.UnknownFormat),
|
||||
[AlertSettingKeys.Windows] = B(settings.WindowsNotifications),
|
||||
[AlertSettingKeys.TelegramChat] = string.IsNullOrWhiteSpace(settings.TelegramChatId) ? null : settings.TelegramChatId.Trim(),
|
||||
[AlertSettingKeys.TelegramToken] = string.IsNullOrWhiteSpace(settings.TelegramToken) ? null : settings.TelegramToken.Trim(),
|
||||
},
|
||||
ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Outcome of «Invia una prova».</summary>
|
||||
/// <param name="Sent">The message left.</param>
|
||||
/// <param name="Message">Italian text for the page: what happened and what to do.</param>
|
||||
public sealed record AlertTestResult(bool Sent, string Message);
|
||||
|
||||
/// <summary>
|
||||
/// «Invia una prova» of the Telegram alerts (spec §13.3). The real sender (one HTTPS call with <c>HttpClient</c>, no
|
||||
/// library, the address never logged because it contains the token) arrives with milestone M8 and replaces
|
||||
/// <see cref="TelegramNotAvailableYet"/>.
|
||||
/// </summary>
|
||||
public interface IAlertTestSender
|
||||
{
|
||||
Task<AlertTestResult> SendTelegramTestAsync(AlertSettings settings, CancellationToken ct);
|
||||
}
|
||||
|
||||
/// <summary>Until M8: no message leaves, and the page says so plainly.</summary>
|
||||
public sealed class TelegramNotAvailableYet : IAlertTestSender
|
||||
{
|
||||
public const string Text =
|
||||
"Le impostazioni sono salvate, ma l'invio dei messaggi Telegram non c'è ancora in questa versione di Febe (arriva con la tappa M8): nessun messaggio è partito, nemmeno di prova.";
|
||||
|
||||
public Task<AlertTestResult> SendTelegramTestAsync(AlertSettings settings, CancellationToken ct) => Task.FromResult(new AlertTestResult(false, Text));
|
||||
}
|
||||
Reference in new issue
Block a user