Skip to content
Commits on Source (2)
......@@ -290,13 +290,20 @@ class boost implements Interfaces\Api
->setType(lcfirst($pages[0]))
->setPriority(false);
if ($manager->checkExisting($boost)) {
return Factory::response([
'status' => 'error',
'message' => "There's already an ongoing boost for this entity"
]);
}
if ($manager->checkExisting($boost)) {
return Factory::response([
'status' => 'error',
'message' => "There's already an ongoing boost for this entity"
]);
}
if ($manager->boostLimitReached($boost)) {
return Factory::response([
'status' => 'error',
'message' => "Maximum of 10 offchain tokens per day exceeded."
]);
}
// Pre-set GUID
if ($bidType == 'tokens' && isset($_POST['guid'])) {
......
......@@ -22,6 +22,7 @@ class ElasticRepository
/**
* Return a list of boosts
* @param array $opts
* @param array $order - optional - null, asc, desc.
* @return Response
*/
public function getList($opts = [])
......@@ -30,11 +31,13 @@ class ElasticRepository
'rating' => 3,
'token' => 0,
'offset' => null,
'order' => null,
'offchain' => null
], $opts);
$must = [];
$must_not = [];
$sort = [ '@timestamp' => 'asc' ];
$sort = [ '@timestamp' => $opts['order'] ?? 'asc' ];
$must[] = [
'term' => [
......@@ -66,6 +69,14 @@ class ElasticRepository
];
}
if ($opts['owner_guid']) {
$must[] = [
'term' => [
'owner_guid' => $opts['owner_guid']
]
];
}
if ($opts['state'] === 'approved') {
$must[] = [
'exists' => [
......@@ -81,6 +92,14 @@ class ElasticRepository
];
}
if ($opts['offchain']) {
$must[] = [
"term" => [
"token_method" => "offchain"
]
];
}
if ($opts['state'] === 'review') {
$must_not[] = [
'exists' => [
......
......@@ -157,4 +157,36 @@ class Manager
return $existingBoost->count() > 0;
}
/**
* True if the boost is invalid due to the offchain boost limit being reached
*
* @param Boost $type the Boost object.
* @return boolean true if the boost limit has been reached.
*/
public function boostLimitReached($boost) {
$offchain = $this->getOffchainBoosts($boost);
$offlineToday = array_filter($offchain->toArray(), function($result) {
return $result->getCreatedTimestamp() > time() - (60*60*24);
});
return count($offlineToday) >= 10;
}
/**
* Gets the users last offchain boosts, from the most recent boost backwards in time.
*
* @param string $type the type of the boost
* @param integer $limit default to 10.
* @return $existingBoosts
*/
public function getOffchainBoosts($boost, $limit = 10) {
$existingBoosts = $this->getList([
'useElastic' => true,
'state' => 'review',
'type' => $boost->getType(),
'limit' => $limit,
'order' => 'desc',
'offchain' => true,
'owner_guid' => $boost->getOwnerGuid(),
]);
return $existingBoosts;
}
}
......@@ -65,5 +65,5 @@ class ElasticRepositorySpec extends ObjectBehavior
$this->add($boost)
->shouldReturn(true);
}
}
......@@ -272,12 +272,12 @@ class ManagerSpec extends ObjectBehavior
function it_should_check_if_the_entity_was_already_boosted(Boost $boost)
{
$this->elasticRepository->getList([
'useElastic' => true,
'state' => 'review',
'type' => 'newsfeed',
'entity_guid' => '123',
'limit' => 1,
'hydrate' => true,
"hydrate" => true,
"useElastic" => true,
"state" => "review",
"type" => "newsfeed",
"entity_guid" => "123",
"limit" => 1
])
->shouldBeCalled()
->willReturn(new Response([$boost], ''));
......@@ -296,4 +296,79 @@ class ManagerSpec extends ObjectBehavior
$this->checkExisting($boost)->shouldReturn(true);
}
function it_should_request_offchain_boosts(Boost $boost)
{
$this->elasticRepository->getList(["hydrate" => true, "useElastic" => true, "state" => "review", "type" => "newsfeed", "limit" => 10, "order" => "desc", "offchain" => true, "owner_guid" => "123"])
->shouldBeCalled()
->willReturn(new Response([$boost], ''));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$boost]));
$boost->getType()
->shouldBeCalled()
->willReturn('newsfeed');
$boost->getOwnerGuid()
->shouldBeCalled()
->willReturn('123');
$this->getOffchainBoosts($boost)->shouldHaveType('Minds\Common\Repository\Response');
}
function it_should_recognise_a_user_has_reached_the_offchain_boost_limit(Boost $boost)
{
$boostArray = [];
for ($i = 1; $i <= 10; $i++) {
$newBoost = new Boost();
$newBoost->setCreatedTimestamp('9999999999999999');
array_push($boostArray, $newBoost);
}
$this->elasticRepository->getList(["hydrate" => true, "useElastic" => true, "state" => "review", "type" => "newsfeed", "limit" => 10, "order" => "desc", "offchain" => true, "owner_guid" => "123"])
->shouldBeCalled()
->willReturn(new Response($boostArray, ''));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response($boostArray));
$boost->getType()
->shouldBeCalled()
->willReturn('newsfeed');
$boost->getOwnerGuid()
->shouldBeCalled()
->willReturn('123');
$this->boostLimitReached($boost)->shouldReturn(true);
}
function it_should_recognise_a_user_has_NOT_reached_the_offchain_boost_limit(Boost $boost)
{
$boostArray = [];
for ($i = 1; $i <= 9; $i++) {
$newBoost = new Boost();
$newBoost->setCreatedTimestamp('9999999999999999');
array_push($boostArray, $newBoost);
}
$this->elasticRepository->getList(["hydrate" => true, "useElastic" => true, "state" => "review", "type" => "newsfeed", "limit" => 10, "order" => "desc", "offchain" => true, "owner_guid" => "123"])
->shouldBeCalled()
->willReturn(new Response($boostArray, ''));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response($boostArray));
$boost->getType()
->shouldBeCalled()
->willReturn('newsfeed');
$boost->getOwnerGuid()
->shouldBeCalled()
->willReturn('123');
$this->boostLimitReached($boost)->shouldReturn(false);
}
}