Merge pull request #2496 from dpalou/MOBILE-3450
MOBILE-3450 sync: Display better error when syncing logged out sitemain
commit
08a155b4e9
|
@ -1984,6 +1984,7 @@
|
||||||
"core.settings.about": "local_moodlemobileapp",
|
"core.settings.about": "local_moodlemobileapp",
|
||||||
"core.settings.appsettings": "local_moodlemobileapp",
|
"core.settings.appsettings": "local_moodlemobileapp",
|
||||||
"core.settings.appversion": "local_moodlemobileapp",
|
"core.settings.appversion": "local_moodlemobileapp",
|
||||||
|
"core.settings.cannotsyncloggedout": "local_moodlemobileapp",
|
||||||
"core.settings.cannotsyncoffline": "local_moodlemobileapp",
|
"core.settings.cannotsyncoffline": "local_moodlemobileapp",
|
||||||
"core.settings.cannotsyncwithoutwifi": "local_moodlemobileapp",
|
"core.settings.cannotsyncwithoutwifi": "local_moodlemobileapp",
|
||||||
"core.settings.colorscheme": "local_moodlemobileapp",
|
"core.settings.colorscheme": "local_moodlemobileapp",
|
||||||
|
|
|
@ -1984,6 +1984,7 @@
|
||||||
"core.settings.about": "About",
|
"core.settings.about": "About",
|
||||||
"core.settings.appsettings": "App settings",
|
"core.settings.appsettings": "App settings",
|
||||||
"core.settings.appversion": "App version",
|
"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.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.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",
|
"core.settings.colorscheme": "Color Scheme",
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
"about": "About",
|
"about": "About",
|
||||||
"appsettings": "App settings",
|
"appsettings": "App settings",
|
||||||
"appversion": "App version",
|
"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.",
|
"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.",
|
"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",
|
"colorscheme": "Color Scheme",
|
||||||
|
|
|
@ -264,58 +264,63 @@ export class CoreSettingsHelper {
|
||||||
* @param siteId ID of the site to synchronize.
|
* @param siteId ID of the site to synchronize.
|
||||||
* @return Promise resolved when synchronized, rejected if failure.
|
* @return Promise resolved when synchronized, rejected if failure.
|
||||||
*/
|
*/
|
||||||
synchronizeSite(syncOnlyOnWifi: boolean, siteId: string): Promise<any> {
|
async synchronizeSite(syncOnlyOnWifi: boolean, siteId: string): Promise<void> {
|
||||||
if (this.syncPromises[siteId]) {
|
if (this.syncPromises[siteId]) {
|
||||||
// There's already a sync ongoing for this site, return the promise.
|
// There's already a sync ongoing for this site, return the promise.
|
||||||
return this.syncPromises[siteId];
|
return this.syncPromises[siteId];
|
||||||
}
|
}
|
||||||
|
|
||||||
const promises = [];
|
const site = await this.sitesProvider.getSite(siteId);
|
||||||
const hasSyncHandlers = this.cronDelegate.hasManualSyncHandlers();
|
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.
|
// 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()) {
|
} 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.
|
const syncPromise = Promise.all([
|
||||||
promises.push(this.filePoolProvider.invalidateAllFiles(siteId).catch(() => {
|
// Invalidate all the site files so they are re-downloaded.
|
||||||
// Ignore errors.
|
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;
|
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<void> {
|
||||||
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue