From 249215ac29a3d9236643ff408afa0c73cc25160e Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Tue, 13 Nov 2018 22:05:43 -0600 Subject: [PATCH 1/3] Added a GET: /search/suggest/user route for @user tags. --- Controllers/api/v2/search/suggest/user.php | 71 ++++++++++++++++++++++ Entities/Repositories/UserRepository.php | 59 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 Controllers/api/v2/search/suggest/user.php create mode 100644 Entities/Repositories/UserRepository.php diff --git a/Controllers/api/v2/search/suggest/user.php b/Controllers/api/v2/search/suggest/user.php new file mode 100644 index 0000000000..166f468653 --- /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 0000000000..73b1682649 --- /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 [3]; + } + } catch (\Exception $e) { + return [2]; + } + + $usernames = []; + foreach ($results as $row) { + $usernames[] = $row['username']; + } + + return $usernames; + } +} From 3efbaee167a85c34e7293fd767138953903af674 Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Tue, 13 Nov 2018 22:59:36 -0600 Subject: [PATCH 2/3] Removed some debug code. --- Entities/Repositories/UserRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Entities/Repositories/UserRepository.php b/Entities/Repositories/UserRepository.php index 73b1682649..78dc63ce27 100644 --- a/Entities/Repositories/UserRepository.php +++ b/Entities/Repositories/UserRepository.php @@ -43,10 +43,10 @@ protected static function fetchUsernames(Cassandra_Client $cql = null): array /** @var Rows $results */ $results = $cql->request($query); if (empty($results)) { - return [3]; + return []; } } catch (\Exception $e) { - return [2]; + return []; } $usernames = []; From 1f683c8ca1dce41738d19217a5354f85e37f94ef Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Tue, 27 Nov 2018 21:32:48 -0600 Subject: [PATCH 3/3] Added a phpspec test. --- .../Repositories/UserRepositorySpec.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Spec/Entities/Repositories/UserRepositorySpec.php diff --git a/Spec/Entities/Repositories/UserRepositorySpec.php b/Spec/Entities/Repositories/UserRepositorySpec.php new file mode 100644 index 0000000000..929bd42040 --- /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'); + } +}