From c0817f9b07ed7afd856084722b094d1b9082b1b2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 15 Jul 2020 09:17:02 +0200 Subject: [PATCH] MOBILE-3450 sync: Display better error when syncing logged out site --- scripts/langindex.json | 1 + src/assets/lang/en.json | 1 + src/core/settings/lang/en.json | 1 + src/core/settings/providers/helper.ts | 81 ++++++++++++++------------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/scripts/langindex.json b/scripts/langindex.json index 8bd92d9cc..758f678d6 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1984,6 +1984,7 @@ "core.settings.about": "local_moodlemobileapp", "core.settings.appsettings": "local_moodlemobileapp", "core.settings.appversion": "local_moodlemobileapp", + "core.settings.cannotsyncloggedout": "local_moodlemobileapp", "core.settings.cannotsyncoffline": "local_moodlemobileapp", "core.settings.cannotsyncwithoutwifi": "local_moodlemobileapp", "core.settings.colorscheme": "local_moodlemobileapp", diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index ddd310e2a..98cf2f4e9 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -1984,6 +1984,7 @@ "core.settings.about": "About", "core.settings.appsettings": "App settings", "core.settings.appversion": "App version", + "core.settings.cannotsyncloggedout": "This site cannot be synchronised because you've logged out. Please try again when you're logged in the site again.", "core.settings.cannotsyncoffline": "Cannot synchronise offline.", "core.settings.cannotsyncwithoutwifi": "Cannot synchronise because the current settings only allow to synchronise when connected to Wi-Fi. Please connect to a Wi-Fi network.", "core.settings.colorscheme": "Color Scheme", diff --git a/src/core/settings/lang/en.json b/src/core/settings/lang/en.json index 5810653ad..d87510a7d 100644 --- a/src/core/settings/lang/en.json +++ b/src/core/settings/lang/en.json @@ -2,6 +2,7 @@ "about": "About", "appsettings": "App settings", "appversion": "App version", + "cannotsyncloggedout": "This site cannot be synchronised because you've logged out. Please try again when you're logged in the site again.", "cannotsyncoffline": "Cannot synchronise offline.", "cannotsyncwithoutwifi": "Cannot synchronise because the current settings only allow to synchronise when connected to Wi-Fi. Please connect to a Wi-Fi network.", "colorscheme": "Color Scheme", diff --git a/src/core/settings/providers/helper.ts b/src/core/settings/providers/helper.ts index 9d4769bfe..4f1d9714e 100644 --- a/src/core/settings/providers/helper.ts +++ b/src/core/settings/providers/helper.ts @@ -264,58 +264,63 @@ export class CoreSettingsHelper { * @param siteId ID of the site to synchronize. * @return Promise resolved when synchronized, rejected if failure. */ - synchronizeSite(syncOnlyOnWifi: boolean, siteId: string): Promise { + async synchronizeSite(syncOnlyOnWifi: boolean, siteId: string): Promise { if (this.syncPromises[siteId]) { // There's already a sync ongoing for this site, return the promise. return this.syncPromises[siteId]; } - const promises = []; + const site = await this.sitesProvider.getSite(siteId); const hasSyncHandlers = this.cronDelegate.hasManualSyncHandlers(); - if (hasSyncHandlers && !this.appProvider.isOnline()) { + if (site.isLoggedOut()) { + // Cannot sync logged out sites. + throw this.translate.instant('core.settings.cannotsyncloggedout'); + } else if (hasSyncHandlers && !this.appProvider.isOnline()) { // We need connection to execute sync. - return Promise.reject(this.translate.instant('core.settings.cannotsyncoffline')); + throw this.translate.instant('core.settings.cannotsyncoffline'); } else if (hasSyncHandlers && syncOnlyOnWifi && this.appProvider.isNetworkAccessLimited()) { - return Promise.reject(this.translate.instant('core.settings.cannotsyncwithoutwifi')); + throw this.translate.instant('core.settings.cannotsyncwithoutwifi'); } - // Invalidate all the site files so they are re-downloaded. - promises.push(this.filePoolProvider.invalidateAllFiles(siteId).catch(() => { - // Ignore errors. - })); + const syncPromise = Promise.all([ + // Invalidate all the site files so they are re-downloaded. + this.utils.ignoreErrors(this.filePoolProvider.invalidateAllFiles(siteId)), + // Invalidate and synchronize site data. + site.invalidateWsCache(), + this.checkSiteLocalMobile(site), + this.sitesProvider.updateSiteInfo(site.getId()), + this.cronDelegate.forceSyncExecution(site.getId()), + ]); - // Get the site to invalidate data. - promises.push(this.sitesProvider.getSite(siteId).then((site) => { - // Invalidate the WS cache. - return site.invalidateWsCache().then(() => { - const subPromises = []; - - // Check if local_mobile was installed in Moodle. - subPromises.push(site.checkIfLocalMobileInstalledAndNotUsed().then(() => { - // Local mobile was added. Throw invalid session to force reconnect and create a new token. - this.eventsProvider.trigger(CoreEventsProvider.SESSION_EXPIRED, {}, siteId); - - return Promise.reject(this.translate.instant('core.lostconnection')); - }, () => { - // Update site info. - return this.sitesProvider.updateSiteInfo(siteId); - })); - - // Execute cron if needed. - subPromises.push(this.cronDelegate.forceSyncExecution(siteId)); - - return Promise.all(subPromises); - }); - })); - - let syncPromise = Promise.all(promises); this.syncPromises[siteId] = syncPromise; - syncPromise = syncPromise.finally(() => { - delete this.syncPromises[siteId]; - }); - return syncPromise; + try { + await syncPromise; + } finally { + delete this.syncPromises[siteId]; + } + } + + /** + * Check if local_mobile was added to the site. + * + * @param site Site to check. + * @return Promise resolved if no action needed. + */ + protected async checkSiteLocalMobile(site: CoreSite): Promise { + try { + // Check if local_mobile was installed in Moodle. + await site.checkIfLocalMobileInstalledAndNotUsed(); + } catch (error) { + // Not added, nothing to do. + return; + } + + // Local mobile was added. Throw invalid session to force reconnect and create a new token. + this.eventsProvider.trigger(CoreEventsProvider.SESSION_EXPIRED, {}, site.getId()); + + throw this.translate.instant('core.lostconnection'); } /**