| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Thanks\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Api\ApiMain; |
| 7 |
use MediaWiki\Extension\Thanks\Storage\LogStore; |
| 8 |
use MediaWiki\Permissions\PermissionManager; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\User\User; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
abstract class ApiThank extends ApiBase { |
| 19 |
|
| 20 |
protected PermissionManager $permissionManager; |
| 21 |
protected LogStore $storage; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
ApiMain $main, |
| 25 |
$action, |
| 26 |
PermissionManager $permissionManager, |
| 27 |
LogStore $storage |
| 28 |
) { |
| 29 |
parent::__construct( $main, $action ); |
| 30 |
$this->permissionManager = $permissionManager; |
| 31 |
$this->storage = $storage; |
| 32 |
} |
| 33 |
|
| 34 |
protected function dieOnBadUser( User $user ) { |
| 35 |
if ( !$user->isNamed() ) { |
| 36 |
$this->dieWithError( 'thanks-error-notloggedin', 'notloggedin' ); |
| 37 |
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) { |
| 38 |
$this->dieWithError( [ 'thanks-error-ratelimited', $user->getName() ], 'ratelimited' ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
protected function dieOnUserBlockedFromTitle( User $user, Title $title ) { |
| 53 |
if ( $this->permissionManager->isBlockedFrom( $user, $title ) ) { |
| 54 |
|
| 55 |
|
| 56 |
$this->dieBlocked( $user->getBlock() ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected function dieOnUserBlockedFromThanks( User $user ) { |
| 69 |
$block = $user->getBlock(); |
| 70 |
if ( |
| 71 |
$block && |
| 72 |
( $block->isSitewide() || $block->appliesToRight( 'thanks' ) ) |
| 73 |
) { |
| 74 |
$this->dieBlocked( $block ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
protected function dieOnBadRecipient( User $user, User $recipient ) { |
| 79 |
if ( $user->getId() === $recipient->getId() ) { |
| 80 |
$this->dieWithError( 'thanks-error-invalidrecipient-self', 'invalidrecipient' ); |
| 81 |
} elseif ( !$this->getConfig()->get( 'ThanksSendToBots' ) && $recipient->isBot() ) { |
| 82 |
$this->dieWithError( 'thanks-error-invalidrecipient-bot', 'invalidrecipient' ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
protected function markResultSuccess( $recipientName ) { |
| 87 |
$this->getResult()->addValue( null, 'result', [ |
| 88 |
'success' => 1, |
| 89 |
'recipient' => $recipientName, |
| 90 |
] ); |
| 91 |
} |
| 92 |
|
| 93 |
protected function haveAlreadyThanked( User $thanker, $uniqueId ) { |
| 94 |
return $this->storage->haveThanked( $thanker, $uniqueId ); |
| 95 |
} |
| 96 |
|
| 97 |
protected function logThanks( User $user, User $recipient, $uniqueId ) { |
| 98 |
$this->storage->thank( $user, $recipient, $uniqueId ); |
| 99 |
} |
| 100 |
|
| 101 |
public function needsToken() { |
| 102 |
return 'csrf'; |
| 103 |
} |
| 104 |
|
| 105 |
public function isWriteMode() { |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
} |