Commit 427a4422 authored by Guy Thouret's avatar Guy Thouret

(chore) Move functionality from Feeds\AttachmentPaywallDelegate to...

(chore) Move functionality from Feeds\AttachmentPaywallDelegate to PropagateProperties delegate - #616
1 merge request!303Propagate Properties when entities get updated
Pipeline #80076718 passed with stages
in 9 minutes and 27 seconds
......@@ -513,9 +513,6 @@ class newsfeed implements Interfaces\Api
$activity->indexes = ["activity:$activity->owner_guid:edits"]; //don't re-index on edit
(new Core\Translation\Storage())->purge($activity->guid);
$attachmentPaywallDelegate = new Core\Feeds\Activity\Delegates\AttachmentPaywallDelegate();
$attachmentPaywallDelegate->onUpdate($activity);
$save->setEntity($activity)
->save();
......
<?php
/**
* ActivityDelegateInterface
* @author edgebal
*/
namespace Minds\Core\Feeds\Activity\Delegates;
use Minds\Entities\Activity;
interface ActivityDelegateInterface
{
public function onAdd();
public function onUpdate(Activity $activity);
}
<?php
/**
* AttachmentPaywallDelegate
* @author edgebal
*/
namespace Minds\Core\Feeds\Activity\Delegates;
use Minds\Core\Di\Di;
use Minds\Core\Entities\Actions\Save;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\Activity;
class AttachmentPaywallDelegate implements ActivityDelegateInterface
{
/** @var EntitiesBuilder */
protected $entitiesBuilder;
/** @var Save */
protected $save;
/**
* AttachmentPaywallDelegate constructor.
* @param EntitiesBuilder $entitiesBuilder
* @param Save $save
*/
public function __construct(
$entitiesBuilder = null,
$save = null
) {
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->save = $save ?: new Save();
}
/**
* @throws \NotImplementedException
*/
public function onAdd()
{
throw new \NotImplementedException();
}
/**
* @param Activity $activity
* @return bool
*/
public function onUpdate(Activity $activity)
{
if ($activity->entity_guid) {
$attachment = $this->entitiesBuilder->single($activity->entity_guid);
if ($attachment->owner_guid == $activity->owner_guid) {
$attachment->access_id = $activity->isPaywall() ? 0 : 2;
if ($attachment->getSubtype() === 'blog') {
$attachment->setHidden($activity->isPaywall());
} else {
$attachment->hidden = $activity->isPaywall();
}
if (method_exists($attachment, 'setFlag')) {
$attachment->setFlag('paywall', (bool) $activity->isPaywall());
}
if (method_exists($attachment, 'setWireThreshold')) {
$attachment->setWireThreshold($activity->getWireThreshold() ?: false);
}
$this->save
->setEntity($attachment)
->save();
}
}
return true;
}
}
......@@ -2,8 +2,10 @@
namespace Minds\Core\Feeds\Delegates;
use Minds\Core\Blogs\Blog;
use Minds\Core\Entities\Propagator\Properties;
use Minds\Entities\Activity;
use Minds\Entities\Entity;
/**
* Class PropagateProperties
......@@ -33,7 +35,7 @@ class PropagateProperties extends Properties
/**
* Propagate activity properties to entity
* @param Activity $from
* @param $to
* @param Entity|Blog $to
* @return mixed
*/
public function fromActivity(Activity $from, $to)
......@@ -46,6 +48,48 @@ class PropagateProperties extends Properties
$to->setTimeModerated((int)$from->getTimeModerated());
}
$to = $this->propagateAttachmentPaywallProperties($from, $to);
return $to;
}
/**
* @param Activity $from
* @param Entity $to
* @return mixed
*/
private function propagateAttachmentPaywallProperties(Activity $from, $to)
{
if ($to->owner_guid == $from->owner_guid) {
$newAccessId = $from->isPaywall() ? 0 : 2;
if ($this->valueHasChanged($to->access_id, $from->access_id)) {
$to->access_id = $newAccessId;
}
$newHidden = $from->isPayWall();
if ($to->getSubtype() === 'blog') {
/** @var $to Blog */
if ($this->valueHasChanged($to->getHidden(), $newHidden)) {
$to->setHidden($newHidden);
}
} else {
if ($this->valueHasChanged($to->hidden, $newHidden)) {
$to->hidden = $newHidden;
}
}
if (method_exists($to, 'setFlag')) {
if ($this->valueHasChanged($to->getFlag('paywall'), (bool)$from->isPaywall())) {
$to->setFlag('paywall', (bool)$from->isPaywall());
}
}
if (method_exists($to, 'setWireThreshold')) {
if ($this->valueHasChanged($to->getWireThreshold(), $from->getWireThreshold())) {
$to->setWireThreshold($from->getWireThreshold() ?: false);
}
}
}
return $to;
}
}
......@@ -48,6 +48,15 @@ class PropagatePropertiesSpec extends ObjectBehavior
$this->activity->getTimeModerated()->shouldBeCalled()->willReturn(12345);
$this->entity->getTimeModerated()->shouldBeCalled()->willReturn(6789);
$this->entity->setTimeModerated(12345)->shouldBeCalled();
$this->entity->get('owner_guid')->shouldBeCalled()->willReturn(123);
$this->activity->get('owner_guid')->shouldBeCalled()->willReturn(123);
$this->activity->isPayWall()->shouldBeCalled()->willReturn(true);
$this->activity->get('access_id')->shouldBeCalled()->willReturn(0);
$this->entity->get('access_id')->shouldBeCalled()->willReturn(2);
$this->entity->set('access_id', 0)->shouldBeCalled();
$this->entity->getSubtype()->shouldBeCalled()->willReturn('image');
$this->entity->get('hidden')->shouldBeCalled()->willReturn(false);
$this->entity->set('hidden', true)->shouldBeCalled();
$this->fromActivity($this->activity, $this->entity);
}
......
Markdown is supported
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