| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Push; |
| 4 |
|
| 5 |
use MediaWiki\Extension\Notifications\Mapper\AbstractMapper; |
| 6 |
use MediaWiki\Storage\NameTableStore; |
| 7 |
use Wikimedia\Rdbms\DBError; |
| 8 |
use Wikimedia\Rdbms\IDatabase; |
| 9 |
|
| 10 |
class SubscriptionManager extends AbstractMapper { |
| 11 |
|
| 12 |
|
| 13 |
private $dbw; |
| 14 |
|
| 15 |
|
| 16 |
private $dbr; |
| 17 |
|
| 18 |
|
| 19 |
private $pushProviderStore; |
| 20 |
|
| 21 |
|
| 22 |
private $pushTopicStore; |
| 23 |
|
| 24 |
|
| 25 |
private $maxSubscriptionsPerUser; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function __construct( |
| 35 |
IDatabase $dbw, |
| 36 |
IDatabase $dbr, |
| 37 |
NameTableStore $pushProviderStore, |
| 38 |
NameTableStore $pushTopicStore, |
| 39 |
int $maxSubscriptionsPerUser |
| 40 |
) { |
| 41 |
parent::__construct(); |
| 42 |
$this->dbw = $dbw; |
| 43 |
$this->dbr = $dbr; |
| 44 |
$this->pushProviderStore = $pushProviderStore; |
| 45 |
$this->pushTopicStore = $pushTopicStore; |
| 46 |
$this->maxSubscriptionsPerUser = $maxSubscriptionsPerUser; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function create( string $provider, string $token, int $centralId, ?string $topic = null ): bool { |
| 58 |
$subscriptions = $this->getSubscriptionsForUser( $centralId ); |
| 59 |
if ( count( $subscriptions ) >= $this->maxSubscriptionsPerUser ) { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
$oldest = $subscriptions[0]; |
| 64 |
foreach ( $subscriptions as $subscription ) { |
| 65 |
if ( $subscription->getUpdated() < $oldest->getUpdated() ) { |
| 66 |
$oldest = $subscription; |
| 67 |
} |
| 68 |
} |
| 69 |
$this->delete( [ $oldest->getToken() ], $centralId ); |
| 70 |
} |
| 71 |
$topicId = $topic ? $this->pushTopicStore->acquireId( $topic ) : null; |
| 72 |
$this->dbw->newInsertQueryBuilder() |
| 73 |
->insertInto( 'echo_push_subscription' ) |
| 74 |
->ignore() |
| 75 |
->row( [ |
| 76 |
'eps_user' => $centralId, |
| 77 |
'eps_provider' => $this->pushProviderStore->acquireId( $provider ), |
| 78 |
'eps_token' => $token, |
| 79 |
'eps_token_sha256' => hash( 'sha256', $token ), |
| 80 |
'eps_data' => null, |
| 81 |
'eps_topic' => $topicId, |
| 82 |
'eps_updated' => $this->dbw->timestamp() |
| 83 |
] ) |
| 84 |
->caller( __METHOD__ ) |
| 85 |
->execute(); |
| 86 |
return (bool)$this->dbw->affectedRows(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function getSubscriptionsForUser( int $centralId ): array { |
| 95 |
$res = $this->dbr->newSelectQueryBuilder() |
| 96 |
->select( '*' ) |
| 97 |
->from( 'echo_push_subscription' ) |
| 98 |
->join( 'echo_push_provider', null, 'eps_provider = epp_id' ) |
| 99 |
->leftJoin( 'echo_push_topic', null, 'eps_topic = ept_id' ) |
| 100 |
->where( [ 'eps_user' => $centralId ] ) |
| 101 |
->caller( __METHOD__ ) |
| 102 |
->fetchResultSet(); |
| 103 |
$result = []; |
| 104 |
foreach ( $res as $row ) { |
| 105 |
$result[] = Subscription::newFromRow( $row ); |
| 106 |
} |
| 107 |
return $result; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function delete( array $tokens, ?int $centralId = null ): int { |
| 119 |
$cond = [ 'eps_token' => $tokens ]; |
| 120 |
if ( $centralId ) { |
| 121 |
$cond['eps_user'] = $centralId; |
| 122 |
} |
| 123 |
$this->dbw->newDeleteQueryBuilder() |
| 124 |
->deleteFrom( 'echo_push_subscription' ) |
| 125 |
->where( $cond ) |
| 126 |
->caller( __METHOD__ ) |
| 127 |
->execute(); |
| 128 |
return $this->dbw->affectedRows(); |
| 129 |
} |
| 130 |
|
| 131 |
} |