<commit_before><?php
/**
 * Minds Newsfeed API
 *
 * @version 1
 * @author <NAME>
 */

namespace Minds\Controllers\api\v1;

use Minds\Api\Factory;
use Minds\Core;
use Minds\Core\Security;
use Minds\Entities;
use Minds\Entities\Activity;
use Minds\Helpers;
use Minds\Helpers\Counters;
use Minds\Interfaces;
use Minds\Interfaces\Flaggable;

class newsfeed implements Interfaces\Api
{
    /**
     * Returns the newsfeed
     * @param array $pages
     *
     * API:: /v1/newsfeed/
     */
    public function get($pages)
    {
        $response = array();

        if (!isset($pages[0])) {
            $pages[0] = 'network';
        }

        switch ($pages[0]) {
            case 'single':
                $activity = new Activity($pages[1]);

                if (!$activity->guid) {
                    return Factory::response(['status' => 'error']);
                }

                return Factory::response(array('activity' => $activity->export()));
                break;
            default:
            case 'personal':
                $options = array(
                    'owner_guid' => isset($pages[1]) ? $pages[1] : elgg_get_logged_in_user_guid()
                );
                break;
            case 'network':
                $options = array(
                    'network' => isset($pages[1]) ? $pages[1] : core\Session::getLoggedInUserGuid()
                );
                break;
            case 'featured':
                $db = Core\Di\Di::_()->get('Database\Cassandra\Indexes');
                $offset = isset($_GET['offset']) ? $_GET['offset'] : "";
                $guids = $db->getRow('activity:featured', ['limit' => 24, 'offset' => $offset]);
                if ($guids) {
                    $options['guids'] = $guids;
                } else {
                    return Factory::response([]);
                }
                break;
            case 'container':
                $options = array(
                    'container_guid' => isset($pages[1]) ? $pages[1] : elgg_get_logged_in_user_guid()
                );
                break;
        }

        if (get_input('count')) {
            $offset = get_input('offset', '');

            if (!$offset) {
                return Factory::response([
                    'count' => 0,
                    'load-previous' => ''
                ]);
            }

            $namespace = Core\Entities::buildNamespace(array_merge([
                'type' => 'activity'
            ], $options));

            $db = Core\Di\Di::_()->get('Database\Cassandra\Indexes');
            $guids = $db->get($namespace, [
                'limit' => 5000,
                'offset' => $offset,
                'reversed' => false
            ]);

            if (isset($guids[$offset])) {
                unset($guids[$offset]);
            }

            if (!$guids) {
                return Factory::response([
                    'count' => 0,
                    'load-previous' => $offset
                ]);
            }

            return Factory::response([
                'count' => count($guids),
                'load-previous' => (string)end(array_values($guids)) ?: $offset
            ]);
        }

        //daily campaign reward
        if (Core\Session::isLoggedIn()) {
            Helpers\Campaigns\HourlyRewards::reward();
        }

        $activity = Core\Entities::get(array_merge(array(
            'type' => 'activity',
            'limit' => get_input('limit', 5),
            'offset' => get_input('offset', '')
        ), $options));
        if (get_input('offset') && !get_input('prepend')) { // don't shift if we're prepending to newsfeed
            array_shift($activity);
        }

        $loadPrevious = (string)current($activity)->guid;

        //   \Minds\Helpers\Counters::incrementBatch($activity, 'impression');

        $disabledBoost = Core\Session::getLoggedinUser()->plus && Core\Session::getLoggedinUser()->disabled_boost;
        if ($pages[0] == 'network' && !get_input('prepend') && !$disabledBoost) { // No boosts when prepending
            try {
                //$limit = isset($_GET['access_token']) || $_GET['offset'] ? 2 : 1;
                $limit = 1;

                $boosts = Core\Boost\Factory::build("Newsfeed")->getBoosts($limit, false, 0, [
                    'priority' => !get_input('offset', '')
                ]);

                foreach ($boosts as $boost) {
                    $boost->boosted = true;
                    array_unshift($activity, $boost);
                    //if (get_input('offset')) {
                    //bug: sometimes views weren't being calculated on scroll down
                    Counters::increment($boost->guid, "impression");
                    Counters::increment($boost->owner_guid, "impression");
                    //}
                }
                if (!$boosts) {
                    $cacher = Core\Data\cache\factory::build('apcu');
                    $offset = $cacher->get(Core\Session::getLoggedinUser()->guid . ":newsfeed-blog-boost-offset") ?: "";<commit_msg>(chore): show two boosts on scroll<commit_after><?php
/**
 * Minds Newsfeed API
 *
 * @version 1
 * @author <NAME>
 */

namespace Minds\Controllers\api\v1;

use Minds\Api\Factory;
use Minds\Core;
use Minds\Core\Security;
use Minds\Entities;
use Minds\Entities\Activity;
use Minds\Helpers;
use Minds\Helpers\Counters;
use Minds\Interfaces;
use Minds\Interfaces\Flaggable;

class newsfeed implements Interfaces\Api
{
    /**
     * Returns the newsfeed
     * @param array $pages
     *
     * API:: /v1/newsfeed/
     */
    public function get($pages)
    {
        $response = array();

        if (!isset($pages[0])) {
            $pages[0] = 'network';
        }

        switch ($pages[0]) {
            case 'single':
                $activity = new Activity($pages[1]);

                if (!$activity->guid) {
                    return Factory::response(['status' => 'error']);
                }

                return Factory::response(array('activity' => $activity->export()));
                break;
            default:
            case 'personal':
                $options = array(
                    'owner_guid' => isset($pages[1]) ? $pages[1] : elgg_get_logged_in_user_guid()
                );
                break;
            case 'network':
                $options = array(
                    'network' => isset($pages[1]) ? $pages[1] : core\Session::getLoggedInUserGuid()
                );
                break;
            case 'featured':
                $db = Core\Di\Di::_()->get('Database\Cassandra\Indexes');
                $offset = isset($_GET['offset']) ? $_GET['offset'] : "";
                $guids = $db->getRow('activity:featured', ['limit' => 24, 'offset' => $offset]);
                if ($guids) {
                    $options['guids'] = $guids;
                } else {
                    return Factory::response([]);
                }
                break;
            case 'container':
                $options = array(
                    'container_guid' => isset($pages[1]) ? $pages[1] : elgg_get_logged_in_user_guid()
                );
                break;
        }

        if (get_input('count')) {
            $offset = get_input('offset', '');

            if (!$offset) {
                return Factory::response([
                    'count' => 0,
                    'load-previous' => ''
                ]);
            }

            $namespace = Core\Entities::buildNamespace(array_merge([
                'type' => 'activity'
            ], $options));

            $db = Core\Di\Di::_()->get('Database\Cassandra\Indexes');
            $guids = $db->get($namespace, [
                'limit' => 5000,
                'offset' => $offset,
                'reversed' => false
            ]);

            if (isset($guids[$offset])) {
                unset($guids[$offset]);
            }

            if (!$guids) {
                return Factory::response([
                    'count' => 0,
                    'load-previous' => $offset
                ]);
            }

            return Factory::response([
                'count' => count($guids),
                'load-previous' => (string)end(array_values($guids)) ?: $offset
            ]);
        }

        //daily campaign reward
        if (Core\Session::isLoggedIn()) {
            Helpers\Campaigns\HourlyRewards::reward();
        }

        $activity = Core\Entities::get(array_merge(array(
            'type' => 'activity',
            'limit' => get_input('limit', 5),
            'offset' => get_input('offset', '')
        ), $options));
        if (get_input('offset') && !get_input('prepend')) { // don't shift if we're prepending to newsfeed
            array_shift($activity);
        }

        $loadPrevious = (string)current($activity)->guid;

        //   \Minds\Helpers\Counters::incrementBatch($activity, 'impression');

        $disabledBoost = Core\Session::getLoggedinUser()->plus && Core\Session::getLoggedinUser()->disabled_boost;
        if ($pages[0] == 'network' && !get_input('prepend') && !$disabledBoost && get_input('offset')) { // No boosts when prepending
            try {
                //$limit = isset($_GET['access_token']) || $_GET['offset'] ? 2 : 1;
                $limit = 2;

                $boosts = Core\Boost\Factory::build("Newsfeed")->getBoosts($limit, false, 0, [
                    'priority' => !get_input('offset', '')
                ]);

                foreach ($boosts as $boost) {
                    $boost->boosted = true;
                    array_unshift($activity, $boost);
                    //if (get_input('offset')) {
                    //bug: sometimes views weren't being calculated on scroll down
                    Counters::increment($boost->guid, "impression");
                    Counters::increment($boost->owner_guid, "impression");
                    //}
                }
                if (!$boosts) {
                    $cacher = Core\Data\cache\factory::build('apcu');
                    $offset = $cacher->get(Core\Session::getLoggedinUser()->guid . ":newsfeed-blog-boost-offset") ?: "";
{'fraction_non_alphanumeric': 0.15664898841092123, 'fraction_numerical': 0.005303476723629935, 'mean_word_length': 1.5205445544554455, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '221113', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2736, 'n_words': 496}