From 698356d55ef5bdc75ae67a3906f144a66b536331 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Wed, 28 Dec 2016 11:18:37 +0000 Subject: [PATCH] Should fix #841 #835 #810 --- Ombi.Core/SettingModels/NewsletterSettings.cs | 15 +- Ombi.Helpers/StringHelper.cs | 32 ++-- Ombi.Services/Jobs/PlexContentCacher.cs | 140 +++++++++++++----- Ombi.Services/Jobs/RecentlyAdded.cs | 5 +- .../Notification/NotificationEngine.cs | 6 +- .../Repository/BaseGenericRepository.cs | 15 ++ Ombi.Store/Repository/IRepository.cs | 3 + 7 files changed, 158 insertions(+), 58 deletions(-) diff --git a/Ombi.Core/SettingModels/NewsletterSettings.cs b/Ombi.Core/SettingModels/NewsletterSettings.cs index 7af719461..8273506ea 100644 --- a/Ombi.Core/SettingModels/NewsletterSettings.cs +++ b/Ombi.Core/SettingModels/NewsletterSettings.cs @@ -36,10 +36,21 @@ namespace Ombi.Core.SettingModels public bool SendRecentlyAddedEmail { get; set; } public bool SendToPlexUsers { get; set; } public string CustomUsers { get; set; } - + [JsonIgnore] - public IEnumerable CustomUsersEmailAddresses => CustomUsers.SplitEmailsByDelimiter(';'); + public IEnumerable CustomUsersEmailAddresses + { + get + { + var retVal = (IEnumerable)new List(); + if (!string.IsNullOrEmpty(CustomUsers)) + { + retVal = CustomUsers.SplitEmailsByDelimiter(';'); + } + return retVal; + } + } } } \ No newline at end of file diff --git a/Ombi.Helpers/StringHelper.cs b/Ombi.Helpers/StringHelper.cs index 3dcd9e5eb..6b94e9606 100644 --- a/Ombi.Helpers/StringHelper.cs +++ b/Ombi.Helpers/StringHelper.cs @@ -25,6 +25,7 @@ // ************************************************************************/ #endregion +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -78,22 +79,25 @@ namespace Ombi.Helpers while (delimiterIndex >= 0) { - delimiterIndex = input.IndexOf(delimiter, startIndex); - string substring = input; - if (delimiterIndex > 0) + if (input != null) { - substring = input.Substring(0, delimiterIndex).Trim(); - } + delimiterIndex = input.IndexOf(delimiter, startIndex); + var substring = input; + if (delimiterIndex > 0) + { + substring = input.Substring(0, delimiterIndex).Trim(); + } - if (!substring.Contains("\"") || substring.IndexOf("\"") != substring.LastIndexOf("\"")) - { - yield return substring; - input = input.Substring(delimiterIndex + 1).Trim(); - startIndex = 0; - } - else - { - startIndex = delimiterIndex + 1; + if (!substring.Contains("\"") || substring.IndexOf("\"", StringComparison.Ordinal) != substring.LastIndexOf("\"", StringComparison.Ordinal)) + { + yield return substring; + input = input.Substring(delimiterIndex + 1).Trim(); + startIndex = 0; + } + else + { + startIndex = delimiterIndex + 1; + } } } } diff --git a/Ombi.Services/Jobs/PlexContentCacher.cs b/Ombi.Services/Jobs/PlexContentCacher.cs index 0df3dadb7..d21d26a8b 100644 --- a/Ombi.Services/Jobs/PlexContentCacher.cs +++ b/Ombi.Services/Jobs/PlexContentCacher.cs @@ -234,64 +234,124 @@ namespace Ombi.Services.Jobs var movies = GetPlexMovies(results); - // Time to destroy the plex movies from the DB - PlexContent.Custom(connection => - { - connection.Open(); - connection.Query("delete from PlexContent where type = @type", new { type = 0 }); - return new List(); - }); + //// Time to destroy the plex movies from the DB + //PlexContent.Custom(connection => + //{ + // connection.Open(); + // connection.Query("delete from PlexContent where type = @type", new { type = 0 }); + // return new List(); + //}); foreach (var m in movies) { - PlexContent.Insert(new PlexContent + if (string.IsNullOrEmpty(m.ProviderId)) + { + Log.Error("Provider Id on movie {0} is null",m.Title); + continue; + } + + // Check if it exists + var item = PlexContent.Custom(connection => { - ProviderId = m.ProviderId, - ReleaseYear = m.ReleaseYear ?? string.Empty, - Title = m.Title, - Type = Store.Models.Plex.PlexMediaType.Movie, - Url = m.Url + connection.Open(); + var media = connection.QueryFirstOrDefault("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { m.ProviderId, type = 0 }); + connection.Dispose(); + return media; }); + + if (item == null) + { + // Doesn't exist, insert it + PlexContent.Insert(new PlexContent + { + ProviderId = m.ProviderId, + ReleaseYear = m.ReleaseYear ?? string.Empty, + Title = m.Title, + Type = Store.Models.Plex.PlexMediaType.Movie, + Url = m.Url + }); + } } + var tv = GetPlexTvShows(results); - // Time to destroy the plex tv from the DB - PlexContent.Custom(connection => - { - connection.Open(); - connection.Query("delete from PlexContent where type = @type", new { type = 1 }); - return new List(); - }); + //// Time to destroy the plex tv from the DB + //PlexContent.Custom(connection => + //{ + // connection.Open(); + // connection.Query("delete from PlexContent where type = @type", new { type = 1 }); + // return new List(); + //}); foreach (var t in tv) { - PlexContent.Insert(new PlexContent + if (string.IsNullOrEmpty(t.ProviderId)) { - ProviderId = t.ProviderId, - ReleaseYear = t.ReleaseYear ?? string.Empty, - Title = t.Title, - Type = Store.Models.Plex.PlexMediaType.Show, - Url = t.Url, - Seasons = ByteConverterHelper.ReturnBytes(t.Seasons) + Log.Error("Provider Id on tv {0} is null", t.Title); + continue; + } + + + // Check if it exists + var item = PlexContent.Custom(connection => + { + connection.Open(); + var media = connection.QueryFirstOrDefault("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { t.ProviderId, type = 1 }); + connection.Dispose(); + return media; }); + + if (item == null) + { + PlexContent.Insert(new PlexContent + { + ProviderId = t.ProviderId, + ReleaseYear = t.ReleaseYear ?? string.Empty, + Title = t.Title, + Type = Store.Models.Plex.PlexMediaType.Show, + Url = t.Url, + Seasons = ByteConverterHelper.ReturnBytes(t.Seasons) + }); + } } var albums = GetPlexAlbums(results); - // Time to destroy the plex movies from the DB - PlexContent.Custom(connection => - { - connection.Open(); - connection.Query("delete from PlexContent where type = @type", new { type = 2 }); - return new List(); - }); + //// Time to destroy the plex movies from the DB + //PlexContent.Custom(connection => + //{ + // connection.Open(); + // connection.Query("delete from PlexContent where type = @type", new { type = 2 }); + // return new List(); + //}); + foreach (var a in albums) { - PlexContent.Insert(new PlexContent + if (string.IsNullOrEmpty(a.ProviderId)) + { + Log.Error("Provider Id on album {0} is null", a.Title); + continue; + } + + + // Check if it exists + var item = PlexContent.Custom(connection => { - ProviderId = a.ProviderId, - ReleaseYear = a.ReleaseYear ?? string.Empty, - Title = a.Title, - Type = Store.Models.Plex.PlexMediaType.Artist, - Url = a.Url + connection.Open(); + var media = connection.QueryFirstOrDefault("select * from PlexContent where ProviderId = @ProviderId and type = @type", new { a.ProviderId, type = 2 }); + connection.Dispose(); + return media; }); + + if (item == null) + { + + PlexContent.Insert(new PlexContent + { + ProviderId = a.ProviderId, + ReleaseYear = a.ReleaseYear ?? string.Empty, + Title = a.Title, + Type = Store.Models.Plex.PlexMediaType.Artist, + Url = a.Url + }); + } } } } diff --git a/Ombi.Services/Jobs/RecentlyAdded.cs b/Ombi.Services/Jobs/RecentlyAdded.cs index 676f4a09b..7e7293578 100644 --- a/Ombi.Services/Jobs/RecentlyAdded.cs +++ b/Ombi.Services/Jobs/RecentlyAdded.cs @@ -472,7 +472,10 @@ namespace Ombi.Services.Jobs { foreach (var user in newletterSettings.CustomUsersEmailAddresses) { - message.Bcc.Add(new MailboxAddress(user, user)); + if (!string.IsNullOrEmpty(user)) + { + message.Bcc.Add(new MailboxAddress(user, user)); + } } } } diff --git a/Ombi.Services/Notification/NotificationEngine.cs b/Ombi.Services/Notification/NotificationEngine.cs index d57d64ab5..44caf75b1 100644 --- a/Ombi.Services/Notification/NotificationEngine.cs +++ b/Ombi.Services/Notification/NotificationEngine.cs @@ -81,7 +81,11 @@ namespace Ombi.Services.Notification continue; } - selectedUsers.Add(requestUser); + // Make sure we do not already have the user + if (!selectedUsers.Contains(requestUser)) + { + selectedUsers.Add(requestUser); + } } //var selectedUsers = users.Select(x => x.Username).Intersect(model.RequestedUsers, StringComparer.CurrentCultureIgnoreCase); diff --git a/Ombi.Store/Repository/BaseGenericRepository.cs b/Ombi.Store/Repository/BaseGenericRepository.cs index c0278972b..48469a8d6 100644 --- a/Ombi.Store/Repository/BaseGenericRepository.cs +++ b/Ombi.Store/Repository/BaseGenericRepository.cs @@ -65,6 +65,14 @@ namespace Ombi.Store.Repository } } + public T Custom(Func func) + { + using (var cnn = Connection) + { + return func(cnn); + } + } + public async Task> CustomAsync(Func>> func) { using (var cnn = Connection) @@ -72,6 +80,13 @@ namespace Ombi.Store.Repository return await func(cnn); } } + public async Task CustomAsync(Func> func) + { + using (var cnn = Connection) + { + return await func(cnn); + } + } public long Insert(T entity) { diff --git a/Ombi.Store/Repository/IRepository.cs b/Ombi.Store/Repository/IRepository.cs index 0a989fe9b..2901e73c9 100644 --- a/Ombi.Store/Repository/IRepository.cs +++ b/Ombi.Store/Repository/IRepository.cs @@ -87,5 +87,8 @@ namespace Ombi.Store.Repository Task> CustomAsync(Func>> func); void DeleteAll(string tableName); Task DeleteAllAsync(string tableName); + + T Custom(Func func); + Task CustomAsync(Func> func); } }