Debian Codegraph @

extensions/Thanks/includes/Api/ApiFlowThank.php

raw symbols SCIP
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 * API module to send Flow thanks notifications
22 *
23 * This API does not prevent sending thanks using post IDs that refer to topic
24 * titles, though Thank buttons are only shown for comments in the UI.
25 *
26 * @ingroup API
27 * @ingroup Extensions
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 // Truncate the title text to prevent issues with database storage.
74 $topicTitleText = $this->getLanguage()->truncateForDatabase( $rawTopicTitleText, 200 );
75 $pageTitle = $this->getPageTitleFromRootPost( $rootPost );
76 $this->dieOnUserBlockedFromTitle( $user, $pageTitle );
77
78 /** @var PostRevision $post */
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 * @param UUID $postId UUID of the post to thank for
103 * @return array containing 'post' and 'root' as keys
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 // @phan-suppress-next-line PhanTypeMismatchReturnNullable T240141
119 return $data;
120 }
121
122 /**
123 * @param PostRevision $post
124 * @return User
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 * @param PostRevision $rootPost
136 * @return Title
137 */
138 private function getPageTitleFromRootPost( PostRevision $rootPost ) {
139 $workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() );
140 return $workflow->getOwnerTitle();
141 }
142
143 /**
144 * @param PostRevision $rootPost
145 * @return Title
146 */
147 private function getTopicTitleFromRootPost( PostRevision $rootPost ) {
148 $workflow = Container::get( 'storage' )->get( 'Workflow', $rootPost->getPostId() );
149 return $workflow->getArticleTitle();
150 }
151
152 /**
153 * @param User $user
154 * @param User $recipient
155 * @param UUID $postId
156 * @param UUID $workflowId
157 * @param string $topicTitleText
158 * @param Title $pageTitle
159 * @param string $postTextExcerpt
160 * @param Title $topicTitle
161 * @throws FlowException
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 // Do one last check to make sure we haven't sent Thanks before
175 if ( $this->haveAlreadyThanked( $user, $uniqueId ) ) {
176 // Pretend the thanks were sent
177 $this->markResultSuccess( $recipient->getName() );
178 return;
179 }
180
181 // Create the notification via Echo extension
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 // And mark the thank in session for a cheaper check to prevent duplicates (T48690).
197 $user->getRequest()->setSessionData( "flow-thanked-{$postId->getAlphadecimal()}", true );
198 // Set success message.
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 * @see ApiBase::getExamplesMessages()
218 * @return array
219 */
220 protected function getExamplesMessages() {
221 return [
222 'action=flowthank&postid=xyz789&token=123ABC'
223 => 'apihelp-flowthank-example-1',
224 ];
225 }
226 }