Commit 78104aa8 authored by Marcelo Rivera's avatar Marcelo Rivera

(feat): v2/boost/fetch/campaigns

1 merge request!235WIP: Boost Campaigns (&24)
Pipeline #70098135 failed with stages
in 3 minutes and 40 seconds
<?php
/**
* Boost & Boost Campaigns fetch
* @author: eiennohi.
*/
namespace Minds\Controllers\api\v2\boost\fetch;
use Minds\Api\Exportable;
use Minds\Api\Factory;
use Minds\Common\Urn;
use Minds\Core\Boost\Campaigns;
use Minds\Core\Boost\Campaigns\Campaign;
use Minds\Core\Boost\Network;
use Minds\Core\Di\Di;
use Minds\Core\Feeds\FeedSyncEntity;
use Minds\Core\Session;
use Minds\Entities;
use Minds\Interfaces;
class campaigns implements Interfaces\Api
{
public function get($pages)
{
Factory::isLoggedIn();
/** @var Entities\User $currentUser */
$currentUser = Session::getLoggedinUser();
if ($currentUser->disabled_boost && $currentUser->isPlus()) {
return Factory::response([
'boosts' => [],
]);
}
// Parse parameters
$type = $pages[0] ?? 'newsfeed';
$limit = abs(intval($_GET['limit'] ?? 2));
$offset = $_GET['offset'] ?? 0;
$rating = intval($_GET['rating'] ?? $currentUser->getBoostRating());
$platform = $_GET['platform'] ?? 'other';
$quality = 0;
$sync = (bool) ($_GET['sync'] ?? true);
if ($limit === 0) {
return Factory::response([
'boosts' => [],
]);
} elseif ($sync && $limit > 500) {
$limit = 500;
} elseif (!$sync && $limit > 50) {
$limit = 50;
}
// Options specific to newly created users (<=1 hour) and iOS users
if ($platform === 'ios') {
$rating = 1; // they can only see safe content
$quality = 90;
} elseif (time() - $currentUser->getTimeCreated() <= 3600) {
$rating = 1; // they can only see safe content
$quality = 75;
}
/** @var Campaigns\Manager $manager */
$manager = Di::_()->get('Boost\Campaigns\Manager');
$data = [];
try {
$result = $manager->fetch([
'limit' => $limit,
'from' => $offset,
'rating' => $rating,
'quality' => $quality,
'type' => $type,
]);
foreach ($result as $entity) {
$feedSyncEntity = (new FeedSyncEntity())
->setGuid((string) $entity->getGuid())
->setOwnerGuid((string) $entity->getOwnerGuid());
if ($entity instanceof Campaign) {
$feedSyncEntity->setUrn($entity->getUrn());
} elseif ($entity instanceof Network\Boost) {
$feedSyncEntity->setUrn(new Urn("urn:boost:{$entity->getType()}:{$entity->getGuid()}"));
}
$data[] = $feedSyncEntity;
}
} catch (\Exception $e) {
error_log($e);
}
return Factory::response([
'boosts' => Exportable::_($data),
'load-next' => $data->getPagingToken() ?: null,
]);
}
public function post($pages)
{
return Factory::response([]);
}
public function put($pages)
{
return Factory::response([]);
}
public function delete($pages)
{
return Factory::response([]);
}
}
......@@ -8,6 +8,7 @@ namespace Minds\Core\Boost\Campaigns;
use Exception;
use Minds\Common\Repository\Response;
use Minds\Core\Boost\Network\Boost;
use Minds\Core\Data\ElasticSearch\Client as ElasticSearchClient;
use Minds\Core\Data\ElasticSearch\Prepared\Search;
use Minds\Core\Data\ElasticSearch\Prepared\Update;
......@@ -216,6 +217,96 @@ class ElasticRepository
return $response;
}
public function fetch(array $opts = [])
{
$opts = array_merge([
'limit' => 24,
'from' => 0,
'rating' => null,
'quality' => null,
'type' => null,
'sort' => 'asc',
], $opts);
$must = [];
if ($opts['offset']) {
$rangeKey = $opts['sort'] === 'asc' ? 'gt' : 'lt';
$must[] = [
'range' => [
'@timestamp' => [
$rangeKey => $opts['offset'],
],
],
];
}
$body = [
'query' => [
'bool' => [
'must' => $must,
],
],
];
$prepared = new Search();
$prepared->query([
'index' => 'minds-boost,minds-boost-campaigns',
'type' => '_doc',
'body' => $body,
'from' => $opts['from'] ?? 0,
'size' => $opts['limit'],
]);
$result = $this->es->request($prepared);
$next = [];
$data = [];
foreach ($result['hits']['hits'] as $doc) {
$entity = null;
switch ($doc['index']) {
case 'minds-boost':
$entity = (new Boost())
->setGuid($doc['_id'])
->setOwnerGuid($doc['_source']['owner_guid'])
->setCreatedTimestamp($doc['_source']['@timestamp'])
->setType($doc['_source']['type']);
$next[0] = $entity->getCreatedTimestamp();
break;
case 'minds-boost-campaigns':
$entity = (new Campaign())
->setUrn("urn:campaign:{$doc['_id']}")
->setType($doc['_source']['type'])
->setOwnerGuid($doc['_source']['owner_guid'])
->setCreatedTimestamp(((int) $doc['_source']['@timestamp']) ?: null);
$next[1] = $entity->getCreatedTimestamp();
break;
default:
continue;
}
$data[] = $entity;
}
uasort($data, function ($a, $b) {
$aTime = $a instanceof Campaign ? $a->getCreatedTimestamp() : $a->getTimeCreated();
$bTime = $b instanceof Campaign ? $b->getCreatedTimestamp() : $b->getTimeCreated();
return $aTime > $bTime;
});
$data = array_slice($data, 0, $opts['limit']);
$response = new Response($data, count($data));
return $response;
}
/**
* @param Campaign $campaign
* @return bool
......
......@@ -119,6 +119,23 @@ class Manager
});
}
/**
* @param array $opts
* @return Response|null
*/
public function fetch(array $opts = [])
{
$response = null;
try {
$response = $this->elasticRepository->fetch($opts);
} catch (Exception $e) {
error_log("[BoostCampaignsManager] {$e}");
}
return $response;
}
/**
* @param $urn
* @return Campaign|null
......
......@@ -23,17 +23,23 @@ use Minds\Traits\MagicAttributes;
* @method long getOwnerGuid()
* @method Boost setOwner()
* @method User getOwner()
*
* @method Boost setRejectedReason(int $reason)
*
* @method int getRejectedReason()
* @method Boost setRejectedReason(int $reason)
* @method int getCompletedTimestamp()
* @method Boost setCompletedTimestamp(int $ts)
* @method int getReviewedTimestamp()
* @method Boost setReviewedTimestamp(int $ts)
* @method int getRejectedTimestamp()
* @method Boost setRejectedTimestamp(int $ts)
* @method int getCreatedTimestamp()
* @method Boost setCreatedTimestamp(int $ts)
* @method int getRevokedTimestamp()
* @method Boost setRevokedTimestamp(int $ts)
* @method array getTags()
* @method Boost setTags(array $value)
* @method string getType()
* @method Boost setType(string $value)
*/
class Boost
{
......
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