Commit 0cdeb613 authored by Ben Hayward's avatar Ben Hayward

updated to meet feedback

1 merge request!251[Sprint/InterestingIguana](feat): Offchain boost rate limiting #475
Pipeline #74209835 canceled with stages
in 49 seconds
......@@ -290,7 +290,7 @@ class boost implements Interfaces\Api
->setType(lcfirst($pages[0]))
->setPriority(false);
if ($manager->checkExisting($boost)) {
if ($manager->isDuplicateBoost($boost)) {
return Factory::response([
'status' => 'error',
'message' => "There's already an ongoing boost for this entity"
......
......@@ -31,7 +31,7 @@ class ElasticRepository
'token' => 0,
'offset' => null,
'order' => null,
'offchain' => null
'offchain' => null,
], $opts);
$must = [];
......@@ -63,15 +63,15 @@ class ElasticRepository
if ($opts['entity_guid']) {
$must[] = [
'term' => [
'entity_guid' => $opts['entity_guid']
]
'entity_guid' => $opts['entity_guid'],
],
];
}
if ($opts['owner_guid']) {
$must[] = [
'term' => [
'owner_guid' => $opts['owner_guid']
'owner_guid' => $opts['owner_guid'],
]
];
}
......@@ -93,8 +93,8 @@ class ElasticRepository
if ($opts['offchain']) {
$must[] = [
"term" => [
"token_method" => "offchain"
'term' => [
'token_method' => 'offchain'
]
];
}
......
......@@ -25,17 +25,23 @@ class Manager
/** @var GuidBuilder $guidBuilder */
private $guidBuilder;
/** @var Config $config */
private $config;
public function __construct(
$repository = null,
$elasticRepository = null,
$entitiesBuilder = null,
$guidBuilder = null
$guidBuilder = null,
$config = null
)
{
$this->repository = $repository ?: new Repository;
$this->elasticRepository = $elasticRepository ?: new ElasticRepository;
$this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
$this->guidBuilder = $guidBuilder ?: new GuidBuilder;
$this->config = $config ?: Di::_()->get('Config');
}
/**
......@@ -145,7 +151,7 @@ class Manager
* @param $boost
* @return bool
*/
public function checkExisting($boost)
public function isDuplicateBoost($boost)
{
$existingBoost = $this->getList([
'useElastic' => true,
......@@ -157,6 +163,7 @@ class Manager
return $existingBoost->count() > 0;
}
/**
* True if the boost is invalid due to the offchain boost limit being reached
*
......@@ -173,12 +180,12 @@ class Manager
});
//reduce the impressions to count the days boosts.
$acc = array_reduce($offlineToday, function($acc, $_boost) {
$acc += $_boost->getImpressions();
return $acc;
$acc = array_reduce($offlineToday, function($carry = 0, $_boost) {
$carry += $_boost->getImpressions();
return $carry;
});
$maxDaily = Di::_()->get('Config')->get('max_daily_boost_views');
$maxDaily = $this->config->get('max_daily_boost_views');
return $acc + $boost->getImpressions() > $maxDaily; //still allow 10k
}
......
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