2019-12-10 16:18:24 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWUsernameCompletionAction class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
|
|
|
|
*/
|
|
|
|
|
2020-09-21 20:13:35 +00:00
|
|
|
var openCommand, insertAndOpenCommand, sequence,
|
|
|
|
controller = require( 'ext.discussionTools.init' ).controller;
|
2020-10-23 11:02:18 +00:00
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
/**
|
|
|
|
* MWUsernameCompletionAction action.
|
|
|
|
*
|
|
|
|
* Controls autocompletion of usernames
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.CompletionAction
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} surface Surface to act on
|
|
|
|
*/
|
|
|
|
function MWUsernameCompletionAction( surface ) {
|
2021-02-09 21:51:28 +00:00
|
|
|
var action = this,
|
|
|
|
relevantUserName = mw.config.get( 'wgRelevantUserName' );
|
2020-05-06 17:52:23 +00:00
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
// Parent constructor
|
|
|
|
MWUsernameCompletionAction.super.call( this, surface );
|
|
|
|
|
2020-10-23 11:02:18 +00:00
|
|
|
// Shared API object so previous requests can be aborted
|
|
|
|
this.api = controller.getApi();
|
2019-12-10 16:18:24 +00:00
|
|
|
this.searchedPrefixes = {};
|
2020-05-06 17:52:23 +00:00
|
|
|
this.localUsers = [];
|
|
|
|
this.ipUsers = [];
|
|
|
|
this.surface.authors.forEach( function ( user ) {
|
|
|
|
if ( mw.util.isIPAddress( user ) ) {
|
|
|
|
action.ipUsers.push( user );
|
2020-11-16 22:48:21 +00:00
|
|
|
} else if ( user !== mw.user.getName() ) {
|
2020-05-06 17:52:23 +00:00
|
|
|
action.localUsers.push( user );
|
|
|
|
}
|
|
|
|
} );
|
2021-03-04 20:59:00 +00:00
|
|
|
if (
|
|
|
|
relevantUserName &&
|
|
|
|
relevantUserName !== mw.user.getName() &&
|
|
|
|
this.localUsers.indexOf( relevantUserName ) === -1
|
|
|
|
) {
|
2021-02-09 21:51:28 +00:00
|
|
|
this.localUsers.push( relevantUserName );
|
|
|
|
this.localUsers.sort();
|
|
|
|
}
|
2019-12-10 16:18:24 +00:00
|
|
|
this.remoteUsers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( MWUsernameCompletionAction, ve.ui.CompletionAction );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
MWUsernameCompletionAction.static.name = 'mwUsernameCompletion';
|
|
|
|
|
2020-06-11 14:56:17 +00:00
|
|
|
MWUsernameCompletionAction.static.methods = OO.copy( MWUsernameCompletionAction.static.methods );
|
|
|
|
MWUsernameCompletionAction.static.methods.push( 'insertAndOpen' );
|
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2020-06-11 14:56:17 +00:00
|
|
|
MWUsernameCompletionAction.prototype.insertAndOpen = function () {
|
2020-06-23 15:23:13 +00:00
|
|
|
// This is opening a window in a slightly weird way, so the normal logging
|
|
|
|
// doesn't catch it. This assumes that the only way to get here is from
|
|
|
|
// the tool. If we add other paths, we'd need to change the logging.
|
|
|
|
ve.track(
|
|
|
|
'activity.' + this.constructor.static.name,
|
|
|
|
{ action: 'window-open-from-tool' }
|
|
|
|
);
|
2020-06-11 14:56:17 +00:00
|
|
|
this.surface.getModel().getFragment().insertContent( '@' ).collapseToEnd().select();
|
2020-06-22 11:22:08 +00:00
|
|
|
return this.open();
|
2020-06-11 14:56:17 +00:00
|
|
|
};
|
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
MWUsernameCompletionAction.prototype.getSuggestions = function ( input ) {
|
2020-07-02 14:12:30 +00:00
|
|
|
var apiPromise,
|
|
|
|
capitalizedInput = input.length > 0 && input[ 0 ].toUpperCase() + input.slice( 1 ),
|
2020-08-16 19:46:52 +00:00
|
|
|
title = mw.Title.makeTitle( mw.config.get( 'wgNamespaceIds' ).user, input ),
|
|
|
|
validatedInput = title ? input : '',
|
2019-12-10 16:18:24 +00:00
|
|
|
action = this;
|
|
|
|
|
|
|
|
this.api.abort(); // Abort all unfinished API requests
|
|
|
|
if ( capitalizedInput && !this.searchedPrefixes[ capitalizedInput ] ) {
|
2020-07-02 14:12:30 +00:00
|
|
|
apiPromise = this.api.get( {
|
2019-12-10 16:18:24 +00:00
|
|
|
action: 'query',
|
|
|
|
list: 'allusers',
|
|
|
|
// Prefix of list=allusers is case sensitive, and users are stored in the DB capitalized, so:
|
|
|
|
auprefix: capitalizedInput,
|
|
|
|
aulimit: this.limit
|
2020-07-02 14:12:30 +00:00
|
|
|
} ).then( function ( response ) {
|
2019-12-10 16:18:24 +00:00
|
|
|
var suggestions = response.query.allusers.map( function ( user ) {
|
|
|
|
return user.name;
|
|
|
|
} ).filter( function ( username ) {
|
2020-05-06 17:52:23 +00:00
|
|
|
// API doesn't return IPs
|
|
|
|
return action.localUsers.indexOf( username ) === -1 &&
|
2019-12-10 16:18:24 +00:00
|
|
|
action.remoteUsers.indexOf( username ) === -1;
|
|
|
|
} );
|
|
|
|
|
|
|
|
action.remoteUsers.push.apply( action.remoteUsers, suggestions );
|
|
|
|
action.remoteUsers.sort();
|
|
|
|
|
|
|
|
action.searchedPrefixes[ capitalizedInput ] = true;
|
|
|
|
} );
|
2020-07-02 14:12:30 +00:00
|
|
|
} else {
|
|
|
|
apiPromise = ve.createDeferred().resolve().promise();
|
2019-12-10 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 14:12:30 +00:00
|
|
|
return apiPromise.then( function () {
|
2019-12-10 16:18:24 +00:00
|
|
|
// By concatenating on-thread authors and remote-fetched authors, both
|
|
|
|
// sorted alphabetically, we'll get our suggestion popup sorted so all
|
|
|
|
// on-thread matches come first.
|
2020-07-02 14:12:30 +00:00
|
|
|
return action.filterSuggestionsForInput(
|
|
|
|
action.localUsers
|
2020-05-11 18:35:48 +00:00
|
|
|
// Show no remote users if no input provided
|
2020-07-02 14:12:30 +00:00
|
|
|
.concat( capitalizedInput ? action.remoteUsers : [] ),
|
2020-05-06 18:15:19 +00:00
|
|
|
// TODO: Consider showing IP users
|
2020-05-12 11:51:20 +00:00
|
|
|
// * Change link to Special:Contributions/<ip> (localised)
|
2020-05-06 18:15:19 +00:00
|
|
|
// * Let users know that mentioning an IP will not create a notification?
|
|
|
|
// .concat( this.ipUsers )
|
2020-08-16 19:46:52 +00:00
|
|
|
validatedInput
|
2020-07-02 14:12:30 +00:00
|
|
|
);
|
|
|
|
} );
|
2019-12-10 16:18:24 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 17:20:23 +00:00
|
|
|
MWUsernameCompletionAction.prototype.getHeaderLabel = function ( input, suggestions ) {
|
|
|
|
var $query;
|
|
|
|
if ( suggestions === undefined ) {
|
|
|
|
$query = $( '<span>' ).text( input );
|
|
|
|
return mw.message( 'discussiontools-replywidget-mention-tool-header', $query ).parseDom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
MWUsernameCompletionAction.prototype.insertCompletion = function ( word, range ) {
|
2020-05-20 11:28:31 +00:00
|
|
|
var fragment,
|
2020-08-01 12:01:37 +00:00
|
|
|
prefix = mw.msg( 'discussiontools-replywidget-mention-prefix' ),
|
2020-06-26 16:23:07 +00:00
|
|
|
title = mw.Title.newFromText( word, mw.config.get( 'wgNamespaceIds' ).user );
|
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
if ( this.surface.getMode() === 'source' ) {
|
|
|
|
// TODO: this should be configurable per-wiki so that e.g. custom templates can be used
|
2020-05-20 11:28:31 +00:00
|
|
|
word = prefix + '[[' + title.getPrefixedText() + '|' + word + ']]';
|
2019-12-10 16:18:24 +00:00
|
|
|
return MWUsernameCompletionAction.super.prototype.insertCompletion.call( this, word, range );
|
|
|
|
}
|
2020-06-26 16:23:07 +00:00
|
|
|
|
|
|
|
fragment = this.surface.getModel().getLinearFragment( range );
|
|
|
|
fragment.removeContent().insertContent( [
|
|
|
|
{ type: 'mwPing', attributes: { user: word } },
|
|
|
|
{ type: '/mwPing' }
|
|
|
|
] );
|
2020-05-20 11:28:31 +00:00
|
|
|
|
2020-06-29 13:57:07 +00:00
|
|
|
fragment.collapseToEnd();
|
2020-05-20 11:28:31 +00:00
|
|
|
|
|
|
|
return fragment;
|
2019-12-10 16:18:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MWUsernameCompletionAction.prototype.shouldAbandon = function ( input ) {
|
|
|
|
// TODO: need to consider whether pending loads from server are happening here
|
|
|
|
return MWUsernameCompletionAction.super.prototype.shouldAbandon.apply( this, arguments ) && input.split( /\s+/ ).length > 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.actionFactory.register( MWUsernameCompletionAction );
|
|
|
|
|
2020-09-21 20:13:35 +00:00
|
|
|
openCommand = new ve.ui.Command(
|
|
|
|
'openMWUsernameCompletions', MWUsernameCompletionAction.static.name, 'open',
|
|
|
|
{ supportedSelections: [ 'linear' ] }
|
2020-06-11 14:56:17 +00:00
|
|
|
);
|
2020-09-21 20:13:35 +00:00
|
|
|
insertAndOpenCommand = new ve.ui.Command(
|
|
|
|
'insertAndOpenMWUsernameCompletions', MWUsernameCompletionAction.static.name, 'insertAndOpen',
|
|
|
|
{ supportedSelections: [ 'linear' ] }
|
2019-12-10 16:18:24 +00:00
|
|
|
);
|
2020-09-21 20:13:35 +00:00
|
|
|
sequence = new ve.ui.Sequence( 'autocompleteMWUsernames', 'openMWUsernameCompletions', '@', 0 );
|
|
|
|
ve.ui.commandRegistry.register( openCommand );
|
|
|
|
ve.ui.commandRegistry.register( insertAndOpenCommand );
|
|
|
|
ve.ui.wikitextCommandRegistry.register( openCommand );
|
|
|
|
ve.ui.wikitextCommandRegistry.register( insertAndOpenCommand );
|
|
|
|
ve.ui.sequenceRegistry.register( sequence );
|
|
|
|
ve.ui.wikitextSequenceRegistry.register( sequence );
|
2019-12-10 16:18:24 +00:00
|
|
|
|
|
|
|
module.exports = MWUsernameCompletionAction;
|