Commit 0aaa07e6 authored by Emiliano Balbuena's avatar Emiliano Balbuena Committed by Mark Harding
Browse files

(fix): Pro channel group listing

parent 023ddf27
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -339,6 +339,18 @@ class Response implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable
        return array_reduce($this->data, $callback, $initialValue);
    }

    /**
     * @param callable $callback
     * @return Response
     */
    public function sort(callable $callback): Response
    {
        $data = $this->data;
        usort($data, $callback);

        return new static($data, $this->pagingToken);
    }

    /**
     * Returns the first element of the Response, or null if empty
     * @return mixed|null
+32 −16
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use Minds\Common\Repository\Response;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Entities\Factory as EntitiesFactory;
use Minds\Entities\Group;
use Minds\Entities\User;
use Minds\Interfaces;

@@ -24,7 +25,6 @@ class content implements Interfaces\Api
        $currentUser = Core\Session::getLoggedinUser();

        $container_guid = $pages[0] ?? null;
        $owner_guid = null;

        if (!$container_guid) {
            return Factory::response([
@@ -61,8 +61,6 @@ class content implements Interfaces\Api
                break;
            case 'groups':
                $type = 'group';
                $container_guid = null;
                $owner_guid = $pages[0];
                break;
            case 'all':
                $type = 'all';
@@ -125,10 +123,10 @@ class content implements Interfaces\Api
        $opts = [
            'cache_key' => $currentUser ? $currentUser->guid : null,
            'container_guid' => $container_guid,
            'owner_guid' => $owner_guid,
            'access_id' => $isOwner && !$forcePublic ? [0, 1, 2, $container_guid] : [2, $container_guid],
            'custom_type' => null,
            'limit' => $limit,
            'offset' => $offset,
            'type' => $type,
            'algorithm' => $algorithm,
            'period' => '7d',
@@ -162,7 +160,11 @@ class content implements Interfaces\Api
        try {
            $result = $this->getData($entities, $opts, $asActivities, $sync);

            if ($opts['algorithm'] !== 'latest' && $result->count() <= static::MIN_COUNT) {
            if (
                $opts['algorithm'] !== 'latest' &&
                $opts['type'] !== 'group' &&
                $result->count() <= static::MIN_COUNT
            ) {
                $opts['algorithm'] = 'latest';
                $result = $this->getData($entities, $opts, $asActivities, $sync);
            }
@@ -190,7 +192,18 @@ class content implements Interfaces\Api
     */
    private function getData($entities, $opts, $asActivities, $sync)
    {
        switch ($opts['type']) {
            case 'group':
                /** @var Core\Groups\Ownership $manager */
                $manager = Di::_()->get('Groups\Ownership');

                $result = $manager
                    ->setUserGuid($opts['container_guid'])
                    ->fetch();

                break;

            default:
                /** @var Core\Feeds\Elastic\Manager $manager */
                $manager = Di::_()->get('Feeds\Elastic\Manager');
                $result = $manager->getList($opts);
@@ -206,6 +219,9 @@ class content implements Interfaces\Api
                    }
                }

                break;
        }

        return $result;
    }

+4 −0
Original line number Diff line number Diff line
@@ -20,5 +20,9 @@ class GroupsProvider extends Provider
        $this->di->bind('Groups\Feeds', function ($di) {
            return new Feeds();
        }, [ 'useFactory'=> false ]);

        $this->di->bind('Groups\Ownership', function ($di) {
            return new Ownership();
        }, [ 'useFactory'=> true ]);
    }
}
+18 −4
Original line number Diff line number Diff line
@@ -634,6 +634,22 @@ class Membership
        $user->context('');
    }

    public function getGroupGuidsByMember($opts = [])
    {
        $opts = array_merge([
            'limit' => 500
        ], $opts);

        // Grab all groups we are a member of
        $this->relDB
            ->setGuid((string) $opts['user_guid']);

        return $this->relDB->get('member', [
            'limit' => $opts['limit'],
            'inverse' => false,
        ]);
    }

    public function getGroupsByMember($opts = [])
    {
        $opts = array_merge([
@@ -649,11 +665,9 @@ class Membership
            'limit' => 1000,
        ])->toArray();

        // Grab all groups we are a member of
        $this->relDB->setGuid($opts['user_guid']);
        $guids = $this->relDB->get('member', [
        $guids = $this->getGroupGuidsByMember([
            'user_guid' => $opts['user_guid'],
            'limit' => 500,
            'inverse' => false,
        ]);

        // Populate all groups to markers
+97 −0
Original line number Diff line number Diff line
<?php
/**
 * Ownership
 * @author edgebal
 */

namespace Minds\Core\Groups;

use Exception;
use Minds\Common\Repository\Response;
use Minds\Core\Di\Di;
use Minds\Core\EntitiesBuilder;
use Minds\Entities\Group;
use Minds\Entities\User;

class Ownership
{
    /** @var Membership */
    protected $manager;

    /** @var EntitiesBuilder */
    protected $entitiesBuilder;

    /** @var string */
    protected $userGuid;

    /**
     * Ownership constructor.
     * @param Membership $manager
     * @param EntitiesBuilder $entitiesBuilder
     */
    public function __construct(
        $manager = null,
        $entitiesBuilder = null
    ) {
        $this->manager = $manager ?: new Membership();
        $this->entitiesBuilder = $entitiesBuilder ?: Di::_()->get('EntitiesBuilder');
    }

    /**
     * @param int|string $userGuid
     * @return Ownership
     */
    public function setUserGuid($userGuid): Ownership
    {
        $this->userGuid = (string) $userGuid;
        return $this;
    }

    /**
     * @param array $opts
     * @return Response
     * @throws Exception
     */
    public function fetch(array $opts = [])
    {
        $opts = array_merge([
            'cap' => 500,
            'offset' => 0,
            'limit' => 12,
        ], $opts);

        $guids = $this->manager->getGroupGuidsByMember([
            'user_guid' => $this->userGuid,
            'limit' => $opts['cap']
        ]);

        $offset = $opts['offset'] ?: 0;
        $limit = $opts['limit'];

        $guids = array_slice($guids, $offset, $limit ?: null);

        if (!$guids) {
            return new Response();
        }

        $user = new User();
        $user->guid = $this->userGuid;

        $response = (new Response(
            $this->entitiesBuilder->get(['guids' => $guids])
        ))->filter(function ($group) use ($user) {
            /** @var Group $group */
            return $group && $group->isPublic() && $group->isOwner($user);
        })->sort(function ($a, $b) {
            /** @var Group $a */
            /** @var Group $b */

            return $b->getMembersCount() <=> $a->getMembersCount();
        });

        $response
            ->setPagingToken($offset + $limit);

        return $response;
    }
}
Loading