...
 
Commits (7)
<?php
/**
* Minds Boost Api endpoint
*
* @version 1
* @author Mark Harding
*
*/
namespace Minds\Controllers\api\v1\boost;
use Minds\Api\Factory;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Entities;
use Minds\Entities\Entity;
use Minds\Helpers\Counters;
use Minds\Interfaces;
use Minds\Core\Boost;
class fetch implements Interfaces\Api
{
......@@ -28,14 +22,16 @@ class fetch implements Interfaces\Api
$user = Core\Session::getLoggedinUser();
if (!$user) {
return Factory::response([
Factory::response([
'status' => 'error',
'message' => 'You must be loggedin to view boosts',
]);
return;
}
if ($user->disabled_boost && $user->isPlus()) {
return Factory::response([]);
Factory::response([]);
return;
}
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 2;
......@@ -54,8 +50,8 @@ class fetch implements Interfaces\Api
$quality = 90;
}
/** @var Core\Boost\Network\Iterator $iterator */
$iterator = Core\Di\Di::_()->get('Boost\Network\Iterator');
/** @var $iterator */
$iterator = new Core\Boost\Network\Iterator();
$iterator->setLimit($limit)
->setRating($rating)
->setQuality($quality)
......@@ -63,23 +59,20 @@ class fetch implements Interfaces\Api
->setType($pages[0])
->setUserGuid($user->getGUID());
if (isset($_GET['rating']) && $pages[0] == 'newsfeed') {
if (isset($_GET['rating']) && $pages[0] == Boost\Network\Boost::TYPE_NEWSFEED) {
$cacher = Core\Data\cache\factory::build('Redis');
$offset = $cacher->get(Core\Session::getLoggedinUser()->guid . ':boost-offset:newsfeed');
$iterator->setOffset($offset);
}
switch ($pages[0]) {
case 'content':
//$iterator->setOffset('');
$iterator->setIncrement(true);
case Boost\Network\Boost::TYPE_CONTENT:
/** @var $entity Entity */
foreach ($iterator as $guid => $entity) {
$response['boosts'][] = array_merge($entity->export(), [
'boosted_guid' => (string) $guid,
'urn' => "urn:boost:content:{$guid}",
]);
/* TODO: Resolve bug below, $entity->guid is private member */
Counters::increment($entity->guid, "impression");
Counters::increment($entity->owner_guid, "impression");
}
......@@ -98,7 +91,7 @@ class fetch implements Interfaces\Api
}
}
break;
case 'newsfeed':
case Boost\Network\Boost::TYPE_NEWSFEED:
foreach ($iterator as $guid => $entity) {
$response['boosts'][] = array_merge($entity->export(), [
'boosted' => true,
......@@ -113,53 +106,21 @@ class fetch implements Interfaces\Api
break;
}
return Factory::response($response);
Factory::response($response);
}
/**
*/
public function post($pages)
{
/* Not Implemented */
}
/**
* @param array $pages
*/
public function put($pages)
{
$expire = Core\Di\Di::_()->get('Boost\Network\Expire');
$metrics = Core\Di\Di::_()->get('Boost\Network\Metrics');
$boost = Core\Boost\Factory::build($pages[0])->getBoostEntity($pages[1]);
if (!$boost) {
return Factory::response([
'status' => 'error',
'message' => 'Boost not found'
]);
}
$count = $metrics->incrementViews($boost);
if ($count > $boost->getImpressions()) {
$expire->setBoost($boost);
$expire->expire();
}
Counters::increment($boost->getEntity()->guid, "impression");
Counters::increment($boost->getEntity()->owner_guid, "impression");
return Factory::response([]);
/* Not Implemented */
}
/**
*/
public function delete($pages)
{
}
private function getSuggestedPosts($opts = [])
{
// @deprecated
return [];
/* Not Implemented */
}
}
This diff is collapsed.
<?php
namespace Minds\Core\Boost;
use Minds\Core;
use Minds\Core\Data;
use Minds\Interfaces;
use Minds\Entities;
/**
* Channel boost handler
* @deprecated Please use the Peer controller instead. This is for polyfill support of mobile only
*/
class Channel implements Interfaces\BoostHandlerInterface
{
private $guid;
public function __construct($options)
{
if (isset($options['destination'])) {
if (is_numeric($options['destination'])) {
$this->guid = $options['destination'];
} elseif (is_string($options['destination'])) {
$lookup = new Data\lookup();
$this->guid = key($lookup->get(strtolower($options['destination'])));
}
}
}
/**
* Boost an entity
* @param object/int $entity - the entity to boost
* @param int $points
* @return boolean
*/
public function boost($entity_guid, $points)
{
$entity = Entities\Factory::build($entity_guid);
$destination = Entities\Factory::build($this->guid);
$boost = (new Entities\Boost\Peer())
->setEntity($entity)
->setType('points')
->setBid($points)
->setDestination($destination)
->setOwner(Core\Session::getLoggedInUser())
->setState('created')
->save();
Core\Events\Dispatcher::trigger('notification', 'boost', [
'to'=> [$boost->getDestination()->guid],
'entity' => $boost->getEntity(),
'notification_view' => 'boost_peer_request',
'params' => [
'bid' => $boost->getBid(),
'type' => $boost->getType(),
'title' => $boost->getEntity()->title ?: $boost->getEntity()->message
]
]);
return $boost->getGuid();
/*if (is_object($entity)) {
$guid = $entity->guid;
} else {
$guid = $entity;
}
$db = new Data\Call('entities_by_time');
$result = $db->insert("boost:channel:$this->guid:review", array($guid => $points));
//send a notification of boost offer
Core\Events\Dispatcher::trigger('notification', 'boost', array(
'to' => array($this->guid),
'entity' => $guid,
'notification_view' => 'boost_request',
'params' => array('points'=>$points),
'points' => $points
));
//send back to use
Core\Events\Dispatcher::trigger('notification', 'boost', array(
'to'=>array(Core\Session::getLoggedinUser()->guid),
'entity' => $guid,
'notification_view' => 'boost_submitted_p2p',
'params' => array(
'points' => $points,
'channel' => isset($_POST['destination']) ? $_POST['destination'] : $this->guid
),
'points' => $points
));
return $result;*/
}
/**
* Return boosts for review
* @param int $limit
* @param string $offset
* @return array
*/
public function getReviewQueue($limit, $offset = "")
{
return false;
}
/**
* Accept a boost and do a remind
* @param object/int $entity
* @param int points
* @return boolean
*/
public function accept($entity, $points)
{
/*if (is_object($entity)) {
$guid = $entity->guid;
} else {
$guid = $entity;
}
$db = new Data\Call('entities_by_time');
$embeded = new Entities\Entity($guid);
$embeded = core\Entities::build($embeded); //more accurate, as entity doesn't do this @todo maybe it should in the future
\Minds\Helpers\Counters::increment($guid, 'remind');
$activity = new Entities\Activity();
$activity->p2p_boosted = true;
switch ($embeded->type) {
case 'activity':
if ($embeded->remind_object) {
$activity->setRemind($embeded->remind_object)->save();
} else {
$activity->setRemind($embeded->export())->save();
}
break;
case 'object':
break;
default:
switch ($embeded->subtype) {
case 'blog':
$message = false;
if ($embeded->owner_guid != elgg_get_logged_in_user_guid()) {
$message = 'via <a href="'.$embeded->getOwnerEntity()->getURL() . '">'. $embeded->getOwnerEntity()->name . '</a>';
}
$activity->p2p_boosted = true;
$activity->setTitle($embeded->title)
->setBlurb(elgg_get_excerpt($embeded->description))
->setURL($embeded->getURL())
->setThumbnail($embeded->getIconUrl())
->setMessage($message)
->setFromEntity($embeded)
->save();
break;
}
}
//remove from review
error_log('user_guid is ' . $this->guid);
$db->removeAttributes("boost:channel:$this->guid:review", array($guid));
$db->removeAttributes("boost:channel:all:review", array("$this->guid:$guid"));
$entity = new \Minds\Entities\Activity($guid);
Core\Events\Dispatcher::trigger('notification', 'boost', array(
'to'=>array($entity->owner_guid),
'entity' => $guid,
'title' => $entity->title,
'notification_view' => 'boost_accepted',
'params' => array('points'=>$points),
'points' => $points
));
return true;
*/
}
/**
* Reject a boost
* @param object/int $entity
* @return boolean
*/
public function reject($entity)
{
///
/// REFUND THE POINTS TO THE USER
///
/* if (is_object($entity)) {
$guid = $entity->guid;
} else {
$guid = $entity;
}
$db = new Data\Call('entities_by_time');
$db->removeAttributes("boost:channel:$this->guid:review", array($guid));
$db->removeAttributes("boost:channel:all:review", array("$this->guid:$guid"));
$entity = new \Minds\Entities\Activity($guid);
Core\Events\Dispatcher::trigger('notification', 'boost', array(
'to'=>array($entity->owner_guid),
'entity' => $guid,
'title' => $entity->title,
'notification_view' => 'boost_rejected',
));
return true;//need to double check somehow..
*/
}
/**
* Return a boost
* @return array
*/
public function getBoost($offset = "")
{
///
//// THIS DOES NOT APPLY BECAUSE IT'S PRE-AGREED
///
}
public function autoExpire()
{
/*$db = new Data\Call('entities_by_time');
$boosts = $db->getRow("boost:channel:all:review");
foreach ($boosts as $boost => $ts) {
list($destination, $guid) = explode(':', $boost);
if (time() > $ts + (3600 * 48)) {
$this->guid = $destination;
$guids = $this->getReviewQueue(1, $guid);
$points = reset($guids);
if (!$destination) {
echo "$guid issue with destination.. \n";
continue;
}
echo "$guid has expired. refunding ($points) points to $destination \n";
$db->removeAttributes("boost:channel:all:review", array($boost));
$db->removeAttributes("boost:channel:$destination:review", array($guid));
$entity = new \Minds\Entities\Activity($guid);
Helpers\Wallet::createTransaction($entity->owner_guid, $points, $guid, "boost refund");
Core\Events\Dispatcher::trigger('notification', 'boost', array(
'to'=>array($entity->owner_guid),
'from'=> $destination,
'entity' => $entity,
'title' => $entity->title,
'notification_view' => 'boost_rejected',
));
} else {
echo "$guid is ok... \n";
}
}*/
}
/**
* @param mixed $entity
* @return boolean
*/
public static function validateEntity($entity)
{
return true;
}
}
<?php
namespace Minds\Core\Boost;
use Minds\Interfaces;
/**
* A factory providing handlers boosting items
*/
class Factory
{
public static function getClassHandler($handler)
{
$handler = ucfirst($handler);
$handler = "Minds\\Core\\Boost\\$handler";
if (class_exists($handler)) {
return $handler;
}
throw new \Exception("Handler not found");
}
/**
* Build the handler
* @param string $handler
* @param array $options (optional)
* @return BoostHandlerInterface
*/
public static function build($handler, $options = [], $db = null)
{
if ($handler == 'newsfeed') {
$handler = 'network';
}
$handler = ucfirst($handler);
$handler = "Minds\\Core\\Boost\\$handler";
if (class_exists($handler)) {
$class = new $handler($options, $db);
if ($class instanceof Interfaces\BoostHandlerInterface) {
return $class;
}
}
throw new \Exception("Handler not found");
}
}
<?php
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces;
/**
* Channel boost handler
*/
class Channel implements Interfaces\BoostHandlerInterface
{
/**
* @param mixed $entity
* @return boolean
*/
public function validateEntity($entity)
{
return true;
}
}
<?php
namespace Minds\Core\Boost;
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces\BoostHandlerInterface;
use Minds\Core;
......@@ -8,7 +9,7 @@ use Minds\Entities;
/**
* Content Boost handler
*/
class Content extends Network implements BoostHandlerInterface
class Content implements BoostHandlerInterface
{
protected $handler = 'content';
......@@ -16,7 +17,7 @@ class Content extends Network implements BoostHandlerInterface
* @param mixed $entity
* @return bool
*/
public static function validateEntity($entity)
public function validateEntity($entity)
{
if (!$entity || !is_object($entity)) {
return false;
......
<?php
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces\BoostHandlerInterface;
/**
* A factory providing handlers boosting items
*/
class Factory
{
public static function getHandler($handler): BoostHandlerInterface
{
$handler = ucfirst($handler);
$handler = "Minds\\Core\\Boost\\Handler\\$handler";
if (class_exists($handler)) {
return new $handler;
}
throw new \Exception("Handler not found");
}
}
<?php
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces\BoostHandlerInterface;
/**
* Newsfeed Boost handler
*/
class Network implements BoostHandlerInterface
{
/**
* @param mixed $entity
* @return boolean
*/
public function validateEntity($entity)
{
return true;
}
}
<?php
namespace Minds\Core\Boost;
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces\BoostHandlerInterface;
use Minds\Core;
......@@ -9,7 +9,7 @@ use Minds\Entities;
/**
* Newsfeed Boost handler
*/
class Newsfeed extends Network implements BoostHandlerInterface
class Newsfeed implements BoostHandlerInterface
{
protected $handler = 'newsfeed';
......@@ -17,7 +17,7 @@ class Newsfeed extends Network implements BoostHandlerInterface
* @param mixed $entity
* @return bool
*/
public static function validateEntity($entity)
public function validateEntity($entity)
{
if (!$entity || !is_object($entity)) {
return false;
......
<?php
namespace Minds\Core\Boost\Handler;
use Minds\Interfaces;
/**
* Peer Boost Handler
*/
class Peer implements Interfaces\BoostHandlerInterface
{
/**
* @param mixed $entity
* @return boolean
*/
public function validateEntity($entity)
{
return true;
}
}
This diff is collapsed.
<?php
namespace Minds\Core\Boost;
use Minds\Core\Di\Di;
use Minds\Interfaces;
/**
* Peer Boost Handler
*/
class Peer implements Interfaces\BoostHandlerInterface
{
private $guid;
public function __construct($options)
{
if (isset($options['destination'])) {
$this->guid = $options['destination'];
}
}
/**
* Boost an entity. Not used.
* @param int|object $entity
* @param int $points
* @return null
*/
public function boost($entity, $points)
{
return null;
}
/**
* Gets a single boost entity
* @param mixed $guid
* @return object
*/
public function getBoostEntity($guid)
{
/** @var Repository $repository */
$repository = Di::_()->get('Boost\Repository');
return $repository->getEntity('peer', $guid);
}
/**
* Return a boost. Not used.
* @deprecated
* @return array
*/
public function getBoost($offset = "")
{
///
//// THIS DOES NOT APPLY BECAUSE IT'S PRE-AGREED
///
}
public function accept($entity, $impressions)
{
return false;
}
/**
* @param mixed $entity
* @return boolean
*/
public static function validateEntity($entity)
{
return true;
}
}
......@@ -6,33 +6,9 @@ namespace Minds\Interfaces;
*/
interface BoostHandlerInterface
{
/**
* Boost an entity, place in a review queue first
* @param object|int $entity - the entity to boost
* @param int $impressions
* @return bool
*/
public function boost($entity, $impressions);
/**
* Accept a boost
* @param object|int $entity
* @param int $impressions
* @return bool
*/
public function accept($entity, $impressions);
/**
* Return a boost
* @return array
*/
public function getBoost();
/**
* @param mixed $entity
* @return boolean
*/
public static function validateEntity($entity);
public function validateEntity($entity);
}