snappymail/dev/Remote/AbstractAjax.js

247 lines
6 KiB
JavaScript
Raw Normal View History

import window from 'window';
import _ from '_';
import $ from '$';
2019-07-05 03:19:24 +08:00
import { TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT, DEFAULT_AJAX_TIMEOUT } from 'Common/Consts';
import { StorageResultType, Notification } from 'Common/Enums';
import { pInt, pString, isUnd } from 'Common/Utils';
2019-07-05 03:19:24 +08:00
import { data as GlobalsData } from 'Common/Globals';
import { ajax } from 'Common/Links';
import { runHook } from 'Common/Plugins';
2016-06-30 08:02:45 +08:00
import * as Settings from 'Storage/Settings';
2019-07-05 03:19:24 +08:00
class AbstractAjaxRemote {
constructor() {
this.oRequests = {};
}
2014-08-20 23:03:12 +08:00
/**
* @param {?Function} fCallback
* @param {string} sRequestAction
* @param {string} sType
* @param {?AjaxJsonDefaultResponse} oData
* @param {boolean} bCached
* @param {*=} oRequestParameters
*/
defaultResponse(fCallback, sRequestAction, sType, oData, bCached, oRequestParameters) {
2019-07-05 03:19:24 +08:00
const fCall = () => {
if (StorageResultType.Success !== sType && GlobalsData.bUnload) {
sType = StorageResultType.Unload;
}
2014-10-06 02:37:31 +08:00
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === sType && oData && !oData.Result) {
if (
oData && [
2019-07-05 03:19:24 +08:00
Notification.AuthError,
Notification.AccessError,
Notification.ConnectionError,
Notification.DomainNotAllowed,
Notification.AccountNotAllowed,
Notification.MailServerError,
Notification.UnknownNotification,
Notification.UnknownError
].includes(oData.ErrorCode)
2019-07-05 03:19:24 +08:00
) {
GlobalsData.iAjaxErrorCount += 1;
}
2014-10-06 02:37:31 +08:00
2019-07-05 03:19:24 +08:00
if (oData && Notification.InvalidToken === oData.ErrorCode) {
GlobalsData.iTokenErrorCount += 1;
}
2014-10-06 02:37:31 +08:00
2019-07-05 03:19:24 +08:00
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
}
2019-07-05 03:19:24 +08:00
}
2014-10-06 02:37:31 +08:00
2019-07-05 03:19:24 +08:00
if (oData.ClearAuth || oData.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
GlobalsData.__APP__.clearClientSideToken();
2019-07-05 03:19:24 +08:00
if (!oData.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload) {
GlobalsData.__APP__.loginAndLogoutReload(false, true);
2014-10-06 02:37:31 +08:00
}
}
}
2019-07-05 03:19:24 +08:00
} else if (StorageResultType.Success === sType && oData && oData.Result) {
GlobalsData.iAjaxErrorCount = 0;
GlobalsData.iTokenErrorCount = 0;
}
2016-07-16 05:29:42 +08:00
2019-07-05 03:19:24 +08:00
runHook('ajax-default-response', [
sRequestAction,
StorageResultType.Success === sType ? oData : null,
sType,
bCached,
oRequestParameters
]);
if (fCallback) {
fCallback(
sType,
StorageResultType.Success === sType ? oData : null,
bCached,
sRequestAction,
oRequestParameters
);
}
};
2019-07-05 03:19:24 +08:00
switch (sType) {
case 'success':
sType = StorageResultType.Success;
break;
case 'abort':
sType = StorageResultType.Abort;
break;
default:
sType = StorageResultType.Error;
break;
}
2014-10-06 02:37:31 +08:00
2019-07-05 03:19:24 +08:00
if (StorageResultType.Error === sType) {
_.delay(fCall, 300);
2019-07-05 03:19:24 +08:00
} else {
fCall();
}
2016-06-30 08:02:45 +08:00
}
/**
* @param {?Function} fResultCallback
* @param {Object} oParameters
* @param {?number=} iTimeOut = 20000
* @param {string=} sGetAdd = ''
* @param {Array=} aAbortActions = []
* @returns {jQuery.jqXHR}
*/
ajaxRequest(fResultCallback, params, iTimeOut = 20000, sGetAdd = '', abortActions = []) {
2019-07-05 03:19:24 +08:00
const isPost = '' === sGetAdd,
headers = {},
2019-07-05 03:19:24 +08:00
start = new window.Date().getTime();
2019-07-05 03:19:24 +08:00
let action = '';
params = params || {};
action = params.Action || '';
2019-07-05 03:19:24 +08:00
if (action && 0 < abortActions.length) {
_.each(abortActions, (actionToAbort) => {
2019-07-05 03:19:24 +08:00
if (this.oRequests[actionToAbort]) {
this.oRequests[actionToAbort].__aborted = true;
2019-07-05 03:19:24 +08:00
if (this.oRequests[actionToAbort].abort) {
this.oRequests[actionToAbort].abort();
}
this.oRequests[actionToAbort] = null;
}
});
2016-06-30 08:02:45 +08:00
}
2019-07-05 03:19:24 +08:00
if (isPost) {
params.XToken = Settings.appSettingsGet('token');
}
const oDefAjax = $.ajax({
type: isPost ? 'POST' : 'GET',
url: ajax(sGetAdd),
async: true,
dataType: 'json',
data: isPost ? params : {},
headers: headers,
timeout: iTimeOut,
global: true
});
oDefAjax.always((oData, sType) => {
let cached = false;
2019-07-05 03:19:24 +08:00
if (oData && oData.Time) {
cached = pInt(oData.Time) > new window.Date().getTime() - start;
2014-10-06 02:37:31 +08:00
}
2020-01-01 01:44:41 +08:00
if (oData && oData.UpdateToken) {
if (GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
GlobalsData.__APP__.setClientSideToken(oData.UpdateToken);
}
}
2019-07-05 03:19:24 +08:00
if (action && this.oRequests[action]) {
if (this.oRequests[action].__aborted) {
sType = 'abort';
}
this.oRequests[action] = null;
}
2014-10-06 02:37:31 +08:00
this.defaultResponse(fResultCallback, action, sType, oData, cached, params);
});
2014-10-06 02:37:31 +08:00
if (action && 0 < abortActions.length && abortActions.includes(action)) {
2019-07-05 03:19:24 +08:00
if (this.oRequests[action]) {
this.oRequests[action].__aborted = true;
2019-07-05 03:19:24 +08:00
if (this.oRequests[action].abort) {
this.oRequests[action].abort();
}
this.oRequests[action] = null;
2016-06-30 08:02:45 +08:00
}
this.oRequests[action] = oDefAjax;
2016-06-30 08:02:45 +08:00
}
2014-08-20 23:03:12 +08:00
2019-07-05 03:19:24 +08:00
// eslint-disable-next-line no-console
2019-06-29 08:17:29 +08:00
oDefAjax.catch(console.log);
return oDefAjax;
2016-06-30 08:02:45 +08:00
}
2014-08-20 23:03:12 +08:00
/**
* @param {?Function} fCallback
* @param {string} sAction
* @param {Object=} oParameters
* @param {?number=} iTimeout
* @param {string=} sGetAdd = ''
* @param {Array=} aAbortActions = []
*/
defaultRequest(fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions) {
oParameters = oParameters || {};
oParameters.Action = sAction;
2016-06-30 08:02:45 +08:00
sGetAdd = pString(sGetAdd);
2016-06-30 08:02:45 +08:00
runHook('ajax-default-request', [sAction, oParameters, sGetAdd]);
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
return this.ajaxRequest(
fCallback,
oParameters,
isUnd(iTimeout) ? DEFAULT_AJAX_TIMEOUT : pInt(iTimeout),
sGetAdd,
aAbortActions
);
}
2016-06-30 08:02:45 +08:00
/**
* @param {?Function} fCallback
*/
noop(fCallback) {
this.defaultRequest(fCallback, 'Noop');
}
/**
* @param {?Function} fCallback
*/
getPublicKey(fCallback) {
this.defaultRequest(fCallback, 'GetPublicKey');
}
/**
* @param {?Function} fCallback
* @param {string} sVersion
*/
jsVersion(fCallback, sVersion) {
this.defaultRequest(fCallback, 'Version', {
'Version': sVersion
});
}
}
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
export { AbstractAjaxRemote, AbstractAjaxRemote as default };