...
 
Commits (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' => [
......@@ -79,7 +87,7 @@ class ElasticRepository
'field' => '@reviewed',
],
];
$sort = [ '@timestamp' => 'asc' ];
$sort = ['@timestamp' => 'asc'];
}
if ($opts['state'] === 'approved' || $opts['state'] === 'review') {
......@@ -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,
......@@ -51,7 +59,7 @@ class Manager
$response = $this->elasticRepository->getList($opts);
if ($opts['state'] === 'review') {
$opts['guids'] = array_map(function($boost) {
$opts['guids'] = array_map(function ($boost) {
return $boost->getGuid();
}, $response->toArray());
......@@ -80,7 +88,7 @@ class Manager
// unset($response[$i]);
}
}
return $response;
}
......@@ -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;
......@@ -67,7 +68,7 @@ class ManagerSpec extends ObjectBehavior
'state' => 'review',
'hydrate' => true,
'useElastic' => true,
'guids' => [ 1, 2 ],
'guids' => [1, 2],
])
->shouldBeCalled()
->willReturn($response);
......@@ -81,7 +82,7 @@ class ManagerSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn((new User)
->set('guid', 1));
$this->entitiesBuilder->single(456)
->shouldBeCalled()
->willReturn((new Activity)
......@@ -139,7 +140,7 @@ class ManagerSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn((new User)
->set('guid', 1));
$this->entitiesBuilder->single(456)
->shouldBeCalled()
->willReturn((new Activity)
......@@ -174,7 +175,7 @@ class ManagerSpec extends ObjectBehavior
{
$this->repository->getList([
'state' => null,
'guids' => [ 123, 456 ],
'guids' => [123, 456],
'hydrate' => true,
'useElastic' => false,
])
......@@ -199,7 +200,7 @@ class ManagerSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn((new User)
->set('guid', 1));
$this->entitiesBuilder->single(456)
->shouldBeCalled()
->willReturn((new Activity)
......@@ -211,7 +212,7 @@ class ManagerSpec extends ObjectBehavior
->set('guid', 2));
$response = $this->getList([
'guids' => [ 123, 456 ],
'guids' => [123, 456],
]);
$response[0]->getEntity()->getGuid()
......@@ -231,10 +232,22 @@ 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);
$boost->getGuid()
->shouldbeCalled()
->willReturn(null);
......@@ -251,21 +264,42 @@ 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' ])
$this->repository->update($boost, ['@timestamp'])
->shouldBeCalled();
$this->elasticRepository->update($boost, [ '@timestamp' ])
$this->elasticRepository->update($boost, ['@timestamp'])
->shouldBeCalled();
$this->update($boost, [ '@timestamp' ] );
$this->update($boost, ['@timestamp']);
}
function it_should_resync_a_boost_on_elasticsearch(Boost $boost)
{
$this->elasticRepository->update($boost, [ '@timestamp' ])
$this->elasticRepository->update($boost, ['@timestamp'])
->shouldBeCalled();
$this->resync($boost, [ '@timestamp' ] );
$this->resync($boost, ['@timestamp']);
}
}