Commit c1be0c6c authored by Mark Harding's avatar Mark Harding

(feat): max subscriptions limit

parent 9b1091df
No related merge requests found
Pipeline #73672483 running with stages
......@@ -34,4 +34,18 @@ class SendNotificationDelegate
}
public function onMaxSubscriptions($subscription)
{
$message = "You are unable to subscribe to new channels as you have over 5000 subscriptions.";
$this->eventsDispatcher->trigger('notification', 'all', [
'to' => [ $subscription->getSubscriberGuid() ],
'entity' => $subscription->getPublisherGuid(),
'notification_view' => 'custom_message',
'from' => 100000000000000519,
'message' => $message,
'params' => [ 'message' => $message],
]);
}
}
......@@ -14,6 +14,7 @@ use Minds\Entities\User;
class Manager
{
const MAX_SUBSCRIPTIONS = 5000;
/** @var Repository $repository */
private $repository;
......@@ -102,10 +103,15 @@ class Manager
$subscription->setSubscriberGuid($this->subscriber->getGuid())
->setPublisherGuid($publisher->getGuid());
if ($this->getSubscriptionsCount() >= static::MAX_SUBSCRIPTIONS) {
$this->sendNotificationDelegate->onMaxSubscriptions($subscription);
throw new TooManySubscriptionsException();
}
$subscription = $this->repository->add($subscription);
$this->eventsDelegate->trigger($subscription);
$this->feedsDelegate->copy($subscription);
//$this->feedsDelegate->copy($subscription);
$this->copyToElasticSearchDelegate->copy($subscription);
$this->cacheDelegate->cache($subscription);
$this->checkRateLimitDelegate->incrementCache($this->subscriber->guid);
......@@ -145,7 +151,7 @@ class Manager
*/
public function getSubscriptionsCount()
{
return $this->subscriber->getSubscriptonsCount(); //TODO: Refactor so we are the source of truth
return $this->subscriber->getSubscriptionsCount(); //TODO: Refactor so we are the source of truth
}
}
<?php
namespace Minds\Core\Subscriptions;
class TooManySubscriptionsException extends \Exception
{
protected $message = "Subscribe to over 5000 channels";
}
\ No newline at end of file
......@@ -98,10 +98,6 @@ class ManagerSpec extends ObjectBehavior
$this->eventsDelegate->trigger($subscription)
->shouldBeCalled();
// Call the feeds delegate
$this->feedsDelegate->copy($subscription)
->shouldBeCalled();
// Call the es delegate
$this->copyToElasticSearchDelegate->copy($subscription)
->shouldBeCalled();
......@@ -119,6 +115,20 @@ class ManagerSpec extends ObjectBehavior
->shouldBe(true);
}
function it_should_not_allow_if_over_5000_subscriptions(User $subscriber)
{
$publisher = (new User)->set('guid', 456);
$subscriber->getSubscriptionsCount()
->willReturn(5000);
$subscriber->getGUID()
->willReturn(123);
$this->setSubscriber($subscriber);
$this->shouldThrow('Minds\Core\Subscriptions\TooManySubscriptionsException')
->duringSubscribe($publisher);
}
function it_should_unsubscribe()
{
// Confusing.. but this is the returned subscription
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment