From af463d0dd79693fa900415c8d0965bce33bc3bd3 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 20:03:26 -0600 Subject: [PATCH 01/12] Added some logging around API calls. --- Ombi.Api/ApiRequest.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 2de72101d..470f591ee 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -84,8 +84,10 @@ namespace Ombi.Api public T ExecuteXml(IRestRequest request, Uri baseUri) where T : class { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); + Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); + Log.Trace("Api Content Response:"); + Log.Trace(response.Content); if (response.ErrorException != null) { @@ -95,12 +97,14 @@ namespace Ombi.Api } var result = DeserializeXml(response.Content); - return result;} + return result; + } public T ExecuteJson(IRestRequest request, Uri baseUri) where T : new() { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); + Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); Log.Trace("Api Content Response:"); Log.Trace(response.Content); if (response.ErrorException != null) From 652650dacc6f682abb4746c1e075e95d1b900ac9 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 21:16:21 -0600 Subject: [PATCH 02/12] Added a missing `await` for an HP AddArtist call. Added some more Trace logging. --- Ombi.Api/ApiRequest.cs | 15 +++------------ Ombi.Core/HeadphonesSender.cs | 8 ++++---- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 470f591ee..32b2f060f 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -58,9 +58,7 @@ namespace Ombi.Api var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); - + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -85,9 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -104,9 +100,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -114,10 +108,7 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - Log.Trace("Deserialzing Object"); var json = JsonConvert.DeserializeObject(response.Content, _settings); - Log.Trace("Finished Deserialzing Object"); - return json; } diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 043ea4e2a..f652eb56e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -90,8 +90,8 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - Log.Info("Artist add result : {0}", artistAdd); + var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } var counter = 0; @@ -116,13 +116,13 @@ namespace Ombi.Core { Thread.Sleep(WaitTime); counter++; - Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus); + Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); if (counter > CounterMax) { Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones."); + Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); return false; } } From e57cdf8c6f833df817fd8f944505d29113e1cbfe Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 21:16:21 -0600 Subject: [PATCH 03/12] Added a missing `await` for an HP AddArtist call. Added some more Trace logging. --- Ombi.Api/ApiRequest.cs | 15 +++------------ Ombi.Core/HeadphonesSender.cs | 8 ++++---- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 470f591ee..32b2f060f 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -58,9 +58,7 @@ namespace Ombi.Api var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); - + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -85,9 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -104,9 +100,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace("Request made to " + client.BaseUrl + " with details of " + request.ToString()); - Log.Trace("Api Content Response:"); - Log.Trace(response.Content); + Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -114,10 +108,7 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - Log.Trace("Deserialzing Object"); var json = JsonConvert.DeserializeObject(response.Content, _settings); - Log.Trace("Finished Deserialzing Object"); - return json; } diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 043ea4e2a..f652eb56e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -90,8 +90,8 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - Log.Info("Artist add result : {0}", artistAdd); + var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } var counter = 0; @@ -116,13 +116,13 @@ namespace Ombi.Core { Thread.Sleep(WaitTime); counter++; - Log.Trace("Artist status {1}. Counter = {0}", counter, artistStatus); + Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); if (counter > CounterMax) { Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("The artist status is still not Active. We have waited long enough, seems to be a big delay in headphones."); + Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); return false; } } From 231a525109f8f01025e6168f9fd0f66dcb72afac Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 22:14:42 -0600 Subject: [PATCH 04/12] Only deserialize response payload in ApiRequest when StatusCode == 200. Will a default return value in other cases cause other issues? --- Ombi.Api/ApiRequest.cs | 25 +++++++++++++++---------- Ombi.Core/HeadphonesSender.cs | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 32b2f060f..65d2455fd 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -56,9 +56,8 @@ namespace Ombi.Api public T Execute(IRestRequest request, Uri baseUri) where T : new() { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -67,15 +66,16 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - return response.Data; + if (response.StatusCode != HttpStatusCode.OK) + return default(T); + else + return response.Data; } public IRestResponse Execute(IRestRequest request, Uri baseUri) { var client = new RestClient { BaseUrl = baseUri }; - var response = client.Execute(request); - return response; } @@ -83,7 +83,7 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); if (response.ErrorException != null) { @@ -92,7 +92,9 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - var result = DeserializeXml(response.Content); + T result = default(T); + if (response.StatusCode == HttpStatusCode.OK) + result = DeserializeXml(response.Content); return result; } @@ -100,7 +102,8 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with details of {request.ToString()}. The response was {response.Content}"); + Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -108,8 +111,10 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - var json = JsonConvert.DeserializeObject(response.Content, _settings); - return json; + T result = default(T); + if (response.StatusCode == HttpStatusCode.OK) + result = JsonConvert.DeserializeObject(response.Content, _settings); + return result; } private T DeserializeXml(string input) diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index f652eb56e..7fb971c9e 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -79,7 +79,7 @@ namespace Ombi.Core request.Approved = true; // Update the record - var updated = RequestService.UpdateRequest(request); + bool updated = RequestService.UpdateRequest(request); return updated; } @@ -90,7 +90,7 @@ namespace Ombi.Core var artistExists = index.Any(x => x.ArtistID == request.ArtistId); if (!artistExists) { - var artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + bool artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } From ec973917b4f12fa1da4341e9a6f09eb1947fed78 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 23:30:03 -0600 Subject: [PATCH 05/12] Tweaked ApiRequest behavior on non-200 responses; think it was breaking login. :-" --- Ombi.Api/ApiRequest.cs | 20 +++++++++++--------- Ombi.UI/Modules/UserLoginModule.cs | 2 -- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 65d2455fd..9566d2be0 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -66,10 +66,11 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode != HttpStatusCode.OK) - return default(T); - else + if (response.StatusCode == HttpStatusCode.OK) return response.Data; + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + } public IRestResponse Execute(IRestRequest request, Uri baseUri) @@ -92,10 +93,11 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - T result = default(T); if (response.StatusCode == HttpStatusCode.OK) - result = DeserializeXml(response.Content); - return result; + return DeserializeXml(response.Content); + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + } public T ExecuteJson(IRestRequest request, Uri baseUri) where T : new() @@ -111,10 +113,10 @@ namespace Ombi.Api throw new ApiRequestException(message, response.ErrorException); } - T result = default(T); if (response.StatusCode == HttpStatusCode.OK) - result = JsonConvert.DeserializeObject(response.Content, _settings); - return result; + return JsonConvert.DeserializeObject(response.Content, _settings); + else + throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } private T DeserializeXml(string input) diff --git a/Ombi.UI/Modules/UserLoginModule.cs b/Ombi.UI/Modules/UserLoginModule.cs index 1fd82cb2e..1f9250ad9 100644 --- a/Ombi.UI/Modules/UserLoginModule.cs +++ b/Ombi.UI/Modules/UserLoginModule.cs @@ -373,7 +373,6 @@ namespace Ombi.UI.Modules return Response.AsJson(new { result = false, message = Resources.UI.UserLogin_IncorrectUserPass }); } - var m = await AuthenticationSetup(userId, username, dateTimeOffset, loginGuid, isOwner, plexSettings.Enable, embySettings.Enable); var landingSettings = await LandingPageSettings.GetSettingsAsync(); @@ -397,7 +396,6 @@ namespace Ombi.UI.Modules return CustomModuleExtensions.LoginAndRedirect(this, m.LoginGuid, null, retVal.ToString()); } return Response.AsJson(new { result = true, url = retVal.ToString() }); - } private async Task LoginUser() From 3ee4ae34d572b20a9a926c5a8b6d6ba86115a565 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Mon, 27 Feb 2017 23:46:06 -0600 Subject: [PATCH 06/12] Reverted (for now) non-200 response handling; added some extra logging. --- Ombi.Api/ApiRequest.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 9566d2be0..3abe469f2 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -63,13 +63,14 @@ namespace Ombi.Api { var message = "Error retrieving response. Check inner details for more info."; Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return response.Data; - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } @@ -89,14 +90,15 @@ namespace Ombi.Api if (response.ErrorException != null) { Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return DeserializeXml(response.Content); - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } @@ -109,14 +111,15 @@ namespace Ombi.Api if (response.ErrorException != null) { Log.Error(response.ErrorException); + Log.Error(response.ErrorException.InnerException); var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } - if (response.StatusCode == HttpStatusCode.OK) + //if (response.StatusCode == HttpStatusCode.OK) return JsonConvert.DeserializeObject(response.Content, _settings); - else - throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + //else + // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); } private T DeserializeXml(string input) From b5fc7692d647e25f450f4e8daddd04064198c0cc Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:48:00 -0600 Subject: [PATCH 07/12] Restore checking of HTTP StatusCode on ApiRequests; remove checking of response.ErrorException --- Ombi.Api/ApiRequest.cs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index 3abe469f2..bb4dabb0a 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -57,8 +57,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { var message = "Error retrieving response. Check inner details for more info."; @@ -66,11 +67,12 @@ namespace Ombi.Api Log.Error(response.ErrorException.InnerException); throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return response.Data; - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } @@ -85,8 +87,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -94,11 +97,12 @@ namespace Ombi.Api var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return DeserializeXml(response.Content); - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } @@ -106,8 +110,9 @@ namespace Ombi.Api { var client = new RestClient { BaseUrl = baseUri }; var response = client.Execute(request); - Log.Trace($"Request made to {client.BaseUrl} with status code {response.StatusCode}. The response was {response.Content}"); + Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); + /** if (response.ErrorException != null) { Log.Error(response.ErrorException); @@ -115,11 +120,12 @@ namespace Ombi.Api var message = "Error retrieving response. Check inner details for more info."; throw new ApiRequestException(message, response.ErrorException); } + **/ - //if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK) return JsonConvert.DeserializeObject(response.Content, _settings); - //else - // throw new Exception($"Got StatusCode={response.StatusCode} for {baseUri}."); + else + throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); } private T DeserializeXml(string input) From f0358c1f5e376e140791f9c3da682b1fb623a3e0 Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:51:28 -0600 Subject: [PATCH 08/12] Remove all the polling/retry logic around HP requests. This was a problem do to not properly awaiting the initial AddArtist API call being sent to HP. Also fix SetAlbumStatus to use ReleaseId instead of MusicBrainsId (same fix previously applied to AddArtist). --- Ombi.Core/HeadphonesSender.cs | 77 +++-------------------------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/Ombi.Core/HeadphonesSender.cs b/Ombi.Core/HeadphonesSender.cs index 7fb971c9e..c96fefa06 100644 --- a/Ombi.Core/HeadphonesSender.cs +++ b/Ombi.Core/HeadphonesSender.cs @@ -88,87 +88,20 @@ namespace Ombi.Core { var index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); var artistExists = index.Any(x => x.ArtistID == request.ArtistId); + bool artistAdd = false; if (!artistExists) { - bool artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); + artistAdd = await Api.AddArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); Log.Info("Artist add result for {1}: {0}", artistAdd, request.ArtistName); } - var counter = 0; - while (index.All(x => x.ArtistID != request.ArtistId)) - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Artist is still not present in the index. Counter = {0}", counter); - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - - if (counter > CounterMax) - { - Log.Trace("Artist is still not present in the index. Counter = {0}. Returning false", counter); - Log.Warn("We have tried adding the artist but it seems they are still not in headphones."); - return false; - } - } - - counter = 0; - var artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); - while (artistStatus != "Active") - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Artist {2} status {1}. Counter = {0}", counter, artistStatus, request.ArtistName); - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - artistStatus = index.Where(x => x.ArtistID == request.ArtistId).Select(x => x.Status).FirstOrDefault(); - if (counter > CounterMax) - { - Log.Trace("Artist status is still not active. Counter = {0}. Returning false", counter); - Log.Warn($"The artist status for {request.ArtistName} is still not Active. We have waited long enough, seems to be a big delay in headphones."); - return false; - } - } - - var addedArtist = index.FirstOrDefault(x => x.ArtistID == request.ArtistId); - var artistName = addedArtist?.ArtistName ?? string.Empty; - counter = 0; - while (artistName.Contains("Fetch failed")) - { - Thread.Sleep(WaitTime); - await Api.RefreshArtist(Settings.ApiKey, Settings.FullUri, request.ArtistId); - - index = await Api.GetIndex(Settings.ApiKey, Settings.FullUri); - - artistName = index?.FirstOrDefault(x => x.ArtistID == request.ArtistId)?.ArtistName ?? string.Empty; - counter++; - if (counter > CounterMax) - { - Log.Trace("Artist fetch has failed. Counter = {0}. Returning false", counter); - Log.Warn("Artist in headphones fetch has failed, we have tried refreshing the artist but no luck."); - return false; - } - } - - return true; + return artistExists || artistAdd; } private async Task SetAlbumStatus(RequestedModel request) { - var counter = 0; - var setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.MusicBrainzId); - - while (!setStatus) - { - Thread.Sleep(WaitTime); - counter++; - Log.Trace("Setting Album status. Counter = {0}", counter); - setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.MusicBrainzId); - if (counter > CounterMax) - { - Log.Trace("Album status is still not active. Counter = {0}. Returning false", counter); - Log.Warn("We tried to se the status for the album but headphones didn't want to snatch it."); - return false; - } - } - return true; + bool setStatus = await Api.QueueAlbum(Settings.ApiKey, Settings.FullUri, request.ReleaseId); + return setStatus; } } } \ No newline at end of file From b7bfc2fcc0fe089decf037885b03883ad72d73df Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:52:43 -0600 Subject: [PATCH 09/12] Move local user login to be the first thing checked; renamed old Api variable to PlexApi now that Emby is in play. --- Ombi.UI/Modules/UserLoginModule.cs | 38 +++++++++++++----------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/Ombi.UI/Modules/UserLoginModule.cs b/Ombi.UI/Modules/UserLoginModule.cs index 1f9250ad9..cd234ca57 100644 --- a/Ombi.UI/Modules/UserLoginModule.cs +++ b/Ombi.UI/Modules/UserLoginModule.cs @@ -65,7 +65,7 @@ namespace Ombi.UI.Modules AuthService = auth; LandingPageSettings = lp; Analytics = a; - Api = api; + PlexApi = api; PlexSettings = plexSettings; Linker = linker; UserLogins = userLogins; @@ -126,7 +126,7 @@ namespace Ombi.UI.Modules private ISettingsService LandingPageSettings { get; } private ISettingsService PlexSettings { get; } private ISettingsService EmbySettings { get; } - private IPlexApi Api { get; } + private IPlexApi PlexApi { get; } private IEmbyApi EmbyApi { get; } private IResourceLinker Linker { get; } private IAnalytics Analytics { get; } @@ -301,12 +301,18 @@ namespace Ombi.UI.Modules var plexSettings = await PlexSettings.GetSettingsAsync(); var embySettings = await EmbySettings.GetSettingsAsync(); - if (plexSettings.Enable) + // attempt local login first as it has the least amount of overhead + userId = CustomUserMapper.ValidateUser(username, password)?.ToString(); + if (userId != null) + { + authenticated = true; + } + else if (userId == null && plexSettings.Enable) { if (settings.UserAuthentication) // Authenticate with Plex { Log.Debug("Need to auth and also provide pass"); - var signedIn = (PlexAuthentication) Api.SignIn(username, password); + var signedIn = (PlexAuthentication) PlexApi.SignIn(username, password); if (signedIn.user?.authentication_token != null) { Log.Debug("Correct credentials, checking if the user is account owner or in the friends list"); @@ -325,9 +331,9 @@ namespace Ombi.UI.Modules } } } - if (embySettings.Enable) + else if (userId == null && embySettings.Enable) { - if (settings.UserAuthentication) // Authenticate with Plex + if (settings.UserAuthentication) // Authenticate with Emby { Log.Debug("Need to auth and also provide pass"); EmbyUser signedIn = null; @@ -358,16 +364,6 @@ namespace Ombi.UI.Modules } } - if (string.IsNullOrEmpty(userId)) - { - // Local user? - userId = CustomUserMapper.ValidateUser(username, password)?.ToString(); - if (userId != null) - { - authenticated = true; - } - } - if (!authenticated) { return Response.AsJson(new { result = false, message = Resources.UI.UserLogin_IncorrectUserPass }); @@ -440,7 +436,7 @@ namespace Ombi.UI.Modules if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex { Log.Debug("Need to auth and also provide pass"); - var signedIn = (PlexAuthentication)Api.SignIn(username, password); + var signedIn = (PlexAuthentication)PlexApi.SignIn(username, password); if (signedIn.user?.authentication_token != null) { Log.Debug("Correct credentials, checking if the user is account owner or in the friends list"); @@ -711,7 +707,7 @@ namespace Ombi.UI.Modules private bool CheckIfUserIsOwner(string authToken, string userName) { - var userAccount = Api.GetAccount(authToken); + var userAccount = PlexApi.GetAccount(authToken); if (userAccount == null) { return false; @@ -721,7 +717,7 @@ namespace Ombi.UI.Modules private string GetOwnerId(string authToken, string userName) { - var userAccount = Api.GetAccount(authToken); + var userAccount = PlexApi.GetAccount(authToken); if (userAccount == null) { return string.Empty; @@ -731,7 +727,7 @@ namespace Ombi.UI.Modules private bool CheckIfUserIsInPlexFriends(string username, string authToken) { - var users = Api.GetUsers(authToken); + var users = PlexApi.GetUsers(authToken); var allUsers = users?.User?.Where(x => !string.IsNullOrEmpty(x.Title)); return allUsers != null && allUsers.Any(x => x.Title.Equals(username, StringComparison.CurrentCultureIgnoreCase)); } @@ -769,7 +765,7 @@ namespace Ombi.UI.Modules private string GetUserIdIsInPlexFriends(string username, string authToken) { - var users = Api.GetUsers(authToken); + var users = PlexApi.GetUsers(authToken); var allUsers = users?.User?.Where(x => !string.IsNullOrEmpty(x.Title)); return allUsers?.Where(x => x.Title.Equals(username, StringComparison.CurrentCultureIgnoreCase)).Select(x => x.Id).FirstOrDefault(); } From f400412568fe69dd07482da6b17c4a4a7d1cda8a Mon Sep 17 00:00:00 2001 From: smcpeck Date: Tue, 28 Feb 2017 20:53:29 -0600 Subject: [PATCH 10/12] Normalized spacing/tabs --- Ombi.UI/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Ombi.UI/Program.cs b/Ombi.UI/Program.cs index b454550c5..2dc1ef5a8 100644 --- a/Ombi.UI/Program.cs +++ b/Ombi.UI/Program.cs @@ -75,7 +75,7 @@ namespace Ombi.UI var s = new Setup(); var cn = s.SetupDb(baseUrl); s.CacheQualityProfiles(); - ConfigureTargets(cn); + ConfigureTargets(cn); SetupLogging(); if (port == -1 || port == 3579) @@ -110,7 +110,7 @@ namespace Ombi.UI { Log.Info("This is not Mono"); Console.WriteLine("Press any key to exit"); - Console.ReadLine(); + Console.ReadLine(); } } } From e46b621a2ddc7e3374d867460cab7aa10e7421e2 Mon Sep 17 00:00:00 2001 From: Shaun McPeck Date: Wed, 1 Mar 2017 00:35:46 -0600 Subject: [PATCH 11/12] Needed to treat a 201 as success, too. + removed some commented out code. --- Ombi.Api/ApiRequest.cs | 36 +++--------------------------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/Ombi.Api/ApiRequest.cs b/Ombi.Api/ApiRequest.cs index bb4dabb0a..c399eae45 100644 --- a/Ombi.Api/ApiRequest.cs +++ b/Ombi.Api/ApiRequest.cs @@ -59,17 +59,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - var message = "Error retrieving response. Check inner details for more info."; - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return response.Data; else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); @@ -89,17 +79,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - var message = "Error retrieving response. Check inner details for more info."; - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return DeserializeXml(response.Content); else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); @@ -112,17 +92,7 @@ namespace Ombi.Api var response = client.Execute(request); Log.Trace($"Request made to {response.ResponseUri} with status code {response.StatusCode}. The response was {response.Content}"); - /** - if (response.ErrorException != null) - { - Log.Error(response.ErrorException); - Log.Error(response.ErrorException.InnerException); - var message = "Error retrieving response. Check inner details for more info."; - throw new ApiRequestException(message, response.ErrorException); - } - **/ - - if (response.StatusCode == HttpStatusCode.OK) + if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created) return JsonConvert.DeserializeObject(response.Content, _settings); else throw new ApiRequestException($"Got StatusCode={response.StatusCode} for {response.ResponseUri}."); From ec9aac61dd5421701bf98934c9d668d54935a1fb Mon Sep 17 00:00:00 2001 From: Jamie Date: Mon, 6 Mar 2017 21:34:34 +0000 Subject: [PATCH 12/12] Fixed around the newsletter and a small feature around the permissions/features (#1215) * fixup! Reset the branch on v2.1.0 tag to get to a shared state between dev and Master. * typo * Fixed a bug in the Plex Newsletter * Another newsletter fix attempt #1163 #1116 --- Ombi.Core.Migration/Migrations/Version2200.cs | 2 +- .../SettingModels/UserManagementSettings.cs | 1 + Ombi.Helpers/Permissions/Permissions.cs | 5 ++++- Ombi.Services/Jobs/EmbyContentCacher.cs | 15 ++++++++++++- Ombi.Services/Jobs/EmbyEpisodeCacher.cs | 10 +++++++++ .../PlexRecentlyAddedNewsletter.cs | 22 ++++++++++++++++--- Ombi.Store/TableCreation.cs | 3 ++- Ombi.UI/Modules/RequestsModule.cs | 9 +++++--- .../UserManagementSettings.cshtml | 3 ++- 9 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Ombi.Core.Migration/Migrations/Version2200.cs b/Ombi.Core.Migration/Migrations/Version2200.cs index 4422846f0..d9069b8d7 100644 --- a/Ombi.Core.Migration/Migrations/Version2200.cs +++ b/Ombi.Core.Migration/Migrations/Version2200.cs @@ -105,7 +105,7 @@ namespace Ombi.Core.Migration.Migrations con.AlterTable("EmbyContent", "ADD", "AddedAt", true, "VARCHAR(50)"); con.AlterTable("EmbyEpisodes", "ADD", "AddedAt", true, "VARCHAR(50)"); - con.AlterTable("PlexContent", "ADD", "ItemID", true, "VARCHAR(100)"); + con.AlterTable("PlexContent", "ADD", "ItemId", true, "VARCHAR(100)"); con.AlterTable("PlexContent", "ADD", "AddedAt", true, "VARCHAR(100)"); } diff --git a/Ombi.Core/SettingModels/UserManagementSettings.cs b/Ombi.Core/SettingModels/UserManagementSettings.cs index 63c38bf1c..e22880164 100644 --- a/Ombi.Core/SettingModels/UserManagementSettings.cs +++ b/Ombi.Core/SettingModels/UserManagementSettings.cs @@ -38,6 +38,7 @@ namespace Ombi.Core.SettingModels public bool UsersCanViewOnlyOwnRequests { get; set; } public bool UsersCanViewOnlyOwnIssues { get; set; } public bool BypassRequestLimit { get; set; } + public bool ViewUsers { get; set; } // Features public bool RecentlyAddedNotification { get; set; } diff --git a/Ombi.Helpers/Permissions/Permissions.cs b/Ombi.Helpers/Permissions/Permissions.cs index bbfe07a4d..161f7f7f3 100644 --- a/Ombi.Helpers/Permissions/Permissions.cs +++ b/Ombi.Helpers/Permissions/Permissions.cs @@ -73,6 +73,9 @@ namespace Ombi.Helpers.Permissions UsersCanViewOnlyOwnIssues = 2048, [Display(Name = "Bypass the request limit")] - BypassRequestLimit = 4096 + BypassRequestLimit = 4096, + + [Display(Name = "User can see who requested")] + ViewUsers = 8192 } } \ No newline at end of file diff --git a/Ombi.Services/Jobs/EmbyContentCacher.cs b/Ombi.Services/Jobs/EmbyContentCacher.cs index a3eacab65..ce8acdc0e 100644 --- a/Ombi.Services/Jobs/EmbyContentCacher.cs +++ b/Ombi.Services/Jobs/EmbyContentCacher.cs @@ -115,7 +115,7 @@ namespace Ombi.Services.Jobs foreach (var item in info.Items) { var movieInfo = EmbyApi.GetInformation(item.Id, EmbyMediaType.Movie, embySettings.ApiKey, - embySettings.AdministratorId, embySettings.FullUri).MovieInformation; + embySettings.AdministratorId, embySettings.FullUri).MovieInformation; ProcessMovies(movieInfo); } } @@ -149,6 +149,12 @@ namespace Ombi.Services.Jobs connection.Dispose(); return media; }); + if (item != null && item.EmbyId != t.Id) + { + // delete this item since the Id has changed + EmbyContent.Delete(item); + item = null; + } if (item == null) { @@ -255,6 +261,13 @@ namespace Ombi.Services.Jobs return media; }); + if (item != null && item.EmbyId != movieInfo.Id) + { + // delete this item since the Id has changed + EmbyContent.Delete(item); + item = null; + } + if (item == null) { // Doesn't exist, insert it diff --git a/Ombi.Services/Jobs/EmbyEpisodeCacher.cs b/Ombi.Services/Jobs/EmbyEpisodeCacher.cs index 387a7dc98..ce313b965 100644 --- a/Ombi.Services/Jobs/EmbyEpisodeCacher.cs +++ b/Ombi.Services/Jobs/EmbyEpisodeCacher.cs @@ -81,6 +81,7 @@ namespace Ombi.Services.Jobs continue; } + // Check it this episode exists var item = Repo.Custom(connection => { @@ -93,6 +94,15 @@ namespace Ombi.Services.Jobs return media; }); + if (item != null) + { + if (item.EmbyId != ep.Id) // The id's dont match, delete it + { + Repo.Delete(item); + item = null; + } + } + if (item == null) { // add it diff --git a/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs b/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs index 0b37ae813..79687636a 100644 --- a/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs +++ b/Ombi.Services/Jobs/RecentlyAddedNewsletter/PlexRecentlyAddedNewsletter.cs @@ -117,12 +117,16 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter var filteredMovies = movie.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); var filteredEp = episodes.Where(m => recentlyAdded.All(x => x.ProviderId != m.ProviderId)).ToList(); - + var filteredSeries = series.Where(x => recentlyAdded.All(c => c.ProviderId != x.ProviderId)).ToList(); var info = new List(); foreach (var m in filteredMovies) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, m.ItemId); + if (i.Video == null) + { + continue; + } info.Add(new PlexRecentlyAddedModel { Metadata = i, @@ -132,10 +136,14 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter GenerateMovieHtml(info, sb); info.Clear(); - foreach (var t in series) + foreach (var t in filteredSeries) { var i = Api.GetMetadata(plexSettings.PlexAuthToken, plexSettings.FullUri, t.ItemId); - + if (i.Directory == null) + { + continue; + + } //var ep = filteredEp.Where(x => x.ShowTitle == t.Title); info.Add(new PlexRecentlyAddedModel { @@ -183,6 +191,14 @@ namespace Ombi.Services.Jobs.RecentlyAddedNewsletter AddedAt = DateTime.UtcNow }); } + foreach (var a in filteredSeries) + { + RecentlyAddedLog.Insert(new RecentlyAddedLog + { + ProviderId = a.ProviderId, + AddedAt = DateTime.UtcNow + }); + } } var escapedHtml = new string(html.Where(c => !char.IsControl(c)).ToArray()); diff --git a/Ombi.Store/TableCreation.cs b/Ombi.Store/TableCreation.cs index e3fc2c3f8..90aad78e5 100644 --- a/Ombi.Store/TableCreation.cs +++ b/Ombi.Store/TableCreation.cs @@ -25,6 +25,7 @@ // *********************************************************************** #endregion +using System; using System.Collections.Generic; using System.Data; using System.Linq; @@ -61,7 +62,7 @@ namespace Ombi.Store { connection.Open(); var result = connection.Query($"PRAGMA table_info({tableName});"); - if (result.Any(x => x.name == newColumn)) + if (result.Any(x => x.name.Equals(newColumn, StringComparison.CurrentCultureIgnoreCase))) { connection.Close(); return; diff --git a/Ombi.UI/Modules/RequestsModule.cs b/Ombi.UI/Modules/RequestsModule.cs index cf14ea26f..8fc65c515 100644 --- a/Ombi.UI/Modules/RequestsModule.cs +++ b/Ombi.UI/Modules/RequestsModule.cs @@ -208,6 +208,8 @@ namespace Ombi.UI.Modules var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests); + var allowViewUsers = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ViewUsers); + var viewModel = dbMovies.Select(movie => new RequestViewModel { ProviderId = movie.ProviderId, @@ -224,7 +226,7 @@ namespace Ombi.UI.Modules Approved = movie.Available || movie.Approved, Title = movie.Title, Overview = movie.Overview, - RequestedUsers = canManageRequest ? movie.AllUsers.ToArray() : new string[] { }, + RequestedUsers = canManageRequest || allowViewUsers ? movie.AllUsers.ToArray() : new string[] { }, ReleaseYear = movie.ReleaseDate.Year.ToString(), Available = movie.Available, Admin = canManageRequest, @@ -294,6 +296,8 @@ namespace Ombi.UI.Modules var canManageRequest = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ManageRequests); + var allowViewUsers = Security.HasAnyPermissions(User, Permissions.Administrator, Permissions.ViewUsers); + var viewModel = dbTv.Select(tv => new RequestViewModel { ProviderId = tv.ProviderId, @@ -310,7 +314,7 @@ namespace Ombi.UI.Modules Approved = tv.Available || tv.Approved, Title = tv.Title, Overview = tv.Overview, - RequestedUsers = canManageRequest ? tv.AllUsers.ToArray() : new string[] { }, + RequestedUsers = canManageRequest || allowViewUsers ? tv.AllUsers.ToArray() : new string[] { }, ReleaseYear = tv.ReleaseDate.Year.ToString(), Available = tv.Available, Admin = canManageRequest, @@ -382,7 +386,6 @@ namespace Ombi.UI.Modules private async Task GetAlbumRequests() { - var settings = PrSettings.GetSettings(); var dbAlbum = await Service.GetAllAsync(); dbAlbum = dbAlbum.Where(x => x.Type == RequestType.Album); if (Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests) && !IsAdmin) diff --git a/Ombi.UI/Views/UserManagementSettings/UserManagementSettings.cshtml b/Ombi.UI/Views/UserManagementSettings/UserManagementSettings.cshtml index 03164a4ef..af9666b69 100644 --- a/Ombi.UI/Views/UserManagementSettings/UserManagementSettings.cshtml +++ b/Ombi.UI/Views/UserManagementSettings/UserManagementSettings.cshtml @@ -1,4 +1,4 @@ -@using Ombi.UI.Helpers +@using Ombi.UI.Helpers @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase @Html.Partial("Shared/Partial/_Sidebar") @@ -25,6 +25,7 @@ @Html.Checkbox(Model.UsersCanViewOnlyOwnIssues, "UsersCanViewOnlyOwnIssues", "Users can only view their own issues") @Html.Checkbox(Model.UsersCanViewOnlyOwnRequests, "UsersCanViewOnlyOwnRequests", "Users can only view their own requests") @Html.Checkbox(Model.BypassRequestLimit, "BypassRequestLimit", "Bypass the request limit") + @Html.Checkbox(Model.ViewUsers, "ViewUsers", "User can see who made requests")