2014-02-26 02:12:47 +00:00
|
|
|
<?php
|
2018-11-10 21:06:01 +00:00
|
|
|
|
|
|
|
use Flow\Container;
|
|
|
|
use Flow\Conversion\Utils;
|
|
|
|
use Flow\Exception\FlowException;
|
|
|
|
use Flow\Model\PostRevision;
|
|
|
|
use Flow\Model\UUID;
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
/**
|
|
|
|
* API module to send Flow thanks notifications
|
|
|
|
*
|
|
|
|
* This API does not prevent sending thanks using post IDs that refer to topic
|
|
|
|
* titles, though Thank buttons are only shown for comments in the UI.
|
|
|
|
*
|
|
|
|
* @ingroup API
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ApiFlowThank extends ApiThank {
|
|
|
|
public function execute() {
|
|
|
|
$user = $this->getUser();
|
|
|
|
$this->dieOnBadUser( $user );
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
2014-04-15 04:28:10 +00:00
|
|
|
try {
|
|
|
|
$postId = UUID::create( $params['postid'] );
|
|
|
|
} catch ( FlowException $e ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = $this->getFlowData( $postId );
|
|
|
|
|
|
|
|
$recipient = $this->getRecipientFromPost( $data['post'] );
|
|
|
|
$this->dieOnBadRecipient( $user, $recipient );
|
|
|
|
|
2014-04-15 04:28:10 +00:00
|
|
|
if ( $this->userAlreadySentThanksForId( $user, $postId ) ) {
|
|
|
|
$this->markResultSuccess( $recipient->getName() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$rootPost = $data['root'];
|
|
|
|
$workflowId = $rootPost->getPostId();
|
2016-04-22 20:13:56 +00:00
|
|
|
$rawTopicTitleText = Utils::htmlToPlaintext(
|
|
|
|
Container::get( 'templating' )->getContent( $rootPost, 'topic-title-html' )
|
|
|
|
);
|
2014-02-26 02:12:47 +00:00
|
|
|
// Truncate the title text to prevent issues with database storage.
|
2018-06-16 00:24:32 +00:00
|
|
|
$topicTitleText = $this->getLanguage()->truncateForDatabase( $rawTopicTitleText, 200 );
|
2014-02-26 02:12:47 +00:00
|
|
|
$pageTitle = $this->getPageTitleFromRootPost( $rootPost );
|
2019-04-26 16:12:19 +00:00
|
|
|
$this->dieOnBlockedUser( $user, $pageTitle );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2016-06-16 16:12:38 +00:00
|
|
|
/** @var PostRevision $post */
|
|
|
|
$post = $data['post'];
|
|
|
|
$postText = Utils::htmlToPlaintext( $post->getContent() );
|
2018-06-16 00:24:32 +00:00
|
|
|
$postText = $this->getLanguage()->truncateForDatabase( $postText, 200 );
|
2016-06-16 16:12:38 +00:00
|
|
|
|
2016-07-28 15:15:37 +00:00
|
|
|
$topicTitle = $this->getTopicTitleFromRootPost( $rootPost );
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
$this->sendThanks(
|
|
|
|
$user,
|
|
|
|
$recipient,
|
|
|
|
$postId,
|
|
|
|
$workflowId,
|
|
|
|
$topicTitleText,
|
2016-06-16 16:12:38 +00:00
|
|
|
$pageTitle,
|
2016-07-28 15:15:37 +00:00
|
|
|
$postText,
|
|
|
|
$topicTitle
|
2014-02-26 02:12:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function userAlreadySentThanksForId( User $user, UUID $id ) {
|
|
|
|
return $user->getRequest()->getSessionData( "flow-thanked-{$id->getAlphadecimal()}" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UUID $postId UUID of the post to thank for
|
2014-08-17 19:42:49 +00:00
|
|
|
* @return array containing 'post' and 'root' as keys
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
|
|
|
private function getFlowData( UUID $postId ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
$rootPostLoader = Container::get( 'loader.root_post' );
|
2019-11-05 16:55:19 +00:00
|
|
|
'@phan-var \Flow\Repository\RootPostLoader $rootPostLoader';
|
2014-02-26 02:12:47 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$data = $rootPostLoader->getWithRoot( $postId );
|
|
|
|
} catch ( FlowException $e ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $data['post'] === null ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
2019-12-22 07:25:15 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
|
2014-02-26 02:12:47 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PostRevision $post
|
2014-08-17 19:42:49 +00:00
|
|
|
* @return User
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
|
|
|
private function getRecipientFromPost( PostRevision $post ) {
|
2014-04-15 04:28:10 +00:00
|
|
|
$recipient = User::newFromId( $post->getCreatorId() );
|
2014-02-26 02:12:47 +00:00
|
|
|
if ( !$recipient->loadFromId() ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
return $recipient;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PostRevision $rootPost
|
2014-08-17 19:42:49 +00:00
|
|
|
* @return Title
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
|
|
|
private function getPageTitleFromRootPost( PostRevision $rootPost ) {
|
|
|
|
$workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() );
|
2014-08-20 10:38:15 +00:00
|
|
|
return $workflow->getOwnerTitle();
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 15:15:37 +00:00
|
|
|
/**
|
|
|
|
* @param PostRevision $rootPost
|
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
private function getTopicTitleFromRootPost( PostRevision $rootPost ) {
|
|
|
|
$workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() );
|
|
|
|
return $workflow->getArticleTitle();
|
|
|
|
}
|
|
|
|
|
2014-02-26 02:12:47 +00:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param User $recipient
|
|
|
|
* @param UUID $postId
|
|
|
|
* @param UUID $workflowId
|
|
|
|
* @param string $topicTitleText
|
|
|
|
* @param Title $pageTitle
|
2016-06-16 16:12:38 +00:00
|
|
|
* @param string $postTextExcerpt
|
2016-07-28 15:15:37 +00:00
|
|
|
* @param Title $topicTitle
|
2016-06-16 16:12:38 +00:00
|
|
|
* @throws FlowException
|
|
|
|
* @throws MWException
|
2014-02-26 02:12:47 +00:00
|
|
|
*/
|
2017-06-20 07:23:13 +00:00
|
|
|
private function sendThanks(
|
|
|
|
User $user,
|
|
|
|
User $recipient,
|
|
|
|
UUID $postId,
|
|
|
|
UUID $workflowId,
|
|
|
|
$topicTitleText,
|
|
|
|
Title $pageTitle,
|
|
|
|
$postTextExcerpt,
|
|
|
|
Title $topicTitle
|
|
|
|
) {
|
2015-06-16 00:53:40 +00:00
|
|
|
$uniqueId = "flow-{$postId->getAlphadecimal()}";
|
|
|
|
// Do one last check to make sure we haven't sent Thanks before
|
|
|
|
if ( $this->haveAlreadyThanked( $user, $uniqueId ) ) {
|
|
|
|
// Pretend the thanks were sent
|
|
|
|
$this->markResultSuccess( $recipient->getName() );
|
|
|
|
return;
|
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
|
|
|
|
// Create the notification via Echo extension
|
2016-04-22 20:13:56 +00:00
|
|
|
EchoEvent::create( [
|
2014-02-26 02:12:47 +00:00
|
|
|
'type' => 'flow-thank',
|
|
|
|
'title' => $pageTitle,
|
2016-04-22 20:13:56 +00:00
|
|
|
'extra' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
'post-id' => $postId->getAlphadecimal(),
|
|
|
|
'workflow' => $workflowId->getAlphadecimal(),
|
|
|
|
'thanked-user-id' => $recipient->getId(),
|
|
|
|
'topic-title' => $topicTitleText,
|
2016-06-16 16:12:38 +00:00
|
|
|
'excerpt' => $postTextExcerpt,
|
2016-07-28 15:15:37 +00:00
|
|
|
'target-page' => $topicTitle->getArticleID(),
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
2014-02-26 02:12:47 +00:00
|
|
|
'agent' => $user,
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-02-26 02:12:47 +00:00
|
|
|
|
2015-06-16 00:53:40 +00:00
|
|
|
// And mark the thank in session for a cheaper check to prevent duplicates (Bug 46690).
|
2014-02-26 02:12:47 +00:00
|
|
|
$user->getRequest()->setSessionData( "flow-thanked-{$postId->getAlphadecimal()}", true );
|
|
|
|
// Set success message.
|
2014-04-15 04:28:10 +00:00
|
|
|
$this->markResultSuccess( $recipient->getName() );
|
2015-06-16 00:53:40 +00:00
|
|
|
$this->logThanks( $user, $recipient, $uniqueId );
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
|
|
|
'postid' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2014-02-26 02:12:47 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
];
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 18:58:27 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2017-09-24 19:54:35 +00:00
|
|
|
* @return array
|
2014-10-29 18:58:27 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2014-10-29 18:58:27 +00:00
|
|
|
'action=flowthank&postid=xyz789&token=123ABC'
|
|
|
|
=> 'apihelp-flowthank-example-1',
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-10-29 18:58:27 +00:00
|
|
|
}
|
2014-02-26 02:12:47 +00:00
|
|
|
}
|