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
|
|
|
|
*/
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var sequence,
|
2020-09-21 20:13:35 +00:00
|
|
|
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 () {
|
2021-03-24 11:30:11 +00:00
|
|
|
var inserted = false,
|
2021-03-19 15:48:23 +00:00
|
|
|
surfaceModel = this.surface.getModel(),
|
2021-03-24 11:30:11 +00:00
|
|
|
fragment = surfaceModel.getFragment();
|
2021-03-19 15:48:23 +00:00
|
|
|
|
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' }
|
|
|
|
);
|
2021-03-19 15:48:23 +00:00
|
|
|
|
|
|
|
// Run the sequence matching logic again to check
|
|
|
|
// if we already have the sequence inserted at the
|
|
|
|
// current offset.
|
2021-03-24 11:30:11 +00:00
|
|
|
if ( fragment.getSelection().isCollapsed() ) {
|
|
|
|
inserted = this.surface.getView().findMatchingSequences().some( function ( item ) {
|
2021-03-19 15:48:23 +00:00
|
|
|
return item.sequence === sequence;
|
2021-03-24 11:30:11 +00:00
|
|
|
} );
|
2021-03-19 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !inserted ) {
|
|
|
|
fragment.insertContent( '@' );
|
|
|
|
}
|
|
|
|
fragment.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 ) {
|
2021-09-20 07:28:34 +00:00
|
|
|
var title = mw.Title.makeTitle( mw.config.get( 'wgNamespaceIds' ).user, input ),
|
2020-08-16 19:46:52 +00:00
|
|
|
validatedInput = title ? input : '',
|
2019-12-10 16:18:24 +00:00
|
|
|
action = this;
|
|
|
|
|
|
|
|
this.api.abort(); // Abort all unfinished API requests
|
2021-04-08 13:46:09 +00:00
|
|
|
var apiPromise;
|
2021-09-20 07:28:34 +00:00
|
|
|
if ( input.length > 0 && !this.searchedPrefixes[ input ] ) {
|
2020-07-02 14:12:30 +00:00
|
|
|
apiPromise = this.api.get( {
|
2019-12-10 16:18:24 +00:00
|
|
|
action: 'query',
|
|
|
|
list: 'allusers',
|
2021-09-20 07:28:34 +00:00
|
|
|
auprefix: input,
|
2021-11-01 14:56:16 +00:00
|
|
|
auprop: 'blockinfo',
|
2021-12-22 23:57:15 +00:00
|
|
|
auwitheditsonly: 1,
|
2021-11-01 15:29:39 +00:00
|
|
|
// Fetch twice as many results as we need so we can filter
|
|
|
|
// blocked users and still probably have some suggestions left
|
|
|
|
aulimit: this.constructor.static.defaultLimit * 2
|
2020-07-02 14:12:30 +00:00
|
|
|
} ).then( function ( response ) {
|
2021-11-01 14:56:16 +00:00
|
|
|
var suggestions = response.query.allusers.filter( function ( user ) {
|
2020-05-06 17:52:23 +00:00
|
|
|
// API doesn't return IPs
|
2021-11-01 14:56:16 +00:00
|
|
|
return action.localUsers.indexOf( user.name ) === -1 &&
|
|
|
|
action.remoteUsers.indexOf( user.name ) === -1 &&
|
|
|
|
// Exclude users with indefinite sitewide blocks:
|
|
|
|
// The only place such users could reply is on their
|
|
|
|
// own user talk page, and in that case the user
|
|
|
|
// will be included in localUsers.
|
|
|
|
!( user.blockexpiry === 'infinite' && !user.blockpartial );
|
|
|
|
} ).map( function ( user ) {
|
|
|
|
return user.name;
|
2019-12-10 16:18:24 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
action.remoteUsers.push.apply( action.remoteUsers, suggestions );
|
|
|
|
action.remoteUsers.sort();
|
|
|
|
|
2021-09-20 07:28:34 +00:00
|
|
|
action.searchedPrefixes[ input ] = true;
|
2019-12-10 16:18:24 +00:00
|
|
|
} );
|
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
|
2021-09-20 07:28:34 +00:00
|
|
|
.concat( input.length > 0 ? 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 ) {
|
|
|
|
if ( suggestions === undefined ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var $query = $( '<span>' ).text( input );
|
2020-07-08 17:20:23 +00:00
|
|
|
return mw.message( 'discussiontools-replywidget-mention-tool-header', $query ).parseDom();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-10 16:18:24 +00:00
|
|
|
MWUsernameCompletionAction.prototype.insertCompletion = function ( word, range ) {
|
2021-04-08 13:46:09 +00:00
|
|
|
var prefix = mw.msg( 'discussiontools-replywidget-mention-prefix' ),
|
2022-01-19 16:29:10 +00:00
|
|
|
suffix = mw.msg( 'discussiontools-replywidget-mention-suffix' ),
|
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
|
2022-01-19 16:29:10 +00:00
|
|
|
word = prefix + '[[' + title.getPrefixedText() + '|' + word + ']]' + suffix;
|
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
|
|
|
|
2021-11-02 23:33:19 +00:00
|
|
|
var fragment = this.surface.getModel().getLinearFragment( range, true );
|
2020-06-26 16:23:07 +00:00
|
|
|
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 );
|
|
|
|
|
2021-04-08 13:46:09 +00:00
|
|
|
var openCommand = new ve.ui.Command(
|
2020-09-21 20:13:35 +00:00
|
|
|
'openMWUsernameCompletions', MWUsernameCompletionAction.static.name, 'open',
|
|
|
|
{ supportedSelections: [ 'linear' ] }
|
2020-06-11 14:56:17 +00:00
|
|
|
);
|
2021-04-08 13:46:09 +00:00
|
|
|
var insertAndOpenCommand = new ve.ui.Command(
|
2020-09-21 20:13:35 +00:00
|
|
|
'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;
|