// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see https://www.gnu.org/licenses/. // using Microsoft.Extensions.Options; namespace slskd.Integrations.Pushbullet { using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text.Json; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using static slskd.Options.IntegrationOptions; /// /// Pushbullet integration service. /// public class PushbulletService : IPushbulletService { private static readonly string PushUri = "https://api.pushbullet.com/v2/pushes"; /// /// Initializes a new instance of the class. /// /// The HttpClientFactory to use. /// The options monitor used to derive application options. /// The logger. public PushbulletService( IHttpClientFactory httpClientFactory, IOptionsMonitor optionsMonitor, ILogger log) { HttpClientFactory = httpClientFactory; OptionsMonitor = optionsMonitor; Log = log; RecentlySent = new MemoryCache(new MemoryCacheOptions()); } private bool Disposed { get; set; } private IHttpClientFactory HttpClientFactory { get; } private ILogger Log { get; } private IOptionsMonitor OptionsMonitor { get; } private PushbulletOptions PushbulletOptions => OptionsMonitor.CurrentValue.Integration.Pushbullet; private IMemoryCache RecentlySent { get; } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } /// /// Sends a push notification to Pushbullet. /// /// The notification title. /// A unique cache key for the notification. /// The notification body. /// The operation context. public Task PushAsync(string title, string cacheKey, string body) { if (!PushbulletOptions.Enabled) { return Task.CompletedTask; } if (string.IsNullOrWhiteSpace(title)) { throw new ArgumentException("A notification title must be supplied", nameof(title)); } if (string.IsNullOrWhiteSpace(cacheKey)) { throw new ArgumentException("A notification cache key must be supplied", nameof(cacheKey)); } if (string.IsNullOrWhiteSpace(body)) { throw new ArgumentException("A notification body must be supplied", nameof(body)); } if (RecentlySent.TryGetValue(cacheKey, out _)) { return Task.CompletedTask; } RecentlySent.Set(cacheKey, value: true, absoluteExpirationRelativeToNow: TimeSpan.FromMilliseconds(PushbulletOptions.CooldownTime)); return PushInternalAsync(title, body); } protected virtual void Dispose(bool disposing) { if (!Disposed) { if (disposing) { RecentlySent.Dispose(); } Disposed = true; } } private async Task PushInternalAsync(string title, string body) { try { title = $"{PushbulletOptions.NotificationPrefix} {title}"; var json = JsonSerializer.Serialize(new { title, body, type = "note", }); var content = new StringContent(json); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); content.Headers.Add("Access-Token", PushbulletOptions.AccessToken); using var http = HttpClientFactory.CreateClient(); http.DefaultRequestHeaders.UserAgent.TryParseAdd($"{Program.AppName} v{Program.FullVersion}"); Log.LogDebug("Sending Pushbullet notification {Title} {Body}", title, body); await Retry.Do( task: () => http.PostAsync(PushUri, content), isRetryable: (attempts, ex) => true, onFailure: (attempts, ex) => Log.LogWarning("Failed attempt {Attempts} to send Pushbullet notification {Title} {Body}: {Message}", attempts, title, body, ex.Message), maxAttempts: PushbulletOptions.RetryAttempts, maxDelayInMilliseconds: 30000); Log.LogInformation("Sent Pushbullet notification {Title} {Body}", title, body); } catch (RetryException ex) { Log.LogError(ex, "Fatal error retrying send of Pushbullet notification {Title} {Body}: {Message}", title, body, ex.Message); } catch (Exception ex) { Log.LogWarning("Failed to send Pushbullet notification {Title} {Body} after {Attempts} attempts: {Message}", title, body, PushbulletOptions.RetryAttempts, ex.Message); } } } }