| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Thanks\Api; |
| 4 |
|
| 5 |
use Flow\Container; |
| 6 |
use Flow\Conversion\Utils; |
| 7 |
use Flow\Exception\FlowException; |
| 8 |
use Flow\Model\PostRevision; |
| 9 |
use Flow\Model\UUID; |
| 10 |
use MediaWiki\Api\ApiBase; |
| 11 |
use MediaWiki\Api\ApiMain; |
| 12 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 13 |
use MediaWiki\Extension\Thanks\Storage\LogStore; |
| 14 |
use MediaWiki\Permissions\PermissionManager; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWiki\User\User; |
| 17 |
use MediaWiki\User\UserFactory; |
| 18 |
use Wikimedia\ParamValidator\ParamValidator; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class ApiFlowThank extends ApiThank { |
| 31 |
|
| 32 |
private UserFactory $userFactory; |
| 33 |
|
| 34 |
public function __construct( |
| 35 |
ApiMain $main, |
| 36 |
$action, |
| 37 |
PermissionManager $permissionManager, |
| 38 |
LogStore $storage, |
| 39 |
UserFactory $userFactory |
| 40 |
) { |
| 41 |
parent::__construct( $main, $action, $permissionManager, $storage ); |
| 42 |
$this->userFactory = $userFactory; |
| 43 |
} |
| 44 |
|
| 45 |
public function execute() { |
| 46 |
$user = $this->getUser(); |
| 47 |
$this->dieOnBadUser( $user ); |
| 48 |
$this->dieOnUserBlockedFromThanks( $user ); |
| 49 |
|
| 50 |
$params = $this->extractRequestParams(); |
| 51 |
|
| 52 |
try { |
| 53 |
$postId = UUID::create( $params['postid'] ); |
| 54 |
} catch ( FlowException $e ) { |
| 55 |
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' ); |
| 56 |
} |
| 57 |
|
| 58 |
$data = $this->getFlowData( $postId ); |
| 59 |
|
| 60 |
$recipient = $this->getRecipientFromPost( $data['post'] ); |
| 61 |
$this->dieOnBadRecipient( $user, $recipient ); |
| 62 |
|
| 63 |
if ( $this->userAlreadySentThanksForId( $user, $postId ) ) { |
| 64 |
$this->markResultSuccess( $recipient->getName() ); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$rootPost = $data['root']; |
| 69 |
$workflowId = $rootPost->getPostId(); |
| 70 |
$rawTopicTitleText = Utils::htmlToPlaintext( |
| 71 |
Container::get( 'templating' )->getContent( $rootPost, 'topic-title-html' ) |
| 72 |
); |
| 73 |
|
| 74 |
$topicTitleText = $this->getLanguage()->truncateForDatabase( $rawTopicTitleText, 200 ); |
| 75 |
$pageTitle = $this->getPageTitleFromRootPost( $rootPost ); |
| 76 |
$this->dieOnUserBlockedFromTitle( $user, $pageTitle ); |
| 77 |
|
| 78 |
|
| 79 |
$post = $data['post']; |
| 80 |
$postText = Utils::htmlToPlaintext( $post->getContent() ); |
| 81 |
$postText = $this->getLanguage()->truncateForDatabase( $postText, 200 ); |
| 82 |
|
| 83 |
$topicTitle = $this->getTopicTitleFromRootPost( $rootPost ); |
| 84 |
|
| 85 |
$this->sendThanks( |
| 86 |
$user, |
| 87 |
$recipient, |
| 88 |
$postId, |
| 89 |
$workflowId, |
| 90 |
$topicTitleText, |
| 91 |
$pageTitle, |
| 92 |
$postText, |
| 93 |
$topicTitle |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
private function userAlreadySentThanksForId( User $user, UUID $id ) { |
| 98 |
return $user->getRequest()->getSessionData( "flow-thanked-{$id->getAlphadecimal()}" ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private function getFlowData( UUID $postId ) { |
| 106 |
$rootPostLoader = Container::get( 'loader.root_post' ); |
| 107 |
'@phan-var \Flow\Repository\RootPostLoader $rootPostLoader'; |
| 108 |
|
| 109 |
try { |
| 110 |
$data = $rootPostLoader->getWithRoot( $postId ); |
| 111 |
} catch ( FlowException $e ) { |
| 112 |
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( $data['post'] === null ) { |
| 116 |
$this->dieWithError( 'thanks-error-invalidpostid', 'invalidpostid' ); |
| 117 |
} |
| 118 |
|
| 119 |
return $data; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private function getRecipientFromPost( PostRevision $post ) { |
| 127 |
$recipient = $this->userFactory->newFromId( $post->getCreatorId() ); |
| 128 |
if ( !$recipient->loadFromId() ) { |
| 129 |
$this->dieWithError( 'thanks-error-invalidrecipient', 'invalidrecipient' ); |
| 130 |
} |
| 131 |
return $recipient; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function getPageTitleFromRootPost( PostRevision $rootPost ) { |
| 139 |
$workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() ); |
| 140 |
return $workflow->getOwnerTitle(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
private function getTopicTitleFromRootPost( PostRevision $rootPost ) { |
| 148 |
$workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() ); |
| 149 |
return $workflow->getArticleTitle(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
private function sendThanks( |
| 164 |
User $user, |
| 165 |
User $recipient, |
| 166 |
UUID $postId, |
| 167 |
UUID $workflowId, |
| 168 |
$topicTitleText, |
| 169 |
Title $pageTitle, |
| 170 |
$postTextExcerpt, |
| 171 |
Title $topicTitle |
| 172 |
) { |
| 173 |
$uniqueId = 'flow-' . $postId->getAlphadecimal(); |
| 174 |
|
| 175 |
if ( $this->haveAlreadyThanked( $user, $uniqueId ) ) { |
| 176 |
|
| 177 |
$this->markResultSuccess( $recipient->getName() ); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
Event::create( [ |
| 183 |
'type' => 'flow-thank', |
| 184 |
'title' => $pageTitle, |
| 185 |
'extra' => [ |
| 186 |
'post-id' => $postId->getAlphadecimal(), |
| 187 |
'workflow' => $workflowId->getAlphadecimal(), |
| 188 |
'thanked-user-id' => $recipient->getId(), |
| 189 |
'topic-title' => $topicTitleText, |
| 190 |
'excerpt' => $postTextExcerpt, |
| 191 |
'target-page' => $topicTitle->getArticleID(), |
| 192 |
], |
| 193 |
'agent' => $user, |
| 194 |
] ); |
| 195 |
|
| 196 |
|
| 197 |
$user->getRequest()->setSessionData( "flow-thanked-{$postId->getAlphadecimal()}", true ); |
| 198 |
|
| 199 |
$this->markResultSuccess( $recipient->getName() ); |
| 200 |
$this->logThanks( $user, $recipient, $uniqueId ); |
| 201 |
} |
| 202 |
|
| 203 |
public function getAllowedParams() { |
| 204 |
return [ |
| 205 |
'postid' => [ |
| 206 |
ParamValidator::PARAM_TYPE => 'string', |
| 207 |
ParamValidator::PARAM_REQUIRED => true, |
| 208 |
], |
| 209 |
'token' => [ |
| 210 |
ParamValidator::PARAM_TYPE => 'string', |
| 211 |
ParamValidator::PARAM_REQUIRED => true, |
| 212 |
], |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
protected function getExamplesMessages() { |
| 221 |
return [ |
| 222 |
'action=flowthank&postid=xyz789&token=123ABC' |
| 223 |
=> 'apihelp-flowthank-example-1', |
| 224 |
]; |
| 225 |
} |
| 226 |
} |