Commit 3bef1f4e authored by Mark Harding's avatar Mark Harding
Browse files

Merge branch 'sprint/FunnyFrog.feat.boost-changes' into 'master'

[Sprint/FunnyFrog] (feat): don't allow to boost if already boosted

Closes #449

See merge request !200
parents 08fca742 a2533cea
Loading
Loading
Loading
Loading
+13 −0
Original line number Original line Diff line number Diff line
<?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);
    }
}
+4 −1
Original line number Original line Diff line number Diff line
@@ -11,6 +11,7 @@ use Minds\Traits\MagicAttributes;
 * @package Minds\Core\Boost\Network
 * @package Minds\Core\Boost\Network
 * @method Boost setGuid(long $guid)
 * @method Boost setGuid(long $guid)
 * @method long getGuid()
 * @method long getGuid()
 * @method string getEntityGuid()
 * @method Boost setEntiyGuid()
 * @method Boost setEntiyGuid()
 * @method Boost setEntity()
 * @method Boost setEntity()
 * @method Entity getEntity()
 * @method Entity getEntity()
@@ -30,7 +31,9 @@ use Minds\Traits\MagicAttributes;
 * @method Boost setRejectedTimestamp(int $ts)
 * @method Boost setRejectedTimestamp(int $ts)
 * @method Boost setCreatedTimestamp(int $ts)
 * @method Boost setCreatedTimestamp(int $ts)
 * @method Boost setRevokedTimestamp(int $ts)
 * @method Boost setRevokedTimestamp(int $ts)
 * @method string getState()
 * @method array getTags()
 * @method Boost setTags(array $value)
 * @method string getType()
 */
 */
class Boost
class Boost
{
{
+11 −1
Original line number Original line Diff line number Diff line
@@ -58,6 +58,14 @@ class ElasticRepository
            ];
            ];
        }
        }


        if ($opts['entity_guid']) {
            $must[] = [
                'term' => [
                    'entity_guid' => $opts['entity_guid']
                ]
            ];
        }

        if ($opts['state'] === 'approved') {
        if ($opts['state'] === 'approved') {
            $must[] = [
            $must[] = [
                'exists' => [
                'exists' => [
@@ -164,6 +172,7 @@ class ElasticRepository
     * Add a boost
     * Add a boost
     * @param Boost $boost
     * @param Boost $boost
     * @return bool
     * @return bool
     * @throws \Exception
     */
     */
    public function add($boost)
    public function add($boost)
    {
    {
@@ -223,6 +232,7 @@ class ElasticRepository
     * Update a boost
     * Update a boost
     * @param Boost $boost
     * @param Boost $boost
     * @return bool
     * @return bool
     * @throws \Exception
     */
     */
    public function update($boost, $fields = [])
    public function update($boost, $fields = [])
    {
    {
+23 −2
Original line number Original line Diff line number Diff line
@@ -2,9 +2,13 @@
/**
/**
 * Network boost manager
 * Network boost manager
 */
 */

namespace Minds\Core\Boost\Network;
namespace Minds\Core\Boost\Network;


use Minds\Common\Repository\Response;
use Minds\Core\Boost\Exceptions\EntityAlreadyBoostedException;
use Minds\Core\Di\Di;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\GuidBuilder;
use Minds\Core\GuidBuilder;


class Manager
class Manager
@@ -15,7 +19,11 @@ class Manager
    /** @var ElasticRepository $repository */
    /** @var ElasticRepository $repository */
    private $elasticRepository;
    private $elasticRepository;


    /** @var EntitiesBuilder $entitiesBuilder */
    private $entitiesBuilder;

    /** @var GuidBuilder $guidBuilder */
    /** @var GuidBuilder $guidBuilder */
    private $guidBuilder;


    public function __construct(
    public function __construct(
        $repository = null,
        $repository = null,
@@ -109,9 +117,22 @@ class Manager
     * Add a boost
     * Add a boost
     * @param Boost $boost
     * @param Boost $boost
     * @return bool
     * @return bool
     * @throws EntityAlreadyBoostedException
     */
     */
    public function add($boost)
    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()) {
        if (!$boost->getGuid()) {
            $boost->setGuid($this->guidBuilder->build());
            $boost->setGuid($this->guidBuilder->build());
        }
        }
+49 −15
Original line number Original line Diff line number Diff line
@@ -2,15 +2,16 @@


namespace Spec\Minds\Core\Boost\Network;
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\Boost;
use Minds\Core\Boost\Network\Repository;
use Minds\Core\Boost\Network\ElasticRepository;
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\EntitiesBuilder;
use Minds\Core\GuidBuilder;
use Minds\Core\GuidBuilder;
use Minds\Entities\Activity;
use Minds\Entities\Activity;
use Minds\Entities\User;
use Minds\Entities\User;
use Minds\Common\Repository\Response;
use PhpSpec\ObjectBehavior;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Prophecy\Argument;


@@ -231,6 +232,18 @@ class ManagerSpec extends ObjectBehavior


    function it_should_add_a_boost(Boost $boost)
    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()
        $this->guidBuilder->build()
            ->shouldBeCalled()
            ->shouldBeCalled()
            ->willReturn(1);
            ->willReturn(1);
@@ -251,6 +264,27 @@ class ManagerSpec extends ObjectBehavior
            ->shouldReturn(true);
            ->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)
    function it_should_update_a_boost(Boost $boost)
    {
    {
        $this->repository->update($boost, ['@timestamp'])
        $this->repository->update($boost, ['@timestamp'])