1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-23 22:25:56 -04:00

Convert Notifiarr Payload to JSON, Standardize with Webhook

This commit is contained in:
Qstick
2022-11-22 21:17:25 -06:00
committed by Mark McDowall
parent cc46ed56b4
commit f59276881a
4 changed files with 220 additions and 360 deletions
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
@@ -10,18 +8,13 @@ using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Webhook
{
public class Webhook : NotificationBase<WebhookSettings>
public class Webhook : WebhookBase<WebhookSettings>
{
private readonly IConfigFileProvider _configFileProvider;
private readonly IConfigService _configService;
private readonly IWebhookProxy _proxy;
public Webhook(IConfigFileProvider configFileProvider,
IConfigService configService,
IWebhookProxy proxy)
public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService)
: base(configFileProvider, configService)
{
_configFileProvider = configFileProvider;
_configService = configService;
_proxy = proxy;
}
@@ -29,129 +22,37 @@ namespace NzbDrone.Core.Notifications.Webhook
public override void OnGrab(GrabMessage message)
{
var remoteEpisode = message.Episode;
var quality = message.Quality;
var payload = new WebhookGrabPayload
{
EventType = WebhookEventType.Grab,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries(message.Series),
Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)),
Release = new WebhookRelease(quality, remoteEpisode),
DownloadClient = message.DownloadClientName,
DownloadClientType = message.DownloadClientType,
DownloadId = message.DownloadId,
CustomFormatInfo = new WebhookCustomFormatInfo(remoteEpisode.CustomFormats, remoteEpisode.CustomFormatScore)
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildOnGrabPayload(message), Settings);
}
public override void OnDownload(DownloadMessage message)
{
var episodeFile = message.EpisodeFile;
var payload = new WebhookImportPayload
{
EventType = WebhookEventType.Download,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries(message.Series),
Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)),
EpisodeFile = new WebhookEpisodeFile(episodeFile),
IsUpgrade = message.OldFiles.Any(),
DownloadClient = message.DownloadClientInfo?.Name,
DownloadClientType = message.DownloadClientInfo?.Type,
DownloadId = message.DownloadId,
CustomFormatInfo = new WebhookCustomFormatInfo(message.EpisodeInfo.CustomFormats, message.EpisodeInfo.CustomFormatScore)
};
if (message.OldFiles.Any())
{
payload.DeletedFiles = message.OldFiles.ConvertAll(x => new WebhookEpisodeFile(x)
{
Path = Path.Combine(message.Series.Path, x.RelativePath)
});
}
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildOnDownloadPayload(message), Settings);
}
public override void OnRename(Series series, List<RenamedEpisodeFile> renamedFiles)
{
var payload = new WebhookRenamePayload
{
EventType = WebhookEventType.Rename,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries(series),
RenamedEpisodeFiles = renamedFiles.ConvertAll(x => new WebhookRenamedEpisodeFile(x))
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildOnRenamePayload(series, renamedFiles), Settings);
}
public override void OnEpisodeFileDelete(EpisodeDeleteMessage deleteMessage)
{
var payload = new WebhookEpisodeDeletePayload
{
EventType = WebhookEventType.EpisodeFileDelete,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries(deleteMessage.Series),
Episodes = deleteMessage.EpisodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)),
EpisodeFile = deleteMessage.EpisodeFile,
DeleteReason = deleteMessage.Reason
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildOnEpisodeFileDelete(deleteMessage), Settings);
}
public override void OnSeriesDelete(SeriesDeleteMessage deleteMessage)
{
var payload = new WebhookSeriesDeletePayload
{
EventType = WebhookEventType.SeriesDelete,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries(deleteMessage.Series),
DeletedFiles = deleteMessage.DeletedFiles
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildOnSeriesDelete(deleteMessage), Settings);
}
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
{
var payload = new WebhookHealthPayload
{
EventType = WebhookEventType.Health,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Level = healthCheck.Type,
Message = healthCheck.Message,
Type = healthCheck.Source.Name,
WikiUrl = healthCheck.WikiUrl?.ToString()
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildHealthPayload(healthCheck), Settings);
}
public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
{
var payload = new WebhookApplicationUpdatePayload
{
EventType = WebhookEventType.ApplicationUpdate,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Message = updateMessage.Message,
PreviousVersion = updateMessage.PreviousVersion.ToString(),
NewVersion = updateMessage.NewVersion.ToString()
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildApplicationUpdatePayload(updateMessage), Settings);
}
public override string Name => "Webhook";
@@ -169,31 +70,7 @@ namespace NzbDrone.Core.Notifications.Webhook
{
try
{
var payload = new WebhookGrabPayload
{
EventType = WebhookEventType.Test,
InstanceName = _configFileProvider.InstanceName,
ApplicationUrl = _configService.ApplicationUrl,
Series = new WebhookSeries()
{
Id = 1,
Title = "Test Title",
Path = "C:\\testpath",
TvdbId = 1234
},
Episodes = new List<WebhookEpisode>()
{
new WebhookEpisode()
{
Id = 123,
EpisodeNumber = 1,
SeasonNumber = 1,
Title = "Test title"
}
}
};
_proxy.SendWebhook(payload, Settings);
_proxy.SendWebhook(BuildTestPayload(), Settings);
}
catch (WebhookException ex)
{