Merge pull request #2328 from dpalou/MOBILE-3387

Mobile 3387
main
Juan Leyva 2020-03-31 11:09:25 +02:00 committed by GitHub
commit fe76cec8d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 81 deletions

View File

@ -91,16 +91,7 @@ export class CoreLoginInitPage {
}); });
} }
return this.sitesProvider.getCurrentSite().getPublicConfig().catch(() => {
return {};
}).then((config) => {
return this.sitesProvider.checkRequiredMinimumVersion(config).then(() => {
// User is logged in, go to site initial page.
return this.loginHelper.goToSiteInitialPage(); return this.loginHelper.goToSiteInitialPage();
}).catch(() => {
return this.loadPage();
});
});
} }
return this.navCtrl.setRoot('CoreLoginSitesPage'); return this.navCtrl.setRoot('CoreLoginSitesPage');

View File

@ -48,7 +48,7 @@ export class CoreSiteInfoCronHandler implements CoreCronHandler {
* @return Interval time (in milliseconds). * @return Interval time (in milliseconds).
*/ */
getInterval(): number { getInterval(): number {
return 3600000; // 1 hour. return 10800000; // 3 hours.
} }
/** /**

View File

@ -171,6 +171,7 @@ export class CoreWSProvider {
/** /**
* Call a Moodle WS using the AJAX API. Please use it if the WS layer is not an option. * Call a Moodle WS using the AJAX API. Please use it if the WS layer is not an option.
* It uses a cache to prevent duplicate requests.
* *
* @param method The WebService method to be called. * @param method The WebService method to be called.
* @param data Arguments to pass to the method. * @param data Arguments to pass to the method.
@ -181,79 +182,19 @@ export class CoreWSProvider {
* - available: 0 if unknown, 1 if available, -1 if not available. * - available: 0 if unknown, 1 if available, -1 if not available.
*/ */
callAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise<any> { callAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise<any> {
let promise; const cacheParams = {
if (typeof preSets.siteUrl == 'undefined') {
return rejectWithError(this.createFakeWSError('core.unexpectederror', true));
} else if (!this.appProvider.isOnline()) {
return rejectWithError(this.createFakeWSError('core.networkerrormsg', true));
}
if (typeof preSets.responseExpected == 'undefined') {
preSets.responseExpected = true;
}
const script = preSets.noLogin ? 'service-nologin.php' : 'service.php',
ajaxData = JSON.stringify([{
index: 0,
methodname: method, methodname: method,
args: this.convertValuesToString(data) args: data,
}]); };
// The info= parameter has no function. It is just to help with debugging. let promise = this.getPromiseHttp('ajax', preSets.siteUrl, cacheParams);
// We call it info to match the parameter name use by Moodle's AMD ajax module.
let siteUrl = preSets.siteUrl + '/lib/ajax/' + script + '?info=' + method;
if (preSets.noLogin && preSets.useGet) { if (!promise) {
// Send params using GET. promise = this.performAjax(method, data, preSets);
siteUrl += '&args=' + encodeURIComponent(ajaxData); promise = this.setPromiseHttp(promise, 'ajax', preSets.siteUrl, cacheParams);
promise = this.http.get(siteUrl).timeout(this.getRequestTimeout()).toPromise();
} else {
promise = this.http.post(siteUrl, ajaxData).timeout(this.getRequestTimeout()).toPromise();
} }
return promise.then((data: any) => { return promise;
// Some moodle web services return null.
// If the responseExpected value is set then so long as no data is returned, we create a blank object.
if (!data && !preSets.responseExpected) {
data = [{}];
}
// Check if error. Ajax layer should always return an object (if error) or an array (if success).
if (!data || typeof data != 'object') {
return rejectWithError(this.createFakeWSError('core.serverconnection', true));
} else if (data.error) {
return rejectWithError(data);
}
// Get the first response since only one request was done.
data = data[0];
if (data.error) {
return rejectWithError(data.exception);
}
return data.data;
}, (data) => {
const available = data.status == 404 ? -1 : 0;
return rejectWithError(this.createFakeWSError('core.serverconnection', true), available);
});
// Convenience function to return an error.
function rejectWithError(exception: any, available?: number): Promise<never> {
if (typeof available == 'undefined') {
if (exception.errorcode) {
available = exception.errorcode == 'invalidrecord' ? -1 : 1;
} else {
available = 0;
}
}
exception.available = available;
return Promise.reject(exception);
}
} }
/** /**
@ -496,6 +437,94 @@ export class CoreWSProvider {
return method + '#' + Md5.hashAsciiStr(url); return method + '#' + Md5.hashAsciiStr(url);
} }
/**
* Call a Moodle WS using the AJAX API.
*
* @param method The WebService method to be called.
* @param data Arguments to pass to the method.
* @param preSets Extra settings and information. Only some
* @return Promise resolved with the response data in success and rejected with an object containing:
* - error: Error message.
* - errorcode: Error code returned by the site (if any).
* - available: 0 if unknown, 1 if available, -1 if not available.
*/
protected performAjax(method: string, data: any, preSets: CoreWSAjaxPreSets): Promise<any> {
let promise;
if (typeof preSets.siteUrl == 'undefined') {
return rejectWithError(this.createFakeWSError('core.unexpectederror', true));
} else if (!this.appProvider.isOnline()) {
return rejectWithError(this.createFakeWSError('core.networkerrormsg', true));
}
if (typeof preSets.responseExpected == 'undefined') {
preSets.responseExpected = true;
}
const script = preSets.noLogin ? 'service-nologin.php' : 'service.php',
ajaxData = JSON.stringify([{
index: 0,
methodname: method,
args: this.convertValuesToString(data)
}]);
// The info= parameter has no function. It is just to help with debugging.
// We call it info to match the parameter name use by Moodle's AMD ajax module.
let siteUrl = preSets.siteUrl + '/lib/ajax/' + script + '?info=' + method;
if (preSets.noLogin && preSets.useGet) {
// Send params using GET.
siteUrl += '&args=' + encodeURIComponent(ajaxData);
promise = this.http.get(siteUrl).timeout(this.getRequestTimeout()).toPromise();
} else {
promise = this.http.post(siteUrl, ajaxData).timeout(this.getRequestTimeout()).toPromise();
}
return promise.then((data: any) => {
// Some moodle web services return null.
// If the responseExpected value is set then so long as no data is returned, we create a blank object.
if (!data && !preSets.responseExpected) {
data = [{}];
}
// Check if error. Ajax layer should always return an object (if error) or an array (if success).
if (!data || typeof data != 'object') {
return rejectWithError(this.createFakeWSError('core.serverconnection', true));
} else if (data.error) {
return rejectWithError(data);
}
// Get the first response since only one request was done.
data = data[0];
if (data.error) {
return rejectWithError(data.exception);
}
return data.data;
}, (data) => {
const available = data.status == 404 ? -1 : 0;
return rejectWithError(this.createFakeWSError('core.serverconnection', true), available);
});
// Convenience function to return an error.
function rejectWithError(exception: any, available?: number): Promise<never> {
if (typeof available == 'undefined') {
if (exception.errorcode) {
available = exception.errorcode == 'invalidrecord' ? -1 : 1;
} else {
available = 0;
}
}
exception.available = available;
return Promise.reject(exception);
}
}
/** /**
* Perform a HEAD request and save the promise while waiting to be resolved. * Perform a HEAD request and save the promise while waiting to be resolved.
* *