2020-05-15 17:19:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EchoPush;
|
|
|
|
|
|
|
|
use CentralIdLookup;
|
|
|
|
use EchoAbstractMapper;
|
|
|
|
use IDatabase;
|
|
|
|
use MediaWiki\Storage\NameTableStore;
|
|
|
|
use User;
|
|
|
|
use Wikimedia\Rdbms\DBError;
|
|
|
|
|
|
|
|
class SubscriptionManager extends EchoAbstractMapper {
|
|
|
|
|
|
|
|
/** @var IDatabase */
|
|
|
|
private $dbw;
|
|
|
|
|
|
|
|
/** @var IDatabase */
|
|
|
|
private $dbr;
|
|
|
|
|
|
|
|
/** @var CentralIdLookup */
|
|
|
|
private $centralIdLookup;
|
|
|
|
|
|
|
|
/** @var NameTableStore */
|
|
|
|
private $pushProviderStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDatabase $dbw primary DB connection (for writes)
|
|
|
|
* @param IDatabase $dbr replica DB connection (for reads)
|
|
|
|
* @param CentralIdLookup $centralIdLookup
|
|
|
|
* @param NameTableStore $pushProviderStore
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
IDatabase $dbw,
|
|
|
|
IDatabase $dbr,
|
|
|
|
CentralIdLookup $centralIdLookup,
|
|
|
|
NameTableStore $pushProviderStore
|
|
|
|
) {
|
|
|
|
parent::__construct();
|
|
|
|
$this->dbw = $dbw;
|
|
|
|
$this->dbr = $dbr;
|
|
|
|
$this->centralIdLookup = $centralIdLookup;
|
|
|
|
$this->pushProviderStore = $pushProviderStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store push subscription information for a user.
|
|
|
|
* @param User $user
|
|
|
|
* @param string $provider Provider name string (validated by presence in the PARAM_TYPE array)
|
|
|
|
* @param string $token Subscriber token provided by the push provider
|
|
|
|
* @throws DBError if the subscription ID already exists in the DB
|
|
|
|
*/
|
|
|
|
public function create( User $user, string $provider, string $token ): void {
|
|
|
|
$this->dbw->insert(
|
|
|
|
'echo_push_subscription',
|
|
|
|
[
|
|
|
|
'eps_user' => $this->getCentralId( $user ),
|
|
|
|
'eps_provider' => $this->pushProviderStore->acquireId( $provider ),
|
|
|
|
'eps_token' => $token,
|
|
|
|
'eps_token_sha256' => hash( 'sha256', $token ),
|
|
|
|
'eps_updated' => $this->dbw->timestamp()
|
2020-06-07 00:59:39 +00:00
|
|
|
],
|
|
|
|
__METHOD__
|
2020-05-15 17:19:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-22 22:02:34 +00:00
|
|
|
* Get all registered subscriptions for a user (by central ID).
|
|
|
|
* @param int $centralId
|
2020-05-15 17:19:03 +00:00
|
|
|
* @return array array of Subscription objects
|
|
|
|
*/
|
2020-05-22 22:02:34 +00:00
|
|
|
public function getSubscriptionsForUser( int $centralId ) {
|
2020-05-15 17:19:03 +00:00
|
|
|
$res = $this->dbr->select(
|
2020-06-02 23:20:06 +00:00
|
|
|
[ 'echo_push_subscription', 'echo_push_provider' ],
|
2020-05-15 17:19:03 +00:00
|
|
|
'*',
|
2020-06-07 00:59:39 +00:00
|
|
|
[ 'eps_user' => $centralId ],
|
2020-06-02 23:20:06 +00:00
|
|
|
__METHOD__,
|
|
|
|
[],
|
|
|
|
[ 'echo_push_provider' => [ 'INNER JOIN', [ 'eps_provider = epp_id' ] ] ]
|
2020-05-15 17:19:03 +00:00
|
|
|
);
|
|
|
|
$result = [];
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$result[] = Subscription::newFromRow( $row );
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a push subscription for a user.
|
|
|
|
* Note: Selecting for the user in addition to the token should be redundant, since tokens
|
|
|
|
* are globally unique and user-specific, but it's probably safest to keep it as a sanity check.
|
|
|
|
* Also, currently the eps_user column is indexed but eps_token is not.
|
|
|
|
* @param User $user
|
|
|
|
* @param string $token Delete the subscription with this token
|
|
|
|
* @return int number of rows deleted
|
|
|
|
* @throws DBError
|
|
|
|
*/
|
|
|
|
public function delete( User $user, string $token ): int {
|
|
|
|
$this->dbw->delete(
|
|
|
|
'echo_push_subscription',
|
|
|
|
[
|
|
|
|
'eps_user' => $this->getCentralId( $user ),
|
|
|
|
'eps_token' => $token,
|
2020-06-07 00:59:39 +00:00
|
|
|
],
|
|
|
|
__METHOD__
|
2020-05-15 17:19:03 +00:00
|
|
|
);
|
|
|
|
return $this->dbw->affectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user's central ID.
|
|
|
|
* @param User $user
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getCentralId( User $user ): int {
|
|
|
|
return $this->centralIdLookup->centralIdFromLocalUser(
|
|
|
|
$user,
|
|
|
|
CentralIdLookup::AUDIENCE_RAW
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|