diff --git a/Controllers/api/v2/search/suggest/user.php b/Controllers/api/v2/search/suggest/user.php new file mode 100644 index 000000000..166f46865 --- /dev/null +++ b/Controllers/api/v2/search/suggest/user.php @@ -0,0 +1,71 @@ + + */ + +namespace Minds\Controllers\api\v2\search\suggest; + +use Minds\Core; +use Minds\Core\Di\Di; +use Minds\Interfaces; +use Minds\Api\Factory; +use Minds\Entities; + +class user implements Interfaces\Api, Interfaces\ApiIgnorePam +{ + /** + * Equivalent to HTTP GET method + * @param array $pages + * @return mixed|null + */ + public function get($pages) + { + $usernames = Entities\Repositories\UserRepository::getUsersList(); + + // Filter by the ones that match. + if (!empty($_GET['username'])) { + $userSearch = $_GET['username']; + $usernames = array_filter($usernames, function($username) use ($userSearch) { + return strpos($username, $userSearch) === 0; + }); + } + + return Factory::response([ + 'entities' => $usernames + ]); + + } + + /** + * Equivalent to HTTP POST method + * @param array $pages + * @return mixed|null + */ + public function post($pages) + { + return Factory::response([]); + } + + /** + * Equivalent to HTTP PUT method + * @param array $pages + * @return mixed|null + */ + public function put($pages) + { + return Factory::response([]); + } + + /** + * Equivalent to HTTP DELETE method + * @param array $pages + * @return mixed|null + */ + public function delete($pages) + { + return Factory::response([]); + } +} diff --git a/Entities/Repositories/UserRepository.php b/Entities/Repositories/UserRepository.php new file mode 100644 index 000000000..78dc63ce2 --- /dev/null +++ b/Entities/Repositories/UserRepository.php @@ -0,0 +1,59 @@ +get('Cache'); + + $usernames = $cache->get('usernames'); + if (!empty($usernames)) { + return $usernames; + } + + $usernames = self::fetchUsernames($cql); + + // Cache for 5 minutes. + $cache->set('usernames', $usernames, 300); + + return $usernames; + } + + protected static function fetchUsernames(Cassandra_Client $cql = null): array + { + /** @var Cassandra_Client $cql */ + $cql = $cql ?: Core\Di\Di::_()->get('Database\Cassandra\Cql'); + $sql = <<query($sql); + + try { + /** @var Rows $results */ + $results = $cql->request($query); + if (empty($results)) { + return []; + } + } catch (\Exception $e) { + return []; + } + + $usernames = []; + foreach ($results as $row) { + $usernames[] = $row['username']; + } + + return $usernames; + } +} diff --git a/Spec/Entities/Repositories/UserRepositorySpec.php b/Spec/Entities/Repositories/UserRepositorySpec.php new file mode 100644 index 000000000..929bd4204 --- /dev/null +++ b/Spec/Entities/Repositories/UserRepositorySpec.php @@ -0,0 +1,53 @@ +_client = $client; + } + + public function it_is_initializable() + { + $this->shouldHaveType('Minds\Entities\Repositories\UserRepository'); + } + + public function it_should_get_the_users_friend_list(Client $client) + { + $rows = new Rows([ + [ 'username' => 'steve' ], + [ 'username' => 'tom' ], + [ 'username' => 'berry' ], + ], ''); + + $client->request(Argument::type(Custom::class)) + ->shouldBeCalled() + ->willReturn($rows); + + /** @var Subject $return */ + $return = $this::getUsersList($this->_client); + + $return->shouldBeArray(); + $return->shouldHaveCount(3); + $return->shouldContain('steve'); + $return->shouldContain('tom'); + } +}