From 860bd04c5986013862ffea0cf10d31d72069208a Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 7 May 2024 17:45:28 -0700 Subject: [PATCH] New: Add artist tags to Webhook and Notifiarr events (cherry picked from commit cc0a284660f139d5f47b27a2c389973e5e888587) Closes #4805 --- .../CustomScript/CustomScript.cs | 28 ++++++++++++++----- .../Notifications/Notifiarr/Notifiarr.cs | 5 ++-- .../Notifications/Webhook/Webhook.cs | 5 ++-- .../Notifications/Webhook/WebhookArtist.cs | 4 ++- .../Notifications/Webhook/WebhookBase.cs | 25 +++++++++++++++-- src/NzbDrone.Core/Tags/TagRepository.cs | 7 +++++ 6 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs index 824f6b9fc..9610ef324 100644 --- a/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs +++ b/src/NzbDrone.Core/Notifications/CustomScript/CustomScript.cs @@ -63,7 +63,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_Release_AlbumCount", remoteAlbum.Albums.Count.ToString()); environmentVariables.Add("Lidarr_Release_AlbumReleaseDates", string.Join(",", remoteAlbum.Albums.Select(e => e.ReleaseDate))); environmentVariables.Add("Lidarr_Release_AlbumTitles", string.Join("|", remoteAlbum.Albums.Select(e => e.Title))); @@ -101,7 +101,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_Album_Id", album.Id.ToString()); environmentVariables.Add("Lidarr_Album_Title", album.Title); environmentVariables.Add("Lidarr_Album_Overview", album.Overview); @@ -139,7 +139,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_TrackFile_Ids", string.Join(",", renamedFiles.Select(e => e.TrackFile.Id))); environmentVariables.Add("Lidarr_TrackFile_Paths", string.Join("|", renamedFiles.Select(e => e.TrackFile.Path))); environmentVariables.Add("Lidarr_TrackFile_PreviousPaths", string.Join("|", renamedFiles.Select(e => e.PreviousPath))); @@ -164,7 +164,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_Album_Id", album.Id.ToString()); environmentVariables.Add("Lidarr_Album_Title", album.Title); environmentVariables.Add("Lidarr_Album_Overview", album.Overview); @@ -201,7 +201,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId.ToString()); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); ExecuteScript(environmentVariables); } @@ -220,7 +220,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId.ToString()); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_Artist_DeletedFiles", deleteMessage.DeletedFiles.ToString()); ExecuteScript(environmentVariables); @@ -241,7 +241,7 @@ namespace NzbDrone.Core.Notifications.CustomScript environmentVariables.Add("Lidarr_Artist_MBId", artist.Metadata.Value.ForeignArtistId); environmentVariables.Add("Lidarr_Artist_Type", artist.Metadata.Value.Type); environmentVariables.Add("Lidarr_Artist_Genres", string.Join("|", artist.Metadata.Value.Genres)); - environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", artist.Tags.Select(t => _tagRepository.Get(t).Label))); + environmentVariables.Add("Lidarr_Artist_Tags", string.Join("|", GetTagLabels(artist))); environmentVariables.Add("Lidarr_Album_Id", album.Id.ToString()); environmentVariables.Add("Lidarr_Album_Title", album.Title); environmentVariables.Add("Lidarr_Album_Overview", album.Overview); @@ -342,5 +342,19 @@ namespace NzbDrone.Core.Notifications.CustomScript return processOutput; } + + private List GetTagLabels(Artist artist) + { + if (artist == null) + { + return null; + } + + return _tagRepository.GetTags(artist.Tags) + .Select(s => s.Label) + .Where(l => l.IsNotNullOrWhiteSpace()) + .OrderBy(l => l) + .ToList(); + } } } diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs index 25ea84301..dccf38f6e 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs @@ -6,6 +6,7 @@ using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Music; using NzbDrone.Core.Notifications.Webhook; +using NzbDrone.Core.Tags; using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Notifiarr @@ -14,8 +15,8 @@ namespace NzbDrone.Core.Notifications.Notifiarr { private readonly INotifiarrProxy _proxy; - public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, IMapCoversToLocal mediaCoverService) - : base(configFileProvider, configService, mediaCoverService) + public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ITagRepository tagRepository, IMapCoversToLocal mediaCoverService) + : base(configFileProvider, configService, tagRepository, mediaCoverService) { _proxy = proxy; } diff --git a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs index 0b78dcbec..489d11d23 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs @@ -5,6 +5,7 @@ using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Music; +using NzbDrone.Core.Tags; using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Webhook @@ -13,8 +14,8 @@ namespace NzbDrone.Core.Notifications.Webhook { private readonly IWebhookProxy _proxy; - public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, IMapCoversToLocal mediaCoverService) - : base(configFileProvider, configService, mediaCoverService) + public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider, IConfigService configService, ITagRepository tagRepository, IMapCoversToLocal mediaCoverService) + : base(configFileProvider, configService, tagRepository, mediaCoverService) { _proxy = proxy; } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookArtist.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookArtist.cs index e758df600..19ba28319 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookArtist.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookArtist.cs @@ -15,12 +15,13 @@ namespace NzbDrone.Core.Notifications.Webhook public string Overview { get; set; } public List Genres { get; set; } public List Images { get; set; } + public List Tags { get; set; } public WebhookArtist() { } - public WebhookArtist(Artist artist) + public WebhookArtist(Artist artist, List tags) { Id = artist.Id; Name = artist.Name; @@ -31,6 +32,7 @@ namespace NzbDrone.Core.Notifications.Webhook Overview = artist.Metadata.Value.Overview; Genres = artist.Metadata.Value.Genres; Images = artist.Metadata.Value.Images.Select(i => new WebhookImage(i)).ToList(); + Tags = tags; } } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs index 6d1013194..2807912f2 100644 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; using System.Linq; +using NzbDrone.Common.Extensions; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Music; +using NzbDrone.Core.Tags; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Webhook @@ -13,12 +15,14 @@ namespace NzbDrone.Core.Notifications.Webhook { private readonly IConfigFileProvider _configFileProvider; private readonly IConfigService _configService; + private readonly ITagRepository _tagRepository; private readonly IMapCoversToLocal _mediaCoverService; - protected WebhookBase(IConfigFileProvider configFileProvider, IConfigService configService, IMapCoversToLocal mediaCoverService) + protected WebhookBase(IConfigFileProvider configFileProvider, IConfigService configService, ITagRepository tagRepository, IMapCoversToLocal mediaCoverService) { _configFileProvider = configFileProvider; _configService = configService; + _tagRepository = tagRepository; _mediaCoverService = mediaCoverService; } @@ -221,7 +225,8 @@ namespace NzbDrone.Core.Notifications.Webhook Id = 1, Name = "Test Name", Path = "C:\\testpath", - MBId = "aaaaa-aaa-aaaa-aaaaaa" + MBId = "aaaaa-aaa-aaaa-aaaaaa", + Tags = new List { "test-tag" } }, Albums = new List { @@ -243,7 +248,7 @@ namespace NzbDrone.Core.Notifications.Webhook _mediaCoverService.ConvertToLocalUrls(artist.Id, MediaCoverEntity.Artist, artist.Metadata.Value.Images); - return new WebhookArtist(artist); + return new WebhookArtist(artist, GetTagLabels(artist)); } private WebhookAlbum GetAlbum(Album album) @@ -257,5 +262,19 @@ namespace NzbDrone.Core.Notifications.Webhook return new WebhookAlbum(album); } + + private List GetTagLabels(Artist artist) + { + if (artist == null) + { + return null; + } + + return _tagRepository.GetTags(artist.Tags) + .Select(s => s.Label) + .Where(l => l.IsNotNullOrWhiteSpace()) + .OrderBy(l => l) + .ToList(); + } } } diff --git a/src/NzbDrone.Core/Tags/TagRepository.cs b/src/NzbDrone.Core/Tags/TagRepository.cs index 4851ad223..5743dd4a6 100644 --- a/src/NzbDrone.Core/Tags/TagRepository.cs +++ b/src/NzbDrone.Core/Tags/TagRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; @@ -9,6 +10,7 @@ namespace NzbDrone.Core.Tags { Tag GetByLabel(string label); Tag FindByLabel(string label); + List GetTags(HashSet tagIds); } public class TagRepository : BasicRepository, ITagRepository @@ -34,5 +36,10 @@ namespace NzbDrone.Core.Tags { return Query(c => c.Label == label).SingleOrDefault(); } + + public List GetTags(HashSet tagIds) + { + return Query(t => tagIds.Contains(t.Id)); + } } }