ULS Frontend: Save preferences only for named users

Add a new method isNamed in ext.uls.common to use the isNamed
method if available else use the isAnon method as usual.

mw.user.isNamed was added in MW 1.40

Bug: T337780
Change-Id: I747c042a95e8edc793a2265a15ed6ba8ae9f1997
This commit is contained in:
Abijeet
2023-08-10 19:44:25 +05:30
committed by jenkins-bot
parent 2434939475
commit 88f0045b94
4 changed files with 24 additions and 13 deletions

View File

@@ -28,20 +28,22 @@
// https://www.mediawiki.org/wiki/Manual:Coding_conventions/JavaScript#Keys
this.preferenceName = 'uls-preferences';
this.username = mw.user.getName();
this.isAnon = mw.user.isAnon();
// For MW < 1.40. ext.uls.isNamed is inlined here to avoid dependency on ext.uls.common.
this.isNamed = typeof mw.user.isNamed === 'function' ? mw.user.isNamed() : !mw.user.isAnon();
this.preferences = null;
this.init();
};
ULSPreferences.prototype = {
init: function () {
if ( this.isAnon ) {
this.preferences = mw.storage.getObject( this.preferenceName );
} else {
if ( this.isNamed ) {
try {
this.preferences = JSON.parse( mw.user.options.get( this.preferenceName ) );
} catch ( e ) {
}
} else {
this.preferences = mw.storage.getObject( this.preferenceName );
}
if ( !$.isPlainObject( this.preferences ) ) {
@@ -78,12 +80,8 @@
var self = this;
callback = callback || function () {};
if ( this.isAnon ) {
// Anonymous user. Save preferences in local storage
mw.storage.setObject( this.preferenceName, this.preferences );
callback.call( this, true );
} else {
// Logged in user. Use MW APIs to change preferences
if ( this.isNamed ) {
// Registered user. Use MW APIs to change preferences
new mw.Api().saveOption(
this.preferenceName,
JSON.stringify( this.preferences )
@@ -92,6 +90,10 @@
} ).fail( function () {
callback.call( self, false );
} );
} else {
// Anonymous user. Save preferences in local storage
mw.storage.setObject( this.preferenceName, this.preferences );
callback.call( this, true );
}
}
};