From 5bf557658d9d371a50d5b6433286e9e34396fe67 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Thu, 9 Jun 2016 16:59:35 +0100 Subject: [PATCH] Just some more async changes --- PlexRequests.Core/StatusChecker.cs | 16 +- PlexRequests.UI.Tests/UserLoginModuleTests.cs | 1 - PlexRequests.UI/Bootstrapper.cs | 4 +- PlexRequests.UI/Modules/AdminModule.cs | 155 +++++++++--------- PlexRequests.UI/Modules/RequestsModule.cs | 6 +- .../Modules/UpdateCheckerModule.cs | 7 +- PlexRequests.UI/Modules/UserLoginModule.cs | 3 - .../Views/Admin/CouchPotato.cshtml | 1 + PlexRequests.UI/Views/Issues/Details.cshtml | 1 + 9 files changed, 97 insertions(+), 97 deletions(-) diff --git a/PlexRequests.Core/StatusChecker.cs b/PlexRequests.Core/StatusChecker.cs index 476a55a45..6d616cb06 100644 --- a/PlexRequests.Core/StatusChecker.cs +++ b/PlexRequests.Core/StatusChecker.cs @@ -51,7 +51,7 @@ namespace PlexRequests.Core return releases.FirstOrDefault(); } - public StatusModel GetStatus() + public async Task GetStatus() { var assemblyVersion = AssemblyHelper.GetProductVersion(); var model = new StatusModel @@ -59,23 +59,23 @@ namespace PlexRequests.Core Version = assemblyVersion, }; - var latestRelease = GetLatestRelease(); - if (latestRelease.Result == null) + var latestRelease = await GetLatestRelease(); + if (latestRelease == null) { return new StatusModel { Version = "Unknown" }; } - var latestVersionArray = latestRelease.Result.Name.Split(new[] { 'v' }, StringSplitOptions.RemoveEmptyEntries); + var latestVersionArray = latestRelease.Name.Split(new[] { 'v' }, StringSplitOptions.RemoveEmptyEntries); var latestVersion = latestVersionArray.Length > 1 ? latestVersionArray[1] : string.Empty; if (!latestVersion.Equals(assemblyVersion, StringComparison.InvariantCultureIgnoreCase)) { model.UpdateAvailable = true; - model.UpdateUri = latestRelease.Result.HtmlUrl; + model.UpdateUri = latestRelease.HtmlUrl; } - model.ReleaseNotes = latestRelease.Result.Body; - model.DownloadUri = latestRelease.Result.Assets[0].BrowserDownloadUrl; - model.ReleaseTitle = latestRelease.Result.Name; + model.ReleaseNotes = latestRelease.Body; + model.DownloadUri = latestRelease.Assets[0].BrowserDownloadUrl; + model.ReleaseTitle = latestRelease.Name; return model; } diff --git a/PlexRequests.UI.Tests/UserLoginModuleTests.cs b/PlexRequests.UI.Tests/UserLoginModuleTests.cs index 2d90f105d..22d000575 100644 --- a/PlexRequests.UI.Tests/UserLoginModuleTests.cs +++ b/PlexRequests.UI.Tests/UserLoginModuleTests.cs @@ -46,7 +46,6 @@ using PlexRequests.UI.Modules; namespace PlexRequests.UI.Tests { [TestFixture] - //[Ignore("Needs some work")] public class UserLoginModuleTests { private Mock> AuthMock { get; set; } diff --git a/PlexRequests.UI/Bootstrapper.cs b/PlexRequests.UI/Bootstrapper.cs index 7aa7ee0be..5cfef5ca7 100644 --- a/PlexRequests.UI/Bootstrapper.cs +++ b/PlexRequests.UI/Bootstrapper.cs @@ -145,7 +145,7 @@ namespace PlexRequests.UI notificationService.Subscribe(new SlackNotification(container.Resolve(), slackService)); } } - + protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) { //CORS Enable @@ -203,7 +203,7 @@ namespace PlexRequests.UI container.Register(); container.Register(); - + // Api container.Register(); container.Register(); diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index d8f2f673c..7414e6b01 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -23,42 +23,43 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ -using System.Net; -using PlexRequests.Helpers.Exceptions; + #endregion +using System; +using System.Diagnostics; +using System.Threading.Tasks; using System.Collections.Generic; using System.Dynamic; using System.Linq; -using MarkdownSharp; +using System.Net; using Nancy; using Nancy.Extensions; using Nancy.ModelBinding; using Nancy.Responses.Negotiation; using Nancy.Validation; - +using Nancy.Json; +using Nancy.Security; using NLog; +using MarkdownSharp; + using PlexRequests.Api; using PlexRequests.Api.Interfaces; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; +using PlexRequests.Helpers.Exceptions; using PlexRequests.Services.Interfaces; using PlexRequests.Services.Notification; using PlexRequests.Store.Models; using PlexRequests.Store.Repository; using PlexRequests.UI.Helpers; using PlexRequests.UI.Models; -using System; -using System.Diagnostics; -using System.Threading.Tasks; -using Nancy.Json; -using Nancy.Security; namespace PlexRequests.UI.Modules { @@ -133,11 +134,11 @@ namespace PlexRequests.UI.Modules LandingSettings = lp; this.RequiresClaims(UserClaims.Admin); - + Get["/"] = _ => Admin(); - Get["/authentication"] = _ => Authentication(); - Post["/authentication"] = _ => SaveAuthentication(); + Get["/authentication", true] = async (x, ct) => await Authentication(); + Post["/authentication", true] = async (x, ct) => await SaveAuthentication(); Post["/"] = _ => SaveAdmin(); @@ -163,7 +164,7 @@ namespace PlexRequests.UI.Modules Get["/emailnotification"] = _ => EmailNotifications(); Post["/emailnotification"] = _ => SaveEmailNotifications(); Post["/testemailnotification"] = _ => TestEmailNotifications(); - Get["/status"] = _ => Status(); + Get["/status", true] = async (x,ct) => await Status(); Get["/pushbulletnotification"] = _ => PushbulletNotifications(); Post["/pushbulletnotification"] = _ => SavePushbulletNotifications(); @@ -181,7 +182,7 @@ namespace PlexRequests.UI.Modules Get["/headphones"] = _ => Headphones(); Post["/headphones"] = _ => SaveHeadphones(); - Post["/createapikey"] = x => CreateApiKey(); + Post["/createapikey"] = x => CreateApiKey(); Post["/autoupdate"] = x => AutoUpdate(); @@ -190,22 +191,22 @@ namespace PlexRequests.UI.Modules Get["/slacknotification"] = _ => SlackNotifications(); Post["/slacknotification"] = _ => SaveSlackNotifications(); - Get["/landingpage", true] = async (x,ct) => await LandingPage(); + Get["/landingpage", true] = async (x, ct) => await LandingPage(); Post["/landingpage", true] = async (x, ct) => await SaveLandingPage(); } - private Negotiator Authentication() + private async Task Authentication() { - var settings = AuthService.GetSettings(); + var settings = await AuthService.GetSettingsAsync(); return View["/Authentication", settings]; } - private Response SaveAuthentication() + private async Task SaveAuthentication() { var model = this.Bind(); - var result = AuthService.SaveSettings(model); + var result = await AuthService.SaveSettingsAsync(model); if (result) { if (!string.IsNullOrEmpty(BaseUrl)) @@ -231,23 +232,23 @@ namespace PlexRequests.UI.Modules private Response SaveAdmin() { var model = this.Bind(); - var valid = this.Validate (model); - if (!valid.IsValid) { - return Response.AsJson(valid.SendJsonError()); - } - - if (!string.IsNullOrWhiteSpace (model.BaseUrl)) { - if (model.BaseUrl.StartsWith ("/") || model.BaseUrl.StartsWith ("\\")) - { - model.BaseUrl = model.BaseUrl.Remove (0, 1); - } - } - var result = PrService.SaveSettings(model); - if (result) { - return Response.AsJson (new JsonResponseModel{ Result = true }); - } + var valid = this.Validate(model); + if (!valid.IsValid) + { + return Response.AsJson(valid.SendJsonError()); + } - return Response.AsJson (new JsonResponseModel{ Result = false, Message = "We could not save to the database, please try again" }); + if (!string.IsNullOrWhiteSpace(model.BaseUrl)) + { + if (model.BaseUrl.StartsWith("/", StringComparison.CurrentCultureIgnoreCase) || model.BaseUrl.StartsWith("\\", StringComparison.CurrentCultureIgnoreCase)) + { + model.BaseUrl = model.BaseUrl.Remove(0, 1); + } + } + var result = PrService.SaveSettings(model); + return Response.AsJson(result + ? new JsonResponseModel { Result = true } + : new JsonResponseModel { Result = false, Message = "We could not save to the database, please try again" }); } private Response RequestAuthToken() @@ -295,40 +296,40 @@ namespace PlexRequests.UI.Modules return Response.AsJson(new { Result = true, Users = string.Empty }); } - try { - var users = PlexApi.GetUsers(token); - if (users == null) - { - return Response.AsJson(string.Empty); - } - if (users.User == null || users.User?.Length == 0) - { - return Response.AsJson(string.Empty); - } + try + { + var users = PlexApi.GetUsers(token); + if (users == null) + { + return Response.AsJson(string.Empty); + } + if (users.User == null || users.User?.Length == 0) + { + return Response.AsJson(string.Empty); + } - var usernames = users.User.Select(x => x.Title); - return Response.AsJson(new {Result = true, Users = usernames}); - } catch (Exception ex) { - Log.Error (ex); - if (ex is WebException || ex is ApiRequestException) { - return Response.AsJson (new { Result = false, Message ="Could not load the user list! We have connectivity problems connecting to Plex, Please ensure we can access Plex.Tv, The error has been logged." }); - } + var usernames = users.User.Select(x => x.Title); + return Response.AsJson(new { Result = true, Users = usernames }); + } + catch (Exception ex) + { + Log.Error(ex); + if (ex is WebException || ex is ApiRequestException) + { + return Response.AsJson(new { Result = false, Message = "Could not load the user list! We have connectivity problems connecting to Plex, Please ensure we can access Plex.Tv, The error has been logged." }); + } - return Response.AsJson (new { Result = false, Message = ex.Message}); - } + return Response.AsJson(new { Result = false, Message = ex.Message }); + } } private Negotiator CouchPotato() { - dynamic model = new ExpandoObject(); var settings = CpService.GetSettings(); - model = settings; - return View["CouchPotato", model]; + return View["CouchPotato", settings]; } - - private Response SaveCouchPotato() { var couchPotatoSettings = this.Bind(); @@ -485,7 +486,7 @@ namespace PlexRequests.UI.Modules { return Response.AsJson(valid.SendJsonError()); } - + var result = EmailService.SaveSettings(settings); if (settings.Enabled) @@ -503,11 +504,11 @@ namespace PlexRequests.UI.Modules : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); } - private Negotiator Status() + private async Task Status() { var checker = new StatusChecker(); - var status = checker.GetStatus(); - var md = new Markdown(new MarkdownOptions { AutoNewLines = true }); + var status = await Cache.GetOrSetAsync(CacheKeys.LastestProductVersion, async () => await checker.GetStatus(), 30); + var md = new Markdown(new MarkdownOptions { AutoNewLines = true, AutoHyperlink = true}); status.ReleaseNotes = md.Transform(status.ReleaseNotes); return View["Status", status]; } @@ -516,12 +517,12 @@ namespace PlexRequests.UI.Modules { var url = Request.Form["url"]; - var startInfo = Type.GetType("Mono.Runtime") != null - ? new ProcessStartInfo("mono PlexRequests.Updater.exe") { Arguments = url } + var startInfo = Type.GetType("Mono.Runtime") != null + ? new ProcessStartInfo("mono PlexRequests.Updater.exe") { Arguments = url } : new ProcessStartInfo("PlexRequests.Updater.exe") { Arguments = url }; Process.Start(startInfo); - + Environment.Exit(0); return Nancy.Response.NoBody; } @@ -734,17 +735,17 @@ namespace PlexRequests.UI.Modules : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); } - private Response CreateApiKey() - { - this.RequiresClaims(UserClaims.Admin); - var apiKey = Guid.NewGuid().ToString("N"); + private Response CreateApiKey() + { + this.RequiresClaims(UserClaims.Admin); + var apiKey = Guid.NewGuid().ToString("N"); var settings = PrService.GetSettings(); - settings.ApiKey = apiKey; + settings.ApiKey = apiKey; PrService.SaveSettings(settings); - return Response.AsJson(apiKey); - } + return Response.AsJson(apiKey); + } private Response TestSlackNotification() { @@ -825,14 +826,14 @@ namespace PlexRequests.UI.Modules if (settings.Enabled && settings.EnabledNoticeTime && string.IsNullOrEmpty(settings.NoticeMessage)) { - return Response.AsJson(new JsonResponseModel { Result = false, Message = "If you are going to enabled the notice, then we need a message!"}); + return Response.AsJson(new JsonResponseModel { Result = false, Message = "If you are going to enabled the notice, then we need a message!" }); } var result = await LandingSettings.SaveSettingsAsync(settings); - return Response.AsJson(result - ? new JsonResponseModel { Result = true } - : new JsonResponseModel { Result = false, Message = "Could not save to Db Please check the logs"}); + return Response.AsJson(result + ? new JsonResponseModel { Result = true } + : new JsonResponseModel { Result = false, Message = "Could not save to Db Please check the logs" }); } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/RequestsModule.cs b/PlexRequests.UI/Modules/RequestsModule.cs index 03ef43ece..8d73c0bb0 100644 --- a/PlexRequests.UI/Modules/RequestsModule.cs +++ b/PlexRequests.UI/Modules/RequestsModule.cs @@ -75,7 +75,7 @@ namespace PlexRequests.UI.Modules CpApi = cpApi; Cache = cache; - Get["/"] = _ => LoadRequests(); + Get["/", true] = async (x, ct) => await LoadRequests(); Get["/movies", true] = async (x, ct) => await GetMovies(); Get["/tvshows", true] = async (c, ct) => await GetTvShows(); Get["/albums", true] = async (x, ct) => await GetAlbumRequests(); @@ -101,9 +101,9 @@ namespace PlexRequests.UI.Modules private ICouchPotatoApi CpApi { get; } private ICacheProvider Cache { get; } - private Negotiator LoadRequests() + private async Task LoadRequests() { - var settings = PrSettings.GetSettings(); + var settings = await PrSettings.GetSettingsAsync(); return View["Index", settings]; } diff --git a/PlexRequests.UI/Modules/UpdateCheckerModule.cs b/PlexRequests.UI/Modules/UpdateCheckerModule.cs index f1b46a805..216288c1a 100644 --- a/PlexRequests.UI/Modules/UpdateCheckerModule.cs +++ b/PlexRequests.UI/Modules/UpdateCheckerModule.cs @@ -25,6 +25,7 @@ // ************************************************************************/ #endregion using System; +using System.Threading.Tasks; using Nancy; @@ -43,14 +44,14 @@ namespace PlexRequests.UI.Modules { Cache = provider; - Get["/"] = _ => CheckLatestVersion(); + Get["/", true] = async (x,ct) => await CheckLatestVersion(); } private ICacheProvider Cache { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); - private Response CheckLatestVersion() + private async Task CheckLatestVersion() { try { @@ -60,7 +61,7 @@ namespace PlexRequests.UI.Modules } var checker = new StatusChecker(); - var release = Cache.GetOrSet(CacheKeys.LastestProductVersion, () => checker.GetStatus(), 30); + var release = await Cache.GetOrSetAsync(CacheKeys.LastestProductVersion, async() => await checker.GetStatus(), 30); return Response.AsJson(release.UpdateAvailable ? new JsonUpdateAvailableModel { UpdateAvailable = true} diff --git a/PlexRequests.UI/Modules/UserLoginModule.cs b/PlexRequests.UI/Modules/UserLoginModule.cs index 1f4b6d4c2..cfa0c03a9 100644 --- a/PlexRequests.UI/Modules/UserLoginModule.cs +++ b/PlexRequests.UI/Modules/UserLoginModule.cs @@ -26,13 +26,11 @@ #endregion using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Nancy; using Nancy.Extensions; -using Nancy.Responses; using Nancy.Responses.Negotiation; using NLog; @@ -41,7 +39,6 @@ using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models.Plex; using PlexRequests.Core; using PlexRequests.Core.SettingModels; -using PlexRequests.Helpers; using PlexRequests.UI.Models; namespace PlexRequests.UI.Modules diff --git a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml index 8558534f2..413dc278f 100644 --- a/PlexRequests.UI/Views/Admin/CouchPotato.cshtml +++ b/PlexRequests.UI/Views/Admin/CouchPotato.cshtml @@ -1,4 +1,5 @@ @using PlexRequests.UI.Helpers +@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase @Html.Partial("_Sidebar") @{ int port; diff --git a/PlexRequests.UI/Views/Issues/Details.cshtml b/PlexRequests.UI/Views/Issues/Details.cshtml index 971a43fbf..ccebcf41e 100644 --- a/PlexRequests.UI/Views/Issues/Details.cshtml +++ b/PlexRequests.UI/Views/Issues/Details.cshtml @@ -1,6 +1,7 @@ @using System.Linq @using PlexRequests.Core.Models @using PlexRequests.UI.Helpers +@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase @{ var baseUrl = Html.GetBaseUrl(); var formAction = string.Empty;