Merge "Acquire a temporary user username before previewing"

This commit is contained in:
jenkins-bot 2023-08-30 13:15:05 +00:00 committed by Gerrit Code Review
commit a1f2185a0f
5 changed files with 67 additions and 10 deletions

View file

@ -363,6 +363,8 @@
"VisualEditor.ParsoidClientFactory", "VisualEditor.ParsoidClientFactory",
"DiscussionTools.CommentParser", "DiscussionTools.CommentParser",
"DiscussionTools.SubscriptionStore", "DiscussionTools.SubscriptionStore",
"TempUserCreator",
"UserFactory",
"SkinFactory", "SkinFactory",
"ConfigFactory", "ConfigFactory",
"RevisionLookup" "RevisionLookup"
@ -395,6 +397,8 @@
"services": [ "services": [
"VisualEditor.ParsoidClientFactory", "VisualEditor.ParsoidClientFactory",
"DiscussionTools.CommentParser", "DiscussionTools.CommentParser",
"TempUserCreator",
"UserFactory",
"SkinFactory" "SkinFactory"
] ]
}, },

View file

@ -16,6 +16,8 @@ use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Request\DerivativeRequest; use MediaWiki\Request\DerivativeRequest;
use MediaWiki\Revision\RevisionLookup; use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWiki\User\UserFactory;
use SkinFactory; use SkinFactory;
use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\StringDef; use Wikimedia\ParamValidator\TypeDef\StringDef;
@ -29,6 +31,8 @@ class ApiDiscussionToolsEdit extends ApiBase {
private CommentParser $commentParser; private CommentParser $commentParser;
private VisualEditorParsoidClientFactory $parsoidClientFactory; private VisualEditorParsoidClientFactory $parsoidClientFactory;
private SubscriptionStore $subscriptionStore; private SubscriptionStore $subscriptionStore;
private TempUserCreator $tempUserCreator;
private UserFactory $userFactory;
private SkinFactory $skinFactory; private SkinFactory $skinFactory;
private Config $config; private Config $config;
private RevisionLookup $revisionLookup; private RevisionLookup $revisionLookup;
@ -39,6 +43,8 @@ class ApiDiscussionToolsEdit extends ApiBase {
VisualEditorParsoidClientFactory $parsoidClientFactory, VisualEditorParsoidClientFactory $parsoidClientFactory,
CommentParser $commentParser, CommentParser $commentParser,
SubscriptionStore $subscriptionStore, SubscriptionStore $subscriptionStore,
TempUserCreator $tempUserCreator,
UserFactory $userFactory,
SkinFactory $skinFactory, SkinFactory $skinFactory,
ConfigFactory $configFactory, ConfigFactory $configFactory,
RevisionLookup $revisionLookup RevisionLookup $revisionLookup
@ -47,6 +53,8 @@ class ApiDiscussionToolsEdit extends ApiBase {
$this->parsoidClientFactory = $parsoidClientFactory; $this->parsoidClientFactory = $parsoidClientFactory;
$this->commentParser = $commentParser; $this->commentParser = $commentParser;
$this->subscriptionStore = $subscriptionStore; $this->subscriptionStore = $subscriptionStore;
$this->tempUserCreator = $tempUserCreator;
$this->userFactory = $userFactory;
$this->skinFactory = $skinFactory; $this->skinFactory = $skinFactory;
$this->config = $configFactory->makeConfig( 'discussiontools' ); $this->config = $configFactory->makeConfig( 'discussiontools' );
$this->revisionLookup = $revisionLookup; $this->revisionLookup = $revisionLookup;
@ -107,7 +115,7 @@ class ApiDiscussionToolsEdit extends ApiBase {
$previewContainer = DOMCompat::getBody( DOMUtils::parseHTML( $previewResultHtml ) ); $previewContainer = DOMCompat::getBody( DOMUtils::parseHTML( $previewResultHtml ) );
$previewThreadItemSet = $this->commentParser->parse( $previewContainer, $title->getTitleValue() ); $previewThreadItemSet = $this->commentParser->parse( $previewContainer, $title->getTitleValue() );
if ( CommentUtils::isSingleCommentSignedBy( if ( CommentUtils::isSingleCommentSignedBy(
$previewThreadItemSet, $this->getUser()->getName(), $previewContainer $previewThreadItemSet, $this->getUserForPreview()->getName(), $previewContainer
) ) { ) ) {
$signature = null; $signature = null;
} else { } else {

View file

@ -8,6 +8,8 @@ use ApiUsageException;
use MediaWiki\Extension\VisualEditor\ApiParsoidTrait; use MediaWiki\Extension\VisualEditor\ApiParsoidTrait;
use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory; use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWiki\User\UserFactory;
use SkinFactory; use SkinFactory;
use Wikimedia\ParamValidator\ParamValidator; use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
@ -19,6 +21,8 @@ class ApiDiscussionToolsPreview extends ApiBase {
private CommentParser $commentParser; private CommentParser $commentParser;
private VisualEditorParsoidClientFactory $parsoidClientFactory; private VisualEditorParsoidClientFactory $parsoidClientFactory;
private TempUserCreator $tempUserCreator;
private UserFactory $userFactory;
private SkinFactory $skinFactory; private SkinFactory $skinFactory;
public function __construct( public function __construct(
@ -26,11 +30,15 @@ class ApiDiscussionToolsPreview extends ApiBase {
string $name, string $name,
VisualEditorParsoidClientFactory $parsoidClientFactory, VisualEditorParsoidClientFactory $parsoidClientFactory,
CommentParser $commentParser, CommentParser $commentParser,
TempUserCreator $tempUserCreator,
UserFactory $userFactory,
SkinFactory $skinFactory SkinFactory $skinFactory
) { ) {
parent::__construct( $main, $name ); parent::__construct( $main, $name );
$this->parsoidClientFactory = $parsoidClientFactory; $this->parsoidClientFactory = $parsoidClientFactory;
$this->commentParser = $commentParser; $this->commentParser = $commentParser;
$this->tempUserCreator = $tempUserCreator;
$this->userFactory = $userFactory;
$this->skinFactory = $skinFactory; $this->skinFactory = $skinFactory;
} }
@ -64,7 +72,9 @@ class ApiDiscussionToolsPreview extends ApiBase {
// Check if there was a signature in a proper place // Check if there was a signature in a proper place
$container = DOMCompat::getBody( DOMUtils::parseHTML( $resultHtml ) ); $container = DOMCompat::getBody( DOMUtils::parseHTML( $resultHtml ) );
$threadItemSet = $this->commentParser->parse( $container, $title->getTitleValue() ); $threadItemSet = $this->commentParser->parse( $container, $title->getTitleValue() );
if ( !CommentUtils::isSingleCommentSignedBy( $threadItemSet, $this->getUser()->getName(), $container ) ) { if ( !CommentUtils::isSingleCommentSignedBy(
$threadItemSet, $this->getUserForPreview()->getName(), $container
) ) {
// If not, add the signature and re-render // If not, add the signature and re-render
$signature = $this->msg( 'discussiontools-signature-prefix' )->inContentLanguage()->text() . '~~~~'; $signature = $this->msg( 'discussiontools-signature-prefix' )->inContentLanguage()->text() . '~~~~';
// Drop opacity of signature in preview to make message body preview clearer. // Drop opacity of signature in preview to make message body preview clearer.

View file

@ -11,6 +11,10 @@ use MediaWiki\Extension\VisualEditor\VisualEditorParsoidClientFactory;
use MediaWiki\Request\DerivativeRequest; use MediaWiki\Request\DerivativeRequest;
use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MediaWiki\User\TempUser\TempUserCreator;
use MediaWiki\User\UserFactory;
use User;
use WebRequest;
use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMCompat;
use Wikimedia\Parsoid\Utils\DOMUtils; use Wikimedia\Parsoid\Utils\DOMUtils;
@ -19,6 +23,8 @@ use Wikimedia\Parsoid\Utils\DOMUtils;
* *
* @property VisualEditorParsoidClientFactory $parsoidClientFactory * @property VisualEditorParsoidClientFactory $parsoidClientFactory
* @property CommentParser $commentParser * @property CommentParser $commentParser
* @property TempUserCreator $tempUserCreator
* @property UserFactory $userFactory
*/ */
trait ApiDiscussionToolsTrait { trait ApiDiscussionToolsTrait {
@ -118,6 +124,20 @@ trait ApiDiscussionToolsTrait {
return $api->getResult(); return $api->getResult();
} }
/**
* @see ApiParse::getUserForPreview
* @return User
*/
private function getUserForPreview() {
$user = $this->getUser();
if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit' ) ) {
return $this->userFactory->newUnsavedTempUser(
$this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
);
}
return $user;
}
/** /**
* @see VisualEditorParsoidClientFactory * @see VisualEditorParsoidClientFactory
* @return ParsoidClient * @return ParsoidClient
@ -153,4 +173,14 @@ trait ApiDiscussionToolsTrait {
*/ */
abstract public function getContext(); abstract public function getContext();
/**
* @return User
*/
abstract public function getUser();
/**
* @return WebRequest
*/
abstract public function getRequest();
} }

View file

@ -799,14 +799,19 @@ ReplyWidget.prototype.preparePreview = function ( wikitext ) {
if ( !wikitext ) { if ( !wikitext ) {
parsePromise = $.Deferred().resolve( null ).promise(); parsePromise = $.Deferred().resolve( null ).promise();
} else { } else {
this.previewRequest = parsePromise = controller.getApi().post( { // Acquire a temporary user username before previewing, so that signatures and
action: 'discussiontoolspreview', // user-related magic words display the temp user instead of IP user in the preview. (T331397)
type: this.isNewTopic ? 'topic' : 'reply', parsePromise = mw.user.acquireTempUserName().then( function () {
page: this.pageName, widget.previewRequest = controller.getApi().post( {
wikitext: wikitext, action: 'discussiontoolspreview',
sectiontitle: title, type: widget.isNewTopic ? 'topic' : 'reply',
useskin: mw.config.get( 'skin' ), page: widget.pageName,
mobileformat: OO.ui.isMobile() wikitext: wikitext,
sectiontitle: title,
useskin: mw.config.get( 'skin' ),
mobileformat: OO.ui.isMobile()
} );
return widget.previewRequest;
} ); } );
} }