mediawiki-extensions-Thanks/ApiThank.php
Kunal Mehta 956a476005 Use log_search to track already sent thanks
We currently use client-side storage to keep track of what revisions and
posts have already been thanked for. This is problematic because client-
side storage is not permanent, making it easy to send duplicate thanks
if you have logged out and in, or switched computers.

This starts storing new thanks in the log_search table, which is
designed for efficient querying of metadata associated with a specific
log entry. With this, we can easily check to see if a user has already
sent thanks for a specific revision.

The UX is a bit weird right now, we only check log_search when actually
sending thanks again, in which case the user thinks they have sent
thanks again, but no duplicate thanks is actually sent.

Bug: T88820
Bug: T53303
Change-Id: Iaf8cbe0776081dc69e82883d8727ba1cfe20e3e1
2015-06-16 11:52:34 -07:00

98 lines
2.6 KiB
PHP

<?php
/**
* Base API module for Thanks
*
* @ingroup API
* @ingroup Extensions
*/
abstract class ApiThank extends ApiBase {
protected function dieIfEchoNotInstalled() {
if ( !class_exists( 'EchoNotifier' ) ) {
$this->dieUsage( 'Echo is not installed on this wiki', 'echonotinstalled' );
}
}
protected function dieOnBadUser( User $user ) {
if ( $user->isAnon() ) {
$this->dieUsage( 'Anonymous users cannot send thanks', 'notloggedin' );
} elseif ( $user->pingLimiter( 'thanks-notification' ) ) {
$this->dieUsageMsg( array( 'actionthrottledtext' ) );
} elseif ( $user->isBlocked() ) {
$this->dieUsageMsg( array( 'blockedtext' ) );
}
}
protected function dieOnBadRecipient( User $user, User $recipient ) {
global $wgThanksSendToBots;
if ( $user->getId() === $recipient->getId() ) {
$this->dieUsage( 'You cannot thank yourself', 'invalidrecipient' );
} elseif ( !$wgThanksSendToBots && in_array( 'bot', $recipient->getGroups() ) ) {
$this->dieUsage( 'Bots cannot be thanked', 'invalidrecipient' );
}
}
protected function markResultSuccess( $recipientName ) {
$this->getResult()->addValue( null, 'result', array(
'success' => 1,
'recipient' => $recipientName,
) );
}
/**
* This checks the log_search data
*
* @param User $thanker
* @param string $uniqueId
* @return bool Whether thanks has already been sent
*/
protected function haveAlreadyThanked( User $thanker, $uniqueId ) {
$dbw = wfGetDB( DB_MASTER );
return (bool)$dbw->selectRow(
array( 'log_search', 'logging' ),
array( 'ls_value' ),
array(
'log_user' => $thanker->getId(),
'ls_field' => 'thankid',
'ls_value' => $uniqueId,
),
__METHOD__,
array(),
array( 'logging' => array( 'INNER JOIN', 'ls_log_id=log_id' ) )
);
}
/**
* @param User $user
* @param User $recipient
* @param string $uniqueId A unique Id to identify the event being thanked for, to use
* when checking for duplicate thanks
*/
protected function logThanks( User $user, User $recipient, $uniqueId ) {
global $wgThanksLogging;
if ( !$wgThanksLogging ) {
return;
}
$logEntry = new ManualLogEntry( 'thanks', 'thank' );
$logEntry->setPerformer( $user );
$logEntry->setRelations( array( 'thankid' => $uniqueId ) );
$target = $recipient->getUserPage();
$logEntry->setTarget( $target );
$logId = $logEntry->insert();
$logEntry->publish( $logId, 'udp' );
}
public function needsToken() {
return 'csrf';
}
// Writes to the Echo database and sometimes log tables.
public function isWriteMode() {
return true;
}
public function getTokenSalt() {
return '';
}
}