From eff7c4b7b7ebd89f88e1a65fe70d5cd02901ba08 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 11 May 2014 17:44:42 -0700 Subject: [PATCH] Auto reload when server has been updated New: Updating will reload UI on navigation --- .../Pipelines/CacheHeaderPipeline.cs | 20 ++--------- .../Pipelines/NzbDroneVersionPipeline.cs | 33 +++++++++++++++++++ src/NzbDrone.Api/NzbDrone.Api.csproj | 1 + src/NzbDrone.Host/NzbDrone.Host.csproj | 1 + .../Owin/MiddleWare/NancyMiddleWare.cs | 2 +- .../MiddleWare/NzbDroneVersionMiddleWare.cs | 32 ++++++++++++++++++ .../Owin/MiddleWare/SignalRMiddleWare.cs | 2 +- src/UI/Mixins/jquery.ajax.js | 25 +++++++++++++- src/UI/Series/SeriesController.js | 4 +-- src/UI/Shared/SignalRBroadcaster.js | 9 ----- 10 files changed, 98 insertions(+), 31 deletions(-) create mode 100644 src/NzbDrone.Api/Extensions/Pipelines/NzbDroneVersionPipeline.cs create mode 100644 src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs diff --git a/src/NzbDrone.Api/Extensions/Pipelines/CacheHeaderPipeline.cs b/src/NzbDrone.Api/Extensions/Pipelines/CacheHeaderPipeline.cs index 183326415..1ac4ae86c 100644 --- a/src/NzbDrone.Api/Extensions/Pipelines/CacheHeaderPipeline.cs +++ b/src/NzbDrone.Api/Extensions/Pipelines/CacheHeaderPipeline.cs @@ -1,18 +1,11 @@ using Nancy; using Nancy.Bootstrapper; -using NzbDrone.Api.Frontend; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Api.Extensions.Pipelines { - public class CacheHeaderPipeline : IRegisterNancyPipeline + public class NzbDroneVersionPipeline : IRegisterNancyPipeline { - private readonly ICacheableSpecification _cacheableSpecification; - - public CacheHeaderPipeline(ICacheableSpecification cacheableSpecification) - { - _cacheableSpecification = cacheableSpecification; - } - public void Register(IPipelines pipelines) { pipelines.AfterRequest.AddItemToStartOfPipeline(Handle); @@ -20,14 +13,7 @@ public void Register(IPipelines pipelines) private void Handle(NancyContext context) { - if (_cacheableSpecification.IsCacheable(context)) - { - context.Response.Headers.EnableCache(); - } - else - { - context.Response.Headers.DisableCache(); - } + context.Response.Headers.Add("X-ApplicationVersion", BuildInfo.Version.ToString()); } } } \ No newline at end of file diff --git a/src/NzbDrone.Api/Extensions/Pipelines/NzbDroneVersionPipeline.cs b/src/NzbDrone.Api/Extensions/Pipelines/NzbDroneVersionPipeline.cs new file mode 100644 index 000000000..183326415 --- /dev/null +++ b/src/NzbDrone.Api/Extensions/Pipelines/NzbDroneVersionPipeline.cs @@ -0,0 +1,33 @@ +using Nancy; +using Nancy.Bootstrapper; +using NzbDrone.Api.Frontend; + +namespace NzbDrone.Api.Extensions.Pipelines +{ + public class CacheHeaderPipeline : IRegisterNancyPipeline + { + private readonly ICacheableSpecification _cacheableSpecification; + + public CacheHeaderPipeline(ICacheableSpecification cacheableSpecification) + { + _cacheableSpecification = cacheableSpecification; + } + + public void Register(IPipelines pipelines) + { + pipelines.AfterRequest.AddItemToStartOfPipeline(Handle); + } + + private void Handle(NancyContext context) + { + if (_cacheableSpecification.IsCacheable(context)) + { + context.Response.Headers.EnableCache(); + } + else + { + context.Response.Headers.DisableCache(); + } + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Api/NzbDrone.Api.csproj b/src/NzbDrone.Api/NzbDrone.Api.csproj index d0c5875ca..9c7f11503 100644 --- a/src/NzbDrone.Api/NzbDrone.Api.csproj +++ b/src/NzbDrone.Api/NzbDrone.Api.csproj @@ -122,6 +122,7 @@ + diff --git a/src/NzbDrone.Host/NzbDrone.Host.csproj b/src/NzbDrone.Host/NzbDrone.Host.csproj index 861ae81e9..8aba54433 100644 --- a/src/NzbDrone.Host/NzbDrone.Host.csproj +++ b/src/NzbDrone.Host/NzbDrone.Host.csproj @@ -101,6 +101,7 @@ + diff --git a/src/NzbDrone.Host/Owin/MiddleWare/NancyMiddleWare.cs b/src/NzbDrone.Host/Owin/MiddleWare/NancyMiddleWare.cs index 9e74c1e71..c6116a46a 100644 --- a/src/NzbDrone.Host/Owin/MiddleWare/NancyMiddleWare.cs +++ b/src/NzbDrone.Host/Owin/MiddleWare/NancyMiddleWare.cs @@ -13,7 +13,7 @@ public NancyMiddleWare(INancyBootstrapper nancyBootstrapper) _nancyBootstrapper = nancyBootstrapper; } - public int Order { get { return 1; } } + public int Order { get { return 2; } } public void Attach(IAppBuilder appBuilder) { diff --git a/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs b/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs new file mode 100644 index 000000000..a7a76644a --- /dev/null +++ b/src/NzbDrone.Host/Owin/MiddleWare/NzbDroneVersionMiddleWare.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using Microsoft.Owin; +using NzbDrone.Common.EnvironmentInfo; +using Owin; + +namespace NzbDrone.Host.Owin.MiddleWare +{ + public class NzbDroneVersionMiddleWare : IOwinMiddleWare + { + public int Order { get { return 0; } } + + public void Attach(IAppBuilder appBuilder) + { + appBuilder.Use(typeof (AddApplicationVersionHeader)); + } + } + + public class AddApplicationVersionHeader : OwinMiddleware + { + public AddApplicationVersionHeader(OwinMiddleware next) + : base(next) + { + } + + public override Task Invoke(OwinRequest request, OwinResponse response) + { + response.AddHeader("X-ApplicationVersion", BuildInfo.Version.ToString()); + + return Next.Invoke(request, response); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Host/Owin/MiddleWare/SignalRMiddleWare.cs b/src/NzbDrone.Host/Owin/MiddleWare/SignalRMiddleWare.cs index 35499eacd..395ff6f2f 100644 --- a/src/NzbDrone.Host/Owin/MiddleWare/SignalRMiddleWare.cs +++ b/src/NzbDrone.Host/Owin/MiddleWare/SignalRMiddleWare.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Host.Owin.MiddleWare { public class SignalRMiddleWare : IOwinMiddleWare { - public int Order { get { return 0; } } + public int Order { get { return 1; } } public SignalRMiddleWare(IContainer container) { diff --git a/src/UI/Mixins/jquery.ajax.js b/src/UI/Mixins/jquery.ajax.js index 5105d8ae8..8186a7784 100644 --- a/src/UI/Mixins/jquery.ajax.js +++ b/src/UI/Mixins/jquery.ajax.js @@ -28,6 +28,29 @@ define( xhr.headers['X-Api-Key'] = window.NzbDrone.ApiKey; } - return original.apply(this, arguments); + return original.apply(this, arguments).done(function (response, status, xhr){ + var version = xhr.getResponseHeader('X-ApplicationVersion'); + + if (!window.NzbDrone || !window.NzbDrone.Version) { + return; + } + + if (version !== window.NzbDrone.Version) { + var vent = require('vent'); + var messenger = require('Shared/Messenger'); + + if (!vent || !messenger) { + return; + } + + messenger.show({ + message : 'NzbDrone has been updated', + hideAfter : 0, + id : 'droneUpdated' + }); + + vent.trigger(vent.Events.ServerUpdated); + } + }); }; }); diff --git a/src/UI/Series/SeriesController.js b/src/UI/Series/SeriesController.js index ed9334b08..5e358683d 100644 --- a/src/UI/Series/SeriesController.js +++ b/src/UI/Series/SeriesController.js @@ -19,7 +19,7 @@ define( series: function () { this.setTitle('NzbDrone'); - AppLayout.mainRegion.show(new SeriesIndexLayout()); + this.showMainRegion(new SeriesIndexLayout()); }, seriesDetails: function (query) { @@ -28,7 +28,7 @@ define( if (series.length !== 0) { var targetSeries = series[0]; this.setTitle(targetSeries.get('title')); - AppLayout.mainRegion.show(new SeriesDetailsLayout({ model: targetSeries })); + this.showMainRegion(new SeriesDetailsLayout({ model: targetSeries })); } else { this.showNotFound(); diff --git a/src/UI/Shared/SignalRBroadcaster.js b/src/UI/Shared/SignalRBroadcaster.js index 38bc4b6dc..928b14e9a 100644 --- a/src/UI/Shared/SignalRBroadcaster.js +++ b/src/UI/Shared/SignalRBroadcaster.js @@ -50,15 +50,6 @@ define( this.signalRconnection.reconnected(function() { tryingToReconnect = false; - - var currentVersion = StatusModel.get('version'); - - var promise = StatusModel.fetch(); - promise.done(function () { - if (StatusModel.get('version') !== currentVersion) { - vent.trigger(vent.Events.ServerUpdated); - } - }); }); this.signalRconnection.disconnected(function () {