Cache the options token for preferences system
Change-Id: I2e9a4326119db390aba36cc5bc6da20c5c16eed1
This commit is contained in:
@@ -21,7 +21,90 @@
|
|||||||
( function ( $, mw, undefined ) {
|
( function ( $, mw, undefined ) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ULSPreferences = function () {
|
var ULSPreferences,
|
||||||
|
cachedOptionsToken = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post to options API with correct token.
|
||||||
|
* If we have no token, get one and try to post.
|
||||||
|
* If we have a cached token try using that, and if it fails, blank out the
|
||||||
|
* cached token and start over.
|
||||||
|
*
|
||||||
|
* @param params {Object} API parameters
|
||||||
|
* @param ok {Function} callback for success
|
||||||
|
* @param err {Function} [optional] error callback
|
||||||
|
* @return {jqXHR}
|
||||||
|
*/
|
||||||
|
function saveOptionsWithToken ( params, ok, err ) {
|
||||||
|
var useTokenToPost, getTokenIfBad;
|
||||||
|
if ( cachedOptionsToken === null ) {
|
||||||
|
// We don't have a valid cached token, so get a fresh one and try posting.
|
||||||
|
// We do not trap any 'badtoken' or 'notoken' errors, because we don't want
|
||||||
|
// an infinite loop. If this fresh token is bad, something else is very wrong.
|
||||||
|
useTokenToPost = function ( token ) {
|
||||||
|
params.token = token;
|
||||||
|
new mw.Api().post( params, ok, err );
|
||||||
|
};
|
||||||
|
return getOptionToken( useTokenToPost, err );
|
||||||
|
} else {
|
||||||
|
// We do have a token, but it might be expired. So if it is 'bad' then
|
||||||
|
// start over with a new token.
|
||||||
|
params.token = cachedOptionsToken;
|
||||||
|
getTokenIfBad = function ( code, result ) {
|
||||||
|
if ( code === 'badtoken' ) {
|
||||||
|
// force a new token, clear any old one
|
||||||
|
cachedOptionsToken = null;
|
||||||
|
saveOptionsWithToken( params, ok, err );
|
||||||
|
} else {
|
||||||
|
err( code, result );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return new mw.Api().post( params, { ok : ok, err : getTokenIfBad });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api helper to grab an options token
|
||||||
|
*
|
||||||
|
* token callback has signature ( String token )
|
||||||
|
* error callback has signature ( String code, Object results, XmlHttpRequest xhr, Exception exception )
|
||||||
|
* Note that xhr and exception are only available for 'http_*' errors
|
||||||
|
* code may be any http_* error code (see mw.Api), or 'token_missing'
|
||||||
|
*
|
||||||
|
* @param tokenCallback {Function} received token callback
|
||||||
|
* @param err {Function} error callback
|
||||||
|
* @return {jqXHR}
|
||||||
|
*/
|
||||||
|
function getOptionToken ( tokenCallback, err ) {
|
||||||
|
var parameters = {
|
||||||
|
action: 'tokens',
|
||||||
|
type: 'options'
|
||||||
|
},
|
||||||
|
ok = function ( data ) {
|
||||||
|
var token;
|
||||||
|
// If token type is not available for this user,
|
||||||
|
// key 'translationreviewtoken' is missing or can contain Boolean false
|
||||||
|
if ( data.tokens && data.tokens.optionstoken ) {
|
||||||
|
token = data.tokens.optionstoken;
|
||||||
|
cachedOptionsToken = token;
|
||||||
|
tokenCallback( token );
|
||||||
|
} else {
|
||||||
|
err( 'token-missing', data );
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ajaxOptions = {
|
||||||
|
ok: ok,
|
||||||
|
err: err,
|
||||||
|
// Due to the API assuming we're logged out if we pass the callback-parameter,
|
||||||
|
// we have to disable jQuery's callback system, and instead parse JSON string,
|
||||||
|
// by setting 'jsonp' to false.
|
||||||
|
jsonp: false
|
||||||
|
};
|
||||||
|
|
||||||
|
return new mw.Api().get( parameters, ajaxOptions );
|
||||||
|
}
|
||||||
|
|
||||||
|
ULSPreferences = function () {
|
||||||
this.preferenceName = 'uls-preferences';
|
this.preferenceName = 'uls-preferences';
|
||||||
this.username = mw.user.getName();
|
this.username = mw.user.getName();
|
||||||
this.isAnon = mw.user.isAnon();
|
this.isAnon = mw.user.isAnon();
|
||||||
@@ -68,8 +151,7 @@
|
|||||||
* @param callback
|
* @param callback
|
||||||
*/
|
*/
|
||||||
save: function ( callback ) {
|
save: function ( callback ) {
|
||||||
var ulsPreferences = this,
|
var ulsPreferences = this;
|
||||||
api;
|
|
||||||
|
|
||||||
callback = callback || $.noop;
|
callback = callback || $.noop;
|
||||||
if ( this.isAnon ) {
|
if ( this.isAnon ) {
|
||||||
@@ -78,25 +160,13 @@
|
|||||||
callback.call( this, true );
|
callback.call( this, true );
|
||||||
} else {
|
} else {
|
||||||
// Logged in user. Use MW apis to change preferences
|
// Logged in user. Use MW apis to change preferences
|
||||||
api = new mw.Api();
|
saveOptionsWithToken( {
|
||||||
|
|
||||||
api.post( {
|
|
||||||
action: 'tokens',
|
|
||||||
type: 'options'
|
|
||||||
} ).done( function ( tokenresult ) {
|
|
||||||
var token = tokenresult.tokens.optionstoken;
|
|
||||||
|
|
||||||
api.post( {
|
|
||||||
action: 'options',
|
action: 'options',
|
||||||
optionname: ulsPreferences.preferenceName,
|
optionname: ulsPreferences.preferenceName,
|
||||||
optionvalue: $.toJSON( ulsPreferences.preferences ),
|
optionvalue: $.toJSON( ulsPreferences.preferences )
|
||||||
token: token
|
}, function () {
|
||||||
} ).done( function ( data ) {
|
|
||||||
callback.call( this, true );
|
callback.call( this, true );
|
||||||
} ).fail( function ( data ) {
|
}, function () {
|
||||||
callback.call( this, false );
|
|
||||||
} );
|
|
||||||
} ).fail( function () {
|
|
||||||
callback.call( this, false );
|
callback.call( this, false );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user