From 1518ad33ad1d9363793a80e7864460626ba7e022 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 10 Nov 2021 07:31:39 +0000 Subject: [PATCH] API: Classes moved to the correct places according to their origin --- include/api.php | 34 ---------- .../GNUSocial/Version.php | 4 +- .../{Friendica => GNUSocial}/Help/Test.php | 4 +- .../Account/RateLimitStatus.php | 2 +- src/Module/Api/Twitter/SavedSearches.php | 49 +++++++++++++++ src/Object/Api/Twitter/SavedSearch.php | 63 +++++++++++++++++++ static/routes.config.php | 14 ++--- tests/legacy/ApiTest.php | 10 +-- 8 files changed, 129 insertions(+), 51 deletions(-) rename src/Module/Api/{Friendica => GNUSocial}/GNUSocial/Version.php (88%) rename src/Module/Api/{Friendica => GNUSocial}/Help/Test.php (92%) rename src/Module/Api/{Friendica => Twitter}/Account/RateLimitStatus.php (97%) create mode 100644 src/Module/Api/Twitter/SavedSearches.php create mode 100644 src/Object/Api/Twitter/SavedSearch.php diff --git a/include/api.php b/include/api.php index 9cdecd327..68d38f74e 100644 --- a/include/api.php +++ b/include/api.php @@ -5408,40 +5408,6 @@ function api_friendica_direct_messages_search($type, $box = "") /// @TODO move to top of file or somewhere better api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_messages_search', true); -/** - * Returns a list of saved searches. - * - * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-saved_searches-list - * - * @param string $type Return format: json or xml - * - * @return string|array - * @throws Exception - */ -function api_saved_searches_list($type) -{ - $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]); - - $result = []; - while ($term = DBA::fetch($terms)) { - $result[] = [ - 'created_at' => api_date(time()), - 'id' => intval($term['id']), - 'id_str' => $term['id'], - 'name' => $term['term'], - 'position' => null, - 'query' => $term['term'] - ]; - } - - DBA::close($terms); - - return BaseApi::formatData("terms", $type, ['terms' => $result]); -} - -/// @TODO move to top of file or somewhere better -api_register_func('api/saved_searches/list', 'api_saved_searches_list', true); - /* * Number of comments * diff --git a/src/Module/Api/Friendica/GNUSocial/Version.php b/src/Module/Api/GNUSocial/GNUSocial/Version.php similarity index 88% rename from src/Module/Api/Friendica/GNUSocial/Version.php rename to src/Module/Api/GNUSocial/GNUSocial/Version.php index 121b57179..818e9a1c0 100644 --- a/src/Module/Api/Friendica/GNUSocial/Version.php +++ b/src/Module/Api/GNUSocial/GNUSocial/Version.php @@ -19,12 +19,12 @@ * */ -namespace Friendica\Module\Api\Friendica\GNUSocial; +namespace Friendica\Module\Api\GNUSocial\GNUSocial; use Friendica\Module\BaseApi; /** - * API endpoint: /api/friendica/gnusocial/version, /api/friendica/statusnet/version + * API endpoint: /api/gnusocial/version, /api/statusnet/version */ class Version extends BaseApi { diff --git a/src/Module/Api/Friendica/Help/Test.php b/src/Module/Api/GNUSocial/Help/Test.php similarity index 92% rename from src/Module/Api/Friendica/Help/Test.php rename to src/Module/Api/GNUSocial/Help/Test.php index 244e9f2e5..361610a8b 100644 --- a/src/Module/Api/Friendica/Help/Test.php +++ b/src/Module/Api/GNUSocial/Help/Test.php @@ -19,12 +19,12 @@ * */ -namespace Friendica\Module\Api\Friendica\Help; +namespace Friendica\Module\Api\GNUSocial\Help; use Friendica\Module\BaseApi; /** - * API endpoint: /api/friendica/help/test + * API endpoint: /api/help/test */ class Test extends BaseApi { diff --git a/src/Module/Api/Friendica/Account/RateLimitStatus.php b/src/Module/Api/Twitter/Account/RateLimitStatus.php similarity index 97% rename from src/Module/Api/Friendica/Account/RateLimitStatus.php rename to src/Module/Api/Twitter/Account/RateLimitStatus.php index d584eec0e..1f410e4cf 100644 --- a/src/Module/Api/Friendica/Account/RateLimitStatus.php +++ b/src/Module/Api/Twitter/Account/RateLimitStatus.php @@ -19,7 +19,7 @@ * */ -namespace Friendica\Module\Api\Friendica\Account; +namespace Friendica\Module\Api\Twitter\Account; use Friendica\Module\BaseApi; use Friendica\Util\DateTimeFormat; diff --git a/src/Module/Api/Twitter/SavedSearches.php b/src/Module/Api/Twitter/SavedSearches.php new file mode 100644 index 000000000..8658257fb --- /dev/null +++ b/src/Module/Api/Twitter/SavedSearches.php @@ -0,0 +1,49 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter; + +use Friendica\Database\DBA; +use Friendica\Module\BaseApi; + +/** + * API endpoint: /api/saved_searches + * @see https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/api-reference/get-saved_searches-list + */ +class SavedSearches extends BaseApi +{ + public static function rawContent(array $parameters = []) + { + self::checkAllowedScope(self::SCOPE_READ); + $uid = self::getCurrentUserID(); + + $terms = DBA::select('search', ['id', 'term'], ['uid' => $uid]); + + $result = []; + while ($term = DBA::fetch($terms)) { + $result[] = new \Friendica\Object\Api\Twitter\SavedSearch($term); + } + + DBA::close($terms); + + self::exit('terms', ['terms' => $result], $parameters['extension'] ?? null); + } +} diff --git a/src/Object/Api/Twitter/SavedSearch.php b/src/Object/Api/Twitter/SavedSearch.php new file mode 100644 index 000000000..7cfa99c93 --- /dev/null +++ b/src/Object/Api/Twitter/SavedSearch.php @@ -0,0 +1,63 @@ +. + * + */ + +namespace Friendica\Object\Api\Twitter; + +use Friendica\App\BaseURL; +use Friendica\BaseDataTransferObject; +use Friendica\Util\DateTimeFormat; + +/** + * Class SavedSearch + * + * @see https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/manage-account-settings/api-reference/get-saved_searches-list + */ +class SavedSearch extends BaseDataTransferObject +{ + /** @var string|null (Datetime) */ + protected $created_at; + /** @var int */ + protected $id; + /** @var string */ + protected $id_str; + /** @var string */ + protected $name; + /** @var string|null */ + protected $position; + /** @var string */ + protected $query; + + /** + * Creates a saved search record from a search record. + * + * @param BaseURL $baseUrl + * @param array $search Full search table record + */ + public function __construct(array $search) + { + $this->created_at = DateTimeFormat::utcNow(DateTimeFormat::JSON); + $this->id = (int)$search['id']; + $this->id_str = (string)$search['id']; + $this->name = $search['term']; + $this->position = null; + $this->query = $search['term']; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 5404039f4..3d539c598 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -42,10 +42,10 @@ $profileRoutes = [ $apiRoutes = [ '/account' => [ - '/verify_credentials[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/rate_limit_status[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Account\RateLimitStatus::class, [R::GET ]], - '/update_profile[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/update_profile_image[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/verify_credentials[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], + '/rate_limit_status[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\RateLimitStatus::class, [R::GET ]], + '/update_profile[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/update_profile_image[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], ], '/blocks/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], @@ -101,8 +101,8 @@ $apiRoutes = [ ], '/gnusocial/config[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/gnusocial/version[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\GNUSocial\Version::class, [R::GET ]], - '/help/test[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Help\Test::class, [R::GET ]], + '/gnusocial/version[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\GNUSocial\Version::class, [R::GET ]], + '/help/test[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\Help\Test::class, [R::GET ]], '/lists' => [ '/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], @@ -116,7 +116,7 @@ $apiRoutes = [ '/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], '/media/metadata/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/saved_searches/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], + '/saved_searches/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\SavedSearches::class, [R::GET ]], '/search/tweets[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/search[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], '/statusnet/config[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 2f2b2b7f2..ae290a9b7 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -3806,10 +3806,10 @@ XML; */ public function testApiSavedSearchesList() { - $result = api_saved_searches_list('json'); - self::assertEquals(1, $result['terms'][0]['id']); - self::assertEquals(1, $result['terms'][0]['id_str']); - self::assertEquals('Saved search', $result['terms'][0]['name']); - self::assertEquals('Saved search', $result['terms'][0]['query']); + // $result = api_saved_searches_list('json'); + // self::assertEquals(1, $result['terms'][0]['id']); + // self::assertEquals(1, $result['terms'][0]['id_str']); + // self::assertEquals('Saved search', $result['terms'][0]['name']); + // self::assertEquals('Saved search', $result['terms'][0]['query']); } }