Commit 94b95611 authored by Mark Harding's avatar Mark Harding
Browse files

(feat): various changes to support with frontend work

parent 691dc667
Pipeline #82710072 failed with stages
in 7 minutes and 27 seconds
......@@ -51,7 +51,7 @@ class incoming implements Interfaces\Api
$urn = "urn:subscription-request:" . implode('-', [ Session::getLoggedInUserGuid(), $subscriberGuid ]);
$request = $manager->get($urn);
if (!$request || $request->getPublisherGuid() != Session::getLoggedInUserGuid()) {
return Factory::response([
'status' => 'error',
......
......@@ -17,7 +17,7 @@ class all implements Interfaces\Api
// Return a list of subscription requests
$manager = Di::_()->get('Subscriptions\Requests\Manager');
$requests = $manager->getIncomingRequests(Session::getLoggedInUserGuid(), []);
$requests = $manager->getIncomingList(Session::getLoggedInUserGuid(), []);
return Factory::response([
'requests' => Factory::exportable($requests),
......
......@@ -5,6 +5,7 @@ namespace Minds\Controllers\api\v2\subscriptions;
use Minds\Api\Factory;
use Minds\Core\Di\Di;
use Minds\Core\Session;
use Minds\Core\Subscriptions\Requests\SubscriptionRequest;
use Minds\Entities\Factory as EntitiesFactory;
use Minds\Interfaces;
......@@ -49,7 +50,7 @@ class outgoing implements Interfaces\Api
$request = new SubscriptionRequest();
$request->setPublisherGuid($pages[0])
->setSubscriberGuid(Session::getLoggedInGuid());
->setSubscriberGuid(Session::getLoggedInUserGuid());
try {
$manager->add($request);
......
......@@ -1452,6 +1452,6 @@ CREATE TABLE minds.subscription_requests (
publisher_guid bigint,
subscriber_guid bigint,
timestamp timestamp,
accepted boolean,
declined boolean,
PRIMARY KEY (publisher_guid, subscriber_guid)
);
\ No newline at end of file
);
......@@ -94,14 +94,19 @@ class Manager
/**
* Subscribe to a publisher
* @param User $publisher
* @param bool $force
* @return Subscription
*/
public function subscribe($publisher)
public function subscribe($publisher, $force = false)
{
$subscription = new Subscription();
$subscription->setSubscriberGuid($this->subscriber->getGuid())
->setPublisherGuid($publisher->getGuid());
if ($publisher->getMode() === 2 && !$force) {
throw new \Exception("Can not subscribe to closed channel, send a request instead");
}
if ($this->getSubscriptionsCount() >= static::MAX_SUBSCRIPTIONS) {
$this->sendNotificationDelegate->onMaxSubscriptions($subscription);
throw new TooManySubscriptionsException();
......
<?php
namespace Minds\Core\Subscriptions\Requests\Delegates;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Subscriptions\Manager as SubscriptionsManager;
use Minds\Core\Subscriptions\Requests\SubscriptionRequest;
class SubscriptionsDelegate
{
/** @var SubscriptionsManager */
private $subscriptionsManager;
/** @var EntitiesBuilder */
private $entitiesBuilder;
public function __construct($subscriptionsManager = null, $entitiesBuilder = null)
{
$this->subscriptionsManager = $subscriptionsManager ?? Di::_()->get('Subscriptions\Manager');
$this->entitiesBuilder = $entitiesBuilder ?? Di::_()->get('EntitiesBuilder');
}
/**
* Called when subscription request is accepted
* @param SubscriptionRequest $subscriptionRequest
......@@ -12,6 +27,10 @@ class SubscriptionsDelegate
*/
public function onAccept(SubscriptionRequest $subscriptionRequest): void
{
// TODO
$subscriber = $this->entitiesBuilder->single($subscriptionRequest->getSubscriberGuid());
$publisher = $this->entitiesBuilder->single($subscriptionRequest->getPublisherGuid());
$this->subscriptionsManager->setSubscriber($subscriber);
$this->subscriptionsManager->subscribe($publisher, true);
}
}
......@@ -40,8 +40,19 @@ class Manager
*/
public function getIncomingList($user_guid, array $opts = [])
{
$opts = array_merge([
'hydrate' => true,
], $opts);
$opts['publisher_guid'] = $user_guid;
$response = $this->repository->getList($opts);
if ($opts['hydrate']) {
foreach ($response as $i => $request) {
$request->setSubscriber($this->entitiesBuilder->single($request->getSubscriberGuid()));
}
}
return $response;
}
......@@ -98,8 +109,7 @@ class Manager
throw new SubscriptionRequestAlreadyCompletedException();
}
$subscriptionRequest->setAccepted(true);
$this->repository->update($subscriptionRequest);
$this->repository->delete($subscriptionRequest);
$this->notificationsDelegate->onAccept($subscriptionRequest);
$this->subscriptionsDelegate->onAccept($subscriptionRequest);
......@@ -124,7 +134,7 @@ class Manager
throw new SubscriptionRequestAlreadyCompletedException();
}
$subscriptionRequest->setAccepted(false);
$subscriptionRequest->setDeclined(true);
$this->repository->update($subscriptionRequest);
$this->notificationsDelegate->onDecline($subscriptionRequest);
......
......@@ -11,7 +11,6 @@ use Minds\Common\Repository\Response;
use Minds\Common\Urn;
use Cassandra\Timestamp;
use Cassandra\Bigint;
use Cassandra\Boolean;
class Repository
{
......@@ -32,6 +31,7 @@ class Repository
{
$opts = array_merge([
'publisher_guid' => null,
'show_declined' => false,
'limit' => 5000,
'token' => '',
], $opts);
......@@ -45,7 +45,7 @@ class Repository
"SELECT * FROM subscription_requests
WHERE publisher_guid = ?",
[
$opts['publisher_guid'],
new Bigint($opts['publisher_guid']),
]
);
$result = $this->db->request($prepared);
......@@ -56,10 +56,11 @@ class Repository
$subscriptionRequest
->setPublisherGuid((string) $row['publisher_guid'])
->setSubscriberGuid((string) $row['subscriber_guid'])
->setTimestampMs((int) $row['timestamp']->time());
->setTimestampMs((int) $row['timestamp']->time())
->setDeclined((bool) $row['declined']);
if ($row['accepted']) {
$subscriptionRequest->setAccepted((bool) $row['accepted']);
if ($subscriptionRequest->isDeclined() && !$opts['show_declined']) {
continue;
}
$response[] = $subscriptionRequest;
......@@ -84,8 +85,8 @@ class Repository
WHERE publisher_guid = ?
AND subscriber_guid = ?",
[
$publisherGuid,
$subscriberGuid,
new Bigint($publisherGuid),
new Bigint($subscriberGuid),
]
);
$result = $this->db->request($prepared);
......@@ -96,15 +97,16 @@ class Repository
$row = $result[0];
if (!$row) {
return null;
}
$subscriptionRequest = new SubscriptionRequest();
$subscriptionRequest
->setPublisherGuid((string) $row['publisher_guid'])
->setSubscriberGuid((string) $row['subscriber_guid'])
->setTimestampMs((int) $row['timestamp']->time());
if ($row['accepted']) {
$subscriptionRequest->setAccepted((bool) $row['accepted']);
}
->setTimestampMs((int) $row['timestamp']->time())
->setDeclined((bool) $row['declined']);
return $subscriptionRequest;
}
......@@ -142,11 +144,11 @@ class Repository
public function update(SubscriptionRequest $subscriptionRequest, array $field = []): bool
{
$statement = "UPDATE subscription_requests
SET accepted = ?
SET declined = ?
WHERE publisher_guid = ?
AND subscriber_guid = ?";
$values = [
new Boolean($subscriptionRequest->getAccepted()),
$subscriptionRequest->isDeclined(),
new Bigint($subscriptionRequest->getPublisherGuid()),
new Bigint($subscriptionRequest->getSubscriberGuid()),
];
......
......@@ -4,15 +4,20 @@
*/
namespace Minds\Core\Subscriptions\Requests;
use Minds\Entities\User;
use Minds\Traits\MagicAttributes;
/**
* @method SubscriptionRequest setPublisherGuid(string $publisherGuid)
* @method string getPublisherGuid()
* @method SubscriptionRequest setPublisher(User $user)
* @method User getPublisher()
* @method SubscriptionRequest setSubscriberGuid(string $subscriberGuid)
* @method string getSubscriberGuid()
* @method SubscriptionRequest setAccepted(bool $accepted)
* @method string getAccepted()
* @method SubscriptionRequest setSubscriber(User $subscriber)
* @method User getSubscriber()
* @method SubscriptionRequest setDeclined(bool $declined)
* @method bool getDeclined()
* @method SubscriptionRequest setTimestampMs(int $timestampMs)
* @method int getTimestampMs()
*/
......@@ -23,11 +28,17 @@ class SubscriptionRequest
/** @var string */
private $publisherGuid;
/** @var User */
private $publisher;
/** @var string */
private $subscriberGuid;
/** @var User */
private $subscriber;
/** @var bool */
private $accepted;
private $declined = false;
/** @var int */
private $timestampMs;
......@@ -48,8 +59,10 @@ class SubscriptionRequest
{
return [
'publisher_guid' => (string) $this->publisherGuid,
'publisher' => $this->publisher ? $this->publisher->export() : null,
'subscriber_guid' => (string) $this->subscriberGuid,
'accepted' => (bool) $this->accepted,
'subscriber' => $this->subscriber ? $this->subscriber->export() : null,
'declined' => (bool) $this->declined,
'timestamp_ms' => $this->timestampMs,
'timestamp_sec' => round($this->timestampMs / 1000),
];
......
......@@ -39,7 +39,7 @@ class RepositorySpec extends ObjectBehavior
'publisher_guid' => '123',
'subscriber_guid' => '456',
'timestamp' => new Timestamp(time()),
'accepted' => null,
'declined' => null,
]
], 'next-page-token'));
......@@ -61,18 +61,19 @@ class RepositorySpec extends ObjectBehavior
'publisher_guid' => new Bigint(123),
'subscriber_guid' => new Bigint(456),
'timestamp' => new Timestamp(time()),
'accepted' => null,
'declined' => null,
],
[
'publisher_guid' => new Bigint('1789'),
'subscriber_guid' => new Bigint('1123'),
'timestamp' => new Timestamp(time()),
'accepted' => new Boolean(true),
'declined' => true,
]
], 'next-page-token'));
$response = $this->getList([
'publisher_guid' => '123',
'show_declined' => true,
]);
$response[0]->getPublisherGuid()
->shouldBe('123');
......@@ -82,7 +83,7 @@ class RepositorySpec extends ObjectBehavior
->shouldBe('1789');
$response[1]->getSubscriberGuid()
->shouldBe('1123');
$response[1]->isAccepted()
$response[1]->isDeclined()
->shouldBe(true);
}
......@@ -110,7 +111,7 @@ class RepositorySpec extends ObjectBehavior
$subscriptionRequest = new SubscriptionRequest();
$subscriptionRequest->setPublisherGuid('123')
->setSubscriberGuid('456')
->setAccepted(true);
->setDeclined(true);
$this->db->request(Argument::that(function ($prepared) {
$values = $prepared->build()['values'];
......
Supports Markdown
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