...
 
Commits (2)
......@@ -8,6 +8,7 @@
namespace Minds\Core\Comments;
use Minds\Common\Urn;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Luid;
......@@ -69,7 +70,7 @@ class Manager
$this->threadNotifications = $threadNotifications ?: new Delegates\ThreadNotifications();
$this->createEventDispatcher = $createEventDispatcher ?: new Delegates\CreateEventDispatcher();
$this->countCache = $countCache ?: new Delegates\CountCache();
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->spam = $spam ?: Di::_()->get('Security\Spam');
}
......@@ -95,7 +96,7 @@ class Manager
}
$response = $this->repository->getList($opts);
if ($opts['is_focused'] === true && $opts['offset']) {
$count = count($response);
$diff = $opts['limit'] - $count;
......@@ -229,8 +230,8 @@ class Manager
public function delete(Comment $comment, $opts = [])
{
$opts = array_merge([
'force' => false,
], $opts);
'force' => false,
], $opts);
if (!$this->acl->write($comment) && !$opts['force']) {
return false; //TODO throw exception
......@@ -271,6 +272,34 @@ class Manager
return null;
}
/**
* @param string|Urn $urn
* @return Comment|null
* @throws \Exception
*/
public function getByUrn($urn)
{
if (is_string($urn)) {
$urn = new Urn($urn);
}
$components = explode(':', $urn->getNss());
if (count($components) !== 5) {
error_log("[CommentsManager]: Invalid Comment URN (${$components})");
return null;
}
$entityGuid = $components[0];
$parentPath = "{$components[1]}:{$components[2]}:{$components[3]}";
$guid = $components[4];
if ($this->legacyRepository->isLegacy($entityGuid)) {
return $this->legacyRepository->getByGuid($guid);
}
return $this->repository->get($entityGuid, $parentPath, $guid);
}
/**
* Counts comments on an entity
* @param int $entity_guid
......
......@@ -16,7 +16,7 @@ use Minds\Entities\Boost\BoostEntityInterface;
class BoostGuidResolverDelegate implements ResolverDelegate
{
/**
* @var Manager
* @var Manager
*/
protected $manager;
......
<?php
/**
* @author: Marcelo
*/
namespace Minds\Core\Entities\Delegates;
use Minds\Common\Urn;
use Minds\Core\Boost\Repository;
use Minds\Core\Comments\Comment;
use Minds\Core\Comments\Manager;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\Boost\BoostEntityInterface;
class CommentGuidResolverDelegate implements ResolverDelegate
{
/**
* @var Manager
*/
protected $manager;
/**
* CommentGuidResolverDelegate constructor.
* @param Manager $manager
*/
public function __construct($manager = null)
{
$this->manager = $manager ?: new Manager();
}
/**
* @param Urn $urn
* @return boolean
*/
public function shouldResolve(Urn $urn)
{
return $urn->getNid() === 'comment';
}
/**
* @param array $urns
* @param array $opts
* @return mixed
*/
public function resolve(array $urns, array $opts = [])
{
$entities = [];
foreach ($urns as $urn) {
/** @var Comment $comment */
$comment = $this->manager->getByUrn($urn);
$entities[] = $comment;
}
return $entities;
}
/**
* @param $urn
* @param Comment $entity
* @return mixed
*/
public function map($urn, $entity)
{
return $entity;
}
/**
* @param Comment $entity
* @return string|null
*/
public function asUrn($entity)
{
if (!$entity) {
return null;
}
return $entity->getUrn();
}
}
\ No newline at end of file
......@@ -79,6 +79,7 @@ class EntityGuidResolverDelegate implements ResolverDelegate
}
/**
* @param $urn
* @param mixed $entity
* @return mixed
*/
......
......@@ -8,12 +8,12 @@
namespace Minds\Core\Entities;
use Minds\Common\Urn;
use Minds\Core\Entities\Delegates\EntityGuidResolverDelegate;
use Minds\Core\Entities\Delegates\BoostGuidResolverDelegate;
use Minds\Core\Entities\Delegates\CommentGuidResolverDelegate;
use Minds\Core\Entities\Delegates\EntityGuidResolverDelegate;
use Minds\Core\Entities\Delegates\ResolverDelegate;
use Minds\Core\Security\ACL;
use Minds\Entities\User;
use Minds\Helpers\Flags;
class Resolver
{
......@@ -42,6 +42,7 @@ class Resolver
$this->resolverDelegates = $resolverDelegates ?: [
EntityGuidResolverDelegate::class => new EntityGuidResolverDelegate(),
BoostGuidResolverDelegate::class => new BoostGuidResolverDelegate(),
CommentGuidResolverDelegate::class => new CommentGuidResolverDelegate(),
];
$this->acl = $acl ?: ACL::_();
......@@ -122,13 +123,13 @@ class Resolver
// Filter out invalid entities
$sorted = array_filter($sorted, function($entity) { return (bool) $entity; });
$sorted = array_filter($sorted, function ($entity) { return (bool) $entity; });
// Filter out forbidden entities
$sorted = array_filter($sorted, function($entity) {
$sorted = array_filter($sorted, function ($entity) {
return $this->acl->read($entity, $this->user);
//&& !Flags::shouldFail($entity);
//&& !Flags::shouldFail($entity);
});
//
......@@ -138,7 +139,7 @@ class Resolver
public function single($urn)
{
$this->urns = [ $urn ];
$this->urns = [$urn];
$entities = $this->fetch();
return $entities[0];
}
......
......@@ -2,12 +2,15 @@
/**
* Notification delegate for Verdicts
*/
namespace Minds\Core\Reports\Verdict\Delegates;
use Minds\Common\Urn;
use Minds\Core\Di\Di;
use Minds\Core\Reports\Verdict\Verdict;
use Minds\Core\Entities\Resolver;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Events\EventsDispatcher;
use Minds\Common\Urn;
use Minds\Core\Reports\Verdict\Verdict;
class NotificationDelegate
{
......@@ -18,25 +21,36 @@ class NotificationDelegate
protected $entitiesBuilder;
/** @var Urn $urn */
protected $urn;
public function __construct($dispatcher = null, $entitiesBuilder = null, $urn = null)
/** @var Resolver */
protected $entitiesResolver;
public function __construct($dispatcher = null, $entitiesBuilder = null, $urn = null, $entitiesResolver = null)
{
$this->dispatcher = $dispatcher ?: Di::_()->get('EventsDispatcher');
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->urn = $urn ?: new Urn;
$this->entitiesResolver = $entitiesResolver ?: new Resolver();
}
/**
* Actioned notification
* @param Verdict $verdict
* @return void
* @throws \Exception
*/
public function onAction(Verdict $verdict)
{
$entityUrn = $verdict->getReport()->getEntityUrn();
$entityGuid = $this->urn->setUrn($entityUrn)->getNss();
$entity = $this->entitiesBuilder->single($entityGuid);
$entity = $this->entitiesResolver->single($this->urn->setUrn($entityUrn));
if (!$entity) {
$entityGuid = $this->urn->setUrn($entityUrn)->getNss();
$entity = $this->entitiesBuilder->single($entityGuid);
}
if ($verdict->isUpheld()) {
$readableAction = 'removed';
......@@ -58,13 +72,13 @@ class NotificationDelegate
} else {
$readableAction .= '. You can appeal this decision';
}
$this->dispatcher->trigger('notification', 'all', [
'to' => [$entity->getOwnerGuid()],
'entity' => $entity,
'from' => 100000000000000519,
'notification_view' => 'report_actioned',
'params' => [ 'action' => $readableAction ],
'params' => ['action' => $readableAction],
]);
}
......
......@@ -2,11 +2,13 @@
namespace Spec\Minds\Core\Reports\Verdict\Delegates;
use Minds\Common\Urn;
use Minds\Core\Entities\Resolver;
use Minds\Core\EntitiesBuilder;
use Minds\Core\Events\EventsDispatcher;
use Minds\Core\Reports\Report;
use Minds\Core\Reports\Verdict\Delegates\NotificationDelegate;
use Minds\Core\Reports\Verdict\Verdict;
use Minds\Core\Reports\Report;
use Minds\Core\Events\EventsDispatcher;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\Entity;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
......@@ -19,11 +21,19 @@ class NotificationDelegateSpec extends ObjectBehavior
/** @var EntitiesBuilder $entitiesBuilder */
private $entitiesBuilder;
function let(EventsDispatcher $dispatcher, EntitiesBuilder $entitiesBuilder)
/** @var Urn */
private $urn;
/** @var Resolver */
private $entitiesResolver;
function let(EventsDispatcher $dispatcher, EntitiesBuilder $entitiesBuilder, Urn $urn, Resolver $entitiesResolver)
{
$this->beConstructedWith($dispatcher, $entitiesBuilder);
$this->beConstructedWith($dispatcher, $entitiesBuilder, $urn, $entitiesResolver);
$this->dispatcher = $dispatcher;
$this->entitiesBuilder = $entitiesBuilder;
$this->urn = $urn;
$this->entitiesResolver = $entitiesResolver;
}
function it_is_initializable()
......@@ -37,6 +47,18 @@ class NotificationDelegateSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn('urn:activity:123');
$this->urn->setUrn('urn:activity:123')
->shouldBeCalled()
->willReturn($this->urn);
$this->entitiesResolver->single($this->urn)
->shouldBeCalled()
->willReturn(null);
$this->urn->getNss()
->shouldBeCalled()
->willReturn('123');
$report->getReasonCode()
->shouldBeCalled()
->willReturn(2);
......@@ -54,7 +76,43 @@ class NotificationDelegateSpec extends ObjectBehavior
$this->entitiesBuilder->single(123)
->willReturn($entity);
$this->dispatcher->trigger('notification', 'all', Argument::that(function($opts) {
$this->dispatcher->trigger('notification', 'all', Argument::that(function ($opts) {
return $opts['params']['action'] === 'marked as nsfw. You can appeal this decision';
}))
->shouldBeCalled();
$this->onAction($verdict);
}
function it_should_send_a_marked_as_nsfw_notification_but_resolving_urn_with_entitiesResolver(Verdict $verdict, Report $report, Entity $entity)
{
$report->getEntityUrn()
->shouldBeCalled()
->willReturn('urn:activity:123');
$this->urn->setUrn('urn:activity:123')
->shouldBeCalled()
->willReturn($this->urn);
$this->entitiesResolver->single($this->urn)
->shouldBeCalled()
->willReturn($entity);
$report->getReasonCode()
->shouldBeCalled()
->willReturn(2);
$report->isAppeal()
->shouldBeCalled()
->willReturn(false);
$verdict->getReport()
->willReturn($report);
$verdict->isUpheld()
->willReturn(true);
$this->dispatcher->trigger('notification', 'all', Argument::that(function ($opts) {
return $opts['params']['action'] === 'marked as nsfw. You can appeal this decision';
}))
->shouldBeCalled();
......@@ -68,6 +126,18 @@ class NotificationDelegateSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn('urn:activity:123');
$this->urn->setUrn('urn:activity:123')
->shouldBeCalled()
->willReturn($this->urn);
$this->entitiesResolver->single($this->urn)
->shouldBeCalled()
->willReturn(null);
$this->urn->getNss()
->shouldBeCalled()
->willReturn('123');
$report->getReasonCode()
->shouldBeCalled()
->willReturn(4);
......@@ -85,7 +155,7 @@ class NotificationDelegateSpec extends ObjectBehavior
$this->entitiesBuilder->single(123)
->willReturn($entity);
$this->dispatcher->trigger('notification', 'all', Argument::that(function($opts) {
$this->dispatcher->trigger('notification', 'all', Argument::that(function ($opts) {
return $opts['params']['action'] === 'removed. You can appeal this decision';
}))
->shouldBeCalled();
......@@ -99,6 +169,18 @@ class NotificationDelegateSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn('urn:activity:123');
$this->urn->setUrn('urn:activity:123')
->shouldBeCalled()
->willReturn($this->urn);
$this->entitiesResolver->single($this->urn)
->shouldBeCalled()
->willReturn(null);
$this->urn->getNss()
->shouldBeCalled()
->willReturn('123');
$report->isAppeal()
->shouldBeCalled()
->willReturn(true);
......@@ -112,7 +194,7 @@ class NotificationDelegateSpec extends ObjectBehavior
$this->entitiesBuilder->single(123)
->willReturn($entity);
$this->dispatcher->trigger('notification', 'all', Argument::that(function($opts) {
$this->dispatcher->trigger('notification', 'all', Argument::that(function ($opts) {
return $opts['params']['action'] === 'restored by the community appeal jury';
}))
->shouldBeCalled();
......@@ -126,6 +208,18 @@ class NotificationDelegateSpec extends ObjectBehavior
->shouldBeCalled()
->willReturn('urn:activity:123');
$this->urn->setUrn('urn:activity:123')
->shouldBeCalled()
->willReturn($this->urn);
$this->entitiesResolver->single($this->urn)
->shouldBeCalled()
->willReturn(null);
$this->urn->getNss()
->shouldBeCalled()
->willReturn('123');
$report->isAppeal()
->shouldBeCalled()
->willReturn(false);
......