From c1e2acf98ab7943de1e58464a9623a48d84a7cbc Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Mon, 7 Jun 2021 17:20:29 +0200 Subject: [PATCH] MOBILE-3320 user: Make preferences fail-safe --- src/addons/calendar/services/calendar.ts | 6 +++--- src/core/features/user/services/user.ts | 15 +++++---------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts index 4dfb9fffd..0d3c84412 100644 --- a/src/addons/calendar/services/calendar.ts +++ b/src/addons/calendar/services/calendar.ts @@ -566,14 +566,14 @@ export class AddonCalendarProvider { */ async getCalendarLookAhead(siteId?: string): Promise { const site = await CoreSites.getSite(siteId); - let value: string | undefined; + let value: string | undefined | null; try { value = await CoreUser.getUserPreference('calendar_lookahead'); } catch { // Ignore errors. } - if (typeof value == 'undefined') { + if (typeof value == 'undefined' || value === null) { value = site.getStoredConfig('calendar_lookahead'); } @@ -588,7 +588,7 @@ export class AddonCalendarProvider { */ async getCalendarTimeFormat(siteId?: string): Promise { const site = await CoreSites.getSite(siteId); - let format: string | undefined; + let format: string | undefined | null; try { format = await CoreUser.getUserPreference('calendar_timeformat'); diff --git a/src/core/features/user/services/user.ts b/src/core/features/user/services/user.ts index 6033f8efd..6dfc08bfa 100644 --- a/src/core/features/user/services/user.ts +++ b/src/core/features/user/services/user.ts @@ -391,7 +391,7 @@ export class CoreUserProvider { * @param siteId Site Id. If not defined, use current site. * @return Preference value or null if preference not set. */ - async getUserPreference(name: string, siteId?: string): Promise { + async getUserPreference(name: string, siteId?: string): Promise { siteId = siteId || CoreSites.getCurrentSiteId(); const preference = await CoreUtils.ignoreErrors(CoreUserOffline.getPreference(name, siteId)); @@ -403,20 +403,15 @@ export class CoreUserProvider { const wsValue = await this.getUserPreferenceOnline(name, siteId); - if (!wsValue) { - if (preference) { - // Return the local value. - return preference.value; - } - - throw new CoreError('Preference not found'); - } - if (preference && preference.value != preference.onlinevalue && preference.onlinevalue == wsValue) { // Sync is pending for this preference, return stored value. return preference.value; } + if (!wsValue) { + return null; + } + await CoreUserOffline.setPreference(name, wsValue, wsValue); return wsValue;