diff --git a/src/Heimdall.Application/Services/INtfyService.cs b/src/Heimdall.Application/Services/INtfyService.cs index f1b0397..4b71e74 100644 --- a/src/Heimdall.Application/Services/INtfyService.cs +++ b/src/Heimdall.Application/Services/INtfyService.cs @@ -2,8 +2,8 @@ namespace Heimdall.Application.Services; public interface INtfyService { - Task SendNotificationAsync(string topic, string title, string message, int priority = 3, CancellationToken ct = default); + Task SendNotificationAsync(string topic, string title, string message, int priority = 3, string? baseUrl = null, CancellationToken ct = default); /// Envoie une notification de test et indique si le serveur a accepté (retour HTTP 2xx). - Task SendTestAsync(string topic, string title, string message, CancellationToken ct = default); + Task SendTestAsync(string topic, string title, string message, string? baseUrl = null, CancellationToken ct = default); } diff --git a/src/Heimdall.Infrastructure/Jobs/SendNotificationJob.cs b/src/Heimdall.Infrastructure/Jobs/SendNotificationJob.cs index b1c8883..1302eb9 100644 --- a/src/Heimdall.Infrastructure/Jobs/SendNotificationJob.cs +++ b/src/Heimdall.Infrastructure/Jobs/SendNotificationJob.cs @@ -29,9 +29,9 @@ public class SendNotificationJob await _emailService.SendEmailAsync(recipient, subject, bodyHtml, ct).ConfigureAwait(false); } - public async Task SendNtfyAsync(string topic, string title, string message, int priority = 3, CancellationToken ct = default) + public async Task SendNtfyAsync(string topic, string title, string message, int priority = 3, string? baseUrl = null, CancellationToken ct = default) { _logger.LogInformation("Sending ntfy notification: {Title}", title); - await _ntfyService.SendNotificationAsync(topic, title, message, priority, ct).ConfigureAwait(false); + await _ntfyService.SendNotificationAsync(topic, title, message, priority, baseUrl, ct).ConfigureAwait(false); } } diff --git a/src/Heimdall.Infrastructure/Services/NotificationService.cs b/src/Heimdall.Infrastructure/Services/NotificationService.cs index 0cd804e..4bcbc77 100644 --- a/src/Heimdall.Infrastructure/Services/NotificationService.cs +++ b/src/Heimdall.Infrastructure/Services/NotificationService.cs @@ -116,7 +116,8 @@ public class NotificationService : INotificationService topic, notification.Title, notification.Message, - ct: ct); + ct: ct, + baseUrl: config.NtfyUrl); } } catch (Exception ex) diff --git a/src/Heimdall.Infrastructure/Services/NtfyService.cs b/src/Heimdall.Infrastructure/Services/NtfyService.cs index 11dac99..e92debc 100644 --- a/src/Heimdall.Infrastructure/Services/NtfyService.cs +++ b/src/Heimdall.Infrastructure/Services/NtfyService.cs @@ -26,11 +26,11 @@ public class NtfyService : INtfyService _logger = logger; } - public async Task SendNotificationAsync(string topic, string title, string message, int priority = 3, CancellationToken ct = default) + public async Task SendNotificationAsync(string topic, string title, string message, int priority = 3, string? baseUrl = null, CancellationToken ct = default) { try { - await SendCoreAsync(topic, title, message, priority, ct).ConfigureAwait(false); + await SendCoreAsync(topic, title, message, priority, baseUrl, ct).ConfigureAwait(false); } catch (Exception ex) { @@ -38,11 +38,11 @@ public class NtfyService : INtfyService } } - public async Task SendTestAsync(string topic, string title, string message, CancellationToken ct = default) + public async Task SendTestAsync(string topic, string title, string message, string? baseUrl = null, CancellationToken ct = default) { try { - var response = await SendCoreAsync(topic, title, message, 4, ct).ConfigureAwait(false); + var response = await SendCoreAsync(topic, title, message, 4, baseUrl, ct).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { var body = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); @@ -57,9 +57,11 @@ public class NtfyService : INtfyService } } - private async Task SendCoreAsync(string topic, string title, string message, int priority, CancellationToken ct) + private async Task SendCoreAsync(string topic, string title, string message, int priority, string? overrideBaseUrl, CancellationToken ct) { var (baseUrl, authToken) = await ResolveConfigAsync(ct).ConfigureAwait(false); + if (!string.IsNullOrEmpty(overrideBaseUrl)) + baseUrl = overrideBaseUrl; var client = _httpClientFactory.CreateClient("Ntfy"); var requestUrl = $"{baseUrl.TrimEnd('/')}/{topic}"; @@ -84,7 +86,7 @@ public class NtfyService : INtfyService using var scope = _services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); - var baseUrl = await AppSettingReader.GetAsync(db, "ntfy.baseUrl", ct).ConfigureAwait(false) + var baseUrl = await AppSettingReader.GetAsync(db, "ntfy.baseurl", ct).ConfigureAwait(false) ?? _configuration.GetValue("Ntfy:BaseUrl") ?? Environment.GetEnvironmentVariable("NTFY__BASE_URL") ?? "http://ntfy:80"; diff --git a/src/Heimdall.Web/Components/Pages/Profile.razor b/src/Heimdall.Web/Components/Pages/Profile.razor index 82556b7..7acf69f 100644 --- a/src/Heimdall.Web/Components/Pages/Profile.razor +++ b/src/Heimdall.Web/Components/Pages/Profile.razor @@ -178,10 +178,15 @@ else if (_profile is not null) } - + Les notifications push utilisent ntfy (serveur auto-hébergé). - Votre topic personnel est heimdall_@_profile.Id. - Installez l'app ntfy et abonnez-vous à ce topic pour recevoir les notifications. + Installez l'app ntfy et abonnez-vous au topic ci-dessous pour recevoir les notifications. + + + Votre topic : @($"heimdall_{_profile?.Id}") + + + Ce topic est unique par utilisateur. Vous pouvez aussi utiliser un topic de site si configuré. @@ -207,6 +212,19 @@ else if (_profile is not null) @if (_savingNotif) { } Enregistrer + +
+ + @(_testingNtfy ? "Test en cours…" : "Tester ntfy") + +
+ @if (_ntfyTestResult is not null) + { + + @_ntfyTestResult.Message + + } } @@ -470,6 +488,8 @@ else if (_profile is not null) private bool _weeklyDigest; private bool _savingNotif; private string _notifError = ""; + private bool _testingNtfy; + private TestActionResult? _ntfyTestResult; private async Task LoadNotificationConfig() { @@ -522,4 +542,24 @@ else if (_profile is not null) } finally { _savingNotif = false; } } + + private async Task TestNtfyForUserAsync() + { + _testingNtfy = true; + _ntfyTestResult = null; + try + { + var topic = _profile is not null ? $"heimdall_{_profile.Id}" : null; + _ntfyTestResult = await Api.TestNtfyAsync(topic) + ?? new TestActionResult(false, "Test impossible (API indisponible)."); + } + catch (Exception ex) + { + _ntfyTestResult = new TestActionResult(false, $"Test impossible : {ex.Message}"); + } + finally + { + _testingNtfy = false; + } + } } diff --git a/src/Heimdall.Web/Controllers/SettingsController.cs b/src/Heimdall.Web/Controllers/SettingsController.cs index 0e42025..f2d1a05 100644 --- a/src/Heimdall.Web/Controllers/SettingsController.cs +++ b/src/Heimdall.Web/Controllers/SettingsController.cs @@ -293,7 +293,7 @@ public class SettingsController : ControllerBase { var topic = string.IsNullOrWhiteSpace(request?.Topic) ? "heimdall-test" : request.Topic; var ok = await ntfyService.SendTestAsync(topic, "Heimdall — test de configuration", - "✅ Notification ntfy reçue : le serveur est joignable et le token est accepté.", ct); + "✅ Notification ntfy reçue : le serveur est joignable et le token est accepté.", ct: ct); return Ok(new { success = ok, diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs index c225664..273ada1 100644 --- a/src/Heimdall.Web/Services/HeimdallApiClient.cs +++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs @@ -618,10 +618,11 @@ public class HeimdallApiClient } /// Send a test ntfy notification (POST /api/settings/ntfy/test). - public async Task TestNtfyAsync() + public async Task TestNtfyAsync(string? topic = null) { var client = CreateClient(); - var response = await client.PostAsync("api/settings/ntfy/test", null); + var payload = new { topic }; + var response = await client.PostAsJsonAsync("api/settings/ntfy/test", payload, Json); if (!response.IsSuccessStatusCode) return null; return await response.Content.ReadFromJsonAsync();