From 3e6cd3c53cb9708f90bd3600ad948f631688a3b6 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Fri, 31 Jan 2020 16:51:33 -0500 Subject: [PATCH] perf(web): don't wait on Sieve server to render UIxPageFrame.wox --- UI/PreferencesUI/UIxJSONPreferences.m | 26 +++++++++---- UI/PreferencesUI/product.plist | 5 +++ UI/WebServerResources/js/Common/Common.app.js | 2 +- .../js/Common/Resource.service.js | 39 +++++++++++++++++++ .../js/Preferences/Preferences.service.js | 29 ++++++++++++++ .../js/Preferences/PreferencesController.js | 3 +- 6 files changed, 94 insertions(+), 10 deletions(-) diff --git a/UI/PreferencesUI/UIxJSONPreferences.m b/UI/PreferencesUI/UIxJSONPreferences.m index e255d61b5..98bcb9fe9 100644 --- a/UI/PreferencesUI/UIxJSONPreferences.m +++ b/UI/PreferencesUI/UIxJSONPreferences.m @@ -76,15 +76,14 @@ static SoProduct *preferencesProduct = nil; return labelsDictionary; } -// -// Used internally -// -- (BOOL) _hasActiveExternalSieveScripts +- (WOResponse *) activeExternalSieveScriptsAction { NGSieveClient *client; + NSDictionary *data; SOGoMailAccount *account; SOGoMailAccounts *folder; SOGoSieveManager *manager; + WOResponse *response; folder = [[[context activeUser] homeFolderInContext: context] mailAccountsFolder: @"Mail" inContext: context]; account = [folder lookupName: @"0" inContext: context acquire: NO]; @@ -92,9 +91,20 @@ static SoProduct *preferencesProduct = nil; client = [manager clientForAccount: account]; if (client) - return [manager hasActiveExternalSieveScripts: client]; + { + if ([manager hasActiveExternalSieveScripts: client]) + response = [self responseWith204]; + else + response = [self responseWithStatus: 404]; + } + else + { + data = [NSDictionary dictionaryWithObjectsAndKeys: + @"An error occured while communicating with the Sieve server", @"message", nil]; + response = [self responseWithStatus: 500 andJSONRepresentation: data]; + } - return NO; + return response; } - (WOResponse *) jsonDefaultsAction @@ -431,8 +441,8 @@ static SoProduct *preferencesProduct = nil; [values setObject: vacation forKey: @"Vacation"]; } - // Detect if an external Sieve script is active - [values setObject: [NSNumber numberWithBool: [self _hasActiveExternalSieveScripts]] forKey: @"hasActiveExternalSieveScripts"]; + // Ignore hasActiveExternalSieveScripts as it will be requested on demand + [values removeObjectForKey: @"hasActiveExternalSieveScripts"]; return [values jsonRepresentation]; } diff --git a/UI/PreferencesUI/product.plist b/UI/PreferencesUI/product.plist index 2299064e5..8f7fabfe8 100644 --- a/UI/PreferencesUI/product.plist +++ b/UI/PreferencesUI/product.plist @@ -31,6 +31,11 @@ protectedBy = "View"; pageName = "UIxIdentities"; }; + activeExternalSieveScripts = { + protectedBy = "View"; + pageName = "UIxJSONPreferences"; + actionName = "activeExternalSieveScripts"; + }; jsonDefaults = { protectedBy = "View"; pageName = "UIxJSONPreferences"; diff --git a/UI/WebServerResources/js/Common/Common.app.js b/UI/WebServerResources/js/Common/Common.app.js index 47a627876..d04899718 100644 --- a/UI/WebServerResources/js/Common/Common.app.js +++ b/UI/WebServerResources/js/Common/Common.app.js @@ -370,7 +370,7 @@ $window.recovered = true; $window.location.href = $window.ApplicationBaseURL + $state.href($state.current); } - else { + else if (!rejection.data.quiet) { // Broadcast the response error $rootScope.$broadcast('http:Error', rejection); } diff --git a/UI/WebServerResources/js/Common/Resource.service.js b/UI/WebServerResources/js/Common/Resource.service.js index d4deacd8d..a64c9d2b4 100644 --- a/UI/WebServerResources/js/Common/Resource.service.js +++ b/UI/WebServerResources/js/Common/Resource.service.js @@ -101,6 +101,45 @@ return deferred.promise; }; + /** + * @function quietFetch + * @memberof Resource.prototype + * @desc Fetch resource using a specific folder, action and/or parameters, but disable the global + * error interceptor. + * @param {string} folderId - the folder on which the action will be applied (ex: addressbook, calendar) + * @param {string} action - the action to be used in the URL + * @param {Object} params - Object parameters injected through the $http service + * @return a promise + */ + Resource.prototype.quietFetch = function(folderId, action, params) { + var deferred = this._q.defer(), + path = [this._path]; + if (folderId) path.push(folderId.split('/')); + if (action) path.push(action); + path = _.compact(_.flatten(path)).join('/'); + + this._http({ + method: 'GET', + url: path, + params: params, + transformResponse: function(data) { + var jsonData; + try { + jsonData = angular.fromJson(data); + } + catch (e) { + jsonData = {}; + } + return angular.extend({ quiet: true }, jsonData); + } + }) + .then(function(response) { + return deferred.resolve(response.data); + }, deferred.reject); + + return deferred.promise; + }; + /** * @function newguid * @memberof Resource.prototype diff --git a/UI/WebServerResources/js/Preferences/Preferences.service.js b/UI/WebServerResources/js/Preferences/Preferences.service.js index d174f4db7..d3682f2a2 100644 --- a/UI/WebServerResources/js/Preferences/Preferences.service.js +++ b/UI/WebServerResources/js/Preferences/Preferences.service.js @@ -253,6 +253,35 @@ return url; }; + /** + * @function hasActiveExternalSieveScripts + * @memberof Preferences.prototype + * @desc Check if the user has an external Sieve script enabled. + */ + Preferences.prototype.hasActiveExternalSieveScripts = function(value) { + var _this = this; + + if (typeof value !== 'undefined') { + this.defaults.hasActiveExternalSieveScripts = value; + } + else if (typeof this.defaults.hasActiveExternalSieveScripts !== 'undefined') { + return this.defaults.hasActiveExternalSieveScripts; + } + else { + // Fetch information from server + this.defaults.hasActiveExternalSieveScripts = false; // default until we receive an answer + Preferences.$$resource.quietFetch('activeExternalSieveScripts') + .then(function() { + _this.defaults.hasActiveExternalSieveScripts = true; + }, function(response) { + _this.defaults.hasActiveExternalSieveScripts = false; + if (response.status === 404) { + return Preferences.$q.resolve(true); + } + }); + } + }; + /** * @function $save * @memberof Preferences.prototype diff --git a/UI/WebServerResources/js/Preferences/PreferencesController.js b/UI/WebServerResources/js/Preferences/PreferencesController.js index d23aedc28..f417adb99 100644 --- a/UI/WebServerResources/js/Preferences/PreferencesController.js +++ b/UI/WebServerResources/js/Preferences/PreferencesController.js @@ -23,6 +23,7 @@ if (Preferences.defaults.SOGoAlternateAvatar) User.$alternateAvatar = Preferences.defaults.SOGoAlternateAvatar; + this.preferences.hasActiveExternalSieveScripts(); this.updateVacationDates(); }; @@ -285,7 +286,7 @@ }; this.manageSieveScript = function(form) { - this.preferences.defaults.hasActiveExternalSieveScripts = false; + this.preferences.hasActiveExternalSieveScripts(false); form.$setDirty(); };