Commit 21f43ead authored by Ben Hayward's avatar Ben Hayward

Updated, added in reduce for counting impressions and extra test

1 merge request!251[Sprint/InterestingIguana](feat): Offchain boost rate limiting #475
Pipeline #69484923 canceled with stages
in 39 seconds
......@@ -300,7 +300,7 @@ class boost implements Interfaces\Api
if ($manager->boostLimitReached($boost)) {
return Factory::response([
'status' => 'error',
'message' => "Maximum of 10 offchain tokens per day exceeded."
'message' => "This boost would exceeded the maximum of 10 offchain tokens per day."
]);
}
......
......@@ -164,12 +164,23 @@ class Manager
* @return boolean true if the boost limit has been reached.
*/
public function boostLimitReached($boost) {
//get offchain boosts
$offchain = $this->getOffchainBoosts($boost);
//filter to get todays offchain transactions
$offlineToday = array_filter($offchain->toArray(), function($result) {
return $result->getCreatedTimestamp() > time() - (60*60*24);
return $result->getCreatedTimestamp() > time() - (60 * 60 * 24);
});
//reduce the impressions to count the days boosts.
$acc = array_reduce($offlineToday, function($acc, $_boost) {
$acc += $_boost->getImpressions();
return $acc;
});
return count($offlineToday) >= 10;
return $acc + $boost->getImpressions() > 10000; //still allow 10k
}
/**
* Gets the users last offchain boosts, from the most recent boost backwards in time.
*
......
......@@ -321,45 +321,51 @@ class ManagerSpec extends ObjectBehavior
function it_should_recognise_a_user_has_reached_the_offchain_boost_limit(Boost $boost)
{
$boostArray = [];
for ($i = 1; $i <= 10; $i++) {
for ($i = 1; $i < 11; $i++) {
$newBoost = new Boost();
$newBoost->setCreatedTimestamp('9999999999999999');
$newBoost->setImpressions(1000);
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->runThroughGetList($boost, $boostArray);
$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++) {
for ($i = 1; $i < 10; $i++) {
$newBoost = new Boost();
$newBoost->setCreatedTimestamp('9999999999999999');
$newBoost->setImpressions(1000);
array_push($boostArray, $newBoost);
}
$this->runThroughGetList($boost, $boostArray);
$this->boostLimitReached($boost)->shouldReturn(false);
}
function it_should_recognise_a_boost_would_take_user_above_offchain_limit(Boost $boost)
{
$boostArray = [];
for ($i = 1; $i < 2; $i++) {
$newBoost = new Boost();
$newBoost->setCreatedTimestamp('9999999999999999');
$newBoost->setImpressions(4501);
array_push($boostArray, $newBoost);
}
$this->runThroughGetList($boost, $boostArray);
$this->boostLimitReached($boost)->shouldReturn(false);
}
function runThroughGetList($boost, $existingBoosts) {
$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, ''));
->willReturn(new Response($existingBoosts, ''));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response($boostArray));
->willReturn(new Response($existingBoosts));
$boost->getType()
->shouldBeCalled()
......@@ -368,7 +374,9 @@ class ManagerSpec extends ObjectBehavior
$boost->getOwnerGuid()
->shouldBeCalled()
->willReturn('123');
$this->boostLimitReached($boost)->shouldReturn(false);
$boost->getImpressions()
->shouldBeCalled()
->willReturn(1000);
}
}
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