aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Integrations
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-05 14:34:12 -0500
committerGitHub <noreply@github.com>2021-07-05 14:34:12 -0500
commitd640eec4f9794daf22ddcdb22aff4cecc637a67a (patch)
tree45ee70a8b70db41b3e89a0310a0e303975fb46e9 /src/slskd/Integrations
parent6e4166314c7cb9ce51f5c6ecff72258be26524b0 (diff)
parentb5e7a1b697c61589d212e9166a3f617ce9a89c62 (diff)
downloadslskd-d640eec4f9794daf22ddcdb22aff4cecc637a67a.tar.gz
Merge pull request #112 from slskd/pushbullet
Add Pushbullet integration for private messages and room mentions
Diffstat (limited to 'src/slskd/Integrations')
-rw-r--r--src/slskd/Integrations/Pushbullet/IPushbulletService.cs36
-rw-r--r--src/slskd/Integrations/Pushbullet/PushbulletService.cs136
2 files changed, 172 insertions, 0 deletions
diff --git a/src/slskd/Integrations/Pushbullet/IPushbulletService.cs b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs
new file mode 100644
index 00000000..6da42326
--- /dev/null
+++ b/src/slskd/Integrations/Pushbullet/IPushbulletService.cs
@@ -0,0 +1,36 @@
+// <copyright file="IPushbulletService.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+namespace slskd.Integrations.Pushbullet
+{
+ using System.Threading.Tasks;
+
+ /// <summary>
+ /// Pushbullet integration service.
+ /// </summary>
+ public interface IPushbulletService
+ {
+ /// <summary>
+ /// Sends a push notification to Pushbullet.
+ /// </summary>
+ /// <param name="title">The notification title.</param>
+ /// <param name="cacheKey">A unique cache key for the notification.</param>
+ /// <param name="body">The notification body.</param>
+ /// <returns>The operation context.</returns>
+ Task PushAsync(string title, string cacheKey, string body);
+ }
+}
diff --git a/src/slskd/Integrations/Pushbullet/PushbulletService.cs b/src/slskd/Integrations/Pushbullet/PushbulletService.cs
new file mode 100644
index 00000000..cb31e6dc
--- /dev/null
+++ b/src/slskd/Integrations/Pushbullet/PushbulletService.cs
@@ -0,0 +1,136 @@
+// <copyright file="PushbulletService.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+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;
+
+ /// <summary>
+ /// Pushbullet integration service.
+ /// </summary>
+ public class PushbulletService : IPushbulletService
+ {
+ private static readonly string PushUri = "https://api.pushbullet.com/v2/pushes";
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="PushbulletService"/> class.
+ /// </summary>
+ /// <param name="httpClientFactory">The HttpClientFactory to use.</param>
+ /// <param name="optionsMonitor">The options monitor used to derive application options.</param>
+ /// <param name="log">The logger.</param>
+ public PushbulletService(
+ IHttpClientFactory httpClientFactory,
+ Microsoft.Extensions.Options.IOptionsMonitor<Options> optionsMonitor,
+ ILogger<PushbulletService> log)
+ {
+ HttpClientFactory = httpClientFactory;
+ Options = optionsMonitor.CurrentValue;
+ Log = log;
+
+ RecentlySent = new MemoryCache(new MemoryCacheOptions());
+ }
+
+ private IHttpClientFactory HttpClientFactory { get; }
+ private Options Options { get; }
+ private ILogger<PushbulletService> Log { get; }
+ private PushbulletOptions PushbulletOptions => Options.Integration.Pushbullet;
+ private IMemoryCache RecentlySent { get; }
+
+ /// <summary>
+ /// Sends a push notification to Pushbullet.
+ /// </summary>
+ /// <param name="title">The notification title.</param>
+ /// <param name="cacheKey">A unique cache key for the notification.</param>
+ /// <param name="body">The notification body.</param>
+ /// <returns>The operation context.</returns>
+ public Task PushAsync(string title, string cacheKey, string body)
+ {
+ 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);
+ }
+
+ 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 + Program.Version);
+
+ 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);
+ }
+ }
+ }
+}