Skip to content
Commits on Source (2)
<?php
namespace Minds\Core\Boost\Exceptions;
use Throwable;
class EntityAlreadyBoostedException extends \Exception
{
public function __construct($code = 0, Throwable $previous = null)
{
parent::__construct("There's already an ongoing boost for this entity", $code, $previous);
}
}
......@@ -11,6 +11,7 @@ use Minds\Traits\MagicAttributes;
* @package Minds\Core\Boost\Network
* @method Boost setGuid(long $guid)
* @method long getGuid()
* @method string getEntityGuid()
* @method Boost setEntiyGuid()
* @method Boost setEntity()
* @method Entity getEntity()
......@@ -30,7 +31,9 @@ use Minds\Traits\MagicAttributes;
* @method Boost setRejectedTimestamp(int $ts)
* @method Boost setCreatedTimestamp(int $ts)
* @method Boost setRevokedTimestamp(int $ts)
* @method string getState()
* @method array getTags()
* @method Boost setTags(array $value)
* @method string getType()
*/
class Boost
{
......
......@@ -58,6 +58,14 @@ class ElasticRepository
];
}
if ($opts['entity_guid']) {
$must[] = [
'term' => [
'entity_guid' => $opts['entity_guid']
]
];
}
if ($opts['state'] === 'approved') {
$must[] = [
'exists' => [
......@@ -164,6 +172,7 @@ class ElasticRepository
* Add a boost
* @param Boost $boost
* @return bool
* @throws \Exception
*/
public function add($boost)
{
......@@ -223,6 +232,7 @@ class ElasticRepository
* Update a boost
* @param Boost $boost
* @return bool
* @throws \Exception
*/
public function update($boost, $fields = [])
{
......
......@@ -2,9 +2,13 @@
/**
* Network boost manager
*/
namespace Minds\Core\Boost\Network;
use Minds\Common\Repository\Response;
use Minds\Core\Boost\Exceptions\EntityAlreadyBoostedException;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\GuidBuilder;
class Manager
......@@ -15,7 +19,11 @@ class Manager
/** @var ElasticRepository $repository */
private $elasticRepository;
/** @var EntitiesBuilder $entitiesBuilder */
private $entitiesBuilder;
/** @var GuidBuilder $guidBuilder */
private $guidBuilder;
public function __construct(
$repository = null,
......@@ -109,9 +117,22 @@ class Manager
* Add a boost
* @param Boost $boost
* @return bool
* @throws EntityAlreadyBoostedException
*/
public function add($boost)
{
$existingBoost = $this->getList([
'useElastic' => true,
'state' => 'review',
'type' => $boost->getType(),
'entity_guid' => $boost->getEntityGuid(),
'limit' => 1
]);
if ($existingBoost->count() > 0) {
throw new EntityAlreadyBoostedException();
}
if (!$boost->getGuid()) {
$boost->setGuid($this->guidBuilder->build());
}
......
......@@ -2,15 +2,16 @@
namespace Spec\Minds\Core\Boost\Network;
use Minds\Core\Boost\Network\Manager;
use Minds\Common\Repository\Response;
use Minds\Core\Boost\Exceptions\EntityAlreadyBoostedException;
use Minds\Core\Boost\Network\Boost;
use Minds\Core\Boost\Network\Repository;
use Minds\Core\Boost\Network\ElasticRepository;
use Minds\Core\Boost\Network\Manager;
use Minds\Core\Boost\Network\Repository;
use Minds\Core\EntitiesBuilder;
use Minds\Core\GuidBuilder;
use Minds\Entities\Activity;
use Minds\Entities\User;
use Minds\Common\Repository\Response;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
......@@ -231,6 +232,18 @@ class ManagerSpec extends ObjectBehavior
function it_should_add_a_boost(Boost $boost)
{
$boost->getType()
->shouldBeCalled()
->willReturn('network');
$boost->getEntityGuid()
->shouldBeCalled()
->willReturn('2');
$this->elasticRepository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response());
$this->guidBuilder->build()
->shouldBeCalled()
->willReturn(1);
......@@ -251,6 +264,27 @@ class ManagerSpec extends ObjectBehavior
->shouldReturn(true);
}
function it_should_not_add_a_boost_if_the_entity_has_already_been_boosted(Boost $boost, Boost $oldBoost)
{
$boost->getType()
->shouldBeCalled()
->willReturn('network');
$boost->getEntityGuid()
->shouldBeCalled()
->willReturn('2');
$this->elasticRepository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$oldBoost]));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$oldBoost]));
$this->shouldThrow(new EntityAlreadyBoostedException())->during('add', [$boost]);
}
function it_should_update_a_boost(Boost $boost)
{
$this->repository->update($boost, ['@timestamp'])
......